Skip to content

feat: report redirects in headers when the client asks for it - #18

Merged
stomita merged 2 commits into
masterfrom
feat/report-redirects
Nov 22, 2026
Merged

stomita merged 2 commits into
masterfrom
feat/report-redirects

Conversation

@stomita

@stomita stomita commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

The proxy does not follow redirects from the endpoint. It returns them to the client, which sends the request for the redirect URL through the proxy again. This works in Node, but not in the browser: fetch with redirect: 'manual' gives an opaque response for a redirect (status 0, no headers), so the client cannot read where to go. For example, revoking a token with test.salesforce.com as the login URL is redirected to the instance, and a browser client cannot follow it.

This PR lets a browser client ask the proxy to report redirects in a form that it can read. It also fixes three problems in how the proxy passes the endpoint response to the client.

Reporting redirects

When the request has the SalesforceProxy-Redirect: report header and the endpoint returns 301, 302, 303, 307 or 308 with a Location header, the proxy returns a 200 response instead, with:

  • SalesforceProxy-Redirect-Status: the status code of the redirect response
  • SalesforceProxy-Redirect-Location: the redirect URL, made absolute

The response has an empty body and Cache-Control: no-store. The other headers of the redirect response are kept, except Location and the headers about the body. If Location is not a valid URL, the proxy returns 502.

Without the request header, redirect responses are not turned into 200 responses, so clients that already read them, such as jsforce in Node, keep working. SalesforceProxy-Redirect is added to Vary of redirect responses, so that caches tell the two forms apart.

With enableCORS, the request header is allowed and the two response headers are exposed. The README has a new "Redirects" section.

The proxy still does not follow redirects itself, for two reasons. It would send requests to hosts it has not checked, while a request for the redirect URL that comes back from the client is checked like any other. And some requests can only be made again by the client, such as requests with DPoP proofs, which are signed for each URL.

The redirect URL can carry sensitive values. For example, Salesforce puts the token in the query of the redirect URL for a revoke request. That is why the response has Cache-Control: no-store. Which pages can read it is decided by the proxy's CORS settings, as for any other response: with enableCORS, pages on any origin unless allowedOrigin is set.

A request header is used, rather than checking for the Origin header, because the proxy can be mounted as middleware on the same origin as the app, and browsers do not send Origin on same-origin GET requests.

Fixes in passing the endpoint response

These apply to all responses:

  • The headers that apply only to the connection with the endpoint (Connection, Keep-Alive, Proxy-Connection, TE, Trailer, Transfer-Encoding, Upgrade, and the headers listed in Connection) were passed to the client. They are now left out, as RFC 9110, Section 7.6.1 requires of intermediaries.
  • With enableCORS, CORS headers in the endpoint response, such as Access-Control-Expose-Headers or Access-Control-Allow-Origin, replaced the proxy's own. They are now left out, so the proxy's settings, including allowedOrigin, are kept.
  • When the endpoint closed the connection in the middle of the body, the client response was left open. It is now closed too.

Testing

  • With a stubbed endpoint, with and without enableCORS:
    • 302 with and without the request header, 307 with a relative Location, 304, 302 without Location, 302 with an empty Location, 200, and a request header value other than report.
    • An invalid Location: the proxy returns 502 and keeps serving.
    • Vary and Cache-Control, connection-specific headers, and CORS headers in the endpoint response.
  • With a local HTTP server as the endpoint that closes the connection in the middle of the body: the client response is closed with an error, where it did not end before.
  • From a browser page on another origin, revoking a token at test.salesforce.com, which returns 302, through the proxy: without the request header, fetch gives an opaque response; with it, the page reads SalesforceProxy-Redirect-Status: 302, the redirect URL and Cache-Control: no-store.

stomita and others added 2 commits November 22, 2026 17:57
The proxy copied all response headers of the endpoint, including the ones
that apply only to the connection with the endpoint, such as Connection,
Keep-Alive and the headers listed in Connection, which intermediaries
must not forward (RFC 9110, Section 7.6.1). With enableCORS, the CORS
headers of the endpoint response also replaced the proxy's own, such as
the exposed headers and the allowedOrigin setting. Leave these headers
out.

When the endpoint closed the connection in the middle of the body, the
client response was left open. Pass the body with stream.pipeline so that
the client response is closed too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The proxy returns redirect responses to the client instead of following
them, but browsers do not let scripts read the status or the Location
header of a redirect response, so a client in the browser cannot follow
the redirect through the proxy. When the request has the
SalesforceProxy-Redirect: report header, return a redirect response as a
200 response with SalesforceProxy-Redirect-Status and
SalesforceProxy-Redirect-Location headers, and expose them for CORS.

The response has Cache-Control: no-store, since the redirect URL can
carry values such as a token, and SalesforceProxy-Redirect is added to
Vary of redirect responses so that caches tell the two forms apart. An
invalid Location gets 502 instead of making the proxy exit. Without the
request header, redirect responses are returned as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stomita
stomita merged commit d79845e into master Nov 22, 2026
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.

1 participant