Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/codeql-qlt-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ jobs:
RUNNER_TMP: ${{ runner.temp }}
# Additional environment variables for specific languages
LGTM_INDEX_XML_MODE: all
# Extract Kotlin unit tests with `-language-version 2.0` (kotlinc 2.4+ no longer supports 1.9)
CODEQL_KOTLIN_LEGACY_TEST_EXTRACTION_KOTLIN2: "true"
shell: bash
run: |
echo "Running CodeQL unit tests for language: ${{ matrix.language }}"
Expand Down
10 changes: 10 additions & 0 deletions languages/java/custom/models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# http4k model limitations

## Known limitations

- `HttpMessage` accessors are modeled as remote sources, so locally constructed responses can also be marked remote. Data extensions cannot restrict these sources to inbound requests or client responses, and they cannot model lambda handler parameters as sources.
- Response body methods are modeled as HTML sinks without checking the response `Content-Type`. This can report XSS for JSON or plain-text responses; unknown or unset content types also cannot be distinguished from HTML by a data extension.
- `LensExtractor` methods are modeled as remote sources for every extractor output. This can mark values from local objects or `RequestContextKey` as remote; limiting sources to known lens factories could miss custom lenses.
- URI update summaries preserve taint on the original `Uri`. They do not distinguish replaced from preserved components, so replacing a tainted component with a constant may still leave the resulting URI tainted.

