fix(javascript): reconstruct HTML script loads accurately (#1042)
* fix: parse HTML script references with HTML semantics Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> * test(javascript): cover exact HTML script resource identity --------- Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> Co-authored-by: N0zoM1z0 <1705004398@qq.com>
This commit is contained in:
co-authored by
N0zoM1z0
parent
53409ab196
commit
1154f2f03c
@@ -0,0 +1,70 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { htmlArtifactReferences } from "./htmlArtifactReferences.js";
|
||||
|
||||
it.each([
|
||||
['<script data-src="decoy.js"></script>', []],
|
||||
['<script x:src="decoy.js"></script>', []],
|
||||
[`<script title='src="decoy.js"'></script>`, []],
|
||||
["<script src=actual.js></script>", ["actual.js"]],
|
||||
['<script title=">" src="actual.js"></script>', ["actual.js"]],
|
||||
['<SCRIPT SRC="actual.js"></SCRIPT>', ["actual.js"]],
|
||||
["<script src='actual.js'></script>", ["actual.js"]],
|
||||
['<script data-src="decoy.js" src="actual.js"></script>', ["actual.js"]],
|
||||
['<script src="actual.js" src="decoy.js"></script>', ["actual.js"]],
|
||||
['<script src="" src="decoy.js"></script>', []],
|
||||
['<script src="actual.js"></script>', ["actual.js"]],
|
||||
['<!-- <script src="decoy.js"></script> -->', []],
|
||||
['<textarea><script src="decoy.js"></script></textarea>', []],
|
||||
['<style><script src="decoy.js"></script></style>', []],
|
||||
[`<script>const text = '<script src="decoy.js">';</script>`, []],
|
||||
[
|
||||
'<template><base href="decoy/"><script src="decoy.js"></script></template>',
|
||||
[],
|
||||
],
|
||||
['<svg><script src="decoy.js"></script></svg>', []],
|
||||
[
|
||||
'<svg><foreignObject><script src="actual.js"></script></foreignObject></svg>',
|
||||
["actual.js"],
|
||||
],
|
||||
// The existing extraction contract does not classify script MIME types.
|
||||
['<script type="application/json" src="actual.js"></script>', ["actual.js"]],
|
||||
])("extracts document references from %s", (text, expected) => {
|
||||
expect(
|
||||
htmlArtifactReferences(text).scripts.map(({ scriptPath }) => scriptPath),
|
||||
).toEqual(expected);
|
||||
});
|
||||
|
||||
it("retains exact opening-tag offsets rather than the script body", () => {
|
||||
const opening = '<script\n title=">" src=actual.js>';
|
||||
const prefix = "<!doctype html>\n\n";
|
||||
const text = `${prefix}${opening}ignored body</script>`;
|
||||
expect(htmlArtifactReferences(text)).toEqual({
|
||||
baseHref: null,
|
||||
scripts: [
|
||||
{
|
||||
scriptPath: "actual.js",
|
||||
startOffset: prefix.length,
|
||||
endOffset: prefix.length + opening.length,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
['<base data-href="decoy/"><base href=actual/>', "actual/"],
|
||||
['<base href="actual/" href="decoy/"><base href="later/">', "actual/"],
|
||||
['<base href=""><base href="decoy/">', ""],
|
||||
['<template><base href="decoy/"></template><base href="actual/">', "actual/"],
|
||||
['<!-- <base href="decoy/"> --><base title=">" href="actual/">', "actual/"],
|
||||
])("selects the first real base href from %s", (text, expected) => {
|
||||
expect(htmlArtifactReferences(text).baseHref).toBe(expected);
|
||||
});
|
||||
|
||||
it("traverses deep valid document content without recursive stack growth", () => {
|
||||
const depth = 12_000;
|
||||
const text = `${"<div>".repeat(depth)}<script src=actual.js></script>${"</div>".repeat(depth)}`;
|
||||
expect(
|
||||
htmlArtifactReferences(text).scripts.map(({ scriptPath }) => scriptPath),
|
||||
).toEqual(["actual.js"]);
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
defaultTreeAdapter,
|
||||
parse,
|
||||
type DefaultTreeAdapterTypes,
|
||||
} from "parse5";
|
||||
|
||||
interface HtmlScriptReference {
|
||||
readonly scriptPath: string;
|
||||
readonly startOffset: number;
|
||||
readonly endOffset: number;
|
||||
}
|
||||
|
||||
/** Extract inert HTML script references and the first document base href. */
|
||||
export const htmlArtifactReferences = (
|
||||
text: string,
|
||||
): {
|
||||
readonly baseHref: string | null;
|
||||
readonly scripts: readonly HtmlScriptReference[];
|
||||
} => {
|
||||
const pending: DefaultTreeAdapterTypes.Node[] = [
|
||||
parse(text, { sourceCodeLocationInfo: true }),
|
||||
];
|
||||
const scripts: HtmlScriptReference[] = [];
|
||||
let baseHref: string | null = null;
|
||||
while (pending.length > 0) {
|
||||
const node = pending.pop();
|
||||
if (node === undefined) continue;
|
||||
if (
|
||||
defaultTreeAdapter.isElementNode(node) &&
|
||||
node.namespaceURI === "http://www.w3.org/1999/xhtml"
|
||||
) {
|
||||
if (node.tagName === "base" && baseHref === null) {
|
||||
const href = node.attrs.find(({ name }) => name === "href");
|
||||
if (href !== undefined) baseHref = href.value;
|
||||
}
|
||||
if (node.tagName === "script") {
|
||||
const src = node.attrs.find(({ name }) => name === "src");
|
||||
const location = node.sourceCodeLocation?.startTag;
|
||||
if (src !== undefined && src.value !== "" && location !== undefined) {
|
||||
scripts.push({
|
||||
scriptPath: src.value,
|
||||
startOffset: location.startOffset,
|
||||
endOffset: location.endOffset,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// Template content belongs to a separate inert document fragment. Only
|
||||
// traverse the actual document children, without recursion on deep input.
|
||||
if ("childNodes" in node) {
|
||||
for (const child of node.childNodes.toReversed()) pending.push(child);
|
||||
}
|
||||
}
|
||||
return { baseHref, scripts };
|
||||
};
|
||||
Reference in New Issue
Block a user