Skip to content

fix(supabase): drop trace headers when a redirect leaves the Supabase hosts - #1895

Merged
spydon merged 5 commits into
mainfrom
lukasklingsbo/sdk-1975-fixsupabase-drop-trace-headers-when-a-redirect-leaves-the
Sep 30, 2026
Merged

spydon merged 5 commits into
mainfrom
lukasklingsbo/sdk-1975-fixsupabase-drop-trace-headers-when-a-redirect-leaves-the

Conversation

@spydon

@spydon spydon commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

TracePropagationClient decided whether to add traceparent, tracestate and baggage from the URL a request is sent to, then left redirects to the inner client. That client copies every header onto the redirected request: dart:io for GET and HEAD redirects and a 303 on a POST, and clients like cupertino_http or cronet_http for writes too. A Supabase host redirecting a traced request to another host therefore handed that host the trace headers, including baggage, which carries whatever the application puts in it.

When the client has added trace headers to a request, it now turns off followRedirects on it and follows the redirects itself:

  • A 303 is followed with a GET without a body (a HEAD stays a HEAD), a 307 or 308 with the same method and body, and a 301 or 302 only for a GET or HEAD, as dart:io does.
  • A body is sent again only when the request is a plain Request. A streamed or multipart request answered with a 307 or 308 is returned unfollowed, since its body cannot be read twice.
  • Location is resolved against the current URL, and maxRedirects and loop detection surface as a ClientException, as IOClient reports them.
  • On each hop, the trace headers the client added are removed once the target is not a Supabase host, and stay removed. Trace headers the caller set themselves are left alone.
  • On a hop to another origin, the headers dart:io drops are removed: authorization, www-authenticate, proxy-authorization, proxy-authenticate, cookie and cookie2. Following redirects here is never less safe than leaving them to dart:io.
  • The final response reports the request the caller sent and the URL it was redirected to, as IOClient does.

Requests without added trace headers and requests that already set followRedirects to false are sent unchanged.

Two behavior changes for requests carrying trace headers: with the default client, a 307 or 308 on a POST, PUT, PATCH or DELETE is now followed, where dart:io returned it. With a custom client that follows those itself, a streamed upload answered with a 307 or 308 now comes back unfollowed.

On the web nothing changes: BrowserClient turns followRedirects: false into a ClientException on any redirect, and the browser gives no access to the hops. The README and the TracePropagationOptions dartdoc go back to the unqualified "third-party hosts never receive them" from before #1887, with the web called out as the exception.

apikey is still copied to every hop, as dart:io does. Dropping it on a redirect to another host is a separate change.

Testing

  • New redirects group in trace_propagation_test.dart, VM only. It covers:
    • trace headers dropped on a GET redirected to a third-party host, and kept between Supabase hosts;
    • trace headers the caller set, kept;
    • credentials dropped on a cross-origin redirect and kept on a same-origin one;
    • a relative Location;
    • a 303 on a POST or PUT followed as a GET without a body;
    • a 307 on a POST and a 308 on a PATCH followed with the same body;
    • a streamed request's 307 and a POST's 302, returned unfollowed;
    • the redirect limit and loop detection;
    • the caller's request and the final URL on the response;
    • requests without trace headers, or with followRedirects: false, left to the inner client.
  • End to end against real dart:io servers, with a traced select() answered with a 302 and an rpc() answered with a 307, both redirected to a third-party host:
    • before this change, the host received traceparent and baggage;
    • after it, the host received neither, the POST body came through intact, and both calls resolved.
  • melos format and melos analyze are clean, and the supabase (220) and supabase_flutter (102) suites pass.

Closes SDK-1975.

Summary by CodeRabbit

  • Bug Fixes
    • On non-web platforms, trace headers are omitted when redirects lead to third-party hosts and retained when redirecting between Supabase hosts. Web redirects remain handled by the browser.
    • Redirects respect redirect limits and HTTP method rules, support relative destinations, and remove credentials when crossing origins. Redirected requests omit content-length and transfer-encoding headers; streamed bodies are not resent when they cannot be replayed.
    • Requests without valid trace context, or when tracing is unavailable, continue through the standard client.

@spydon
spydon requested a review from a team as a code owner September 29, 2026 09:26
@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: supabase/supabase-flutter/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: c460410b-3682-4d6c-afb3-08a1053a2ae7

📥 Commits

Reviewing files that changed from the base of the PR and between bca354a and 5c9dc1f.

📒 Files selected for processing (3)
  • packages/supabase/lib/src/trace_http_client.dart
  • packages/supabase/test/trace_propagation_test.dart
  • packages/supabase_auth/test/auth_state_test.dart

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


📝 Walkthrough

Walkthrough

TracePropagationClient now manually follows eligible redirects on non-web platforms for redirect-following GET, HEAD, and POST requests. It manages trace and credential headers across hops. Tests and documentation cover the redirect behavior.

Changes

Trace propagation across redirects

Layer / File(s) Summary
Select redirect handling
packages/supabase/lib/src/trace_http_client.dart
The client checks URL and trace-context eligibility. It selects manual redirect handling for redirect-following GET, HEAD, and POST requests off-web.
Follow redirects and validate behavior
packages/supabase/lib/src/trace_http_client.dart, packages/supabase/lib/src/trace_propagation.dart, packages/supabase/test/trace_propagation_test.dart, packages/supabase_flutter/README.md
The client handles redirect limits and loops, resolves redirect locations, converts POST to GET on 303, and updates headers between hops. Tests and documentation cover redirect behavior.

