Skip to content

Commit 1bc2495

Browse files
hi-ogawaclaude
andauthored
fix(ui): mount index.html handler on the ui base path (#11517)
Co-authored-by: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4ef000a commit 1bc2495

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

‎packages/ui/node/index.ts‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,31 @@ export default (harness: PluginHarness): Vite.Plugin => {
138138
})
139139

140140
// serve index.html with api token
141+
// Mounted on `base` so Connect applies the same matching as vitestUiAuth
142+
// and the sirv handler below. Connect strips `base`, so the UI root is `/`.
141143
// oxlint-disable-next-line prefer-arrow-callback
142-
server.middlewares.use(function vitestUiHtmlMiddleware(req, res, next) {
143-
if (req.url) {
144-
const url = new URL(req.url, 'http://localhost')
145-
if (url.pathname === base) {
146-
// vitestUiAuth already validated the request and set the cookie;
147-
// redirect to strip the token from the URL
148-
if (isValidApiRequest(ctx.config, req)) {
149-
res.statusCode = 302
150-
res.setHeader('Location', base)
151-
res.end()
152-
return
153-
}
154-
const html = clientIndexHtml.replace(
155-
'<!-- !LOAD_METADATA! -->',
156-
`<script>window.VITEST_API_TOKEN = ${JSON.stringify(ctx.config.api.token)};window.VITEST_UI_THEME = ${JSON.stringify(ctx.config.uiOptions.theme)}</script>`,
157-
)
158-
res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate')
159-
res.setHeader('Referrer-Policy', 'no-referrer')
160-
res.setHeader('Content-Type', 'text/html; charset=utf-8')
161-
res.write(html)
162-
res.end()
163-
return
164-
}
144+
server.middlewares.use(base, function vitestUiHtmlMiddleware(req, res, next) {
145+
const { pathname } = new URL(req.url ?? '/', 'http://localhost')
146+
if (pathname !== '/') {
147+
return next()
165148
}
166-
next()
149+
// vitestUiAuth already validated the request and set the cookie;
150+
// redirect to strip the token from the URL
151+
if (isValidApiRequest(ctx.config, req)) {
152+
res.statusCode = 302
153+
res.setHeader('Location', base)
154+
res.end()
155+
return
156+
}
157+
const html = clientIndexHtml.replace(
158+
'<!-- !LOAD_METADATA! -->',
159+
`<script>window.VITEST_API_TOKEN = ${JSON.stringify(ctx.config.api.token)};window.VITEST_UI_THEME = ${JSON.stringify(ctx.config.uiOptions.theme)}</script>`,
160+
)
161+
res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate')
162+
res.setHeader('Referrer-Policy', 'no-referrer')
163+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
164+
res.write(html)
165+
res.end()
167166
})
168167

169168
server.middlewares.use(

‎test/ui/test/ui.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Page } from '@playwright/test'
22
import type { PreviewServer } from 'vite'
33
import type { Vitest } from 'vitest/node'
44
import { existsSync } from 'node:fs'
5+
import { get as httpGet } from 'node:http'
56
import { expect, test } from '@playwright/test'
67
import { join } from 'pathe'
78
import { resolveApiToken } from '../../../packages/vitest/src/node/config/apiToken'
@@ -75,6 +76,24 @@ test.describe('ui', () => {
7576
await expect(badToken.text()).resolves.toContain('Vitest UI requires authentication.')
7677
})
7778

79+
test('does not serve ui html for non-normalized paths that resolve to base', async () => {
80+
// raw http client to send `..` without normalization
81+
const url = new URL(pageUrl)
82+
const res = await new Promise<{ status: number; text: string }>((resolve, reject) => {
83+
httpGet(
84+
{ hostname: url.hostname, port: Number(url.port), path: '/x/../__vitest__/' },
85+
(res) => {
86+
let text = ''
87+
res.on('data', (chunk) => {
88+
text += chunk
89+
})
90+
res.on('end', () => resolve({ status: res.statusCode ?? 0, text }))
91+
},
92+
).on('error', reject)
93+
})
94+
expect(res).toEqual({ status: 404, text: '' })
95+
})
96+
7897
test('blocks unauthenticated coverage requests', async ({ request }) => {
7998
const base = new URL(pageUrl)
8099
base.search = ''

0 commit comments

Comments
 (0)