From d44cc153404808ff09e361b607295e0757a1c65c Mon Sep 17 00:00:00 2001 From: mattebit Date: Wed, 23 Aug 2023 13:34:44 +0200 Subject: [PATCH] added matches regex to checks --- doc/language.md | 2 ++ tool/src/main/java/migt/Check.java | 46 ++++++++++++++++++++++-- tool/src/test/java/Checks_Test.java | 56 +++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/doc/language.md b/doc/language.md index d6db10e..9ee089e 100644 --- a/doc/language.md +++ b/doc/language.md @@ -522,6 +522,8 @@ The Checks tag is a list of Check elements, which can be defined with: - `is in` the value is between a list of values - `is not in` the value is not between a list of values - `is subset of` used to check that a matched JSON array is a subset of the given array. Is subset means that all the values in the matched array are present in the given array. + - `matches regex` when using `check param` or `check` in json, you can use this tag to execute a regex to the matched value of the parameter or the value of the json key specified with the jsonpath + - `not matches regex` as `match regex` but the result is true when the regex doesn't match Note that you can use `check regex` OR `check` OR `check param`. diff --git a/tool/src/main/java/migt/Check.java b/tool/src/main/java/migt/Check.java index e0f7bf4..3747931 100644 --- a/tool/src/main/java/migt/Check.java +++ b/tool/src/main/java/migt/Check.java @@ -136,6 +136,14 @@ public Check(JSONObject json_check) throws ParsingException { value_list.add(act_enc); } break; + case "matches regex": + this.op = MATCHES_REGEX; + this.op_val = json_check.getString("matches regex"); + break; + case "not matches regex": + this.op = NOT_MATCHES_REGEX; + this.op_val = json_check.getString("not matches regex"); + break; } } catch (JSONException e) { throw new ParsingException("error in parsing check: " + e); @@ -183,7 +191,7 @@ public void loader(Operation_API api) { * @param input the input content * @return the result of the check */ - private boolean execute_regex(String input) { + private boolean execute_regex(String input) throws ParsingException { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); applicable = true; @@ -426,6 +434,18 @@ private boolean execute_json() throws ParsingException { throw new ParsingException("Matched single element in jwt, but should be an array when using IS SUBSET OF"); return value_list.containsAll(found_array); + case MATCHES_REGEX: { + if (value_is_array) throw new ParsingException("Check error: cannot execute a regex over a list"); + Pattern p = Pattern.compile(op_val); + Matcher m = p.matcher(found); + return m.find(); + } + case NOT_MATCHES_REGEX: { + if (value_is_array) throw new ParsingException("Check error: cannot execute a regex over a list"); + Pattern p = Pattern.compile(op_val); + Matcher m = p.matcher(found); + return !m.find(); + } } return false; @@ -437,7 +457,7 @@ private boolean execute_json() throws ParsingException { * @param val_to_check the value to check * @return the result of the check */ - public boolean do_check(String val_to_check) { + public boolean do_check(String val_to_check) throws ParsingException { try { if (this.op == null && val_to_check.length() != 0) { // if it passed all the splits without errors, the param is present, but no checks are specified @@ -473,6 +493,18 @@ public boolean do_check(String val_to_check) { return value_list.contains(val_to_check); // TODO check case IS_NOT_IN: return !value_list.contains(val_to_check); + case MATCHES_REGEX: { + Pattern p = Pattern.compile(op_val); + Matcher m = p.matcher(val_to_check); + return m.find(); + } + case NOT_MATCHES_REGEX: { + Pattern p = Pattern.compile(op_val); + Matcher m = p.matcher(val_to_check); + return !m.find(); + } + default: + throw new ParsingException("Unsupported operand for Check in a message: " + op.toString()); } } catch (ArrayIndexOutOfBoundsException e) { //e.printStackTrace(); @@ -564,7 +596,9 @@ public enum CheckOps { IS_NOT_PRESENT, IS_IN, IS_NOT_IN, - IS_SUBSET_OF; + IS_SUBSET_OF, + MATCHES_REGEX, + NOT_MATCHES_REGEX; /** * Function that given a String, returns the corresponding CheckOps enum's value @@ -588,6 +622,12 @@ public static CheckOps fromString(String input) throws ParsingException { return IS_IN; case "is not in": return IS_NOT_IN; + case "is subset of": + return IS_SUBSET_OF; + case "matches regex": + return MATCHES_REGEX; + case "not matches regex": + return NOT_MATCHES_REGEX; default: throw new ParsingException("invalid check operation"); } diff --git a/tool/src/test/java/Checks_Test.java b/tool/src/test/java/Checks_Test.java index 9120357..958604c 100644 --- a/tool/src/test/java/Checks_Test.java +++ b/tool/src/test/java/Checks_Test.java @@ -278,4 +278,60 @@ void test_check_json_array_is_subset_of_wrong() throws ParsingException { c.execute(new ArrayList()); assertFalse(c.getResult()); } + + @Test + @DisplayName("check") + void test_check_json_matches_regex() throws ParsingException { + String check_str = "{\n" + + " \"in\": \"header\",\n" + + " \"check\": \"$.pageInfo.pagePic\",\n" + + " \"matches regex\": \"example\"\n" + + "}"; + + Check c = initCheck_json(check_str); + c.execute(new ArrayList()); + assertTrue(c.getResult()); + } + + @Test + @DisplayName("check") + void test_check_json_matches_regex_wrong() throws ParsingException { + String check_str = "{\n" + + " \"in\": \"header\",\n" + + " \"check\": \"$.pageInfo.pagePic\",\n" + + " \"matches regex\": \"exampsle\"\n" + + "}"; + + Check c = initCheck_json(check_str); + c.execute(new ArrayList()); + assertFalse(c.getResult()); + } + + @Test + @DisplayName("check") + void test_check_json_not_matches_regex() throws ParsingException { + String check_str = "{\n" + + " \"in\": \"header\",\n" + + " \"check\": \"$.pageInfo.pagePic\",\n" + + " \"not matches regex\": \"exampsle\"\n" + + "}"; + + Check c = initCheck_json(check_str); + c.execute(new ArrayList()); + assertTrue(c.getResult()); + } + + @Test + @DisplayName("check") + void test_check_json_not_matches_regex_wrong() throws ParsingException { + String check_str = "{\n" + + " \"in\": \"header\",\n" + + " \"check\": \"$.pageInfo.pagePic\",\n" + + " \"not matches regex\": \"example\"\n" + + "}"; + + Check c = initCheck_json(check_str); + c.execute(new ArrayList()); + assertFalse(c.getResult()); + } }