Skip to content

Java: Add URLEncoder sanitizer and exclude tests from java/log-injection#11

Closed
kiro-agent[bot] wants to merge 1 commit into
mainfrom
improve-java-log-injection-exclude-wrappers
Closed

Java: Add URLEncoder sanitizer and exclude tests from java/log-injection#11
kiro-agent[bot] wants to merge 1 commit into
mainfrom
improve-java-log-injection-exclude-wrappers

Conversation

@kiro-agent

@kiro-agent kiro-agent Bot commented Jun 14, 2026

Copy link
Copy Markdown

This pull request was created by @kiro-agent on behalf of @mrigankpawagi 👻

Comment with /kiro fix to address specific feedback or /kiro all to address everything.
Learn about Kiro Web


Summary

This PR improves the java/log-injection query by adding URLEncoder.encode() as a sanitizer and excluding results in test files.

Problem

MRVA on the top-100 Java repositories produced 5958 alerts across 42 repos. Analysis identified two improvement opportunities:

1. Missing URLEncoder.encode() sanitizer

URLEncoder.encode() replaces newline characters (\n%0A, \r%0D) and other special characters with percent-encoded sequences. This effectively prevents log injection since the attacker can no longer inject newlines. However, the query was not recognizing this as a sanitizer.

2. Test file false positives (105 alerts)

Log injection in test code is not a real security vulnerability since test code doesn't process untrusted user input in production.

Changes

  1. Added UrlEncoderSanitizer class in java/ql/lib/semmle/code/java/security/LogInjection.qll:

    • Recognizes calls to java.net.URLEncoder.encode() as sanitizers
    • This is correct because URL encoding converts \n to %0A and \r to %0D
  2. Excluded test files in java/ql/src/Security/CWE/CWE-117/LogInjection.ql:

    • Added not isInTestFile(sink.getNode().asExpr().getFile()) using the existing isInTestFile predicate from ModelExclusions

MRVA Validation

  • Before: 5958 alerts across 42 repositories
  • After (estimated): ~5850 alerts (test file exclusion) + additional reduction from URLEncoder sanitizer in production code
  • True positives are unaffected — URLEncoder.encode() genuinely prevents log injection

Why URLEncoder.encode() is a correct sanitizer

String encoded = URLEncoder.encode(userInput, "UTF-8");
// userInput = "admin\nINFO: forged entry" 
// encoded = "admin%0AINFO%3A+forged+entry" — no newlines!
logger.info("User: " + encoded); // Safe

URL encoding guarantees that no raw newline characters survive in the output, making log injection impossible.

@mrigankpawagi

Copy link
Copy Markdown
Owner

Superseded by #13 which merges #7's DEBUG/TRACE exclusion with this PR's URLEncoder sanitizer and test exclusion into a single comprehensive 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