Skip to content

Java: Use controlledString predicate in ExecUnescaped.ql to reduce false positives#5

Closed
mrigankpawagi wants to merge 1 commit into
mainfrom
improve-java-exec-unescaped-controlled-string
Closed

Java: Use controlledString predicate in ExecUnescaped.ql to reduce false positives#5
mrigankpawagi wants to merge 1 commit into
mainfrom
improve-java-exec-unescaped-controlled-string

Conversation

@mrigankpawagi

Copy link
Copy Markdown
Owner

Summary

This PR improves the java/concatenated-command-line query (ExecUnescaped.ql) by replacing the ad-hoc saneString predicate with the shared controlledString predicate from semmle.code.java.security.ControlledString.

Problem

The query's local saneString predicate was overly simplistic — it only recognized StringLiteral, NullLiteral, and variables assigned from those. This caused false positives for many expressions that are clearly programmer-controlled, such as:

  • Numeric type conversions (e.g., Integer.toString(port))
  • Enum constants
  • Class name literals (.class.getName())
  • Final fields initialized with constants
  • Results of arithmetic on literals

MRVA on top-100 Java repositories confirmed these patterns appearing frequently as false positives.

Changes

Replaced the ad-hoc saneString predicate body with a call to the shared controlledString predicate, which already handles all the above cases and is maintained centrally for the entire Java CodeQL library.

Why this is correct

The controlledString predicate in ControlledString.qll is specifically designed to recognize expressions whose values are determined at compile time or are otherwise programmer-controlled. It subsumes everything the original saneString did and adds many more patterns. This is the same predicate already used by SqlConcatenated.ql for the same purpose.

…lse positives

Replace the weak local saneString predicate with the comprehensive
controlledString predicate from ControlledString.qll. This reduces
false positives for expressions involving numeric types, enum constants,
class names, and other programmer-controlled values.
@mrigankpawagi

Copy link
Copy Markdown
Owner Author

Superseded by #12 which consolidates the best of this PR (#5) and #10 into a single improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation Java

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants