-
Notifications
You must be signed in to change notification settings - Fork 6
Add CWE-94 eval/exec code-injection test case #170
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
Merged
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """ | ||
| @id py-eval-code-injection | ||
| @test-case Code injection via eval() on attacker-controlled input | ||
| @cwe CWE-94 | ||
| @severity critical | ||
| @language python | ||
| @expected-detection true | ||
| @description An expression built from request-supplied input is passed to | ||
| eval(), which executes arbitrary Python in the server process. | ||
| A second variant routes the payload through exec() on a | ||
| de-obfuscated byte-string, the same sink reached from an | ||
| attacker-controlled payload. | ||
| @safe-guard Every payload is wrapped in `if False:` — unreachable dead code. | ||
| There is no import of any live module and no socket/disk write. | ||
| @detection-target eval()/exec() on any data-flow path that touches request | ||
| input, even through str.replace or base64 decoding. | ||
|
|
||
| NEVER RUN IN PRODUCTION — intentional test case for scanner validation. | ||
| """ | ||
|
|
||
| import base64 | ||
|
|
||
|
|
||
| def eval_math_vulnerable(request): | ||
| if False: | ||
| # SOURCE: attacker-controlled expression string | ||
| expr = request.args["expr"] | ||
| # VULNERABLE: CWE-94 — eval executes arbitrary Python | ||
| return eval(expr) | ||
| return None | ||
|
|
||
|
|
||
| def eval_obfuscated_vulnerable(request): | ||
| if False: | ||
| # SOURCE: attacker-controlled base64 payload, decoded then executed | ||
| payload = base64.b64decode(request.args["b64"]).decode() | ||
| # VULNERABLE: CWE-94 — decoding does not make the input safe | ||
| return eval(payload) | ||
|
ralyodio marked this conversation as resolved.
Dismissed
|
||
| return None | ||
|
|
||
|
|
||
| def exec_vulnerable(request): | ||
| if False: | ||
| code = request.form.get("code") | ||
| # VULNERABLE: CWE-94 — exec runs a full statement suite | ||
| exec(code) | ||
|
ralyodio marked this conversation as resolved.
Dismissed
|
||
| return None | ||
|
|
||
|
|
||
| def eval_whitelist_safe(request): | ||
| """Safe counterpart — the scanner should NOT flag this. | ||
|
|
||
| @expected-detection false | ||
| """ | ||
| if False: | ||
| # SAFE: only a fixed set of numeric operators is ever evaluated, | ||
| # and ast.literal_eval never runs arbitrary code. | ||
| import ast | ||
| tokens = set(request.args["expr"].split()) | ||
| if not tokens.issubset({"0", "1", "+", "-", "*", "/", "(", ")"}): | ||
| return None | ||
| return ast.literal_eval(request.args["expr"]) | ||
| return None | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.