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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'node:fs'
import http from 'node:http'
import type { AddressInfo } from 'node:net'
import os from 'node:os'
import path from 'node:path'
import { describe, expect, onTestFinished, test, vi } from 'vitest'
Expand Down Expand Up @@ -707,3 +709,23 @@ describe('indexHtml middleware — safe module paths', () => {
})
})
})

describe('indexHtml middleware — malformed URI', () => {
test('responds 404 to an .html URL with a malformed escape', async () => {
const server = await createTestServer()
const httpServer = http.createServer(server.middlewares)
await new Promise<void>((resolve) =>
httpServer.listen(0, '127.0.0.1', resolve),
)
onTestFinished(() => {
httpServer.close()
})
const { port } = httpServer.address() as AddressInfo

for (const url of ['/%E0%A4%A.html', `${FS_PREFIX}%E0%A4%A.html`]) {
const res = await fetch(`http://127.0.0.1:${port}${url}`)
expect(res.status, url).toBe(404)
}
expect((await fetch(`http://127.0.0.1:${port}/`)).status).toBe(200)
})
})
17 changes: 11 additions & 6 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,17 @@ export function indexHtmlMiddleware(
}

let filePath: string
if (isDev) {
filePath = moduleUrlToFilePath(decodeURIComponent(url), root)
} else {
filePath = normalizePath(
path.resolve(path.join(root, decodeURIComponent(url))),
)
try {
if (isDev) {
filePath = moduleUrlToFilePath(decodeURIComponent(url), root)
} else {
filePath = normalizePath(
path.resolve(path.join(root, decodeURIComponent(url))),
)
}
} catch {
// ignore malformed URI
return next()
}

if (isDev) {
Expand Down
Loading