Skip to content

Commit a270391

Browse files
authored
fix(server): escape HTML attributes (#23995)
1 parent 7139ef3 commit a270391

4 files changed

Lines changed: 9 additions & 7 deletions

File tree

‎packages/vite/src/node/server/middlewares/__tests__/indexHtml.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('indexHtml middleware — inline script proxy cache', () => {
5050
).toBeTruthy()
5151

5252
const proxyModuleUrl = browserUrlToModuleUrl(
53-
proxyUrlMatch![1],
53+
proxyUrlMatch![1].replaceAll('&', '&'),
5454
server.config.decodedBase,
5555
)
5656
const result =
@@ -85,7 +85,7 @@ describe('indexHtml middleware — inline script proxy cache', () => {
8585
).toBeTruthy()
8686

8787
const proxyModuleUrl = browserUrlToModuleUrl(
88-
proxyUrlMatch![1],
88+
proxyUrlMatch![1].replaceAll('&', '&'),
8989
server.config.decodedBase,
9090
)
9191
const result =
@@ -108,7 +108,7 @@ describe('indexHtml middleware — inline script proxy cache', () => {
108108
'devHtmlHook should have rewritten the inline <script> to a ?html-proxy src',
109109
).toBeTruthy()
110110
expect(proxyUrlMatch![1]).toMatchInlineSnapshot(
111-
`"/@id/__x00__/evil.example.com/payload.js?/../../package.json?html-proxy&index=0.js"`,
111+
`"/@id/__x00__/evil.example.com/payload.js?/../../package.json?html-proxy&amp;index=0.js"`,
112112
)
113113
expect(proxyUrlMatch![1]).not.toMatch(/^\/\//)
114114
})

‎packages/vite/src/node/server/middlewares/base.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import escapeHTML from 'escape-html'
12
import type { Connect } from '#dep-types/connect'
23
import {
34
cleanUrl,
@@ -47,8 +48,8 @@ export function baseMiddleware(
4748
'Content-Type': 'text/html',
4849
})
4950
res.end(
50-
`The server is configured with a public base URL of ${base} - ` +
51-
`did you mean to visit <a href="${redirectPath}">${redirectPath}</a> instead?`,
51+
`The server is configured with a public base URL of ${escapeHTML(base)} - ` +
52+
`did you mean to visit <a href="${escapeHTML(redirectPath)}">${escapeHTML(redirectPath)}</a> instead?`,
5253
)
5354
return
5455
} else {

‎packages/vite/src/node/server/middlewares/indexHtml.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs'
22
import fsp from 'node:fs/promises'
33
import path from 'node:path'
4+
import escapeHTML from 'escape-html'
45
import MagicString from 'magic-string'
56
import type { DefaultTreeAdapterMap, Token } from 'parse5'
67
import type { SourceMapInput } from 'rolldown'
@@ -280,7 +281,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
280281
s.update(
281282
node.sourceCodeLocation!.startOffset,
282283
node.sourceCodeLocation!.endOffset,
283-
`<script type="module" src="${browserUrl}"></script>`,
284+
`<script type="module" src="${escapeHTML(browserUrl)}"></script>`,
284285
)
285286
preTransformRequest(server!, moduleUrl)
286287
}

‎playground/html/__tests__/html.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ test.runIf(!isBundled)(
538538
expect(response.status).toBe(200)
539539
expect(html).toContain('url(./protocol-relative.png)')
540540
expect(html).toContain(
541-
'src="/protocol-relative.html?html-proxy&index=0.js"',
541+
'src="/protocol-relative.html?html-proxy&amp;index=0.js"',
542542
)
543543
expect(html).not.toContain('url(//protocol-relative.png)')
544544
expect(html).not.toContain('src="//protocol-relative.html?html-proxy')

0 commit comments

Comments
 (0)