fix(javascript): bound semantic expansion and resolve lexical receivers

This commit is contained in:
morluto
2026-10-09 00:49:30 +08:00
parent eaf5734083
commit 16f4609d68
26 changed files with 868 additions and 78 deletions
@@ -2,7 +2,10 @@ import * as t from "@babel/types";
import { compareCodePoints } from "../canonicalOrdering.js";
import type { ElectronStaticValue } from "./electronStaticAnalysisTypes.js";
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
import {
readExactJavaScriptLiteral,
semanticStaticPropertyName,
} from "./javascriptAstValues.js";
import { sourceSlice } from "./javascriptStaticAnalysisHelpers.js";
/** Preserve one literal value or the exact inert expression. */
@@ -142,10 +145,9 @@ const literalValue = (
):
| { readonly found: true; readonly value: string | number | boolean | null }
| { readonly found: false } => {
if (t.isStringLiteral(node) || t.isNumericLiteral(node))
return { found: true, value: node.value };
if (t.isBooleanLiteral(node)) return { found: true, value: node.value };
if (t.isNullLiteral(node)) return { found: true, value: null };
if (node === null || node === undefined) return { found: false };
const literal = readExactJavaScriptLiteral(node);
if (literal.found) return literal;
if (t.isTemplateLiteral(node) && node.expressions.length === 0)
return {
found: true,
@@ -1,4 +1,29 @@
import * as t from "@babel/types";
import type { JavaScriptSemanticPrimitive } from "./javascriptSemanticValueTypes.js";
/** Read one syntax-level primitive only when its value is exact. */
export const readExactJavaScriptLiteral = (
node: t.Node,
):
| { readonly found: true; readonly value: JavaScriptSemanticPrimitive }
| { readonly found: false } => {
if (t.isStringLiteral(node)) return { found: true, value: node.value };
if (t.isNumericLiteral(node) && Number.isFinite(node.value))
return { found: true, value: node.value };
if (t.isBooleanLiteral(node)) return { found: true, value: node.value };
if (t.isNullLiteral(node)) return { found: true, value: null };
if (
t.isTemplateLiteral(node) &&
node.expressions.length === 0 &&
node.quasis.length === 1
) {
const cooked = node.quasis[0]?.value.cooked;
return cooked === null || cooked === undefined
? { found: false }
: { found: true, value: cooked };
}
return { found: false };
};
/** Read an identifier or literal property name without evaluating syntax. */
export const propertyName = (node: t.Node): string => {
@@ -22,6 +22,8 @@ import type {
} from "./javascriptSemanticState.js";
import {
currentSemanticScope,
resolveSemanticBindingState,
semanticResolutionBlocked,
resolveSemanticBindingFromScope,
semanticScopeId,
semanticVariableScope,
@@ -35,6 +37,7 @@ import {
import { collectJavaScriptDerivedSemantics } from "./javascriptSemanticDerivedAnalysis.js";
import { propertyName, range } from "./javascriptStaticAnalysisHelpers.js";
import { semanticCoverage } from "./javascriptSemanticCoverage.js";
import { semanticResourceLimitsIn } from "./javascriptSemanticResourceLimits.js";
import {
parseJavaScriptSource,
type ParsedJavaScriptSource,
@@ -111,7 +114,15 @@ export const analyzeParsedJavaScriptSemantics = (
references,
moduleLinks,
...derived,
coverage: semanticCoverage(parserPartial),
coverage: semanticCoverage(
parserPartial,
semanticResourceLimitsIn([
...bindings.map(({ value }) => value),
...callables.flatMap((callable) =>
callable.returnSites.map(({ value }) => value),
),
]),
),
limitations: [
...(parserPartial
? [
@@ -127,6 +138,122 @@ export const analyzeParsedJavaScriptSemantics = (
};
};
/** Receiver identity facts for the overloaded `.open` syntax only. */
export type JavaScriptOpenReceiverFact =
| "window"
| "indexed-db"
| "cache-storage"
| "local"
| "local-indexed-db"
| "local-cache-storage";
/**
* Resolve only the lexical receiver identity needed to distinguish browser
* `window.open` from XHR and global storage `.open` calls. This deliberately
* builds definitions without running semantic value or provenance analysis.
*/
export const classifyParsedJavaScriptOpenReceivers = (
file: ParsedJavaScriptSource,
): ReadonlyMap<number, JavaScriptOpenReceiverFact> => {
const state = createState(file.program);
collectDefinitions(file.program, state);
const facts = new Map<number, JavaScriptOpenReceiverFact>();
traverseJavaScriptAst(file.program, {
enter: (node) => {
if (
(!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) ||
(!t.isMemberExpression(node.callee) &&
!t.isOptionalMemberExpression(node.callee)) ||
semanticStaticPropertyKey(
node.callee.property,
node.callee.computed,
) !== "open" ||
!t.isIdentifier(node.callee.object)
)
return;
const receiver = node.callee.object.name;
const binding = resolveSemanticBindingState(state, node, receiver);
if (binding === undefined) {
if (semanticResolutionBlocked(state, node, receiver)) return;
const fact = openGlobalFact(receiver);
if (fact !== undefined) facts.set(node.start ?? -1, fact);
return;
}
if (receiver === "indexedDB" || receiver === "caches") {
facts.set(
node.start ?? -1,
receiver === "indexedDB" ? "local-indexed-db" : "local-cache-storage",
);
return;
}
if (binding.mutable || binding.initializers.length !== 1) return;
const initializer = binding.initializers[0]?.node;
if (!t.isIdentifier(initializer)) return;
const initializerBinding = resolveSemanticBindingState(
state,
initializer,
initializer.name,
);
if (initializerBinding !== undefined) {
if (initializer.name === "indexedDB" || initializer.name === "caches") {
const localFact =
initializer.name === "indexedDB"
? "local-indexed-db"
: "local-cache-storage";
facts.set(node.start ?? -1, localFact);
if (
initializerBinding.mutable ||
initializerBinding.initializers.length !== 1
)
return;
const nested = initializerBinding.initializers[0]?.node;
if (t.isIdentifier(nested)) {
const nestedFact = resolveGlobalAliasFact(
state,
initializer,
nested.name,
);
if (nestedFact !== undefined)
facts.set(node.start ?? -1, nestedFact);
}
} else if (initializer.name === "window") {
facts.set(node.start ?? -1, "local");
}
return;
}
const aliasFact = resolveGlobalAliasFact(
state,
initializer,
initializer.name,
);
if (aliasFact !== undefined) facts.set(node.start ?? -1, aliasFact);
},
});
return facts;
};
const openGlobalFact = (
name: string,
): JavaScriptOpenReceiverFact | undefined => {
if (name === "window") return "window";
if (name === "indexedDB") return "indexed-db";
if (name === "caches") return "cache-storage";
return undefined;
};
const resolveGlobalAliasFact = (
state: JavaScriptSemanticAnalysisState,
node: t.Node,
name: string,
): JavaScriptOpenReceiverFact | undefined => {
if (
resolveSemanticBindingState(state, node, name) !== undefined ||
semanticResolutionBlocked(state, node, name)
)
return undefined;
return openGlobalFact(name);
};
const createState = (program: t.Program): JavaScriptSemanticAnalysisState => {
const root: JavaScriptSemanticScopeState = {
scopeId: semanticScopeId("program", program),
@@ -1,7 +1,13 @@
import * as t from "@babel/types";
import { describe, expect, it } from "vitest";
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
import {
analyzeJavaScriptSemantics,
analyzeParsedJavaScriptSemantics,
} from "./javascriptSemanticAnalysis.js";
import { topLevelBinding } from "./javascriptSemanticAnalysis.fixture.js";
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
import { readExactJavaScriptLiteral } from "./javascriptAstValues.js";
const conditionalCandidates = [
'"one"',
@@ -18,6 +24,42 @@ const valueOf = (expression: string) =>
analyzeJavaScriptSemantics(`const answer = ${expression};`),
"answer",
).value;
describe("exact JavaScript literal reader", () => {
it("reads cooked atoms and rejects raw-only or interpolated templates", () => {
const cookedElement = t.templateElement(
{ raw: "raw", cooked: "raw" },
true,
);
cookedElement.value.cooked = "cooked";
const rawOnlyElement = t.templateElement(
{ raw: "raw", cooked: "raw" },
true,
);
rawOnlyElement.value.cooked = null;
expect(readExactJavaScriptLiteral(t.stringLiteral(""))).toEqual({
found: true,
value: "",
});
expect(
readExactJavaScriptLiteral(t.templateLiteral([cookedElement], [])),
).toEqual({ found: true, value: "cooked" });
expect(
readExactJavaScriptLiteral(t.templateLiteral([rawOnlyElement], [])),
).toEqual({ found: false });
expect(
readExactJavaScriptLiteral(
t.templateLiteral(
[
t.templateElement({ raw: "", cooked: "" }, false),
t.templateElement({ raw: "", cooked: "" }, true),
],
[t.identifier("value")],
),
),
).toEqual({ found: false });
});
});
const valuesOf = (expressions: readonly string[]) => {
const ir = analyzeJavaScriptSemantics(
expressions
@@ -176,3 +218,86 @@ describe("logical primitive unions", () => {
);
});
});
describe("semantic expression resource bounds", () => {
it("returns an explicit unknown before expanding a large addition product", () => {
const term = '(choice ? "a" : "b")';
const expression = Array.from({ length: 20 }, () => term).join(" + ");
const value = valueOf(expression);
expect(value).toMatchObject({
status: "unknown",
resourceLimit: "primitive-candidates",
reason: expect.stringMatching(/primitive candidate budget exceeded/i),
});
expect(
analyzeJavaScriptSemantics(`const answer = ${expression};`).coverage,
).toMatchObject({
status: "partial",
omittedCount: null,
resourceLimits: ["primitive-candidates"],
});
});
it("returns an explicit unknown for a deeply nested unary AST without throwing", () => {
const parsed = parseJavaScriptSource("const answer = true;");
const declaration = parsed?.program.body[0];
const declarator = t.isVariableDeclaration(declaration)
? declaration.declarations[0]
: undefined;
if (parsed === null || !t.isVariableDeclarator(declarator))
throw new Error("Expected parsed binding initializer");
let expression: t.Expression = t.booleanLiteral(true);
for (let index = 0; index < 5_000; index += 1)
expression = t.unaryExpression("!", expression, true);
declarator.init = expression;
const ir = analyzeParsedJavaScriptSemantics(parsed);
expect(ir.coverage).toMatchObject({
status: "partial",
omittedCount: null,
resourceLimits: ["expression-depth"],
});
expect(topLevelBinding(ir, "answer").value).toEqual({
status: "unknown",
resourceLimit: "expression-depth",
reason: expect.stringMatching(
/semantic expression depth budget exceeded/i,
),
});
});
it("bounds nested conditional evaluation and provenance walks", () => {
const parsed = parseJavaScriptSource("const answer = true;");
const declaration = parsed?.program.body[0];
const declarator = t.isVariableDeclaration(declaration)
? declaration.declarations[0]
: undefined;
if (parsed === null || !t.isVariableDeclarator(declarator))
throw new Error("Expected parsed binding initializer");
let expression: t.Expression = t.booleanLiteral(true);
for (let index = 0; index < 5_000; index += 1)
expression = t.conditionalExpression(
t.identifier("condition"),
expression,
t.booleanLiteral(false),
);
declarator.init = expression;
const ir = analyzeParsedJavaScriptSemantics(parsed);
const value = topLevelBinding(ir, "answer").value;
expect(value).toMatchObject({
status: "unknown",
resourceLimit: "expression-depth",
reason: expect.stringMatching(
/semantic expression depth budget exceeded/i,
),
});
});
it("preserves exact shallow addition alternatives", () => {
expect(valueOf('(choice ? "a" : "b") + (other ? "1" : "2")')).toEqual({
status: "union",
values: ["a1", "a2", "b1", "b2"],
});
});
});
@@ -1,3 +1,5 @@
import type { JavaScriptSemanticResourceLimit } from "./javascriptSemanticValueTypes.js";
/** Exact retention state for one callable's direct returns. */
export type JavaScriptSemanticReturnCoverage = {
readonly retainedCount: number;
@@ -9,21 +11,34 @@ export type JavaScriptSemanticReturnCoverage = {
/** Coverage state for one semantic recovery pass. */
export type JavaScriptSemanticCoverage =
| {
readonly status: "complete" | "partial";
readonly status: "complete";
readonly omittedCount: 0;
readonly resourceLimits?: readonly JavaScriptSemanticResourceLimit[];
}
| {
readonly status: "partial";
readonly omittedCount: 0 | null;
readonly resourceLimits?: readonly JavaScriptSemanticResourceLimit[];
}
| {
readonly status: "failed";
readonly omittedCount: null;
readonly resourceLimits?: readonly JavaScriptSemanticResourceLimit[];
};
/** Classify whole-file semantic coverage from parser recovery. */
/** Classify whole-file semantic coverage from parsing and value bounds. */
export const semanticCoverage = (
parserPartial: boolean,
): JavaScriptSemanticCoverage => ({
status: parserPartial ? "partial" : "complete",
omittedCount: 0,
});
resourceLimits: readonly JavaScriptSemanticResourceLimit[] = [],
): JavaScriptSemanticCoverage =>
resourceLimits.length > 0
? { status: "partial", omittedCount: null, resourceLimits }
: { status: parserPartial ? "partial" : "complete", omittedCount: 0 };
/** Read typed resource-limit classifications from any coverage state. */
export const semanticCoverageResourceLimits = (
coverage: JavaScriptSemanticCoverage,
): readonly JavaScriptSemanticResourceLimit[] => coverage.resourceLimits ?? [];
/** Classify one callable's direct-return coverage. */
export const semanticReturnCoverage = (
@@ -11,6 +11,7 @@ import {
} from "./javascriptSemanticState.js";
import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js";
import {
readExactJavaScriptLiteral,
semanticStaticPropertyKey,
semanticStaticPropertyName,
} from "./javascriptAstValues.js";
@@ -262,10 +263,12 @@ export const dataEffectLiteralString = (
| t.ArgumentPlaceholder
| undefined,
): string | null => {
if (t.isStringLiteral(node)) return node.value;
if (t.isTemplateLiteral(node) && node.expressions.length === 0)
return node.quasis[0]?.value.cooked ?? node.quasis[0]?.value.raw ?? null;
return null;
if (node === undefined) return null;
const literal = readExactJavaScriptLiteral(node);
if (literal.found && typeof literal.value === "string") return literal.value;
return t.isTemplateLiteral(node) && node.expressions.length === 0
? (node.quasis[0]?.value.raw ?? null)
: null;
};
/** Read a member callee from call or construction syntax. */
@@ -250,6 +250,7 @@ export const JAVASCRIPT_SEMANTIC_UNKNOWN_REASONS = [
"eval-or-generated-code",
"incomplete-module",
"missing-source",
"resource-limit",
"unsupported-syntax",
] as const;
@@ -8,6 +8,7 @@ import type {
export type {
JavaScriptSemanticPrimitive,
JavaScriptSemanticProperty,
JavaScriptSemanticResourceLimit,
JavaScriptSemanticValue,
} from "./javascriptSemanticValueTypes.js";
@@ -6,23 +6,35 @@ import type {
} from "./javascriptSemanticIr.js";
import { compareCodePoints } from "../canonicalOrdering.js";
import { semanticPrimitiveKey } from "./javascriptSemanticProvenance.js";
import { readExactJavaScriptLiteral } from "./javascriptAstValues.js";
import {
SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT,
semanticResourceLimitUnknown,
} from "./javascriptSemanticResourceLimits.js";
/**
* Keep eight independent binary choices exact, while bounding the next
* exponential expansion before it allocates hundreds more alternatives.
*/
export const MAX_SEMANTIC_PRIMITIVE_CANDIDATES =
SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT;
/** Normalize one bounded collection of possible primitive values. */
export const semanticPrimitiveSet = (
values: readonly JavaScriptSemanticPrimitive[],
): JavaScriptSemanticValue => {
if (
values.some((value) => typeof value === "number" && !Number.isFinite(value))
)
return {
status: "unknown",
reason: "Nonfinite numbers are outside the JSON primitive lattice.",
};
const unique = [
...new Map(
values.map((value) => [semanticPrimitiveKey(value), value]),
).values(),
].sort((left, right) =>
const byKey = new Map<string, JavaScriptSemanticPrimitive>();
for (const value of values) {
if (typeof value === "number" && !Number.isFinite(value))
return {
status: "unknown",
reason: "Nonfinite numbers are outside the JSON primitive lattice.",
};
byKey.set(semanticPrimitiveKey(value), value);
if (byKey.size > MAX_SEMANTIC_PRIMITIVE_CANDIDATES)
return semanticResourceLimitUnknown("primitive-candidates");
}
const unique = [...byKey.values()].sort((left, right) =>
compareCodePoints(semanticPrimitiveKey(left), semanticPrimitiveKey(right)),
);
const only = unique[0];
@@ -47,10 +59,5 @@ export const semanticPrimitiveValue = (
):
| { readonly found: true; readonly value: JavaScriptSemanticPrimitive }
| { readonly found: false } => {
if (t.isStringLiteral(node)) return { found: true, value: node.value };
if (t.isNumericLiteral(node) && Number.isFinite(node.value))
return { found: true, value: node.value };
if (t.isBooleanLiteral(node)) return { found: true, value: node.value };
if (t.isNullLiteral(node)) return { found: true, value: null };
return { found: false };
return readExactJavaScriptLiteral(node);
};
@@ -0,0 +1,47 @@
import type {
JavaScriptSemanticResourceLimit,
JavaScriptSemanticValue,
} from "./javascriptSemanticValueTypes.js";
export const SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT = 256;
export const SEMANTIC_EXPRESSION_DEPTH_LIMIT = 256;
const RESOURCE_LIMIT_REASON: Record<JavaScriptSemanticResourceLimit, string> = {
"primitive-candidates":
"Primitive candidate budget exceeded (maximum 256 alternatives).",
"expression-depth":
"Semantic expression depth budget exceeded (maximum 256 nested expressions).",
};
/** Make an explicit unknown value tagged with the semantic resource bound. */
export const semanticResourceLimitUnknown = (
resourceLimit: JavaScriptSemanticResourceLimit,
): JavaScriptSemanticValue => ({
status: "unknown",
reason: RESOURCE_LIMIT_REASON[resourceLimit],
resourceLimit,
});
/** Collect resource-limit classifications from nested semantic values. */
export const semanticResourceLimitsIn = (
values: readonly JavaScriptSemanticValue[],
): readonly JavaScriptSemanticResourceLimit[] => {
const found = new Set<JavaScriptSemanticResourceLimit>();
const pending = [...values];
while (pending.length > 0) {
const value = pending.pop();
if (value === undefined) continue;
if (value.status === "unknown" && value.resourceLimit !== undefined)
found.add(value.resourceLimit);
else if (value.status === "object")
for (const property of value.properties) pending.push(property.value);
else if (value.status === "array")
for (const item of value.items) pending.push(item);
}
return [...found].sort();
};
/** Display label for one stable resource classification. */
export const semanticResourceLimitReason = (
resourceLimit: JavaScriptSemanticResourceLimit,
): string => RESOURCE_LIMIT_REASON[resourceLimit];
@@ -1,6 +1,11 @@
/** Primitive values admitted into the constant lattice. */
export type JavaScriptSemanticPrimitive = string | number | boolean | null;
/** Resource bound that prevented an exact semantic value. */
export type JavaScriptSemanticResourceLimit =
| "primitive-candidates"
| "expression-depth";
type JavaScriptSemanticObjectValue = {
readonly status: "object";
readonly properties: readonly JavaScriptSemanticProperty[];
@@ -44,6 +49,7 @@ export type JavaScriptSemanticValue =
| {
readonly status: "unknown" | "ambiguous" | "cycle";
readonly reason: string;
readonly resourceLimit?: JavaScriptSemanticResourceLimit;
};
/** One statically named object-literal property. */
@@ -5,6 +5,7 @@ import { invalidateSemanticMutationPath } from "./javascriptSemanticMutationValu
import type {
JavaScriptBindingProvenance,
JavaScriptSemanticProperty,
JavaScriptSemanticResourceLimit,
JavaScriptSemanticValue,
} from "./javascriptSemanticIr.js";
import {
@@ -23,36 +24,57 @@ import {
uniqueSemanticOrigins,
} from "./javascriptSemanticProvenance.js";
import {
MAX_SEMANTIC_PRIMITIVE_CANDIDATES,
semanticPrimitiveCandidates as primitiveCandidates,
semanticPrimitiveSet as primitiveSet,
semanticPrimitiveValue as primitiveValue,
} from "./javascriptSemanticPrimitives.js";
import {
SEMANTIC_EXPRESSION_DEPTH_LIMIT,
SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT,
semanticResourceLimitUnknown,
semanticResourceLimitReason,
} from "./javascriptSemanticResourceLimits.js";
interface EvaluationContext {
readonly state: JavaScriptSemanticAnalysisState;
readonly bindings: ReadonlySet<string>;
readonly expressionDepth: number;
}
// Cap expression recursion independently from graph nodes and primitive unions.
const isSemanticResourceLimit = (
value: JavaScriptSemanticValue,
): value is Extract<
JavaScriptSemanticValue,
{ readonly status: "unknown" | "ambiguous" | "cycle" }
> & { readonly resourceLimit: JavaScriptSemanticResourceLimit } =>
value.status === "unknown" && value.resourceLimit !== undefined;
/** Evaluate one binding in the bounded constant-value lattice. */
export const evaluateSemanticBinding = (
binding: JavaScriptSemanticBindingState,
state: JavaScriptSemanticAnalysisState,
): JavaScriptSemanticValue =>
evaluateBinding(binding, { state, bindings: new Set() });
evaluateBinding(binding, { state, bindings: new Set(), expressionDepth: 0 });
/** Evaluate one arbitrary inert expression in the established lexical state. */
export const evaluateSemanticExpression = (
node: t.Node,
state: JavaScriptSemanticAnalysisState,
): JavaScriptSemanticValue =>
evaluateExpression(node, { state, bindings: new Set() });
evaluateExpression(node, { state, bindings: new Set(), expressionDepth: 0 });
/** Follow module provenance through destructuring, members, and aliases. */
export const evaluateSemanticProvenance = (
binding: JavaScriptSemanticBindingState,
state: JavaScriptSemanticAnalysisState,
): JavaScriptBindingProvenance =>
provenanceForBinding(binding, { state, bindings: new Set() });
provenanceForBinding(binding, {
state,
bindings: new Set(),
expressionDepth: 0,
});
const evaluateBinding = (
binding: JavaScriptSemanticBindingState,
@@ -85,6 +107,8 @@ const evaluateExpression = (
node: t.Node,
context: EvaluationContext,
): JavaScriptSemanticValue => {
if (context.expressionDepth > SEMANTIC_EXPRESSION_DEPTH_LIMIT)
return semanticResourceLimitUnknown("expression-depth");
const literal = primitiveValue(node);
if (literal.found) return { status: "literal", value: literal.value };
if (t.isIdentifier(node)) {
@@ -128,18 +152,23 @@ const evaluateTemplate = (
let candidates = [""];
for (let index = 0; index < node.quasis.length; index += 1) {
const quasi = node.quasis[index];
// Semantic template evaluation retains its established raw fallback for
// synthetic AST inputs; exact atom readers remain cooked-only.
const text = quasi?.value.cooked ?? quasi?.value.raw ?? "";
candidates = candidates.map((prefix) => `${prefix}${text}`);
const expression = node.expressions[index];
if (expression === undefined) continue;
const values = primitiveCandidates(
evaluateExpression(expression, nestedContext(context)),
);
if (values === null)
const evaluated = evaluateExpression(expression, nestedContext(context));
const values = primitiveCandidates(evaluated);
if (values === null) {
if (isSemanticResourceLimit(evaluated)) return evaluated;
return {
status: "unknown",
reason: "Template expression is not a bounded primitive.",
};
}
if (candidates.length > SEMANTIC_PRIMITIVE_CANDIDATE_LIMIT / values.length)
return semanticResourceLimitUnknown("primitive-candidates");
candidates = candidates.flatMap((prefix) =>
values.map((value) => `${prefix}${String(value)}`),
);
@@ -311,8 +340,13 @@ const addPrimitiveValues = (
): JavaScriptSemanticValue => {
const left = primitiveCandidates(leftValue);
const right = primitiveCandidates(rightValue);
if (left === null || right === null)
if (left === null || right === null) {
if (isSemanticResourceLimit(leftValue)) return leftValue;
if (isSemanticResourceLimit(rightValue)) return rightValue;
return { status: "unknown", reason: "Non-primitive addition." };
}
if (left.length > MAX_SEMANTIC_PRIMITIVE_CANDIDATES / right.length)
return semanticResourceLimitUnknown("primitive-candidates");
const values = left.flatMap((leftValue) =>
right.map((rightValue) =>
typeof leftValue === "string" || typeof rightValue === "string"
@@ -327,11 +361,12 @@ const evaluateUnary = (
node: t.UnaryExpression,
context: EvaluationContext,
): JavaScriptSemanticValue => {
const argument = primitiveCandidates(
evaluateExpression(node.argument, nestedContext(context)),
);
if (argument === null)
const evaluated = evaluateExpression(node.argument, nestedContext(context));
const argument = primitiveCandidates(evaluated);
if (argument === null) {
if (isSemanticResourceLimit(evaluated)) return evaluated;
return { status: "unknown", reason: "Non-primitive unary operand." };
}
if (node.operator === "!")
return primitiveSet(argument.map((value) => !value));
if (node.operator === "+")
@@ -436,6 +471,11 @@ const provenanceForExpression = (
node: t.Node,
context: EvaluationContext,
): JavaScriptBindingProvenance => {
if (context.expressionDepth > SEMANTIC_EXPRESSION_DEPTH_LIMIT)
return semanticUnresolvedProvenance(
"unknown",
semanticResourceLimitReason("expression-depth"),
);
const required = semanticRequireOrigin(node, context.state);
if (required !== undefined) return semanticOriginsProvenance([required]);
if (t.isIdentifier(node)) {
@@ -506,6 +546,8 @@ const provenanceForExpression = (
const mergeValues = (
values: readonly JavaScriptSemanticValue[],
): JavaScriptSemanticValue => {
const resourceLimit = values.find(isSemanticResourceLimit);
if (resourceLimit !== undefined) return resourceLimit;
const primitives = values.flatMap(
(value) => primitiveCandidates(value) ?? [],
);
@@ -521,6 +563,7 @@ const nestedContext = (
bindingId?: string,
): EvaluationContext => ({
state: context.state,
expressionDepth: context.expressionDepth + 1,
bindings:
bindingId === undefined
? context.bindings
@@ -27,6 +27,10 @@ import {
type JavaScriptAnalysisAccumulator as AnalysisAccumulator,
} from "./javascriptStaticAnalysisState.js";
import type { JavaScriptStaticAnalysis } from "./javascriptStaticAnalysisTypes.js";
import {
classifyParsedJavaScriptOpenReceivers,
type JavaScriptOpenReceiverFact,
} from "./javascriptSemanticAnalysis.js";
import {
parseJavaScriptSource,
type ParsedJavaScriptSource,
@@ -39,16 +43,21 @@ export const analyzeJavaScriptStaticSource = (
const file = parseJavaScriptSource(source);
return file === null
? failedJavaScriptStaticAnalysis()
: analyzeParsedJavaScriptStaticSource(source, file);
: analyzeParsedJavaScriptStaticSource(
source,
file,
classifyParsedJavaScriptOpenReceivers(file),
);
};
/** Recover static structure from an already parsed JavaScript artifact. */
export const analyzeParsedJavaScriptStaticSource = (
source: string,
file: ParsedJavaScriptSource,
openReceiverFacts?: ReadonlyMap<number, JavaScriptOpenReceiverFact>,
): JavaScriptStaticAnalysis => {
const accumulator = createJavaScriptAnalysisAccumulator();
traverseStaticSource(source, file, accumulator);
traverseStaticSource(source, file, accumulator, openReceiverFacts);
addSourceMapDirectives(source, file.comments ?? [], accumulator);
return finalizeStaticAnalysis(source, file, accumulator);
};
@@ -57,11 +66,12 @@ const traverseStaticSource = (
source: string,
file: ParsedJavaScriptSource,
accumulator: AnalysisAccumulator,
openReceiverFacts?: ReadonlyMap<number, JavaScriptOpenReceiverFact>,
): void => {
traverseJavaScriptAst(file, {
enter: (node) => {
accumulator.visitedNodes += 1;
inspectNode(source, node, accumulator);
inspectNode(source, node, accumulator, openReceiverFacts);
return undefined;
},
});
@@ -140,16 +150,20 @@ const inspectNode = (
source: string,
node: t.Node,
accumulator: AnalysisAccumulator,
openReceiverFacts?: ReadonlyMap<number, JavaScriptOpenReceiverFact>,
): void => {
const findings = {
source,
accumulator,
...(openReceiverFacts === undefined ? {} : { openReceiverFacts }),
};
inspectElectronStaticNode(node, findings);
if (t.isCallExpression(node)) {
inspectBundlerRegistration(source, node, accumulator);
inspectEsbuildWrapper(source, node, accumulator);
inspectCall(source, node, findings);
} else if (t.isOptionalCallExpression(node)) {
inspectCall(source, node, findings);
} else if (t.isNewExpression(node)) inspectCall(source, node, findings);
if (
(t.isImportDeclaration(node) || t.isExportAllDeclaration(node)) &&
@@ -28,10 +28,15 @@ import type {
JavaScriptReferenceInput as ReferenceInput,
} from "./javascriptStaticAnalysisState.js";
type JavaScriptCallLike =
| t.CallExpression
| t.OptionalCallExpression
| t.NewExpression;
/** Inspect one call or new expression for references, endpoints, storage, and roles. */
export const inspectCall = (
source: string,
node: t.CallExpression | t.NewExpression,
node: JavaScriptCallLike,
context: FindingContext,
): void => {
const { accumulator } = context;
@@ -92,7 +97,7 @@ export const inspectCall = (
};
const requireCallSpecifier = (
node: t.CallExpression | t.NewExpression,
node: JavaScriptCallLike,
name: string,
moduleRequireName: string | null,
): { readonly specifier: string | undefined } | null => {
@@ -109,7 +114,7 @@ const requireCallSpecifier = (
};
const chunkLoadSpecifier = (
node: t.CallExpression | t.NewExpression,
node: JavaScriptCallLike,
name: string,
moduleRequireName: string | null,
): { readonly specifier: string | undefined } | null => {
@@ -123,7 +128,7 @@ const chunkLoadSpecifier = (
const vitePreloadDependencySpecifiers = (
source: string,
node: t.CallExpression | t.NewExpression,
node: JavaScriptCallLike,
name: string,
): readonly string[] => {
if (name !== "__vite__mapDeps" || !t.isArrayExpression(node.arguments[0]))
@@ -147,7 +152,7 @@ const vitePreloadDependencyTable = (source: string): readonly string[] => {
};
const inspectEndpointCall = (
node: t.CallExpression | t.NewExpression,
node: JavaScriptCallLike,
name: string,
first: string | undefined,
context: FindingContext,
@@ -162,7 +167,13 @@ const inspectEndpointCall = (
value: first,
mechanism: `call:${name}`,
});
const endpoint = endpointArgumentHelper(name, node.arguments, node.callee);
const receiverFact = context.openReceiverFacts?.get(node.start ?? -1);
const endpoint = endpointArgumentHelper(
name,
node.arguments,
node.callee,
receiverFact,
);
if (endpoint !== undefined)
addEndpoint(context, {
node,
@@ -209,12 +220,22 @@ export const inspectRoleProperty = (
};
const inspectStorageCall = (
node: t.CallExpression | t.NewExpression,
node: JavaScriptCallLike,
name: string,
first: string | undefined,
context: FindingContext,
): void => {
const storage = storageKind(name);
const receiverFact = context.openReceiverFacts?.get(node.start ?? -1);
const storage =
receiverFact === "local" ||
receiverFact === "local-indexed-db" ||
receiverFact === "local-cache-storage"
? undefined
: receiverFact === "indexed-db"
? "indexed-db"
: receiverFact === "cache-storage"
? "cache-storage"
: storageKind(name);
if (storage === undefined) return;
addLocatedFinding(context, {
collection: context.accumulator.storage,
@@ -437,22 +437,38 @@ const xhrOpenUrl = (
name: string,
methodNode: t.Node | null | undefined,
urlNode: t.Node | null | undefined,
receiverFact?:
| "window"
| "indexed-db"
| "cache-storage"
| "local"
| "local-indexed-db"
| "local-cache-storage",
): string | undefined => {
const url = stringValue(urlNode);
if (url === undefined) return undefined;
const method = stringValue(methodNode);
// IndexedDB and Cache Storage `open` take a name and a version, so a
// receiver spelled as storage needs a standard HTTP method literal.
const storage = storageKind(name) !== undefined;
const storage =
receiverFact === "local-indexed-db" ||
receiverFact === "local-cache-storage" ||
receiverFact === "indexed-db" ||
receiverFact === "cache-storage" ||
storageKind(name) !== undefined;
if (method === undefined)
return storage ||
receiverFact === "window" ||
WINDOW_OPEN_CALL.test(name) ||
FS_OPEN_FLAGS.has(url) ||
BROWSING_CONTEXT_KEYWORDS.has(url.toLowerCase())
? undefined
: url;
return XHR_METHODS.has(method.toUpperCase()) ||
(!storage && /^[A-Z][A-Z-]*$/u.test(method))
((!storage ||
receiverFact === "local-indexed-db" ||
receiverFact === "local-cache-storage") &&
/^[A-Z][A-Z-]*$/u.test(method))
? url
: undefined;
};
@@ -504,10 +520,19 @@ export const endpointArgument = (
| t.ArgumentPlaceholder
)[],
callee: t.Node,
receiverFact?:
| "window"
| "indexed-db"
| "cache-storage"
| "local"
| "local-indexed-db"
| "local-cache-storage",
): string | undefined => {
if (receiverFact === "window") return undefined;
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
return stringValue(args[0]);
if (name.endsWith(".open")) return xhrOpenUrl(name, args[0], args[1]);
if (name.endsWith(".open"))
return xhrOpenUrl(name, args[0], args[1], receiverFact);
const method = ["get", "post", "put", "patch", "delete", "request"].find(
(candidate) => name === candidate || name.endsWith(`.${candidate}`),
);
@@ -55,6 +55,15 @@ export interface JavaScriptAnalysisAccumulator {
export interface JavaScriptFindingContext {
readonly source: string;
readonly accumulator: JavaScriptAnalysisAccumulator;
readonly openReceiverFacts?: ReadonlyMap<
number,
| "window"
| "indexed-db"
| "cache-storage"
| "local"
| "local-indexed-db"
| "local-cache-storage"
>;
}
/** Candidate import, require, worker, or service-worker reference. */