@interlace/eslint-devkit 1.12.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interlace/eslint-devkit",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "description": "Toolkit for building ESLint plugins in TypeScript — AST helpers, type utilities, rule-creation scaffolding, security benchmarks, and SARIF output.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -15,3 +15,4 @@ export * from './browser-escape-hatch-rule';
15
15
  export * from './system-prompt-injection-rule';
16
16
  export * from './message-source';
17
17
  export * from './raw-identifier-rule';
18
+ export * from './module-evidence';
@@ -1 +1 @@
1
- "use strict";var __createBinding=this&&this.__createBinding||(Object.create?(function(o,m,k,k2){if(k2===void 0)k2=k;var desc=Object.getOwnPropertyDescriptor(m,k);if(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable)){desc={enumerable:true,get:function(){return m[k]}}}Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){if(k2===void 0)k2=k;o[k2]=m[k]}));var __exportStar=this&&this.__exportStar||function(m,exports2){for(var p in m)if(p!=="default"&&!Object.prototype.hasOwnProperty.call(exports2,p))__createBinding(exports2,m,p)};Object.defineProperty(exports,"__esModule",{value:true});__exportStar(require("./rule-creator"),exports);__exportStar(require("./mock-context"),exports);__exportStar(require("./sql-injection-rule"),exports);__exportStar(require("./unscoped-mutation-rule"),exports);__exportStar(require("./require-tls-rule"),exports);__exportStar(require("./mass-assignment-rule"),exports);__exportStar(require("./hardcoded-credentials-rule"),exports);__exportStar(require("./sdk-api-key-rule"),exports);__exportStar(require("./browser-escape-hatch-rule"),exports);__exportStar(require("./system-prompt-injection-rule"),exports);__exportStar(require("./message-source"),exports);__exportStar(require("./raw-identifier-rule"),exports);
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?(function(o,m,k,k2){if(k2===void 0)k2=k;var desc=Object.getOwnPropertyDescriptor(m,k);if(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable)){desc={enumerable:true,get:function(){return m[k]}}}Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){if(k2===void 0)k2=k;o[k2]=m[k]}));var __exportStar=this&&this.__exportStar||function(m,exports2){for(var p in m)if(p!=="default"&&!Object.prototype.hasOwnProperty.call(exports2,p))__createBinding(exports2,m,p)};Object.defineProperty(exports,"__esModule",{value:true});__exportStar(require("./rule-creator"),exports);__exportStar(require("./mock-context"),exports);__exportStar(require("./sql-injection-rule"),exports);__exportStar(require("./unscoped-mutation-rule"),exports);__exportStar(require("./require-tls-rule"),exports);__exportStar(require("./mass-assignment-rule"),exports);__exportStar(require("./hardcoded-credentials-rule"),exports);__exportStar(require("./sdk-api-key-rule"),exports);__exportStar(require("./browser-escape-hatch-rule"),exports);__exportStar(require("./system-prompt-injection-rule"),exports);__exportStar(require("./message-source"),exports);__exportStar(require("./raw-identifier-rule"),exports);__exportStar(require("./module-evidence"),exports);
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Copyright (c) 2025 Ofri Peretz
3
+ * Licensed under the MIT License. Use of this source code is governed by the
4
+ * MIT license that can be found in the LICENSE file.
5
+ */
6
+ import type { TSESTree } from '@typescript-eslint/utils';
7
+ /**
8
+ * Extra evidence a plugin accepts beyond an import.
9
+ *
10
+ * Most SDKs need none: if the file does not import the package, it does not use
11
+ * the SDK. Two do, and both were established by measuring rather than guessing:
12
+ *
13
+ * - **Lambda** — 184 of 413 handler files (45%) import nothing AWS, because
14
+ * `aws-lambda` is a types package and a plain JS handler imports nothing.
15
+ * - **Express** — 68 of 114 files with a `(req, res)`-shaped function (60%)
16
+ * import no `express`, because route modules receive `app` from a caller.
17
+ *
18
+ * Anything supplied here is an *additional* way to open the gate, so a loose
19
+ * predicate re-introduces the false positives the gate exists to remove. It
20
+ * must identify the SDK's own contract — a handler export, a middleware
21
+ * signature — never a name that merely sounds related.
22
+ */
23
+ export type ExtraEvidence = (node: TSESTree.Node) => boolean;
24
+ export interface ModuleEvidenceConfig {
25
+ /** Exact package roots, e.g. `['pg', 'postgres']`. */
26
+ readonly packages?: readonly string[];
27
+ /** Whole scopes, e.g. `['@ai-sdk']` — every package under them counts. */
28
+ readonly scopes?: readonly string[];
29
+ /** Package-root prefixes, e.g. `['serverless-']`. */
30
+ readonly prefixes?: readonly string[];
31
+ /** Non-import shapes that also mean "this file uses the SDK". */
32
+ readonly extraEvidence?: ExtraEvidence;
33
+ }
34
+ /**
35
+ * Build a per-file "does this file use my SDK?" probe.
36
+ *
37
+ * Every SDK-specific plugin gates every rule on one of these. Measured over
38
+ * 107,382 files across 107 repositories, **40% of all SDK-plugin findings were
39
+ * about an SDK the file never imports** — `lambda-security` reported on any
40
+ * `try/catch`, `postgresql-security` on any `.connect()`, and the seven SQL
41
+ * plugins on each other. A rule that matches a method name is detecting a word,
42
+ * not an interface.
43
+ *
44
+ * This lives in the devkit because it previously did not: five plugins each
45
+ * carried their own copy, so the two false-negative classes the audit found —
46
+ * TypeScript's `import =` and Deno's specifiers — had to be fixed five times,
47
+ * and the next such class would have been fixed ten. One implementation, one
48
+ * fix.
49
+ *
50
+ * The returned probe caches per `Program`, because `create` runs once per rule:
51
+ * an uncached probe walks the whole AST once for every rule in the plugin, and
52
+ * the files paying that cost are mostly the ones the gate exists to skip.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * export const fileUsesPostgres = createModuleEvidence({
57
+ * packages: ['pg', 'postgres', 'slonik'],
58
+ * scopes: ['@neondatabase'],
59
+ * });
60
+ *
61
+ * // in a rule:
62
+ * create(context) {
63
+ * if (!fileUsesPostgres(context.sourceCode.ast)) return {};
64
+ * …
65
+ * }
66
+ * ```
67
+ */
68
+ export declare function createModuleEvidence(config: ModuleEvidenceConfig): (ast: TSESTree.Program) => boolean;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createModuleEvidence=createModuleEvidence;const ast_node_types_1=require("../ast-node-types");function normalizeSpecifier(specifier){if(specifier.startsWith("npm:"))return specifier.slice(4);const deno=/^https?:\/\/deno\.land\/x\/([^@/]+)/.exec(specifier);return deno?deno[1]:specifier}function importEqualsSpecifier(node){if(node.type!==ast_node_types_1.AST_NODE_TYPES.TSImportEqualsDeclaration)return null;const ref=node.moduleReference;if(ref.type!==ast_node_types_1.AST_NODE_TYPES.TSExternalModuleReference)return null;return ref.expression.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof ref.expression.value==="string"?ref.expression.value:null}function bindsRequire(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.FunctionExpression||node.type===ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression){return node.params.some(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name==="require")}if(node.type===ast_node_types_1.AST_NODE_TYPES.Program||node.type===ast_node_types_1.AST_NODE_TYPES.BlockStatement){return node.body.some(stmt=>stmt.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclaration&&stmt.declarations.some(d=>d.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&d.id.name==="require"))}return false}function createModuleEvidence(config){const packages=new Set(config.packages??[]);const scopes=new Set(config.scopes??[]);const prefixes=config.prefixes??[];const extra=config.extraEvidence;const owns=raw=>{const specifier=normalizeSpecifier(raw);if(specifier.startsWith(".")||specifier.startsWith("/"))return false;if(specifier.startsWith("@")){const[scope,name]=specifier.split("/");if(scopes.has(scope))return true;const root2=name===void 0?scope:`${scope}/${name}`;return packages.has(root2)||prefixes.some(p=>root2.startsWith(p))}const root=specifier.split("/")[0];return packages.has(root)||prefixes.some(p=>root.startsWith(p))};const dynamicLoadSpecifier=(node,requireIsShadowed)=>{const argument=node.type===ast_node_types_1.AST_NODE_TYPES.ImportExpression?node.source:!requireIsShadowed&&node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&node.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.name==="require"?node.arguments[0]:void 0;return argument?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof argument.value==="string"?argument.value:null};const cache=new WeakMap;const compute=ast=>{let found=false;const visit=(node,requireIsShadowed)=>{if((node.type===ast_node_types_1.AST_NODE_TYPES.ImportDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.ExportNamedDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.ExportAllDeclaration)&&node.source?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof node.source.value==="string"&&owns(node.source.value)){found=true;return}const importEquals=importEqualsSpecifier(node);if(importEquals!==null&&owns(importEquals)){found=true;return}const dynamic=dynamicLoadSpecifier(node,requireIsShadowed);if(dynamic!==null&&owns(dynamic)){found=true;return}if(extra?.(node)){found=true;return}const shadowedHere=requireIsShadowed||bindsRequire(node);for(const key of Object.keys(node)){if(key==="parent")continue;const value=node[key];if(Array.isArray(value)){for(const child of value){if(child&&typeof child.type==="string"){visit(child,shadowedHere);if(found)return}}}else if(value&&typeof value==="object"&&typeof value.type==="string"){visit(value,shadowedHere);if(found)return}}};visit(ast,false);return found};return ast=>{const cached=cache.get(ast);if(cached!==void 0)return cached;const result=compute(ast);cache.set(ast,result);return result}}