Skip to content

Commit

Permalink
fix: proxy html path should be encoded (#15223)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Dec 3, 2023
1 parent 019c6b2 commit 5b85040
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
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 @@ -170,7 +170,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 @@ -192,9 +191,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[] = []
const inlineStyles: InlineStyleAttribute[] = []
Expand Down Expand Up @@ -434,6 +434,11 @@ function preTransformRequest(server: ViteDevServer, url: string, base: string) {
if (!server.config.server.preTransformRequests) return

// transform all url as non-ssr as html includes client-side assets only
url = unwrapId(stripBase(url, base))
try {
url = unwrapId(stripBase(decodeURI(url), base))
} catch {
// ignore
return
}
server.warmupRequest(url)
}
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

0 comments on commit 5b85040

Please sign in to comment.