Skip to content

fix: don't trust incoming request IDs by default - #696

Merged
uniqueg merged 3 commits into
devfrom
fix/695-request-id-trust
Nov 4, 2026
Merged

uniqueg merged 3 commits into
devfrom
fix/695-request-id-trust

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

server.request_id.trust_incoming now defaults to false, in the configuration model and in RequestIdMiddleware. Each request gets a generated identifier, unless a deployment behind a proxy that sets or checks the header opts in. So clients no longer choose the identifier in logs, the response header, audit events and the records of gateways.

Closes #695

  • Docs: rewrites the garbled paragraph of docs/configuration/server.md; docs/migration-0.21.md names the option, its effect on audit events and the change for gateways, whose request_id() took a client's value unconditionally in 0.20; adjusts docs/configuration/log.md, docs/plugins.md and docs/helm-chart.md.
  • Chart: nothing to change, as it doesn't configure the option; the Helm guide says how to set it.
  • Gateway example: already sets trust_incoming: True explicitly for its integration test, with the overlay no-trust.yaml for the other value. Its comment now warns against copying that.
  • inbound_from_request() still copies a client's X-Request-ID into the inbound headers, as before. The docs now say so, rather than claiming that the header is ignored.

Verification

  • Failing tests first: 4 failed before the change (test_not_trusted, TestApp::test_default, test_request_id_of_client, adapter test_request_id), then passed.
  • coverage run -m pytest against a MongoDB 7.0.34 of its own, on the first commit: 4250 passed, 2 skipped, 100% coverage. After the review fixes (docs, comments, tests): the affected test files, 421 passed.
  • ruff check, ruff format --check, mypy, mkdocs build --strict, ast-grep scan, semgrep, and pre-commit on the 14 changed files: passed.
  • Not run locally: tests/integration_gateway_tests.py (needs Docker) and the Helm chart tests; these ran in CI.
  • CI on edaba99: CI passed, including all test jobs, Integration test, Chart, Chart install, Lint, image scans and CodeQL.
  • Sourcery's findings fixed in 3cf7708 (docs, docstrings, an example comment): mypy, ruff, mkdocs build --strict and pre-commit passed locally; CI ran again before the merge.
  • ingress-nginx passing on a client's value: checked in its nginx.tmpl (map of $http_x_request_id) on main.

Declined review findings

  • Keep the explanation in server.md only and link to it from the migration guide and the Helm guide (low): the migration guide is what the issue asks to name this in, and it should stand on its own for people upgrading.
  • Invert the gateway example, so that the base config uses the default and an overlay opts in (low): the example sets the option explicitly for its integration test, as decided, and is marked as not for production. A comment warns against copying it.
  • Remove X-Request-ID from the inbound headers in the adapter (low, offered as an alternative): it changes what plugins see, beyond this issue. Documented instead.

🤖 Generated with Claude Code

uniqueg and others added 2 commits November 4, 2026 16:25
`server.request_id.trust_incoming` defaulted to true, so every FOCA app
took a valid `X-Request-ID` from any client, into logs, the response
header and audit events. Default to false, also in the middleware, so
that deployments behind a proxy that sets or checks the header opt in.

Gateways took the client's value unconditionally in 0.20; the migration
guide names that change and its effect on audit events and records.

Closes #695

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Say that the header isn't used as the identifier rather than ignored, as
inbound requests keep it for plugins; name both conditions of opting in
in the Helm guide, and records of gateways among what clients could
choose. Warn against copying the opt-in of the gateway example. Check
the generated identifier of audit events and the default of the model.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR changes request ID handling from trusted-by-default to generated-by-default: middleware and configuration now ignore client-supplied IDs unless explicitly enabled for a suitably isolated proxy deployment, with tests covering the behavioral impact and documentation updated for logging, auditing, gateways, plugins, Helm, and migration.

Sequence diagram for generated request ID handling

sequenceDiagram
    participant Client
    participant RequestIdMiddleware
    participant App
    participant Logs
    participant Audit
    participant Gateway

    Client->>RequestIdMiddleware: HTTP request with X-Request-ID
    alt trust_incoming is false
        RequestIdMiddleware->>RequestIdMiddleware: Generate request ID
    else trust_incoming is true
        RequestIdMiddleware->>RequestIdMiddleware: Validate X-Request-ID
        alt valid request header
            RequestIdMiddleware->>RequestIdMiddleware: Use incoming request ID
        else invalid or missing header
            RequestIdMiddleware->>RequestIdMiddleware: Generate request ID
        end
    end
    RequestIdMiddleware->>App: Forward request with request ID context
    App->>Logs: Record request ID
    App->>Audit: Record request ID
    App->>Gateway: Record request ID
    App-->>Client: Response with X-Request-ID
Loading

File-Level Changes

Change Details Files
Make incoming request IDs opt-in, ensuring generated IDs are used by default across application responses, logs, audit events, and gateway provenance.
  • Changed middleware and configuration defaults for trust_incoming to false.
  • Preserved validated incoming-ID behavior when deployments explicitly opt in, including custom headers and invalid-value fallback.
  • Added coverage for default, trusted, and untrusted behavior in middleware, app configuration, adapter context, audit events, and logging context.
foca/context.py
foca/models/config.py
tests/test_context.py
tests/models/test_config.py
tests/test_audit.py
tests/plugins/test_plugin_adapter.py
tests/utils/test_logging_context.py
Document the security and migration implications of no longer trusting client-supplied request IDs by default.
  • Explained the proxy requirements and risks of enabling trust_incoming, including proxy-specific header behavior.
  • Documented effects on audit events, gateway records, Helm deployments, plugins, and 0.20-to-0.21 migration.
  • Clarified that X-Request-ID remains available in inbound request headers even when it is not used as the request identifier.
docs/configuration/server.md
docs/configuration/log.md
docs/helm-chart.md
docs/migration-0.21.md
docs/plugins.md
Keep the gateway integration example explicitly trusted while warning that it is test-only configuration.
  • Retained the explicit opt-in required by the integration test.
  • Added a warning not to copy the setting without a proxy that controls the header.
examples/gateway/config.yaml
Align provenance documentation with the distinction between generated run IDs and optionally trusted request IDs.
  • Updated failure-record documentation to describe the configurable source of request_id while retaining random run IDs as record keys.
foca/plugins/provenance.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3e4f57c) to head (3cf7708).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #696   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           65        65           
  Lines         8523      8523           
=========================================
  Hits          8523      8523           
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 4, 2026 16:18

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="docs/configuration/server.md" line_range="92-93" />
<code_context>
+By default, each request gets a new random identifier, and what clients send in the
+header isn't used as the identifier. `header` can't be a standard header such as `Content-Length`.
+
+Set `trust_incoming: true` if a proxy in front of the app sets or checks the header,
+and clients can't reach the app past it, e.g., to have the proxy's identifier in the
+app's logs. The app then takes the identifier from the request header if it has at most
+128 letters, digits and `._:-`, and otherwise generates one. Without such a proxy,
</code_context>
<issue_to_address>
**Clients control trusted request IDs**

When a proxy validates the header but forwards a client's value while `trust_incoming` is enabled, `RequestIdMiddleware` accepts any syntactically valid incoming value, so clients still choose the response, log, audit, and gateway-record identifiers.

Clarify that the option is safe only when the proxy replaces the incoming value and clients cannot reach the app directly.

Also at `docs/helm-chart.md:256-257`, `docs/migration-0.21.md:140-141`, `foca/context.py:81-82`.
</issue_to_address>

### Comment 2
<location path="docs/migration-0.21.md" line_range="149-152" />
<code_context>
+This changes the behaviour of gateways: the `request_id()` of `foca.plugins.adapter`
+took a valid `X-Request-ID` of any client in 0.20, unconditionally, and now follows
+[`server.request_id`](configuration/server.md#request-ids). By default, the field
+`request_id` of [pipeline runs and their records](plugins.md#provenance) therefore has the
+generated identifier, not what the client sent. Clients and dashboards that look up
+these records by their own value need to take the identifier from the response header
+instead, or a deployment that meets the conditions above sets the option to `true`.

 ## Command-line tool
</code_context>
<issue_to_address>
**Worker records lack request IDs**

When a gateway processes a request through its Celery worker, the worker path calls `PluginHost.start` without `request_id`, so its pipeline records still have no request identifier and cannot be correlated using the HTTP response header, contrary to the migration guidance.

Qualify the migration guidance for synchronous gateway requests and explain that worker-based gateways must explicitly propagate an identifier if they want it recorded and correlated.
</issue_to_address>

Linked issues

#695 fix: don't trust incoming request IDs by default · 3 of 3 met

# Requirement Status Notes
1 Default server.request_id.trust_incoming to false and let deployments behind a proxy that handles the header opt in. ✅ Met The configuration model and RequestIdMiddleware default to false; the app passes the configured value to the middleware, which accepts valid incoming IDs when enabled.
2 Rewrite the garbled paragraph in docs/configuration/server.md. ✅ Met The section now explains the default, opt-in conditions, validation of incoming IDs, and the risk of proxies passing through client values.
3 In docs/migration-0.21.md, name the option and its effect on audit events, including the change for gateways whose old behaviour was unconditional. ✅ Met The migration guide identifies the option, explains its effect on audit events and gateway records, and describes gateways' unconditional use of valid client IDs in 0.20.

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and deployments that relied on a proxy or client’s X-Request-ID will instead get a generated ID unless they opt in, so audit and gateway provenance records may contain IDs that their existing lookups cannot correlate to. Reverting restores the old behavior for future requests, but it does not change records already written.

Blocking findings: docs/configuration/server.md:93


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread docs/configuration/server.md Resolved
Comment thread docs/migration-0.21.md Resolved
A proxy that only checks the format of the header leaves the choice to
clients, as the app takes any valid value: it must replace or reject
what they send. Also say that runs in Celery tasks only have the
identifier that the app passes on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 8e23b72 into dev Nov 4, 2026
59 of 83 checks passed
@uniqueg
uniqueg deleted the fix/695-request-id-trust branch November 4, 2026 16:49
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.

fix: don't trust incoming request IDs by default

1 participant