Skip to content

Commit ddf1c28

Browse files
committed
fix(analyze): check host header on analyze stats server
1 parent 26d7c6e commit ddf1c28

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/nuxt-cli/src/commands/analyze.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { defu } from 'defu'
1010
import { join, relative } from 'pathe'
1111
import { serve } from 'srvx'
1212

13+
import { isAllowedHost } from '../dev/host-check'
1314
import { resolveDotenvFileNames } from '../utils/args'
1415
import { overrideEnv } from '../utils/env'
1516
import { ActionableError } from '../utils/errors'
@@ -203,9 +204,15 @@ export default defineCommand({
203204

204205
logger.step('Starting stats server...')
205206

207+
const hostname = process.env.HOST || 'localhost'
208+
const allowedHosts = new Set([hostname.toLowerCase()])
209+
206210
await serve({
207-
hostname: process.env.HOST || 'localhost',
211+
hostname,
208212
fetch(request) {
213+
if (!isAllowedHost(request.headers.get('host') ?? undefined, allowedHosts)) {
214+
return new Response('Forbidden: this host is not allowed.', { status: 403, headers: { 'content-type': 'text/plain' } })
215+
}
209216
const pathname = new URL(request.url).pathname.replace(/\/$/, '')
210217
if (reports.has(pathname)) {
211218
const report = reports.get(pathname)

0 commit comments

Comments
 (0)