mirror of
https://github.com/qybaihe/bla.git
synced 2026-11-20 08:20:59 +08:00
Merge pull request #250 from DeryFerd/fix/self-seal-binding-name-validation
fix(runtime): require a plain identifier for self-seal / sidecar bindingName overrides
This commit is contained in:
@@ -352,6 +352,12 @@ export function generateSelfSealSnippet(
|
||||
const bindingName =
|
||||
options.bindingName ??
|
||||
prng.identifier({ style: "camel", hint: "integrityDigest", entropy: 4 });
|
||||
// The name is interpolated verbatim into `var <name> = …` declarations, so
|
||||
// anything that is not a plain identifier would break or inject into the
|
||||
// generated preamble.
|
||||
if (!/^[$A-Za-z_][$A-Za-z0-9_]*$/.test(bindingName)) {
|
||||
throw new Error("self-seal bindingName must be a valid JavaScript identifier");
|
||||
}
|
||||
const envFingerprint = options.envFingerprint !== false;
|
||||
const siblings = (options.siblingPaths ?? []).filter(
|
||||
(p) => typeof p === "string" && p.length > 0,
|
||||
|
||||
@@ -464,6 +464,11 @@ export function generateSidecarJsPreamble(
|
||||
}
|
||||
const tag = expectedHex.slice(0, 8);
|
||||
const bindingName = options.bindingName ?? `_verifySiblingDigest_${tag}`;
|
||||
// Interpolated verbatim into `var <name> = …` declarations — require a plain
|
||||
// identifier so an override cannot break or inject into the preamble.
|
||||
if (!/^[$A-Za-z_][$A-Za-z0-9_]*$/.test(bindingName)) {
|
||||
throw new Error("sidecar bindingName must be a valid JavaScript identifier");
|
||||
}
|
||||
const expectedU32 = parseInt(tag, 16) >>> 0;
|
||||
const sidecarsLiteral = JSON.stringify(sidecars);
|
||||
const entryLiteral = JSON.stringify(options.entryPath);
|
||||
|
||||
@@ -37,6 +37,22 @@ describe("self-seal helper (#40.5)", () => {
|
||||
expect(snip.code.toLowerCase()).not.toMatch(/\bbla[_-]?seal\b/);
|
||||
});
|
||||
|
||||
it("rejects a bindingName that is not a JavaScript identifier", () => {
|
||||
const prng = new SeededPrng("self-seal-unit-hostile");
|
||||
expect(() =>
|
||||
generateSelfSealSnippet(prng, {
|
||||
siblingPaths: ["./sib-a.js"],
|
||||
bindingName: "x; alert(1);//",
|
||||
}),
|
||||
).toThrow(/identifier/);
|
||||
// PRNG-derived names stay valid.
|
||||
expect(() =>
|
||||
generateSelfSealSnippet(new SeededPrng("self-seal-unit-derived"), {
|
||||
siblingPaths: ["./sib-a.js"],
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("offline digest helper is deterministic for identical inputs", () => {
|
||||
const a = measureSelfSealDigestOffline([
|
||||
{ pathLabel: "main.js", bytes: Buffer.from("hello-module") },
|
||||
|
||||
@@ -58,6 +58,27 @@ describe("sidecar JS inject strip⇒fail (vertical slice)", () => {
|
||||
).toThrow(/64-char/);
|
||||
});
|
||||
|
||||
it("generateSidecarJsPreamble rejects a bindingName that is not a JS identifier", () => {
|
||||
const hex = "a".repeat(64);
|
||||
expect(() =>
|
||||
generateSidecarJsPreamble({
|
||||
entryPath: "main.js",
|
||||
sidecarPaths: ["sib.js"],
|
||||
expectedHex: hex,
|
||||
bindingName: "x; alert(1);//",
|
||||
}),
|
||||
).toThrow(/identifier/);
|
||||
// Valid identifiers stay accepted.
|
||||
expect(() =>
|
||||
generateSidecarJsPreamble({
|
||||
entryPath: "main.js",
|
||||
sidecarPaths: ["sib.js"],
|
||||
expectedHex: hex,
|
||||
bindingName: "_verifySiblingDigest_ok1",
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("bindSidecarEntanglement returns status bound with inject + digest", () => {
|
||||
const entryBytes = new TextEncoder().encode("module.exports = { ok: true };\n");
|
||||
const sibBytes = new TextEncoder().encode("sidecar-payload\n");
|
||||
|
||||
Reference in New Issue
Block a user