Repository navigation
feat: report redirects in headers when the client asks for it - #18
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
fetchwithredirect: '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 withtest.salesforce.comas 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: reportheader and the endpoint returns 301, 302, 303, 307 or 308 with aLocationheader, the proxy returns a 200 response instead, with:SalesforceProxy-Redirect-Status: the status code of the redirect responseSalesforceProxy-Redirect-Location: the redirect URL, made absoluteThe response has an empty body and
Cache-Control: no-store. The other headers of the redirect response are kept, exceptLocationand the headers about the body. IfLocationis 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-Redirectis added toVaryof 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: withenableCORS, pages on any origin unlessallowedOriginis set.A request header is used, rather than checking for the
Originheader, because the proxy can be mounted as middleware on the same origin as the app, and browsers do not sendOriginon same-origin GET requests.Fixes in passing the endpoint response
These apply to all responses:
Connection,Keep-Alive,Proxy-Connection,TE,Trailer,Transfer-Encoding,Upgrade, and the headers listed inConnection) were passed to the client. They are now left out, as RFC 9110, Section 7.6.1 requires of intermediaries.enableCORS, CORS headers in the endpoint response, such asAccess-Control-Expose-HeadersorAccess-Control-Allow-Origin, replaced the proxy's own. They are now left out, so the proxy's settings, includingallowedOrigin, are kept.Testing
enableCORS:Location, 304, 302 withoutLocation, 302 with an emptyLocation, 200, and a request header value other thanreport.Location: the proxy returns 502 and keeps serving.VaryandCache-Control, connection-specific headers, and CORS headers in the endpoint response.test.salesforce.com, which returns 302, through the proxy: without the request header,fetchgives an opaque response; with it, the page readsSalesforceProxy-Redirect-Status: 302, the redirect URL andCache-Control: no-store.