Skip to content

Commit aff7812

Browse files
authored
fix(html): ignore malformed src attrs (#19397)
1 parent 39fab6d commit aff7812

File tree

1 file changed

+18
-4
lines changed
  • packages/vite/src/node/plugins

1 file changed

+18
-4
lines changed

packages/vite/src/node/plugins/html.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,11 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
534534
const processedEncodedUrl = await processSrcSet(
535535
attr.value,
536536
async ({ url }) => {
537-
const decodedUrl = decodeURI(url)
538-
if (!isExcludedUrl(decodedUrl)) {
537+
const decodedUrl = decodeURIIfPossible(url)
538+
if (
539+
decodedUrl !== undefined &&
540+
!isExcludedUrl(decodedUrl)
541+
) {
539542
const result = await processAssetUrl(url)
540543
return result !== decodedUrl
541544
? encodeURIPath(result)
@@ -550,8 +553,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
550553
})(),
551554
)
552555
} else if (attr.type === 'src') {
553-
const url = decodeURI(attr.value)
554-
if (checkPublicFile(url, config)) {
556+
const url = decodeURIIfPossible(attr.value)
557+
if (url === undefined) {
558+
// ignore it
559+
} else if (checkPublicFile(url, config)) {
555560
overwriteAttrValue(
556561
s,
557562
attr.location,
@@ -1580,3 +1585,12 @@ function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string {
15801585
function incrementIndent(indent: string = '') {
15811586
return `${indent}${indent[0] === '\t' ? '\t' : ' '}`
15821587
}
1588+
1589+
function decodeURIIfPossible(input: string): string | undefined {
1590+
try {
1591+
return decodeURI(input)
1592+
} catch {
1593+
// url is malformed, probably a interpolate syntax of template engines
1594+
return
1595+
}
1596+
}

0 commit comments

Comments
 (0)