Skip to content

Commit

Permalink
Added decode regex in url and head of message
Browse files Browse the repository at this point in the history
  • Loading branch information
mattebit committed Nov 9, 2023
1 parent 07eae0b commit 12a5b58
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 98 deletions.
21 changes: 19 additions & 2 deletions doc/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ In decode operations is possible to use check operations to check the decoded co
Needed tags:

- `type` and/or `encodings`
- `decode param`
- `decode param` or `decode regex`
- `from` select from where decode the content (HTTP message section or previous decode output)

optional tags:
Expand Down Expand Up @@ -390,7 +390,7 @@ In this table you can find a description of all the tags available for this Oper
| input module (container) | Available tags | Required | value type | allowed values |
| --------------------------- | ----------------- | -------- | ---------------------- | -------------------------------------- |
| standard Operation | from | yes | str | head, body, url |
| | decode param | yes | str | \* |
| | decode param or decode regex | yes | str or str(regex) | \* |
| decode Operation (type=jwt) | from | yes | str | jwt header, jwt payload, jwt signature |
| | decode param | yes | str(JSON path) | \* |
| \* | type | | str | xml, jwt |
Expand Down Expand Up @@ -451,6 +451,23 @@ Example of decoding a jwt from the url of a message in the asd parameter, and th
]
```

Example of decoding a value in the url with a regex

```json
{
"from": "url",
"decode regex": "(?<=request=)([^&]+)",
"type": "jwt",
"edits": [
{
"jwt from": "payload",
"jwt edit": "iss",
"value": "https://www.example.com/"
}
]
}
```

## Session Operation

Session operations are used to edit the session tracks given the actual test progress.
Expand Down
139 changes: 93 additions & 46 deletions tool/src/main/java/migt/DecodeOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class DecodeOperation extends Module {
public String decoded_content; // the decoded content
public String decode_target; // aka decode_param how to decode the raw content
public boolean is_regex;
public DecodeOperationFrom from; // where the raw content is. Depending on the containing module, can be other things
public List<Encoding> encodings; // the list of encoding to decode and rencode
public DecodeOpType type; // the type of the decoded param (used only to edit its content)
Expand Down Expand Up @@ -55,6 +56,10 @@ public DecodeOperation(JSONObject decode_op_json) throws ParsingException {
case "decode param":
decode_target = decode_op_json.getString("decode param");
break;
case "decode regex":
decode_target = decode_op_json.getString("decode regex");
is_regex = true;
break;
case "encodings":
JSONArray encodings = decode_op_json.getJSONArray("encodings");
Iterator<Object> it = encodings.iterator();
Expand Down Expand Up @@ -101,45 +106,6 @@ public DecodeOperation(JSONObject decode_op_json) throws ParsingException {
}
}

/**
* Decodes a parameter from a message, given the message section and the list of encodings to be applied during
* decoding
*
* @param ms The message section that contains the parameter to be decoded
* @param encodings The list of encodings to be applied to decode the parameter
* @param messageInfo The message to be decoded
* @param isRequest True if the message containing the parameter is a request
* @param decode_param The name of the parameter to be decoded
* @return The decoded parameter as a string
* @throws ParsingException If problems are encountered during decoding
*/
public static String decodeParam(DecodeOperationFrom ms,
List<Encoding> encodings,
HTTPReqRes messageInfo,
Boolean isRequest,
String decode_param) throws ParsingException {
String decoded_param = "";
// TODO add regex selection
switch (ms) {
case HEAD:
decoded_param = decode(
encodings, messageInfo.getHeadParam(isRequest, decode_param));
break;
case BODY:
decoded_param = decode(
encodings, messageInfo.getBodyRegex(isRequest, decode_param));
break;
case URL:
decoded_param = decode(
encodings, messageInfo.getUrlParam(decode_param));
break;
}

decoded_param = Tools.removeNewline(decoded_param);

return decoded_param;
}

/**
* Decode the given string, with the given ordered encodings
* Example taken from
Expand Down Expand Up @@ -351,6 +317,61 @@ public static byte[] compress(byte[] data, boolean gzip) throws IOException {
return output;
}

/**
* Decodes a parameter from a message, given the message section and the list of encodings to be applied during
* decoding
*
* @param ms The message section that contains the parameter to be decoded
* @param encodings The list of encodings to be applied to decode the parameter
* @param messageInfo The message to be decoded
* @param isRequest True if the message containing the parameter is a request
* @param decode_param The name of the parameter to be decoded
* @return The decoded parameter as a string
* @throws ParsingException If problems are encountered during decoding
*/
public String decodeParam(DecodeOperationFrom ms,
List<Encoding> encodings,
HTTPReqRes messageInfo,
Boolean isRequest,
String decode_param) throws ParsingException {
String decoded_param = "";
if (is_regex) {
switch (ms) {
case HEAD:
decoded_param = decode(
encodings, messageInfo.getHeadRegex(isRequest, decode_param));
break;
case BODY:
decoded_param = decode(
encodings, messageInfo.getBodyRegex(isRequest, decode_param));
break;
case URL:
decoded_param = decode(
encodings, messageInfo.getUrlRegex(decode_param));
break;
}
} else {
switch (ms) {
case HEAD:
decoded_param = decode(
encodings, messageInfo.getHeadParam(isRequest, decode_param));
break;
case BODY:
decoded_param = decode(
encodings, messageInfo.getBodyRegex(isRequest, decode_param));
break;
case URL:
decoded_param = decode(
encodings, messageInfo.getUrlParam(decode_param));
break;
}
}

decoded_param = Tools.removeNewline(decoded_param);

return decoded_param;
}

public void init() {
decoded_content = "";
decode_target = "";
Expand All @@ -361,6 +382,7 @@ public void init() {
type = DecodeOpType.NONE;
editOperations = new ArrayList<>();
jwt = new JWT();
is_regex = false;
}

@Override
Expand Down Expand Up @@ -417,13 +439,38 @@ public API exporter() throws ParsingException {
String encoded = encode(encodings, decoded_content);

if (imported_api instanceof Operation_API) {
Tools.editMessageParam(
decode_target,
from,
((Operation_API) imported_api).message,
((Operation_API) imported_api).is_request,
encoded,
true);
switch (from) {
case HEAD:
if (is_regex) {
((Operation_API) imported_api).message.editHeadRegex(
((Operation_API) imported_api).is_request, decode_target, encoded);
} else {
((Operation_API) imported_api).message.editHeadParam(
((Operation_API) imported_api).is_request, decode_target, encoded
); // TODO test
}
break;
case BODY:
((Operation_API) imported_api).message.editBodyRegex(
((Operation_API) imported_api).is_request, decode_target, encoded
); // TODO test
break;
case URL:
if (is_regex) {
((Operation_API) imported_api).message.editUrlRegex(
decode_target, encoded
);
} else {
((Operation_API) imported_api).message.editUrlParam(
decode_target, encoded
); //TODO test
}
break;
case JWT_HEADER:
case JWT_PAYLOAD:
case JWT_SIGNATURE:
throw new ParsingException("invalid from section in decode operation should be a message section");
}

// the previous function should already have updated the message inside api
return imported_api;
Expand Down
13 changes: 2 additions & 11 deletions tool/src/main/java/migt/EditOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,7 @@ public void execute_Operation_API() throws ParsingException {
message.editUrlParam(what, value);
break;
case EDIT_REGEX:
String old_url = message.getUrl();
Pattern p = Pattern.compile(what);
Matcher m = p.matcher(old_url);
String new_url = m.replaceAll(value);
message.setRequest_url(new_url);
message.editUrlRegex(what, value);
break;
case ADD:
message.addUrlParam(what, value);
Expand Down Expand Up @@ -465,12 +461,7 @@ public void execute_Operation_API() throws ParsingException {
break;
case EDIT_REGEX:
// For each header applies regex
message.getHeaders(is_request).replaceAll(header -> {
Pattern p = Pattern.compile(what);
Matcher m = p.matcher(header);
header = m.replaceAll(value);
return header;
});
message.editHeadRegex(is_request, what, value);
break;
case ADD:
message.addHeadParameter(is_request, what, value);
Expand Down
82 changes: 78 additions & 4 deletions tool/src/main/java/migt/HTTPReqRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public HTTPReqRes(IHttpRequestResponsePersisted message, IExtensionHelpers helpe
* @param helpers an istance of the IExtensionHelpers
* @param isRequest true if the message is a request, false otherwise
*/
public HTTPReqRes(IHttpRequestResponse message, IExtensionHelpers helpers, Boolean isRequest, int index) {
public HTTPReqRes(IHttpRequestResponse message, IExtensionHelpers helpers, boolean isRequest, int index) {
if (!isRequest) {
this.isResponse = true;
this.setResponse(message.getResponse());
Expand Down Expand Up @@ -385,6 +385,40 @@ public String getUrlParam(String param) {
return "";
}

/**
* Execute a regex over the complete url and return the value matched
*
* @param regex the regex to execute
* @return the matched value
*/
public String getUrlRegex(String regex) {
if (!isRequest || request_url == null) {
throw new RuntimeException("Trying to access the url of a response message");
}

String res = "";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(request_url);
if (m.find()) {
res = m.group();
}
return res;
}

/**
* Edit this message's URL with a regex, everything matched will be replaced by new_value
*
* @param regex the regex to execute
* @param new_value the value to substitute to matched content
*/
public void editUrlRegex(String regex, String new_value) {
String old_url = getUrl();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(old_url);
String new_url = m.replaceAll(new_value);
setRequest_url(new_url);
}

/**
* Edits the given parameter value with the new given value
*
Expand Down Expand Up @@ -550,7 +584,7 @@ public void addUrlParam(String name, String value) {
* @param param the parameter name to be searched
* @return the value of the parameter
*/
public String getHeadParam(Boolean isRequest, String param) {
public String getHeadParam(boolean isRequest, String param) {
List<String> headers = isRequest ? this.headers_req : this.headers_resp;

for (String s : headers) {
Expand All @@ -562,6 +596,26 @@ public String getHeadParam(Boolean isRequest, String param) {
return "";
}

/**
* Execute a regex over the headers and return the first value matched
*
* @param regex the regex to execute
* @return the matched value
*/
public String getHeadRegex(boolean isRequest, String regex) {
List<String> headers = isRequest ? this.headers_req : this.headers_resp;

String res = "";
Pattern p = Pattern.compile(regex);
for (String s : headers) {
Matcher m = p.matcher(s);
if (m.find()) {
res = m.group();
}
}
return res;
}


/**
* Edits the Header of the given message
Expand All @@ -570,7 +624,7 @@ public String getHeadParam(Boolean isRequest, String param) {
* @param param the name of the header
* @param new_value the new value
*/
public void editHeadParam(Boolean isRequest, String param, String new_value) {
public void editHeadParam(boolean isRequest, String param, String new_value) {
List<String> headers = isRequest ? this.headers_req : this.headers_resp;

int indx = -1;
Expand All @@ -589,6 +643,26 @@ public void editHeadParam(Boolean isRequest, String param, String new_value) {
}
}

/**
* Edit the header of the message with a regex
*
* @param isRequest select the request or response message
* @param regex the regex to execute
* @param new_value the new value to substitute
*/
public void editHeadRegex(boolean isRequest, String regex, String new_value) {
if (!isResponse) {
throw new RuntimeException("tried to edit headers of response not yet received");
}

getHeaders(isRequest).replaceAll(header -> {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(header);
header = m.replaceAll(new_value);
return header;
});
}

/**
* Adds a Header to the given message
*
Expand Down Expand Up @@ -650,7 +724,7 @@ public void removeHeadParameter(boolean isRequest, String name) {
* @param regex the parameter to be searched as a regex, everything matched by this will be returned as a value
* @return the value of the parameter
*/
public String getBodyRegex(Boolean isRequest, String regex) {
public String getBodyRegex(boolean isRequest, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(new String(getBody(isRequest), StandardCharsets.UTF_8));

Expand Down
Loading

0 comments on commit 12a5b58

Please sign in to comment.