Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add safety checks around accesses for document.currentScript.src #696

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pagefind_ui/default/svelte/ui.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,21 @@
[
`Pagefind couldn't be loaded from ${this.options.bundlePath}pagefind.js`,
`You can configure this by passing a bundlePath option to PagefindUI`,
`[DEBUG: Loaded from ${
document?.currentScript?.src ?? "no known script location"
}]`,
].join("\n")
);
// Important: Check that the element is indeed a <script> node, to avoid a DOM clobbering vulnerability
if (
document?.currentScript &&
document.currentScript.tagName.toUpperCase() === "SCRIPT"
) {
console.error(
`[DEBUG: Loaded from ${
document.currentScript.src ?? "bad script location"
}]`
);
} else {
console.error("no known script location");
}
}

if (!excerpt_length) {
Expand Down
9 changes: 6 additions & 3 deletions pagefind_ui/default/ui-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import PagefindSvelte from "./svelte/ui.svelte";

let scriptBundlePath;
try {
scriptBundlePath = new URL(document.currentScript.src).pathname.match(
/^(.*\/)(?:pagefind-)?ui.js.*$/
)[1];
// Important: Check that the element is indeed a <script> node, to avoid a DOM clobbering vulnerability
if (document?.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') {
scriptBundlePath = new URL(document.currentScript.src).pathname.match(
/^(.*\/)(?:pagefind-)?ui.js.*$/
)[1];
}
} catch (e) {
scriptBundlePath = "/pagefind/";
}
Expand Down
27 changes: 20 additions & 7 deletions pagefind_ui/modular/modular-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const sleep = async (ms = 50) =>

let scriptBundlePath;
try {
scriptBundlePath = new URL(document.currentScript.src).pathname.match(
/^(.*\/)(?:pagefind-)?modular-ui.js.*$/
)[1];
// Important: Check that the element is indeed a <script> node, to avoid a DOM clobbering vulnerability
if (document?.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') {
scriptBundlePath = new URL(document.currentScript.src).pathname.match(
/^(.*\/)(?:pagefind-)?modular-ui.js.*$/
)[1];
}
} catch (e) {
scriptBundlePath = "/pagefind/";
}
Expand Down Expand Up @@ -166,12 +169,22 @@ export class Instance {
console.error(
[
`Pagefind couldn't be loaded from ${this.options.bundlePath}pagefind.js`,
`You can configure this by passing a bundlePath option to PagefindComposable Instance`,
`[DEBUG: Loaded from ${
document?.currentScript?.src ?? "no known script location"
}]`,
`You can configure this by passing a bundlePath option to PagefindComposable Instance`
].join("\n")
);
// Important: Check that the element is indeed a <script> node, to avoid a DOM clobbering vulnerability
if (
document?.currentScript &&
document.currentScript.tagName.toUpperCase() === "SCRIPT"
) {
console.error(
`[DEBUG: Loaded from ${
document.currentScript?.src ?? "bad script location"
}]`
);
} else {
console.error("no known script location");
}
}

await imported_pagefind.options(this.pagefindOptions || {});
Expand Down
Loading