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,10 @@ describe('dev proxy', () => {
expect(canProxyRequest(event)).toBeTruthy()
})
})
describe('proxyStorefrontRequest', () => {
test('should throw an error when URL hostname does not match expected host (SSRF protection)', async () => {
const event = createH3Event('GET', '//evil.com/some-path')
await expect(proxyStorefrontRequest(event, ctx)).rejects.toThrow('Request failed: Hostname mismatch')
})
})
})
5 changes: 5 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,11 @@ 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'))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be nice if we could show the mismatched hosts in the error so it's a bit more clear what happened (host vs 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
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function buildCookies(session: DevServerSession, ctx: Pick<DevServerRende
function buildStorefrontUrl(session: DevServerSession, {path, sectionId, appBlockId, query}: DevServerRenderContext) {
const baseUrl = buildBaseStorefrontUrl(session)
const url = `${baseUrl}${path}`

const params = new URLSearchParams({
_fd: '0',
pb: '0',
Expand Down
Loading