-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(cors): resolve regex false negative and add value-based regex coverage #1376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96a572a
fix(cors): resolve regex false negative and add value-based regex cov…
Shirshaw64p 4a07c5c
Update nettacker/modules/vuln/http_cors.yaml
Shirshaw64p fbeded7
Update tests/test_yaml_regexes.py
Shirshaw64p ab03ac9
Merge branch 'master' into master
securestep9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ def get_yaml_files(): | |
|
|
||
|
|
||
| def load_yaml(file_path): | ||
| with open(file_path, "r") as f: | ||
| with open(file_path, "r", encoding="utf-8") as f: | ||
| return yaml.safe_load(f) | ||
|
|
||
|
|
||
|
|
@@ -28,12 +28,28 @@ def extract_http_regexes(payloads): | |
| if payload.get("library") != "http": | ||
| continue | ||
| for step in payload.get("steps", []): | ||
| conditions = step.get("response", {}).get("conditions", {}) | ||
| if "content" in conditions and "regex" in conditions["content"]: | ||
| regexes.append(conditions["content"]["regex"]) | ||
| response = step.get("response", {}) | ||
| regexes.extend(extract_regex_values(response)) | ||
| return regexes | ||
|
|
||
|
|
||
| def extract_regex_values(response): | ||
| def _collect(node): | ||
| regexes = [] | ||
| if isinstance(node, dict): | ||
| for key, value in node.items(): | ||
| if key == "regex" and isinstance(value, str): | ||
| regexes.append(value) | ||
| else: | ||
| regexes.extend(_collect(value)) | ||
| elif isinstance(node, list): | ||
| for item in node: | ||
| regexes.extend(_collect(item)) | ||
| return regexes | ||
|
|
||
| return _collect(response.get("conditions", {})) | ||
|
|
||
|
|
||
| def extract_socket_regexes(file_name, payloads): | ||
| regexes = [] | ||
|
|
||
|
|
@@ -85,3 +101,21 @@ def test_yaml_regexes_valid(yaml_file): | |
|
|
||
| for regex in regexes: | ||
| assert is_valid_regex(regex), f"Invalid regex in {yaml_file}: `{regex}`" | ||
|
|
||
|
|
||
| def test_http_cors_reflected_origin_regex_matches_expected_origin(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While more tests are generally good, I am not sure if this way of testing is extendable to all modules. Can you not extend the |
||
| data = load_yaml("nettacker/modules/vuln/http_cors.yaml") | ||
| payloads = data.get("payloads", []) | ||
|
|
||
| origins = ("http://evil.com", "https://evil.com") | ||
| matching_regexes = [] | ||
|
|
||
| for regex in extract_http_regexes(payloads): | ||
| compiled_regex = re.compile(regex) | ||
| if all(compiled_regex.fullmatch(origin) for origin in origins): | ||
| matching_regexes.append(regex) | ||
|
|
||
| assert matching_regexes, ( | ||
| "Expected at least one http_cors regex to fully match both reflected origins: " | ||
| "`http://evil.com` and `https://evil.com`" | ||
| ) | ||
|
Shirshaw64p marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't the current
extract_socket_regexesandextract_http_regexesdo? Why do you need a separate function here?