Skip to content

Fix CORS preflight handling in the app dev reverse proxy - #8803

Open
Dylan-GJ wants to merge 1 commit into
Shopify:mainfrom
Dylan-GJ:fix-app-dev-proxy-cors-preflight
Open

Dylan-GJ wants to merge 1 commit into
Shopify:mainfrom
Dylan-GJ:fix-app-dev-proxy-cors-preflight

Conversation

@Dylan-GJ

@Dylan-GJ Dylan-GJ commented Oct 6, 2026 •

Copy link
Copy Markdown

WHY are these changes introduced?

Fixes #8259

#7164 made the shopify app dev reverse proxy answer CORS OPTIONS requests itself, because some dev servers don't handle them. The response never includes Access-Control-Allow-Credentials, so credentialed cross-origin requests (fetch(..., {credentials: "include"})) fail the preflight even when the app's backend is configured correctly.

Adding the header in the proxy isn't safe: it reflects any Origin, so it would authorize every site. Reverting #7164 (see #8320) would bring back #7324 and #5660 for backends without an OPTIONS handler.

WHAT is this pull request doing?

The proxy now forwards OPTIONS requests to the target app, which decides CORS, credentials included. The target's answer is relayed only when it is a 2xx that allows the request's origin (or when the request has no Origin, so it isn't a browser preflight).

In every other case the proxy responds itself, with the same response as before and never with credentials: error or redirect statuses, a 2xx without CORS headers (Vite's default server.cors, Express's automatic Allow response, Next.js, Flask, Django), or an unreachable target. A browser would reject all of these in production too. This keeps #5660 and #7324 working with the Vite frontend of the app templates, which answers the preflight before it reaches the backend.

A deliberate "no" from the target sent as a 2xx is overridden for non-credentialed requests, which is acceptable for local development.

Hop-by-hop headers are not relayed from the target's preflight response.

How to manually test your changes?

  1. Run shopify app dev with a backend that answers OPTIONS with Access-Control-Allow-Origin and Access-Control-Allow-Credentials: true for a trusted origin. Send a preflight to the tunnel URL:
    curl -i -X OPTIONS -H "Origin: <trusted origin>" -H "Access-Control-Request-Method: POST" <tunnel url>/<path>
    The backend's headers, including credentials, are returned.
  2. Repeat against a backend that doesn't allow the origin: no OPTIONS handler (404/405), or a Vite dev server in front of it with the default server.cors. The proxy answers with a 204 reflecting the origin, without Access-Control-Allow-Credentials.

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes
  • I've considered analytics changes to measure impact
  • The change is user-facing — I've identified the correct bump type (patch for bug fixes · minor for new features · major for breaking changes) and added a changeset with pnpm changeset add

@Dylan-GJ
Dylan-GJ requested a review from a team as a code owner October 6, 2026 15:37
@Dylan-GJ

Dylan-GJ commented Oct 6, 2026

Copy link
Copy Markdown
Author

I have signed the CLA!

@Dylan-GJ

Copy link
Copy Markdown
Author

Hi @alexanderMontague, you merged #7164, so I'd appreciate your eyes on this one when you have a moment.

This follows up on a regression from that PR (#8259): the proxy's synthetic preflight response never includes Access-Control-Allow-Credentials, so credentials: "include" requests fail. Instead of reverting, #8803 forwards OPTIONS to the target app and only falls back to the #7164 response (never with credentials) when the target answers 4xx/5xx or is unreachable.

CI is green and the diff is small (proxy, its tests, one changeset). Happy to adjust anything. Thanks!

@Dylan-GJ

Copy link
Copy Markdown
Author

Hi @isaacroldan, sorry for the extra ping. This PR has been open for about five weeks without a review, and I tried @alexanderMontague (who merged #7164) earlier without luck, so I'm reaching out to you as you've been reviewing a lot of recent PRs.

It fixes #8259, a regression from #7164: the app dev proxy answers CORS preflights itself without Access-Control-Allow-Credentials, so credentials: "include" requests fail. #8803 forwards OPTIONS to the target app and only falls back to the previous behavior (never with credentials) when the target answers 4xx/5xx or is unreachable.

I also checked the #5660 setup (an Express backend without any CORS middleware): the OPTIONS request reaches the backend and gets its response.

CI is green, and the diff is small (proxy, its tests, one changeset). If someone else on the team is a better fit, a pointer would be very welcome. Thanks!

@Dylan-GJ
Dylan-GJ force-pushed the fix-app-dev-proxy-cors-preflight branch from 8f0c19f to eceb673 Compare November 10, 2026 15:16

@isaacroldan isaacroldan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Dylan-GJ, thanks for this and sorry for the wait!

Forwarding the preflight to the app makes sense, only the app knows which origins to trust and that's what happens in production anyway. The fallback is the problem, it brings back #5660 and most likely #7324.

Since 5.4.14 / 4.5.9 Vite's default server.cors only allows localhost origins, and it answers the preflight before server.proxy runs, with a 204 and no Access-Control-Allow-Origin. We relay that as-is and the browser blocks it. That's the web/frontend (shopify-frontend-template-react) of the Node, PHP and Ruby templates, shopify app init doesn't list them anymore but existing apps still use them.

Repro: Vite 5.4.21 with that template's server config, /api proxied to an Express backend whose cors() allows https://extensions.shopifycdn.com, then through the CLI proxy:

curl -i -X OPTIONS <tunnel url>/api/data \
  -H 'Origin: https://extensions.shopifycdn.com' \
  -H 'Access-Control-Request-Method: POST' \
  -H 'Access-Control-Request-Headers: authorization,content-type'

On main the proxy answers 204 with the origin reflected and it passes. With this PR you get Vite's own 204 without Access-Control-Allow-Origin and the backend never sees the request. In production there's no Vite in front, the backend's cors() answers and it passes.

Your #5660 check was Express on its own, in #5660 Vite sits in front of Express and answers first.

I'd only relay a 2xx that allows the request's origin and send the proxy's own 204 for everything else, that keeps #8259 fixed (suggestion inline).

About CI, I haven't approved the workflow runs for this head so tests, lint and type-check haven't run yet. I'll approve them after your next push :)

Comment on lines +127 to +132
const statusCode = targetResponse.statusCode ?? 500
if (statusCode >= 400) {
targetResponse.resume()
respondToPreflight(req, res)
return
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a preflight only passes with a 2xx that allows the origin, and this relays anything below 400. So besides Vite we'd also relay Express's automatic 200 + Allow (for a path with routes), the auto-answers in Next.js route handlers, Flask and Django when the app has no CORS handling, and any 3xx (a redirected preflight is a network error). Auth middleware that redirects session-less requests will redirect every preflight, preflights never carry cookies or Authorization.

Those fail in production too, same as the 404 we already fall back on, so I'd treat them all the same. An OPTIONS without Origin isn't a browser preflight, so that 2xx can go through as-is:

Suggested change
const statusCode = targetResponse.statusCode ?? 500
if (statusCode >= 400) {
targetResponse.resume()
respondToPreflight(req, res)
return
}
const statusCode = targetResponse.statusCode ?? 500
const {origin} = req.headers
const allowOrigin = targetResponse.headers['access-control-allow-origin']
const targetAllowsOrigin = origin === undefined || allowOrigin === '*' || allowOrigin === origin
if (statusCode < 200 || statusCode >= 300 || !targetAllowsOrigin) {
targetResponse.resume()
respondToPreflight(req, res)
return
}

Apps that grant credentials for the origin still get relayed so #8259 stays fixed, and the fallback is the same 204 main sends today. Downside is that a deliberate allowlist "no" sent as a 2xx gets overridden for non-credentialed requests, same trade-off the doc comment already accepts for a 403.

Can we update that comment to the new rule? the PR description and the commit message also describe the 4xx/5xx rule

Comment on lines +94 to +103
test('keeps the target response when it does not authorize the origin', {retry: 2}, async ({setup}) => {
const response = await fetch(`${protocol}://localhost:${setup.proxyPort}/path1/test`, {
method: 'OPTIONS',
headers: {Origin: 'https://evil.example', 'Access-Control-Request-Method': 'POST'},
agent,
})
expect(response.status).toBe(204)
expect(response.headers.get('access-control-allow-origin')).toBeNull()
expect(response.headers.get('access-control-allow-credentials')).toBeNull()
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the fix in http-reverse-proxy.ts this test flips: a 2xx without Access-Control-Allow-Origin is also what Vite's default and Express send when the app did nothing, so we can't treat it as a deliberate rejection. It should expect the fallback (origin reflected, no credentials) and get a new name.

Can we also add 200 and 302 to the status loop below? That covers the plain 2xx and redirect cases with the existing target.

Non blocking: every relayed preflight answer in the suite is an empty 204, so removing selfHandleResponse at http-reverse-proxy.ts L74 wouldn't fail anything, even though the proxy library would then pipe the body twice. A relayed 2xx with a chunked body (target calls writeHead and then end('ok')) asserting the exact body would catch that.

The proxy answered every OPTIONS request itself without
Access-Control-Allow-Credentials, which broke credentialed cross-origin
requests. Preflights now reach the target app, which decides CORS.

The target's answer is only relayed when it is a 2xx that allows the request's
origin. Otherwise (error or redirect status, no CORS headers as with Vite's
default server.cors, or an unreachable target) the proxy responds itself,
without credentials, as before.
@Dylan-GJ
Dylan-GJ force-pushed the fix-app-dev-proxy-cors-preflight branch from eceb673 to 5bde938 Compare November 11, 2026 14:44
@Dylan-GJ

Copy link
Copy Markdown
Author

Thanks @isaacroldan, this is a great catch and a clear explanation, I hadn't considered Vite answering the preflight before server.proxy. My #5660 check was Express on its own, as you pointed out.

I pushed the changes:

  • Applied your suggestion: the target's answer is relayed only when it is a 2xx allowing the request's origin (or when there is no Origin). Everything else gets the proxy's own 204, without credentials. I updated the doc comment, the commit message and the PR description to describe that rule.
  • The "target does not authorize the origin" test now expects the fallback and was renamed, and 200 and 302 are in the status loop.
  • Added two tests: a relayed 2xx with a body written in several chunks, which fails if selfHandleResponse is removed (I checked that by hand), and an OPTIONS without Origin that is relayed as-is.

I also tested both setups end to end with shopify app dev and a Cloudflare tunnel, using your curl with Origin: https://extensions.shopifycdn.com:

  • Vite 5.4.21 in front of Express (/api proxied to an Express backend whose cors() allows that origin, default server.cors): Vite answers the preflight with a 204 without Access-Control-Allow-Origin (I confirmed it by hitting Vite directly). Through the tunnel I get the proxy's own 204: the origin reflected, Access-Control-Max-Age: 86400, no Access-Control-Allow-Credentials.
  • Express alone with cors({origin: [...], credentials: true}): the backend's response is relayed as-is, with Access-Control-Allow-Credentials: true, Express's Access-Control-Allow-Methods, Vary and no Max-Age, so CORS error on shopify app dev on the cli reverse proxy with credentials: "include" #8259 is fixed.

Could you approve the workflow runs when you get a chance? Thanks!

const targetServer1 = http.createServer((req, res) => {
if (req.method === 'OPTIONS' && req.url?.endsWith('/with-body')) {
// Writes the body in several chunks, as a real app would after sending headers.
res.writeHead(200, req.headers.origin ? {'Access-Control-Allow-Origin': req.headers.origin} : {})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @isaacroldan, this CodeQL alert is a false positive. The flagged line (http-reverse-proxy.test.ts:198) is the target server of a test fixture, for the /with-body path. It reflects the request origin only so the test can check that a body is relayed. It never runs outside the test suite, so it is not shipped in the CLI. That handler does not send Access-Control-Allow-Credentials, and the proxy itself never does either.

Could you dismiss the alert as "Used in tests"? I do not have the permission to do it. Alternatively, I can change the fixture to use the fixed authorizedOrigin constant if you would rather not dismiss it.

This branch has not been deployed

No deployments
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.

CORS error on shopify app dev on the cli reverse proxy with credentials: "include"

3 participants