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: injectQuery double encoding #18246

Merged
merged 1 commit into from
Oct 4, 2024
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
6 changes: 6 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ describe('injectQuery', () => {
'/usr/vite/東京 %20 hello?direct',
)
})

test('path with url-encoded path as query parameter', () => {
const src = '/src/module.ts?url=https%3A%2F%2Fusr.vite%2F'
const expected = '/src/module.ts?t=1234&url=https%3A%2F%2Fusr.vite%2F'
expect(injectQuery(src, 't=1234')).toEqual(expected)
})
})

describe('resolveHostname', () => {
Expand Down
24 changes: 10 additions & 14 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import type MagicString from 'magic-string'

import type { TransformResult } from 'rollup'
import { createFilter as _createFilter } from '@rollup/pluginutils'
import { cleanUrl, isWindows, slash, withTrailingSlash } from '../shared/utils'
import {
cleanUrl,
isWindows,
slash,
splitFileAndPostfix,
withTrailingSlash,
} from '../shared/utils'
import { VALID_ID_PREFIX } from '../shared/constants'
import {
CLIENT_ENTRY,
Expand Down Expand Up @@ -311,20 +317,10 @@ export function removeRawQuery(url: string): string {
return url.replace(rawRE, '$1').replace(trailingSeparatorRE, '')
}

const replacePercentageRE = /%/g
export function injectQuery(url: string, queryToInject: string): string {
// encode percents for consistent behavior with pathToFileURL
// see #2614 for details
Comment on lines -316 to -317
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was irrelevant because pathToFileURL was removed in #10449.

const resolvedUrl = new URL(
url.replace(replacePercentageRE, '%25'),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works by simply removing this url.replace, but I think we can use splitFileAndPostfix and avoid new URL here.

'relative:///',
)
const { search, hash } = resolvedUrl
let pathname = cleanUrl(url)
pathname = isWindows ? slash(pathname) : pathname
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash ?? ''
}`
const { file, postfix } = splitFileAndPostfix(url)
const normalizedFile = isWindows ? slash(file) : file
return `${normalizedFile}?${queryToInject}${postfix[0] === '?' ? `&${postfix.slice(1)}` : /* hash only */ postfix}`
}

const timestampRE = /\bt=\d{13}&?\b/
Expand Down