Skip to content

Potential fix for code scanning alert no. 2: HTTP response splitting#14

Open
davewichers wants to merge 2 commits intomainfrom
alert-autofix-2
Open

Potential fix for code scanning alert no. 2: HTTP response splitting#14
davewichers wants to merge 2 commits intomainfrom
alert-autofix-2

Conversation

@davewichers
Copy link
Member

Potential fix for https://github.com/aspectsecurity/TestCodeQL/security/code-scanning/2

In general, to fix HTTP response splitting issues when writing user input into headers (including cookies), you must either validate and reject dangerous characters (like \r and \n) or encode the value so that any such characters are neutralized before being placed in the header. For cookie values, a common approach is to restrict them to a safe character set (for example, URL-encode or base64-encode) or explicitly strip CR/LF and other control characters.

For this specific case, the minimal, behavior-preserving fix is to sanitize str before it is used in new javax.servlet.http.Cookie("SomeCookie", str);. The rest of the logic (reading from cookies, optional InputStream handling, and the informational HTML output) can remain unchanged. A good approach is to introduce a small helper method that removes CR and LF (and possibly other non-printable control characters) from the value and then use the sanitized string when creating the cookie. This avoids changing how the value is displayed in the HTML response (it is already HTML-encoded there) and only affects what is sent in the Set-Cookie header.

Concretely:

  • In Benchmark00087, add a private static sanitization method, e.g. sanitizeForCookie(String value), that removes \r and \n (and optionally other control characters) from the input.
  • Call this method to create a sanitized variable (e.g. String safeStr = sanitizeForCookie(str);) right before the new Cookie(...) invocation.
  • Use safeStr as the cookie value when constructing SomeCookie.
  • The rest of the method, including the log/HTML message that uses str and encodeForHTML, can remain unchanged.

No new external libraries are needed; a simple replace or replaceAll implementation is sufficient.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

davewichers and others added 2 commits February 4, 2026 16:38
Fix response splitting in: Benchmark00087.java

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@davewichers davewichers marked this pull request as ready for review February 4, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant