Skip to content

feat(hooks): let the embedder answer a refused forwarded header - #178

Merged
polaz merged 1 commit into
mainfrom
feat/#177
Dec 1, 2026
Merged

polaz merged 1 commit into
mainfrom
feat/#177

Conversation

@polaz

@polaz polaz commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

  • New embedding hook ForwardedHeaderRefusal (ProxyServer::with_forwarded_header_refusal, TranscodeOptions::with_forwarded_header_refusal). When a transcoded request is refused because a header in forwarded_headers carries a value gRPC metadata cannot carry (empty, outside visible ASCII, bad base64 under a -bin name), the hook receives the request view (RequestParts: method, path and query as received, headers, resolved client address) and the refused header's name, and returns a RouteResponse (status, headers, body) or None for the default body.
  • The request is refused whatever the hook answers: the upstream is never called, and the hook has no way to forward the value.
  • Without the hook the behaviour and the INVALID_ARGUMENT (400) body are unchanged.
  • The forwarded metadata is built where the request line is known, so the hook sees the request as the client sent it, before a router the proxy is nested in strips its prefix.
  • README documents the hook under Upstream controls and in the list of embedding hooks.

Why: a route whose protocol has its own error format could not give that format's answer, because the request never reached its handler and no hook was consulted. An OAuth token endpoint behind the transcoder must answer an empty DPoP header with 400 and {"error":"invalid_dpop_proof"} (RFC 9449 section 5, RFC 6749 section 5.2).

Testing

  • cargo nextest run --all-features: in tests/forwarded_headers.rs, each case against a remote and an in-process upstream:
    • an empty value and a non-ASCII value without the hook get the unchanged generic body;
    • the same values with the hook get the hook's status, Content-Type and body; the hook saw the method, path, query and header name;
    • a hook that declines (None) leaves the generic body;
    • carriable values never reach the hook, and the upstream sees every value.
      The upstream's call count is 0 in every refused case.
  • cargo test --doc (hook example), cargo clippy --all-targets -D warnings with all features and with no default features, cargo fmt --check, cargo doc -D warnings.

Closes #177

- New hook ForwardedHeaderRefusal (ProxyServer::with_forwarded_header_refusal,
  TranscodeOptions::with_forwarded_header_refusal): for a transcoded request
  refused because a forwarded header carries a value gRPC metadata cannot
  carry, it gets the request view and the header's name and returns a
  RouteResponse, or None for the default INVALID_ARGUMENT body
- The request is refused whatever the hook answers; the upstream is never
  called and the hook has no way to forward the value
- Without the hook the behaviour and body are unchanged
- The forwarded metadata is now built in `start`, where the request line is
  known, so the hook sees the method, path and query as received

Why: a route whose protocol has its own error format (an RFC 9449 token
endpoint answering `invalid_dpop_proof`) could not give that answer, since the
request never reached its handler and no hook was consulted.

Closes #177
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Applications can now customize the response when a forwarded header value cannot be represented as gRPC metadata.
    • If no custom response is provided, the proxy keeps its existing invalid-argument response. Refused requests do not reach the upstream.
  • Documentation
    • Clarified that requests rejected due to forwarded header values never reach the upstream.

The proxy adds an optional hook that can provide a response when a forwarded-header value cannot be represented as gRPC metadata. The request remains refused and does not reach the upstream. Tests cover hook responses, default errors, and carriable values.

Changes: Forwarded-header refusal handling

Layer / File(s) Summary
Hook API and route wiring
src/hooks.rs, src/transcode/mod.rs, src/lib.rs, README.md
A public hook accepts request parts and the refused header name, and can return an optional RouteResponse. Builder methods configure the hook and attach it to routes. The README describes this behavior.
Metadata conversion and refusal response
src/transcode/mod.rs, src/embed.rs
Forwarded-header conversion runs before request preparation. If conversion fails, the proxy returns the hook response or the existing invalid-argument response without calling the upstream. route_response converts RouteResponse values into Axum responses.
Refusal behavior tests
tests/forwarded_headers.rs
Tests cover empty and non-ASCII values with and without a hook, hook inputs, upstream call counts, and forwarding of repeated carriable values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant TranscodeStart
  participant ForwardedHeaderRefusal
  participant Upstream
  Client->>TranscodeStart: Send request with forwarded header
  TranscodeStart->>TranscodeStart: Convert forwarded header to gRPC metadata
  TranscodeStart->>ForwardedHeaderRefusal: Report refused header and request parts
  ForwardedHeaderRefusal-->>TranscodeStart: Return optional RouteResponse
  TranscodeStart-->>Client: Return hook response or default error
Loading
Merge Risk: Minimal · up to 8f141

This change adds an optional hook that lets an embedder shape the response when a forwarded header cannot be carried as gRPC metadata. Without the hook, behavior and the default error body are unchanged. Refused requests still never reach the upstream. The change is additive and the tests cover the new paths, so no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the feature added: an embedder hook that answers a forwarded header refused because it cannot be represented as gRPC metadata. The changeset adds ForwardedHeaderRefusal, with_forwarded…
Description check Passed The description states the new ForwardedHeaderRefusal hook, its builder methods, the conditions under which it runs, and the fact that the upstream is never called. It also lists the README update, th…
Linked Issues check Passed [#177] requires: (1) no behavior change without the hook; (2) with the hook, a refused forwarded header on a matching request gets the hook's status and body, and the upstream is not called; (3) the h…
Out of Scope Changes check Passed All changed areas connect to #177. README.md documents the new hook (direct documentation of the feature). src/hooks.rs adds the hook trait and its import, and the RouteResponse doc now lists it as a …
Full details: Title check

Explanation

The title names the feature added: an embedder hook that answers a forwarded header refused because it cannot be represented as gRPC metadata. The changeset adds ForwardedHeaderRefusal, with_forwarded_header_refusal on ProxyServer and TranscodeOptions, and tests covering refused headers. The title is concise, specific, and follows the conventional-commit style. It does not describe the full scope (README, tests), which is acceptable.

Full details: Description check

Explanation

The description states the new ForwardedHeaderRefusal hook, its builder methods, the conditions under which it runs, and the fact that the upstream is never called. It also lists the README update, the testing performed, and the motivating OAuth example. This is directly related to the changeset.

Full details: Linked Issues check

Explanation

[#177] requires: (1) no behavior change without the hook; (2) with the hook, a refused forwarded header on a matching request gets the hook's status and body, and the upstream is not called; (3) the hook sees the header name, method and path, and cannot force forwarding; (4) tests for empty and non-ASCII values, with and without the hook. The whole-PR summary shows: ForwardedHeaderRefusal in src/hooks.rs (respond(&self, &RequestParts, &HeaderName) -> Option<RouteResponse>), wired through ProxyServer::with_forwarded_header_refusal (src/lib.rs) and TranscodeOptions::with_forwarded_header_refusal (src/transcode/mod.rs). Refusal runs in start before request preparation, so the upstream is not called. The hook return value cannot change the refusal. Without a hook, the generic INVALID_ARGUMENT body is kept. tests/forwarded_headers.rs covers empty and non-ASCII values with and without the hook, a declining hook, and a call count of 0 for refused cases. The #177 example (dpop, invalid_dpop_proof) is covered by the hook test. Non-coding items (manual checks, release steps) do not apply. Evidence is the PR summary; I did not read the code hunks directly.

Full details: Out of Scope Changes check

Explanation

All changed areas connect to #177. README.md documents the new hook (direct documentation of the feature). src/hooks.rs adds the hook trait and its import, and the RouteResponse doc now lists it as a producer. src/lib.rs adds the builder method on ProxyServer. src/transcode/mod.rs moves forwarded-metadata conversion into start so the hook sees the request before prefix stripping, copies the option per route, and changes prepare to accept converted metadata; this refactor supports the required behavior. src/embed.rs extracts a route_response helper that extra-route handlers now use; this is a small refactor that shares the response conversion the hook needs. tests/forwarded_headers.rs adds the required tests and call counting. No unrelated feature or change is shown in the summary. Uncertainty: the summary does not list every hunk, so the verdict relies on the summary's description.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: b23e6d2a-4a73-45e9-be36-d5bc3935464e
Commits

Reviewing files that changed from the base of the PR and between c9b03b0 and 8f1418b.

Files selected for processing: 6

  • README.md
  • src/embed.rs
  • src/hooks.rs
  • src/lib.rs
  • src/transcode/mod.rs
  • tests/forwarded_headers.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T07:55:09.852440Z 8f1418b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Adds ForwardedHeaderRefusal so embedders can choose the response when a forwarded header cannot become gRPC metadata.

  • Transcoded requests can get a custom answer when a forwarded header value cannot cross gRPC.

Reviews (1) · Last reviewed commit: "feat(hooks): let the embedder answer a r..." · Reviewed by Greptile

@polaz
polaz enabled auto-merge (squash) December 1, 2026 07:59
@polaz
polaz merged commit 5eb64ce into main Dec 1, 2026
12 checks passed
@polaz
polaz deleted the feat/#177 branch December 1, 2026 08:00
@sw-release-bot sw-release-bot Bot mentioned this pull request Dec 1, 2026
polaz pushed a commit that referenced this pull request Dec 1, 2026
## 🤖 New release

* `structured-proxy`: 8.2.0 -> 9.0.0 (⚠ API breaking changes)

### ⚠ `structured-proxy` breaking changes

```text
--- failure auto_trait_impl_removed: auto trait no longer implemented ---

Description:
A public type has stopped implementing one or more auto traits. This can break downstream code that depends on the traits being implemented.
        ref: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.51.0/src/lints/auto_trait_impl_removed.ron

Failed in:
  type TranscodeOptions is no longer UnwindSafe, in /tmp/.tmplUABYd/structured-proxy/src/transcode/mod.rs:207
  type TranscodeOptions is no longer RefUnwindSafe, in /tmp/.tmplUABYd/structured-proxy/src/transcode/mod.rs:207
```

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[9.0.0](v8.2.0...v9.0.0)
- 2026-12-01

### Added

- *(hooks)* let the embedder answer a refused forwarded header
([#178](#178))

### Other

- *(deps)* bump taiki-e/install-action from 2.90.3 to 2.90.8
([#175](#175))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

Co-authored-by: sw-release-bot[bot] <255865126+sw-release-bot[bot]@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.

Let the embedder shape the answer to a refused forwarded header

1 participant