Priority: ➖ Normal

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant TracePropagationClient
  participant InnerClient
  participant RedirectHost
  Caller->>TracePropagationClient: Send request with trace context
  TracePropagationClient->>InnerClient: Send request with eligible trace headers
  InnerClient->>TracePropagationClient: Return redirect response
  TracePropagationClient->>InnerClient: Send eligible redirected request
  InnerClient->>RedirectHost: Forward redirected request
Loading

Suggested reviewers: grdsdev

Merge Risk: ⚪ Minimal · up to 5c9dc

Traced requests that are redirected to third-party hosts now drop the added trace headers, and redirect handling otherwise matches the previous behavior. No merge-blocking risk introduced by this change remains.

Security Architecture Review

Security architecture risk: 🟠 High · up to 5c9dc

Redirects can now resend some traced write requests to a new destination. That can expose the request body and API key, including when a redirect switches from HTTPS to HTTP. The risk depends on whether a server returns such a redirect; ordinary third-party redirects do receive stronger trace-header protection.

Retained concerns

  • High · security · observed: A 307/308 to another host now replays a traced, materialized write request with its body and apikey. Cross-origin credential stripping removes Authorization and cookies, but not apikey. This expands the destinations that can receive a write body and a potentially privileged Supabase key compared with default delegated redirects.
  • High · security · inferred: Newly followed write redirects can target HTTP on a recognized Supabase host. Host-only trace eligibility retains client-added trace data, while the replayed request can carry its body and apikey over that unencrypted hop. Trace retention on downgrades predated the PR for redirects already followed; write-body reachability is the material change.
Security review details

Security Blast Radius

  • inferred — The newly reachable destination can receive a replayable write body and the configured Supabase key from traced requests across Auth, REST, Functions, or Storage paths. The key may be publishable or, in a trusted server-side client, secret; exposure requires a redirect response that selects the destination.

Security Findings and Attack Paths

  • observed — The retained apikey-forwarding and host-only trace findings describe header behavior present on followed redirects. The PR changes their effective exposure by following additional write redirects with a replayable body; the record does not establish an attacker-controlled production Location.

Trust Boundaries and Controls

  • observed — On an origin change, the new path removes Authorization, authentication, proxy, and cookie headers. Outside recognized Supabase hosts it removes trace headers that this client added, but leaves caller-supplied trace headers and apikey untouched.

Resilience and Maintainability Implications

  • observed — Only a materialized Request body is replayed; a streamed 307/308 request is returned unfollowed. Redirect limits and loop detection bound the number of manual hops.

Hardening Proposals

  • proposed — Define an explicit destination policy for body-bearing redirects, including whether apikey can leave the original origin, and require an appropriate transport boundary before forwarding trace data or privileged request contents.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: removing trace headers when redirects leave Supabase hosts.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

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

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Capability matrix drift detected

The following capabilities are marked implemented in flutter but have no registered symbols to verify:

  • client.observability.diagnostic_logging (no symbols list — cannot confirm implementation exists)

These may have been renamed, removed, or never registered. Please update the capability matrix.
See: https://github.com/supabase/sdk/blob/main/packages/capability-matrix/docs/capability-matrix.md

@spydon
spydon merged commit d9c0827 into main Sep 30, 2026
44 checks passed
@spydon
spydon deleted the lukasklingsbo/sdk-1975-fixsupabase-drop-trace-headers-when-a-redirect-leaves-the branch September 30, 2026 09:27
spydon added a commit to supabase/supabase-py that referenced this pull request Nov 3, 2026
**What this changes**

The functions client stops forwarding the Supabase `apiKey` header when
a response redirects to a different host. A redirect that stays on the
Supabase host is unaffected, and `follow_redirects` stays on, so normal
behavior does not change.

**The problem**

`AsyncFunctionsClient` and `SyncFunctionsClient` create their default
`httpx` client with `follow_redirects=True`, and every request carries
the project `apiKey` header. `httpx` removes the `Authorization` header
when a response redirects away from the origin, but it forwards custom
headers unchanged. So if an invoked edge function responds with a
redirect to another host, `httpx` follows it and sends the `apiKey`
header to that host.

When the SDK is created with the `service_role` key, which is the normal
server side configuration, that key is secret and bypasses Row Level
Security. Forwarding it to a host the project does not control is a
credential disclosure. The trigger is a cross host redirect from an
invoked function, for example an open redirect driven by user input, or
a function authored by a lower trust teammate.

**The fix**

Wrap the default client in a small `httpx` subclass that overrides
`_redirect_headers` to drop the `apiKey` header when the redirect target
host differs from the current host. This mirrors how `httpx` already
treats `Authorization`, and how the other Supabase client libraries
handle redirect headers (see
supabase/supabase-flutter#1895, which strips
credential headers when a redirect leaves the Supabase hosts). A user
supplied `http_client` is left untouched, since its redirect policy is
the caller's to set.

**Tests**

Two tests on the functions client, async and sync:
- a cross host redirect drops both `apiKey` and `Authorization` at the
next hop
- a same host redirect keeps the `apiKey`

**Related**

This is the same class of issue as
GHSA-r4q5-vmmm-2653
(custom authentication headers leaked to cross domain redirect targets),
in the Python SDK.

**Scope**

This PR covers the functions client, where the redirect is driven by
user deployed function code and is the realistic exploit path. The same
`follow_redirects=True` plus `apiKey` pattern exists in the auth,
storage, and postgrest clients, which talk to Supabase controlled hosts.
Happy to extend the same guard to those in a follow up if you prefer it
in one place.

---------

Co-authored-by: Lukas Klingsbo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants