Skip to content

Commit

Permalink
fix(html): ignore malformed src attrs (#19397)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Feb 10, 2025
1 parent 39fab6d commit aff7812
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,11 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
const processedEncodedUrl = await processSrcSet(
attr.value,
async ({ url }) => {
const decodedUrl = decodeURI(url)
if (!isExcludedUrl(decodedUrl)) {
const decodedUrl = decodeURIIfPossible(url)
if (
decodedUrl !== undefined &&
!isExcludedUrl(decodedUrl)
) {
const result = await processAssetUrl(url)
return result !== decodedUrl
? encodeURIPath(result)
Expand All @@ -550,8 +553,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
})(),
)
} else if (attr.type === 'src') {
const url = decodeURI(attr.value)
if (checkPublicFile(url, config)) {
const url = decodeURIIfPossible(attr.value)
if (url === undefined) {
// ignore it
} else if (checkPublicFile(url, config)) {
overwriteAttrValue(
s,
attr.location,
Expand Down Expand Up @@ -1580,3 +1585,12 @@ function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string {
function incrementIndent(indent: string = '') {
return `${indent}${indent[0] === '\t' ? '\t' : ' '}`
}

function decodeURIIfPossible(input: string): string | undefined {
try {
return decodeURI(input)
} catch {
// url is malformed, probably a interpolate syntax of template engines
return
}
}

0 comments on commit aff7812

Please sign in to comment.