Skip to content

Commit

Permalink
fix: encode URLs correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
pzerelles committed Dec 11, 2023
1 parent 25d7924 commit c667958
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function renderAssetUrlInJS(
chunk.viteMetadata!.importedAssets.add(cleanUrl(file))
const filename = file + postfix
const replacement = toOutputFilePathInJS(
filename,
encodeURI(filename),
'asset',
chunk.fileName,
'js',
Expand Down Expand Up @@ -275,7 +275,7 @@ function fileToDevUrl(id: string, config: ResolvedConfig) {
rtn = path.posix.join(FS_PREFIX, id)
}
const base = joinUrlSegments(config.server?.origin ?? '', config.base)
return joinUrlSegments(base, removeLeadingSlash(rtn))
return encodeURI(joinUrlSegments(base, removeLeadingSlash(rtn)))
}

export function getPublicAssetFilename(
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const filename = this.getFileName(fileHash) + postfix
chunk.viteMetadata!.importedAssets.add(cleanUrl(filename))
return toOutputFilePathInCss(
filename,
encodeURI(filename),
'asset',
cssAssetName,
'css',
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
url = joinUrlSegments(base, url)
}

return [url, resolved.id]
// only encode if the url can't be parsed
return [encodeURI(url), resolved.id]
}

const orderedImportedUrls = new Array<string | undefined>(imports.length)
Expand Down
2 changes: 1 addition & 1 deletion playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ describe('unicode url', () => {
? `data:application/javascript;base64,${Buffer.from(src).toString(
'base64',
)}`
: `/foo/bar/テスト-測試-white space.js`,
: encodeURI(`/foo/bar/テスト-測試-white space.js`),
)
})
})
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr/__tests__/ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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`,
`${url}?"></script><script>console.log('html proxy is not encoded')</script>`,
)

expect(browserLogs).not.toContain('html proxy is not encoded')
Expand Down

0 comments on commit c667958

Please sign in to comment.