-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(ssr): preloading fixes and tweaks #7506
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
Conversation
- use json again because overall it takes less time to parse - don't preload so that the critical bundles have precedence - don't exclude preloader from critical bundles
🦋 Changeset detectedLatest commit: d9446cf The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@@ -88,16 +81,21 @@ | |||
opts.push(`Q:${minPreloadProbability}`); | |||
} | |||
const optsStr = opts.length ? `,{${opts.join(',')}}` : ''; | |||
const script = `let b=fetch("${base}q-bundle-graph-${manifestHash}.json");import("${base}${preloadChunk}").then(({l,p})=>{l(${JSON.stringify(base)},b${optsStr});p(${JSON.stringify(referencedBundles)});})`; |
Check warning
Code scanning / CodeQL
Improper code sanitization Medium
improperly sanitized value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 2 months ago
To fix the problem, we need to ensure that the referencedBundles
array is properly sanitized before being included in the JavaScript code. We can achieve this by escaping potentially dangerous characters in each string within the referencedBundles
array. This can be done by creating a utility function to escape unsafe characters and applying it to each element of the array before passing it to JSON.stringify
.
- Create a utility function
escapeUnsafeChars
to escape potentially dangerous characters. - Apply this function to each element of the
referencedBundles
array before passing it toJSON.stringify
. - Update the relevant lines in the
includePreloader
function to use the sanitized array.
-
Copy modified lines R84-R85 -
Copy modified lines R130-R147
@@ -83,3 +83,4 @@ | ||
const optsStr = opts.length ? `,{${opts.join(',')}}` : ''; | ||
const script = `let b=fetch("${base}q-bundle-graph-${manifestHash}.json");import("${base}${preloadChunk}").then(({l,p})=>{l(${JSON.stringify(base)},b${optsStr});p(${JSON.stringify(referencedBundles)});})`; | ||
const sanitizedBundles = referencedBundles.map(bundle => escapeUnsafeChars(bundle)); | ||
const script = `let b=fetch("${base}q-bundle-graph-${manifestHash}.json");import("${base}${preloadChunk}").then(({l,p})=>{l(${JSON.stringify(base)},b${optsStr});p(${JSON.stringify(sanitizedBundles)});})`; | ||
/** | ||
@@ -128 +129,19 @@ | ||
}; | ||
|
||
function escapeUnsafeChars(str: string): string { | ||
const charMap: { [key: string]: string } = { | ||
'<': '\\u003C', | ||
'>': '\\u003E', | ||
'/': '\\u002F', | ||
'\\': '\\\\', | ||
'\b': '\\b', | ||
'\f': '\\f', | ||
'\n': '\\n', | ||
'\r': '\\r', | ||
'\t': '\\t', | ||
'\0': '\\0', | ||
'\u2028': '\\u2028', | ||
'\u2029': '\\u2029' | ||
}; | ||
return str.replace(/[<>\b\f\n\r\t\0\u2028\u2029]/g, x => charMap[x]); | ||
} |
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
9712be5
to
f26a942
Compare
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 🙏
No description provided.