Skip to content

Commit 521f75d

Browse files
committed
avoid vulnerability in string.replace
1 parent 828f9e9 commit 521f75d

3 files changed

Lines changed: 26 additions & 226 deletions

File tree

packages/dom-expressions/src/server.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ export function renderToStream(code, options = {}) {
154154
const first = html.indexOf(placeholder);
155155
if (first === -1) return;
156156
const last = html.indexOf(`<!--!$/${id}-->`, first + placeholder.length);
157-
html = html.replace(
158-
html.slice(first, last + placeholder.length + 1),
159-
resolveSSRNode(payloadFn())
160-
);
157+
html = html.slice(0, first) + resolveSSRNode(escape(payloadFn())) + html.slice(last + placeholder.length + 1);
161158
},
162159
serialize(id, p, wait) {
163160
const serverOnly = sharedConfig.context.noHydrate;
@@ -513,7 +510,7 @@ export function getHydrationKey() {
513510
}
514511

515512
export function useAssets(fn) {
516-
sharedConfig.context.assets.push(() => resolveSSRNode(fn()));
513+
sharedConfig.context.assets.push(() => resolveSSRNode(escape(fn())));
517514
}
518515

519516
export function getAssets() {
@@ -567,7 +564,9 @@ function injectAssets(assets, html) {
567564
if (!assets || !assets.length) return html;
568565
let out = "";
569566
for (let i = 0, len = assets.length; i < len; i++) out += assets[i]();
570-
return html.replace(`</head>`, out + `</head>`);
567+
const index = html.indexOf("</head>");
568+
if (index === -1) return html;
569+
return html.slice(0, index) + out + html.slice(index);
571570
}
572571

573572
function injectScripts(html, scripts, nonce) {

packages/dom-expressions/test/ssr/ssr.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const Comp2 = () => {
7575
const Comp3 = () => {
7676
const greeting = "Hello",
7777
name = "<div/>";
78-
r.useAssets(() => `<link rel="modulepreload" href="chunk.js">`)
78+
r.useAssets(() => r.ssr`<link rel="modulepreload" href="chunk.js">`)
7979
return r.ssr`<span> ${r.escape(greeting)} ${r.escape(name)}${r.HydrationScript()}${r.getAssets()}</span>`;
8080
};
8181

0 commit comments

Comments
 (0)