Skip to content

Commit 41bb354

Browse files
authored
fix: backport #15223, proxy html path should be encoded (#15226)
1 parent f7f53aa commit 41bb354

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

packages/vite/src/node/server/middlewares/indexHtml.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ const devHtmlHook: IndexHtmlTransformHook = async (
150150
) => {
151151
const { config, moduleGraph, watcher } = server!
152152
const base = config.base || '/'
153-
htmlPath = decodeURI(htmlPath)
154153

155154
let proxyModulePath: string
156155
let proxyModuleUrl: string
@@ -172,9 +171,10 @@ const devHtmlHook: IndexHtmlTransformHook = async (
172171

173172
const s = new MagicString(html)
174173
let inlineModuleIndex = -1
175-
const proxyCacheUrl = cleanUrl(proxyModulePath).replace(
176-
normalizePath(config.root),
177-
'',
174+
// The key to the proxyHtml cache is decoded, as it will be compared
175+
// against decoded URLs by the HTML plugins.
176+
const proxyCacheUrl = decodeURI(
177+
cleanUrl(proxyModulePath).replace(normalizePath(config.root), ''),
178178
)
179179
const styleUrl: AssetNode[] = []
180180

@@ -348,7 +348,12 @@ export function indexHtmlMiddleware(
348348
function preTransformRequest(server: ViteDevServer, url: string, base: string) {
349349
if (!server.config.server.preTransformRequests) return
350350

351-
url = unwrapId(stripBase(url, base))
351+
try {
352+
url = unwrapId(stripBase(decodeURI(url), base))
353+
} catch {
354+
// ignore
355+
return
356+
}
352357

353358
// transform all url as non-ssr as html includes client-side assets only
354359
server.transformRequest(url).catch((e) => {

playground/ssr/__tests__/ssr.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from 'vitest'
22
import { port, serverLogs } from './serve'
3-
import { editFile, page, withRetry } from '~utils'
3+
import { browserLogs, editFile, isServe, page, withRetry } from '~utils'
44

55
const url = `http://localhost:${port}`
66

@@ -29,3 +29,11 @@ test('should restart ssr', async () => {
2929
)
3030
})
3131
})
32+
33+
test.runIf(isServe)('html proxy is encoded', async () => {
34+
await page.goto(
35+
`${url}?%22%3E%3C/script%3E%3Cscript%3Econsole.log(%27html proxy is not encoded%27)%3C/script%3E`,
36+
)
37+
38+
expect(browserLogs).not.toContain('html proxy is not encoded')
39+
})

playground/ssr/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>SSR</title>
7+
<script type="module">
8+
// Inline script for testing html-proxy encoding
9+
console.log('from inline script')
10+
</script>
711
</head>
812
<body>
913
<h1>SSR</h1>

0 commit comments

Comments
 (0)