Skip to content

[NEW DATA EXTENSION] http4k request sources and response sinks (Java/Kotlin) - #7

Draft
felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/data-extension-create-http4k
Draft

felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/data-extension-create-http4k

Conversation

Copilot AI commented Oct 9, 2026 •

Copy link
Copy Markdown

📝 Data Extension Information

  • Language: java (Kotlin)
  • Extension Name(s): http4k-core.model.yml, http4k-lens.model.yml, http4k-routing.model.yml
  • Extension Types: sourceModel, sinkModel, summaryModel
  • Target Library/Framework: http4k
  • Library Modules Covered: org.http4k.core (plus core.body, core.cookie), org.http4k.lens, org.http4k.routing

🎯 Description

What This Data Extension Models

CodeQL has no http4k models. As a result, request data is never treated as remote input, and response bodies and headers are not sinks. These models let the existing java/sql-injection, java/xss, java/unvalidated-url-redirection, java/http-response-splitting and java/path-injection queries detect http4k flows.

  • Sources (remote)
    • HttpMessage: bodyString(), getBody(), header(String), headerValues(String)
    • Request: query, queries, getUri
    • Extension functions: form(Request,String), cookie(Request,String)
    • Lenses: LensExtractor.invoke/get/extract(Object), including subtypes. This covers Query.required(...), Header.* and Body.string(...) lenses.
    • Path parameters: Request.path(String), both routing.ExtensionsKt (6.x) and RoutingKt (5.x). This goes beyond the issue's list.
  • Summaries (taint)
    • Body: getPayload, getStream, getText, and Body(...)/Body.create
    • Uri: getQuery, getPath, getHost and the other component getters, plus toString and queryParametersEncoded
    • Cookie.getValue
  • Sinks
    • Response.body(String | Body | InputStream,Long), Argument[0]: html-injection
    • Response.header / replaceHeader(String,String), Argument[1]: response-splitting and url-redirection

Design notes and known limitations:

  • Uri getters are summaries, not sources. The issue proposed Uri.getQuery() as a source. Modeling it as a summary avoids flagging constant Uri.of("..."). request.uri is the source, so request.uri.query flows are still caught.
  • No content-type check for XSS. Response.body cannot check the response content type, so JSON responses that echo input will also be reported.
  • No Location check for url-redirection. The url-redirection sink cannot restrict to the Location header name, so any tainted response-header value is reported.
  • http4k ≥ 6.49.0.0 strips CR/LF. These versions strip CR/LF from header names and values (sanitizeHeader), so response-splitting results are false positives there. Versions ≤ 6.48 are not protected, including 5.13.0.0 used by the reference app. The sink is kept because MaD cannot tell versions apart.
  • Default-argument calls are not modeled. Kotlin calls that rely on default arguments resolve to synthetic $default methods. For example, Response.body(stream) without a length is not matched. Modeling HttpMessage.body$default would also match Request, so it is left out.
  • Lens sources are broad. They also fire on RequestContextKey lenses and on lenses applied to client-side Responses.

Threat Model

remote

Example Vulnerable Code

fun sql(request: Request): Response {
    val id = Query.required("id")(request)                    // source
    conn.createStatement().executeQuery("SELECT * FROM u WHERE id = $id") // java/sql-injection
    return Response(Status.OK)
}

fun redirect(request: Request): Response {
    val target = request.uri.query.substringAfter("target=")  // source + summary
    return Response(Status.MOVED_PERMANENTLY).header("Location", target)  // java/unvalidated-url-redirection, java/http-response-splitting
}

fun xss(request: Request): Response =
    Response(Status.OK).header("Content-Type", "text/html")
        .body("<p>${request.query("data")}</p>")               // java/xss

Example Safe Code

fun safe(request: Request): Response =
    Response(Status.FOUND).header("Location", "/home").body("<p>static</p>")

📦 Extension Details

Extension YAML

The models are in a new pack, languages/java/custom/models/ (languages-java-custom-models, library: true, extensionTargets: codeql/java-all). Excerpt:

