Skip to content

Commit

Permalink
fix: backport #15223, proxy html path should be encoded (#15226) (#15227
Browse files Browse the repository at this point in the history
)
  • Loading branch information
patak-dev authored Dec 3, 2023
1 parent b50db04 commit fe8b209
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 @@ -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

0 comments on commit fe8b209

Please sign in to comment.