Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backport #15223, proxy html path should be encoded #15226

Merged
merged 1 commit into from
Dec 3, 2023
Merged
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
15 changes: 10 additions & 5 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ const devHtmlHook: IndexHtmlTransformHook = async (
) => {
const { config, moduleGraph, watcher } = server!
const base = config.base || '/'
htmlPath = decodeURI(htmlPath)

let proxyModulePath: string
let proxyModuleUrl: string
Expand All @@ -172,9 +171,10 @@ const devHtmlHook: IndexHtmlTransformHook = async (

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

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

url = unwrapId(stripBase(url, base))
try {
url = unwrapId(stripBase(decodeURI(url), base))
} catch {
// ignore
return
}

// transform all url as non-ssr as html includes client-side assets only
server.transformRequest(url).catch((e) => {
Expand Down
10 changes: 9 additions & 1 deletion playground/ssr/__tests__/ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { port, serverLogs } from './serve'
import { editFile, page, withRetry } from '~utils'
import { browserLogs, editFile, isServe, page, withRetry } from '~utils'

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

Expand Down Expand Up @@ -29,3 +29,11 @@ test('should restart ssr', async () => {
)
})
})

test.runIf(isServe)('html proxy is encoded', async () => {
await page.goto(
`${url}?%22%3E%3C/script%3E%3Cscript%3Econsole.log(%27html proxy is not encoded%27)%3C/script%3E`,
)

expect(browserLogs).not.toContain('html proxy is not encoded')
})
4 changes: 4 additions & 0 deletions playground/ssr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SSR</title>
<script type="module">
// Inline script for testing html-proxy encoding
console.log('from inline script')
</script>
</head>
<body>
<h1>SSR</h1>
Expand Down