feat(java): detect direct positive-index access into String.split(...) result without length check (CWE-248/754) - #187
Open
ai-anant wants to merge 1 commit into
Conversation
…ithout length check
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.
Summary
New generic Java rule detecting the pattern where the result of
String.split(...)is indexed at a positive literal offset in the same expression (e.g.someString.split(SEP)[1],...split(SEP)[3], or...split(SEP)[2].someMethod()) without first verifying the split-array length.Why
When the input string does not contain enough separator-delimited segments, the direct index throws an uncaught
ArrayIndexOutOfBoundsException. If the split input is at all attacker-influenced (file contents, log lines, request data, structured report payloads), this is an uncaught-exception denial-of-service (CWE-248 / CWE-754) that aborts the enclosing build/request/worker thread. The >0-index access is distinct from index0, which is always safe forString.split(never throws), hence the rule targets only positive literal offsets.Rule
java/indexing/unchecked-split-index.yamlcodevigilant.java.indexing.unchecked-split-indexsplit($SEP)[$N],split($SEP, $LIMIT)[$N],split($SEP)[$N].$METHOD($ARGS)with$Na positive integer literal ([1-9][0-9]*).Test
0— silent.semgrep --validatepasses;semgrep --config java/passes on the tree.