import * as t from "@babel/types"; import { compareUnicodeCodePoints, compositeKey, } from "../unicodeCodePointOrder.js"; import { stripQueryAndFragment } from "../artifactPathSyntax.js"; import type { ElectronNativeAddonBindingFinding } from "./electronStaticAnalysisTypes.js"; import { isTypeOnlyExportSpecifier, isTypeOnlyImport, semanticStaticModuleSource, } from "./javascriptModuleDeclaration.js"; import { addLocatedFinding } from "./javascriptStaticAnalysisFindings.js"; import { argumentNode, range } from "./javascriptStaticAnalysisHelpers.js"; import { stringValue, propertyName, semanticStaticPropertyKey, unwrapJavaScriptExpression, } from "./javascriptAstValues.js"; import { semanticRequireCallAt } from "./javascriptSemanticRequireOrigin.js"; import { commonJsExportApplicability, commonJsRuntimeExportValues, indexCommonJsExportReceiversSteps, type CommonJsExportReceiverEffect, type CommonJsExportSite, } from "./javascriptCommonJsExportReceivers.js"; import type { JavaScriptReceiverIndex } from "./javascriptSemanticAnalysis.js"; import type { JavaScriptFindingContext } from "./javascriptStaticAnalysisState.js"; interface NativeBindingInput { readonly context: JavaScriptFindingContext; readonly node: t.Node; readonly specifier: string; readonly kind: ElectronNativeAddonBindingFinding["binding_kind"]; readonly moduleKind: ElectronNativeAddonBindingFinding["module_kind"]; readonly members: readonly string[]; readonly namespaceAccess: boolean; readonly dynamicMemberAccess: boolean; } /** Inspect JavaScript-side imports and re-exports of native .node addons. */ export const inspectElectronNativeNode = ( node: t.Node, context: JavaScriptFindingContext, ): void => { if ( t.isImportDeclaration(node) || t.isExportNamedDeclaration(node) || t.isExportAllDeclaration(node) ) inspectStaticBinding(node, context); else if (t.isVariableDeclarator(node) || t.isAssignmentExpression(node)) { const value = nativeCallReceiver( t.isVariableDeclarator(node) ? node.init : node.right, ); // Admission is syntax only; the shared call owner proves loader and arguments later. if ( value !== undefined && value.arguments.some((argument) => t.isArrayExpression(argument) ? argument.elements.some((element) => isNativeSpecifier(stringValue(element) ?? "", "require"), ) : isNativeSpecifier(stringValue(argument) ?? "", "require"), ) ) context.accumulator.pendingNativeAddonBindings.push(node); } }; /** Shared loader, argument and receiver facts settle before native projection. */ export function* finishElectronNativeBindingsSteps( context: JavaScriptFindingContext, index: JavaScriptReceiverIndex, ): Generator { const pending = context.accumulator.pendingNativeAddonBindings; if (pending.length === 0) return; yield* index.indexMemberMutationFactsSteps(); const receivers = pending.some((node) => t.isAssignmentExpression(node)) ? yield* indexCommonJsExportReceiversSteps(index.bindings) : new Map(); for (const node of pending) { if (t.isVariableDeclarator(node)) inspectRequireBinding(node, context, index); else inspectReExport(node, context, index, receivers.get(node)); } pending.length = 0; } const inspectStaticBinding = ( node: t.ImportDeclaration | t.ExportNamedDeclaration | t.ExportAllDeclaration, context: JavaScriptFindingContext, ): void => { if (node.source == null || !isNativeSpecifier(node.source.value, "import")) return; const source = semanticStaticModuleSource(node); if (source === null) return; const members: string[] = []; let namespaceAccess = false; if (t.isExportAllDeclaration(node)) namespaceAccess = true; else { // Type-only bindings do not request native-addon members. let hasRuntimeSpecifier = node.specifiers.length === 0; for (const specifier of node.specifiers) { if ( (t.isImportSpecifier(specifier) && isTypeOnlyImport(specifier)) || (t.isExportSpecifier(specifier) && isTypeOnlyExportSpecifier(specifier)) ) continue; hasRuntimeSpecifier = true; if (t.isImportSpecifier(specifier)) members.push(propertyName(specifier.imported)); else if (t.isExportSpecifier(specifier)) members.push(propertyName(specifier.local)); else if ( t.isImportDefaultSpecifier(specifier) || t.isExportDefaultSpecifier(specifier) ) members.push("default"); else if ( t.isImportNamespaceSpecifier(specifier) || t.isExportNamespaceSpecifier(specifier) ) namespaceAccess = true; } if (!hasRuntimeSpecifier) return; } addBinding({ context, node, specifier: source.value, kind: t.isImportDeclaration(node) ? "import" : "re-export", moduleKind: "import", members, namespaceAccess, dynamicMemberAccess: false, }); }; const inspectRequireBinding = ( node: t.VariableDeclarator, context: JavaScriptFindingContext, index: JavaScriptReceiverIndex, ): void => { const required = nativeRequire(node.init, context, index); if (required === undefined) return; addBinding({ context, node, specifier: required.specifier, kind: "require", moduleKind: "require", ...(required.hasMemberAccess ? required.access : bindingMembers(node.id)), }); }; const inspectReExport = ( node: t.AssignmentExpression, context: JavaScriptFindingContext, index: JavaScriptReceiverIndex, receiver: CommonJsExportReceiverEffect | undefined, ): void => { const exported = commonJsRuntimeExportValues(node, index.bindings)[0]; if (exported === undefined) return; if (exported.loaderAuthority === "candidate") { context.accumulator.unknownFindings += 1; return; } const applicability = commonJsExportApplicability( receiver, exported.exportedName, ); if (applicability === "detached") return; if (applicability === "uncertain") { context.accumulator.unknownFindings += 1; return; } // The export owner leaves arithmetic/logical stores without a selected RHS. const value = exported.value; if (value == null) return; const required = nativeRequire(value, context, index); if (required === undefined) return; addBinding({ context, node, specifier: required.specifier, kind: "re-export", moduleKind: "require", ...required.access, }); }; const addBinding = (input: NativeBindingInput): void => { const { context, node, specifier, kind } = input; const members = [...new Set(input.members)].sort(compareUnicodeCodePoints); addLocatedFinding(context, { collection: context.accumulator.nativeAddonBindings, // Members are a variable-length source-derived list: compositeKey keeps // ["a\0b"] distinct from ["a", "b"] instead of collapsing one binding. key: compositeKey([ "native-addon-binding", kind, specifier, members, input.namespaceAccess, input.dynamicMemberAccess, ]), node, value: { specifier, binding_kind: kind, module_kind: input.moduleKind, members, namespace_access: input.namespaceAccess, dynamic_member_access: input.dynamicMemberAccess, module_key: null, location: range(node), }, }); }; const nativeCallReceiver = ( node: t.Node | null | undefined, ): t.CallExpression | t.OptionalCallExpression | undefined => { if (node == null) return undefined; node = unwrapJavaScriptExpression(node).node; while (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) node = unwrapJavaScriptExpression(node.object).node; return t.isCallExpression(node) || t.isOptionalCallExpression(node) ? node : undefined; }; const nativeRequire = ( node: t.Node | null | undefined, context: JavaScriptFindingContext, index: JavaScriptReceiverIndex, ): | { readonly specifier: string; readonly hasMemberAccess: boolean; readonly access: NativeMemberAccess; } | undefined => { if (node == null) return undefined; node = unwrapJavaScriptExpression(node).node; let member: string | null = null; let hasMemberAccess = false; while (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) { if (!t.isNode(node.object)) return undefined; const property = semanticStaticPropertyKey(node.property, node.computed); // Walking inward leaves the first member consumed from the addon itself. member = property; hasMemberAccess = true; node = unwrapJavaScriptExpression(node.object).node; } if (!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) return undefined; const call = semanticRequireCallAt( node, index.bindings, context.accumulator.factoryRequireBindingIds, index, ); if ( call === undefined || call.loader === "local" || call.operation !== "load" ) return undefined; const specifier = stringValue(argumentNode(call.arguments?.[0])); if (specifier === undefined || !isNativeSpecifier(specifier, "require")) return undefined; if ( call.loader === "unknown" || !call.sourceRelative || call.sourceRelativePath !== undefined ) { context.accumulator.unknownFindings += 1; return undefined; } return { specifier, hasMemberAccess, access: { members: member === null ? [] : [member], namespaceAccess: !hasMemberAccess, dynamicMemberAccess: hasMemberAccess && member === null, }, }; }; interface NativeMemberAccess { readonly members: readonly string[]; readonly namespaceAccess: boolean; readonly dynamicMemberAccess: boolean; } const bindingMembers = (pattern: t.Node): NativeMemberAccess => { if (!t.isObjectPattern(pattern)) return { members: [], namespaceAccess: true, dynamicMemberAccess: false }; const members: string[] = []; let namespaceAccess = false; let dynamicMemberAccess = false; for (const property of pattern.properties) { if (t.isRestElement(property)) { namespaceAccess = true; continue; } const key = semanticStaticPropertyKey(property.key, property.computed); if (key === null) dynamicMemberAccess = true; else members.push(key); } return { members, namespaceAccess, dynamicMemberAccess }; }; const isNativeSpecifier = ( specifier: string, moduleKind: ElectronNativeAddonBindingFinding["module_kind"], ): boolean => { const path = moduleKind === "require" ? specifier : stripQueryAndFragment(specifier); // Extension dispatch uses exact spelling, independently of filesystem casing. return path.endsWith(".node"); };