Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-swans-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme': patch
---

Protect SSRF vulnerability in proxy requests when hosts don't match
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {canProxyRequest, getProxyStorefrontHeaders, injectCdnProxy, patchRenderingResponse} from './proxy.js'
import {
canProxyRequest,
getProxyStorefrontHeaders,
injectCdnProxy,
patchRenderingResponse,
proxyStorefrontRequest,
} from './proxy.js'
import {describe, test, expect} from 'vitest'
import {createEvent} from 'h3'
import {IncomingMessage, ServerResponse} from 'node:http'
Expand Down Expand Up @@ -338,4 +344,18 @@ describe('dev proxy', () => {
expect(canProxyRequest(event)).toBeTruthy()
})
})
describe('proxyStorefrontRequest', () => {
test('should reject hostname mismatch and throw error for non-CDN paths (SSRF protection)', async () => {
const event = createH3Event('GET', '//evil.com/some-path')
await expect(proxyStorefrontRequest(event, ctx)).rejects.toThrow(
'Request failed: Hostname mismatch. Expected host: my-store.myshopify.com. Resulting URL hostname: evil.com',
)
})
test('should reject hostname mismatch and throw error for CDN paths (SSRF protection)', async () => {
const event = createH3Event('GET', '/ext/cdn//evil.com/some-path')
await expect(proxyStorefrontRequest(event, ctx)).rejects.toThrow(
'Request failed: Hostname mismatch. Expected host: cdn.shopify.com. Resulting URL hostname: evil.com',
)
})
})
})
7 changes: 7 additions & 0 deletions packages/theme/src/cli/utilities/theme-environment/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ export function proxyStorefrontRequest(event: H3Event, ctx: DevServerContext): P
const host = event.path.startsWith(EXTENSION_CDN_PREFIX) ? 'cdn.shopify.com' : ctx.session.storeFqdn
const url = new URL(path, `https://${host}`)

// Check that we aren't redirecting to external hosts
if (url.hostname !== host) {
return Promise.reject(
new Error(`Request failed: Hostname mismatch. Expected host: ${host}. Resulting URL hostname: ${url.hostname}`),
)
}

// When a .css.liquid or .js.liquid file is requested but it doesn't exist in SFR,
// it will be rendered with a query string like `assets/file.css?1234`.
// For some reason, after refreshing, this rendered URL keeps the wrong `?1234`
Expand Down
Loading