Redirect sinks for http4k are handled by the custom Java query in `languages/java/custom/src/http4k/UrlRedirect.ql`, which only treats `Location` header values as redirects.
198 changes: 198 additions & 0 deletions languages/java/custom/models/http4k-core.model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# Models for the http4k Kotlin HTTP toolkit core module (`org.http4k:http4k-core`).
# https://github.com/http4k/http4k
#
# Signatures were verified against the compiled http4k-core 5.13.0.0 and 6.58.0.0 jars.
extensions:
- addsTo:
pack: codeql/java-all
extensible: sourceModel
data:
# Request (and HTTP client response) data.
- ["org.http4k.core", "HttpMessage", True, "bodyString", "()", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.core", "HttpMessage", True, "getBody", "()", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.core", "HttpMessage", True, "header", "(String)", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.core", "HttpMessage", True, "headerValues", "(String)", "", "ReturnValue", "remote", "manual"]
Comment on lines +11 to +14

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented this as a known limitation in languages/java/custom/models/README.md. Data extensions cannot narrow these HttpMessage sources to inbound requests or client responses, and modeling handler parameters requires customized QL.

- ["org.http4k.core", "Request", True, "getUri", "()", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.core", "Request", True, "queries", "(String)", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.core", "Request", True, "query", "(String)", "", "ReturnValue", "remote", "manual"]
# Kotlin extension functions `Request.form(name)` and `Request.cookie(name)`.
- ["org.http4k.core.body", "FormBodyKt", False, "form", "(Request,String)", "", "ReturnValue", "remote", "manual"]
- [
"org.http4k.core.cookie",
"CookieExtensionsKt",
False,
"cookie",
"(Request,String)",
"",
"ReturnValue",
"remote",
"manual"
]

- addsTo:
pack: codeql/java-all
extensible: sinkModel
data:
# `Response.body(...)` writes the HTTP response body. http4k responses are not
# HTML-encoded and the content type cannot be checked by a data extension.
- ["org.http4k.core", "Response", True, "body", "(Body)", "", "Argument[0]", "html-injection", "manual"]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented this as a known limitation. Data extensions cannot make the Response.body sink conditional on Content-Type; distinguishing non-HTML responses while retaining unknown types requires a customized XSS query.

- [
"org.http4k.core",
"Response",
True,
"body",
"(InputStream,Long)",
"",
"Argument[0]",
"html-injection",
"manual"
]
- ["org.http4k.core", "Response", True, "body", "(String)", "", "Argument[0]", "html-injection", "manual"]
# Response header values. http4k only strips CR/LF from header values from
# version 6.49.0.0, so `response-splitting` applies to earlier versions.
# URL redirection is modeled in the custom query because the header name
# cannot be constrained by a data extension.
- [
"org.http4k.core",
"Response",
True,
"header",
"(String,String)",
"",
"Argument[1]",
"response-splitting",
"manual"
]
Comment thread
felickz marked this conversation as resolved.
Resolved
- [
"org.http4k.core",
"Response",
True,
"header",
"(String,String)",
"",
"Argument[0]",
"response-splitting",
"manual"
]
- [
"org.http4k.core",
"Response",
True,
"replaceHeader",
"(String,String)",
"",
"Argument[1]",
"response-splitting",
"manual"
]
- [
"org.http4k.core",
"Response",
True,
"replaceHeader",
"(String,String)",
"",
"Argument[0]",
"response-splitting",
"manual"
]

- addsTo:
pack: codeql/java-all
extensible: summaryModel
data:
- ["org.http4k.core", "Body", True, "getPayload", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Body", True, "getStream", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Body", True, "getText", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- [
"org.http4k.core",
"Body",
False,
"create",
"(InputStream,Long)",
"",
"Argument[0]",
"ReturnValue",
"taint",
"manual"
]
- ["org.http4k.core", "Body", True, "create", "(ByteBuffer)", "", "Argument[0]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Body", False, "create", "(String)", "", "Argument[0]", "ReturnValue", "taint", "manual"]
Comment thread
felickz marked this conversation as resolved.
Resolved
- [
"org.http4k.core",
"Body$Companion",
False,
"create",
"(InputStream,Long)",
"",
"Argument[0]",
"ReturnValue",
"taint",
"manual"
]
- [
"org.http4k.core",
"Body$Companion",
False,
"create",
"(ByteBuffer)",
"",
"Argument[0]",
"ReturnValue",
"taint",
"manual"
]
- [
"org.http4k.core",
"Body$Companion",
False,
"create",
"(String)",
"",
"Argument[0]",
"ReturnValue",
"taint",
"manual"
]
- ["org.http4k.core", "Uri", False, "getAuthority", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "getFragment", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "getHost", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "getPath", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "getQuery", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
Comment thread
felickz marked this conversation as resolved.
Resolved
- ["org.http4k.core", "Uri", False, "getScheme", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "getUserInfo", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "toString", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", True, "of", "(String)", "", "Argument[0]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri$Companion", False, "of", "(String)", "", "Argument[0]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "scheme", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "userInfo", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "host", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "port", "(Integer)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "path", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "query", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "fragment", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- ["org.http4k.core", "Uri", False, "authority", "(String)", "", "Argument[this]", "ReturnValue", "taint", "manual"]
- [
"org.http4k.core",
"UriKt",
False,
"queryParametersEncoded",
"(Uri)",
"",
"Argument[0]",
"ReturnValue",
"taint",
"manual"
]
- [
"org.http4k.core.cookie",
"Cookie",
False,
"getValue",
"()",
"",
"Argument[this]",
"ReturnValue",
"taint",
"manual"
]
13 changes: 13 additions & 0 deletions languages/java/custom/models/http4k-lens.model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Models for the http4k lens API (`org.http4k.lens`, shipped in `org.http4k:http4k-core`).
# https://www.http4k.org/guide/reference/lens/
#
# Lenses built with `Query`, `Header`, `Path`, `FormField`, `Body`, etc. extract values
# from an HTTP message via `lens(request)`, `lens.extract(request)` or `lens[request]`.
extensions:
- addsTo:
pack: codeql/java-all
extensible: sourceModel
data:
- ["org.http4k.lens", "LensExtractor", True, "extract", "(Object)", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.lens", "LensExtractor", True, "get", "(Object)", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.lens", "LensExtractor", True, "invoke", "(Object)", "", "ReturnValue", "remote", "manual"]
Comment on lines +11 to +13

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented this as a known limitation. Generic LensExtractor source rows cannot distinguish request-backed lenses from local, context, or custom lenses; narrowing them could omit custom lens values.

11 changes: 11 additions & 0 deletions languages/java/custom/models/http4k-routing.model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Models for the http4k routing extensions (`org.http4k.routing`, shipped in `org.http4k:http4k-core`).
#
# `Request.path(name)` returns a path parameter. It is compiled into `RoutingKt`
# in http4k 5.x and into `ExtensionsKt` in http4k 6.x.
extensions:
- addsTo:
pack: codeql/java-all
extensible: sourceModel
data:
- ["org.http4k.routing", "ExtensionsKt", False, "path", "(Request,String)", "", "ReturnValue", "remote", "manual"]
- ["org.http4k.routing", "RoutingKt", False, "path", "(Request,String)", "", "ReturnValue", "remote", "manual"]
7 changes: 7 additions & 0 deletions languages/java/custom/models/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: languages-java-custom-models
version: 0.0.1
library: true
extensionTargets:
codeql/java-all: "*"
dataExtensions:
- "**/*.model.yml"
48 changes: 48 additions & 0 deletions languages/java/custom/src/http4k/UrlRedirect.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @name URL redirection from remote source in http4k
* @description URL redirection based on unvalidated user-input
* may cause redirection to malicious web sites.
* @kind path-problem
* @problem.severity error
* @security-severity 6.1
* @precision high
* @id java/unvalidated-url-redirection
* @tags security
* external/cwe/cwe-601
*/

import java
import semmle.code.java.dataflow.DataFlow
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.dataflow.TaintTracking
import semmle.code.java.security.UrlRedirect

private class Http4kResponseLocationSink extends UrlRedirectSink {
Http4kResponseLocationSink() {
exists(MethodCall call |
call.getMethod().getDeclaringType().hasQualifiedName("org.http4k.core", "Response") and
call.getMethod().hasName(["header", "replaceHeader"]) and
call.getArgument(0).(CompileTimeConstantExpr).getStringValue().toLowerCase() = "location" and
this.asExpr() = call.getArgument(1)
)
}
}

module Http4kUrlRedirectConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource }

predicate isSink(DataFlow::Node sink) { sink instanceof UrlRedirectSink }

predicate isBarrier(DataFlow::Node node) { node instanceof UrlRedirectSanitizer }

predicate observeDiffInformedIncrementalMode() { any() }
}

module Http4kUrlRedirectFlow = TaintTracking::Global<Http4kUrlRedirectConfig>;

import Http4kUrlRedirectFlow::PathGraph

from Http4kUrlRedirectFlow::PathNode source, Http4kUrlRedirectFlow::PathNode sink
where Http4kUrlRedirectFlow::flowPath(source, sink)
select sink.getNode(), source, sink, "Untrusted URL redirection depends on a $@.", source.getNode(),
"user-provided value"
Loading
Loading