Repository navigation
fix(java): flag non-literal Runtime.exec args anywhere after -c - #4121
Open
protonnegativo wants to merge 1 commit into
Open
protonnegativo wants to merge 1 commit into
protonnegativo wants to merge 1 commit into
Conversation
command-injection-formatted-runtime-call only matched when the
non-literal argument came right after "-c", and the pattern-not
clauses dropped any call whose first three arguments were literals.
So exec(new String[]{"/bin/sh", "-c", "some_tool", input}) was missed.
Match $ARG at any position after "-c" and require it to be non-literal
through a metavariable-pattern. This replaces the three pattern-not
clauses: an all-literal call has no non-literal $ARG, so it is still
not reported.
Fixes semgrep#3763
protonnegativo
marked this pull request as ready for review
October 24, 2026 12:11
|
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Link to an issue, if relevant
Fixes #3763
command-injection-formatted-runtime-callmissed this case from the issue:The shell patterns only matched when the non-literal argument came right after
"-c", and thepattern-notclauses dropped any call whose first three arguments were literals.This changes
"-c",$ARG,...to"-c",...,$ARG,...and replaces the threepattern-notclauses with ametavariable-patternthat requires$ARGto be non-literal. An all-literal call has no such$ARG, so it is still not reported.Side effect: the same check now also applies to the
$EXECUTEand$BASHvariants, so calls likeexec(command, "-c", "literal")are no longer reported. The oldpattern-notclauses didn't cover those, and the existing tests don't exercise them.Tests added to
command-injection-formatted-runtime-call.java: the case above (ruleid) and{"/bin/sh", "-c", "some_tool", "literal"}(ok).Before:
After:
Also ran
semgrep --validate --configon the rule,semgrep test java(130/130, same asdevelop) and the lints fromsemgrep-rule-lints.yaml(0 findings), with semgrep 1.182.1.