extensions:
  - addsTo:
      pack: codeql/java-all
      extensible: sourceModel
    data:
      - ["org.http4k.core", "Request", True, "query", "(String)", "", "ReturnValue", "remote", "manual"]
      - ["org.http4k.lens", "LensExtractor", True, "invoke", "(Object)", "", "ReturnValue", "remote", "manual"]
  - addsTo:
      pack: codeql/java-all
      extensible: sinkModel
    data:
      - ["org.http4k.core", "Response", True, "body", "(String)", "", "Argument[0]", "html-injection", "manual"]
      - ["org.http4k.core", "Response", True, "header", "(String,String)", "", "Argument[1]", "url-redirection", "manual"]
  - addsTo:
      pack: codeql/java-all
      extensible: summaryModel
    data:
      - ["org.http4k.core", "Uri", True, "getQuery", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"]

Access Path Explanation

Signatures are the JVM signatures of the compiled Kotlin, checked with javap against http4k-core 5.13.0.0 and 6.58.0.0. They match across both versions.

  • Properties map to getters: request.uri is getUri().
  • Top-level extension functions map to <File>Kt with the receiver as Argument[0], for example FormBodyKt.form(Request,String).
  • Body(...) resolves to Body$Companion.create.
  • Generic lens methods erase to (Object).

Related changes:

  • Model pack location. The models are in their own pack because an extension pack must be library: true with no dependencies. The query pack in custom/src cannot host them, and the data-extension prompt that points there should be updated in a follow-up.
  • Test pack. languages/java/custom/test/qlpack.yml now depends on codeql/java-queries and the models pack.
  • CI workflow. codeql-qlt-unit-tests.yml now sets CODEQL_KOTLIN_LEGACY_TEST_EXTRACTION_KOTLIN2=true. Without it, codeql test run passes -language-version 1.9 to kotlinc, which kotlinc 2.4+ rejects.
  • CI coverage gap. The workflow's path filters do not include languages/*/custom/**, so these tests do not run in CI yet. Extending the filters is a suggested follow-up.

🧪 Testing

  • Extension YAML resolves without errors
  • Database created with sample code (codeql database create or codeql test extract)
  • Single query verified with extension applied (codeql query run --additional-packs=<model-pack-dir>)
  • Unit tests pass with extension applied (codeql test run --additional-packs=<model-pack-dir>)
  • Positive test cases (vulnerable code detected)
  • Negative test cases (safe code not flagged)

The five queries were run against Kotlin databases built with the real 5.13.0.0 and 6.58.0.0 jars. With no models there were 0 results. With the models, every positive case was detected and no constant case was flagged.

Unit tests are in languages/java/custom/test/http4k/. They use Kotlin stubs, inline expectations and one qlref per query, and cover each source and sink shape plus constant-value negatives. With the models removed, the tests report 48 missing results.

📋 Checklist

  • Extension YAML is valid and properly formatted
  • Extension placed in correct location (languages/[language]/custom/src/). Placed in languages/java/custom/models/ instead, because an extension pack cannot be a query pack (see above).
  • qlpack.yml includes dataExtensions configuration
  • Access paths verified via API graph queries. Java MaD does not use API graphs; signatures were verified with javap and real-jar databases instead.
  • No false positives in test cases
  • Documentation/comments included in YAML

🔗 References


Note: This data extension was developed following CodeQL Models as Data best practices.

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add data extension for http4k request sources and response sinks [NEW DATA EXTENSION] http4k request sources and response sinks (Java/Kotlin) Oct 9, 2026
Copilot AI requested a review from felickz October 9, 2026 01:26
@felickz
felickz requested a balanced review from Copilot October 30, 2026 18:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved taint-modeling gaps cause both false positives and missed vulnerabilities.

8 open issues
What changed in this PR

Adds a Java/Kotlin CodeQL model pack for http4k so existing security queries can trace request data through framework APIs to injection sinks, addressing the coverage gap in issue #1.

Changes:

  • Models request sources, response sinks, and taint propagation across core, lens, and routing APIs.
  • Adds Kotlin fixtures, API stubs, and expected results for five security queries.
  • Connects the test pack to the models and enables Kotlin 2 extraction in CI.
File Description
languages/​java/​custom/​test/​qlpack.yml Adds query and model pack dependencies.
languages/​java/​custom/​test/​http4k/​XSS.qlref Configures XSS regression testing.
languages/​java/​custom/​test/​http4k/​XSS.expected Records expected XSS flows.
languages/​java/​custom/​test/​http4k/​UrlRedirect.qlref Configures redirect regression testing.
languages/​java/​custom/​test/​http4k/​UrlRedirect.expected Records expected redirect flows.
languages/​java/​custom/​test/​http4k/​TaintedPath.qlref Configures path-injection regression testing.
languages/​java/​custom/​test/​http4k/​TaintedPath.expected Records expected path-injection flows.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​routing/​extensions.kt Stubs path-parameter extraction.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​lens/​query.kt Stubs query-lens construction.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​lens/​lens.kt Stubs lens implementations and inheritance.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​lens/​header.kt Stubs header-lens construction.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​lens/​extractInject.kt Defines the generic lens-extraction interface.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​lens/​body.kt Stubs string body-lens construction.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​core/​Uri.kt Stubs URI accessors and factories.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​core/​http.kt Stubs core HTTP message and body APIs.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​core/​cookie/​CookieExtensions.kt Stubs request-cookie extraction.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​core/​cookie/​Cookie.kt Defines cookie test properties.
languages/​java/​custom/​test/​http4k/​stubs/​org/​http4k/​core/​body/​FormBody.kt Stubs form-value extraction.
languages/​java/​custom/​test/​http4k/​SqlTainted.qlref Configures SQL-injection regression testing.
languages/​java/​custom/​test/​http4k/​SqlTainted.expected Records expected SQL-injection flows.
languages/​java/​custom/​test/​http4k/​ResponseSplitting.qlref Configures response-splitting regression testing.
languages/​java/​custom/​test/​http4k/​ResponseSplitting.expected Records expected header-injection flows.
languages/​java/​custom/​test/​http4k/​Http4kRoutes.kt Adds vulnerable flows and constant-value controls.
languages/​java/​custom/​models/​qlpack.yml Declares the Java data-extension pack.
languages/​java/​custom/​models/​http4k-routing.model.yml Models path-parameter sources across http4k versions.
languages/​java/​custom/​models/​http4k-lens.model.yml Models lens extraction as remote input.
languages/​java/​custom/​models/​http4k-core.model.yml Adds core sources, sinks, and taint summaries.
.github/​workflows/​codeql-qlt-unit-tests.yml Enables Kotlin 2 test extraction in CI.

Review effort: Balanced

Comment on lines +11 to +14
- ["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"]

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.

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.

Comment on lines +11 to +13
- ["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"]

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.

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
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.

[Data Extension Create]: CWE-89, CWE-79, CWE-601, CWE-113, CWE-22 HIGH - http4k request sources and response sinks (Java/Kotlin)

3 participants