Skip to content
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

Spring: Add Semgrep metavar for exact path/route variables #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Gby56
Copy link

@Gby56 Gby56 commented Aug 9, 2023

Attempt at fixing/improving #10 and #12
Adding a $PATH metavar in the rules allows you to really extract the exact values
Only on unauthorized paths for now, I tried also supporting that for the other rules but it would break weirdly

The final graph will show something more readable like:

ln 22: PostMapping "/api/book/{bookId}"

(Can't run the tests on my machine, weird python parallel processing issues)

Gby56 added 2 commits August 9, 2023 17:46
…in:/usr/local/sbin:/usr/local/opt/[email protected]/bin:/Users/gabrielmarquet/.nvm/versions/node/v17.8.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt:/Users/gabrielmarquet/Library/Python/2.7/bin:/Users/gabrielmarquet/Library/Python/3.8/bin:/Users/gabrielmarquet/go/bin:/Users/gabrielmarquet/.local/bin metavar to spring rules and use them in D3 viz
@mschwager
Copy link
Owner

(Can't run the tests on my machine, weird python parallel processing issues)

I'm guessing this is this bug: semgrep/semgrep#8120

@Gby56
Copy link
Author

Gby56 commented Aug 14, 2023

Correct !

@Gby56
Copy link
Author

Gby56 commented Aug 30, 2023

The parallelism bug is gone, but now I'm having a weird issue.

poetry run semgrep --test --config routes/rules/ tests/test_rules/
....

Found rule id mismatch - file=/Users/gabrielmarquet/Desktop/route-detect/tests/test_rules/spring.java 'ruleid' annotation with no YAML rule={'spring-route-unauthenticated'}
Failing due to rule id mismatch. There is a test denoted with 'ruleid: <rule name>' where the rule name does not exist or is not expected in the test file.

@property
def rd_route(self):
if "$PATH" in self.metavars: #For some rules we have extracted a path in the $PATH metavar
return "{} {}".format(self.rd_method, self.metavars['$PATH']['abstract_content'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "{} {}".format(self.rd_method, self.metavars['$PATH']['abstract_content'])
return "{} {}".format(self.rd_method, self.metavar_content('$PATH')

@property
def rd_method(self):
# split by space and deduplicate and return first element
return list(set(self.metavars['$METHOD']['abstract_content'].split(' ')))[0]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return list(set(self.metavars['$METHOD']['abstract_content'].split(' ')))[0]
return list(set(self.metavar_content('$METHOD').split(' ')))[0]

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why split on ' ' here and take the first element? It seems cleaner to me to just use the $METHOD content.

@@ -150,7 +150,8 @@ rules:
patterns:
- pattern: |
@$METHOD
$RETURNTYPE $FUNC(...) { ... }
@Path($PATH)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I vaguely remember that these Path annotations are optional, so this will break some existing findings: https://docs.oracle.com/javaee/6/api/javax/ws/rs/Path.html.

Generally I like the idea of using a consistent metavariables (e.g. $PATH, $METHOD, etc.) across all languages and frameworks, but I think they have to be optional in terms of detection. Many frameworks have an implicit default in certain cases, and we don't want to miss detecting routes because we're expecting explicit values everywhere.

I'm not sure how best to architect these rules such that we can easily find routes and their optional path, method, etc, information.

Copy link
Author

@Gby56 Gby56 Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that explains why the tests are failing, probably because the rule I modified is breaking some detections

Either we make very general rules without too many metavariables, so that we cover widely the detected variations of code, and then refine (using regex or whatever, I thought about it before)

Or we can figure out a way to make the rules more specialized, versions with and without the metavars, and fix the test examples

@mschwager
Copy link
Owner

The parallelism bug is gone, but now I'm having a weird issue.

poetry run semgrep --test --config routes/rules/ tests/test_rules/
....

Found rule id mismatch - file=/Users/gabrielmarquet/Desktop/route-detect/tests/test_rules/spring.java 'ruleid' annotation with no YAML rule={'spring-route-unauthenticated'}
Failing due to rule id mismatch. There is a test denoted with 'ruleid: <rule name>' where the rule name does not exist or is not expected in the test file.

I believe this means there is a test with a ruleid, and the rule isn't firing there.

@Gby56
Copy link
Author

Gby56 commented Sep 13, 2023

Sorry, for the delay, will take a look at this asap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants