@interlace/eslint-devkit 1.9.0 → 1.10.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 +1 -1
- package/src/aria-definitions.js +1 -990
- package/src/ast/ast-utils.js +1 -133
- package/src/ast/index.js +1 -17
- package/src/ast-node-types.js +1 -174
- package/src/index.js +1 -43
- package/src/messaging/config.js +1 -98
- package/src/messaging/constants.js +1 -678
- package/src/messaging/formatters-ai.js +2 -176
- package/src/messaging/formatters-v2.js +10 -212
- package/src/messaging/formatters.js +3 -186
- package/src/messaging/index.js +1 -21
- package/src/messaging/types-v2.js +1 -2
- package/src/messaging/types.js +1 -2
- package/src/node/fs.js +1 -104
- package/src/node/index.js +1 -19
- package/src/node/os.js +1 -52
- package/src/node/path.js +1 -143
- package/src/resolver/dependency-analysis.js +1 -740
- package/src/resolver/index.js +1 -20
- package/src/resolver/resolver-adapter.js +1 -92
- package/src/resolver/resolver-detection.js +1 -189
- package/src/resolver/resolver.js +1 -206
- package/src/resolver.js +1 -17
- package/src/rule-creation/browser-escape-hatch-rule.js +1 -100
- package/src/rule-creation/hardcoded-credentials-rule.js +1 -143
- package/src/rule-creation/index.d.ts +1 -0
- package/src/rule-creation/index.js +1 -26
- package/src/rule-creation/mass-assignment-rule.js +1 -121
- package/src/rule-creation/mock-context.js +1 -39
- package/src/rule-creation/raw-identifier-rule.js +1 -140
- package/src/rule-creation/require-tls-rule.js +1 -190
- package/src/rule-creation/rule-creator.js +1 -70
- package/src/rule-creation/sdk-api-key-rule.js +1 -121
- package/src/rule-creation/sql-injection-rule.js +1 -207
- package/src/rule-creation/system-prompt-injection-rule.d.ts +78 -0
- package/src/rule-creation/system-prompt-injection-rule.js +1 -0
- package/src/rule-creation/unscoped-mutation-rule.js +1 -234
- package/src/security/index.js +1 -17
- package/src/security/security-utils.js +1 -461
- package/src/types/index.js +1 -18
- package/src/types/meta-augmentation.js +1 -2
- package/src/types/type-utils.js +1 -222
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
* Configuration for one vendor's instantiation of the CWE-1427 detector.
|
|
9
|
+
*
|
|
10
|
+
* The contract is: `modules` decides *whether* the rule is armed in a file,
|
|
11
|
+
* `requestPaths` decides *which calls* it looks at, and `systemPromptProps`
|
|
12
|
+
* decides *which option* inside those calls holds the instruction text. The
|
|
13
|
+
* `messages: [{ role: 'system' }]` array form is read for every vendor and is
|
|
14
|
+
* not configurable — every SDK in this family spells it the same way.
|
|
15
|
+
*
|
|
16
|
+
* `requestPaths` is what keeps two vendors' instantiations off the same line;
|
|
17
|
+
* see the note on that field before adding a path.
|
|
18
|
+
*/
|
|
19
|
+
export interface SystemPromptInjectionRuleConfig {
|
|
20
|
+
ruleName: string;
|
|
21
|
+
/** Vendor name for the message, e.g. `OpenAI`. */
|
|
22
|
+
vendor: string;
|
|
23
|
+
/** Module names that open the gate — exact specifier or subpath. */
|
|
24
|
+
modules: readonly string[];
|
|
25
|
+
/**
|
|
26
|
+
* Qualified member paths whose first argument carries the request, e.g.
|
|
27
|
+
* `messages.create` or `completions.create`. Matched as a suffix of the
|
|
28
|
+
* callee's member path.
|
|
29
|
+
*
|
|
30
|
+
* The *path*, not the leaf method: `create` alone is not SDK-specific, and
|
|
31
|
+
* matching on it meant a file importing two SDKs reported one line twice —
|
|
32
|
+
* measured, and exactly what the taxonomy contract forbids. Anthropic's
|
|
33
|
+
* `messages.create` and OpenAI's `completions.create` are distinct; a bare
|
|
34
|
+
* `generateText(...)` has no member path at all, which is what keeps
|
|
35
|
+
* vercel-ai-security's shape out.
|
|
36
|
+
*/
|
|
37
|
+
requestPaths: readonly string[];
|
|
38
|
+
/** Option names that hold the system prompt directly, e.g. `system`. */
|
|
39
|
+
systemPromptProps: readonly string[];
|
|
40
|
+
docsUrl: string;
|
|
41
|
+
documentationLink: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Is this expression static instruction text?
|
|
45
|
+
*
|
|
46
|
+
* Static means: a string literal, a template with no interpolations, or a
|
|
47
|
+
* concatenation of those. An interpolation, a call, an await or a member
|
|
48
|
+
* expression can all carry content this file cannot see.
|
|
49
|
+
*
|
|
50
|
+
* A bare identifier is the one deliberate exception, and only as the WHOLE
|
|
51
|
+
* value: `const SYSTEM = '…'` above the call is the correct pattern and by far
|
|
52
|
+
* the most common one. Following it is the data-flow analysis these rules exist
|
|
53
|
+
* to avoid, and reporting every `system: SYSTEM_PROMPT` would make the rule
|
|
54
|
+
* unusable. Inside a concatenation the same identifier is the dynamic half, so
|
|
55
|
+
* the exception does not apply there — see isStaticText.
|
|
56
|
+
*/
|
|
57
|
+
export declare function isStaticInstruction(node: TSESTree.Node): boolean;
|
|
58
|
+
/** Static name of a property key, or undefined when it isn't statically known. */
|
|
59
|
+
export declare function staticKey(prop: TSESTree.Node): string | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Is this object a chat message whose role is the system role?
|
|
62
|
+
*
|
|
63
|
+
* Only a literal `'system'` counts. `{ role: someRole }` is not knowably the
|
|
64
|
+
* system turn, and guessing would report user turns — which are *supposed* to
|
|
65
|
+
* carry untrusted content.
|
|
66
|
+
*/
|
|
67
|
+
export declare function isSystemMessage(node: TSESTree.Node): boolean;
|
|
68
|
+
/** The `content` value of a message object, if it has a static `content` key. */
|
|
69
|
+
export declare function messageContent(node: TSESTree.ObjectExpression): TSESTree.Node | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Dot-joined member path of a callee, root object excluded.
|
|
72
|
+
*
|
|
73
|
+
* `client.chat.completions.create` -> `chat.completions.create`.
|
|
74
|
+
* A bare `generateText` -> undefined: no member path, so no raw-SDK rule can
|
|
75
|
+
* match it and the Vercel AI SDK's form stays with its own plugin.
|
|
76
|
+
*/
|
|
77
|
+
export declare function calleePath(callee: TSESTree.Node): string | undefined;
|
|
78
|
+
export declare function createSystemPromptInjectionRule(config: SystemPromptInjectionRuleConfig): import("./rule-creator").RuleModuleWithName<"untrustedSystemPrompt", [], unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isStaticInstruction=isStaticInstruction;exports.staticKey=staticKey;exports.isSystemMessage=isSystemMessage;exports.messageContent=messageContent;exports.calleePath=calleePath;exports.createSystemPromptInjectionRule=createSystemPromptInjectionRule;const ast_node_types_1=require("../ast-node-types");const rule_creator_1=require("./rule-creator");const messaging_1=require("../messaging");const sdk_api_key_rule_1=require("./sdk-api-key-rule");function isStaticInstruction(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return true;return isStaticText(node)}function isStaticText(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal)return typeof node.value==="string";if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral)return node.expressions.length===0;if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return isStaticText(node.left)&&isStaticText(node.right)}return false}function staticKey(prop){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property||prop.computed)return void 0;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return prop.key.name;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"){return prop.key.value}return void 0}function isSystemMessage(node){if(node.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)return false;return node.properties.some(prop=>{if(staticKey(prop)!=="role")return false;const value=prop.value;return value.type===ast_node_types_1.AST_NODE_TYPES.Literal&&value.value==="system"})}function messageContent(node){for(const prop of node.properties){if(staticKey(prop)==="content")return prop.value}return void 0}function calleePath(callee){const parts=[];let node=callee;while(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){if(node.computed||node.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;parts.unshift(node.property.name);node=node.object}return parts.length>0?parts.join("."):void 0}function createSystemPromptInjectionRule(config){const promptProps=new Set(config.systemPromptProps);const paths=config.requestPaths;return(0,rule_creator_1.createRule)({name:config.ruleName,meta:{type:"problem",docs:{url:config.docsUrl,description:`Disallow untrusted content built into the ${config.vendor} system prompt`,cwe:"CWE-1427",cvss:8.1},messages:{untrustedSystemPrompt:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:`Untrusted Content in ${config.vendor} System Prompt`,cwe:"CWE-1427",owasp:"A03:2021",cvss:8.1,description:"The system prompt is built at runtime, so whatever is spliced in is read by the model as instructions rather than as data \u2014 anyone controlling that value controls the agent",severity:"HIGH",compliance:["SOC2"],fix:"Keep the system prompt a static string. Pass runtime values as a separate user message, where the model treats them as data \u2014 never interpolate them into the instructions.",documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let importsSdk=false;const candidates=[];function inspectRequest(options){for(const prop of options.properties){const key=staticKey(prop);if(key===void 0)continue;const value=prop.value;if(promptProps.has(key)){collect(value);continue}if(key==="messages"&&value.type===ast_node_types_1.AST_NODE_TYPES.ArrayExpression){for(const element of value.elements){if(element===null||!isSystemMessage(element))continue;const content=messageContent(element);if(content!==void 0)collect(content)}}}}function collect(value){if(value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){for(const prop of value.properties){const key=staticKey(prop);if(key==="text")collect(prop.value);if(key==="parts"){const parts=prop.value;if(parts.type!==ast_node_types_1.AST_NODE_TYPES.ArrayExpression)continue;for(const part of parts.elements){if(part!==null)collect(part)}}}return}if(!isStaticInstruction(value))candidates.push(value)}return{ImportDeclaration(node){if((0,sdk_api_key_rule_1.matchesModule)(String(node.source.value),config.modules))importsSdk=true},CallExpression(node){if(node.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.name==="require"&&node.arguments[0]?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof node.arguments[0].value==="string"&&(0,sdk_api_key_rule_1.matchesModule)(node.arguments[0].value,config.modules)){importsSdk=true;return}const path=calleePath(node.callee);if(path===void 0)return;if(!paths.some(p=>path===p||path.endsWith(`.${p}`)))return;const request=node.arguments[0];if(request?.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression)inspectRequest(request)},"Program:exit"(){if(!importsSdk)return;for(const node of candidates){context.report({node,messageId:"untrustedSystemPrompt"})}}}}})}
|
|
@@ -1,234 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.propertyKeyName = propertyKeyName;
|
|
4
|
-
exports.hasTruthyKey = hasTruthyKey;
|
|
5
|
-
exports.hasArgumentScope = hasArgumentScope;
|
|
6
|
-
exports.chainMethodNames = chainMethodNames;
|
|
7
|
-
exports.driverBindings = driverBindings;
|
|
8
|
-
exports.receiverBaseName = receiverBaseName;
|
|
9
|
-
exports.createUnscopedMutationRule = createUnscopedMutationRule;
|
|
10
|
-
const ast_node_types_1 = require("../ast-node-types");
|
|
11
|
-
const messaging_1 = require("../messaging");
|
|
12
|
-
const DEFAULT_SCOPE_KEYS = ['where'];
|
|
13
|
-
const DEFAULT_SCOPE_METHODS = ['where'];
|
|
14
|
-
function propertyKeyName(prop) {
|
|
15
|
-
if (prop.type !== ast_node_types_1.AST_NODE_TYPES.Property)
|
|
16
|
-
return undefined;
|
|
17
|
-
if (prop.computed)
|
|
18
|
-
return undefined;
|
|
19
|
-
if (prop.key.type === ast_node_types_1.AST_NODE_TYPES.Identifier)
|
|
20
|
-
return prop.key.name;
|
|
21
|
-
if (prop.key.type === ast_node_types_1.AST_NODE_TYPES.Literal && typeof prop.key.value === 'string') {
|
|
22
|
-
return prop.key.value;
|
|
23
|
-
}
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
function hasTruthyKey(obj, keys) {
|
|
27
|
-
return obj.properties.some((prop) => {
|
|
28
|
-
if (prop.type !== ast_node_types_1.AST_NODE_TYPES.Property)
|
|
29
|
-
return false;
|
|
30
|
-
const name = propertyKeyName(prop);
|
|
31
|
-
if (name === undefined || !keys.includes(name))
|
|
32
|
-
return false;
|
|
33
|
-
return !(prop.value.type === ast_node_types_1.AST_NODE_TYPES.Literal && !prop.value.value);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
function hasArgumentScope(args, scopeKeys) {
|
|
37
|
-
return args.some((arg) => {
|
|
38
|
-
if (arg.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression) {
|
|
39
|
-
return arg.properties.some((prop) => {
|
|
40
|
-
if (prop.type === ast_node_types_1.AST_NODE_TYPES.SpreadElement)
|
|
41
|
-
return true;
|
|
42
|
-
const name = propertyKeyName(prop);
|
|
43
|
-
if (name === undefined)
|
|
44
|
-
return true;
|
|
45
|
-
if (!scopeKeys.includes(name))
|
|
46
|
-
return false;
|
|
47
|
-
if (prop.type === ast_node_types_1.AST_NODE_TYPES.Property &&
|
|
48
|
-
prop.value.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression &&
|
|
49
|
-
prop.value.properties.length === 0) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return arg.type !== ast_node_types_1.AST_NODE_TYPES.Literal;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
function chainMethodNames(node) {
|
|
59
|
-
let root = node;
|
|
60
|
-
for (;;) {
|
|
61
|
-
const parent = root.parent;
|
|
62
|
-
if (!parent)
|
|
63
|
-
break;
|
|
64
|
-
if (parent.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression && parent.object === root) {
|
|
65
|
-
root = parent;
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
if (parent.type === ast_node_types_1.AST_NODE_TYPES.CallExpression && parent.callee === root) {
|
|
69
|
-
root = parent;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
const names = new Set();
|
|
75
|
-
let cursor = root;
|
|
76
|
-
for (;;) {
|
|
77
|
-
if (cursor.type === ast_node_types_1.AST_NODE_TYPES.CallExpression) {
|
|
78
|
-
cursor = cursor.callee;
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
if (cursor.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression) {
|
|
82
|
-
if (!cursor.computed && cursor.property.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
|
|
83
|
-
names.add(cursor.property.name);
|
|
84
|
-
}
|
|
85
|
-
cursor = cursor.object;
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
return names;
|
|
91
|
-
}
|
|
92
|
-
function driverBindings(program, modules) {
|
|
93
|
-
const names = new Set();
|
|
94
|
-
const isDriver = (source) => modules.some((m) => source === m || source.startsWith(`${m}/`));
|
|
95
|
-
for (const stmt of program.body) {
|
|
96
|
-
if (stmt.type === ast_node_types_1.AST_NODE_TYPES.ImportDeclaration) {
|
|
97
|
-
if (typeof stmt.source.value !== 'string' || !isDriver(stmt.source.value))
|
|
98
|
-
continue;
|
|
99
|
-
for (const spec of stmt.specifiers)
|
|
100
|
-
names.add(spec.local.name);
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
if (stmt.type !== ast_node_types_1.AST_NODE_TYPES.VariableDeclaration)
|
|
104
|
-
continue;
|
|
105
|
-
for (const decl of stmt.declarations) {
|
|
106
|
-
const init = decl.init;
|
|
107
|
-
if (!init ||
|
|
108
|
-
init.type !== ast_node_types_1.AST_NODE_TYPES.CallExpression ||
|
|
109
|
-
init.callee.type !== ast_node_types_1.AST_NODE_TYPES.Identifier ||
|
|
110
|
-
init.callee.name !== 'require') {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
const arg = init.arguments[0];
|
|
114
|
-
if (!arg ||
|
|
115
|
-
arg.type !== ast_node_types_1.AST_NODE_TYPES.Literal ||
|
|
116
|
-
typeof arg.value !== 'string' ||
|
|
117
|
-
!isDriver(arg.value)) {
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
if (decl.id.type === ast_node_types_1.AST_NODE_TYPES.Identifier)
|
|
121
|
-
names.add(decl.id.name);
|
|
122
|
-
if (decl.id.type === ast_node_types_1.AST_NODE_TYPES.ObjectPattern) {
|
|
123
|
-
for (const prop of decl.id.properties) {
|
|
124
|
-
if (prop.type === ast_node_types_1.AST_NODE_TYPES.Property && prop.value.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
|
|
125
|
-
names.add(prop.value.name);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return names;
|
|
132
|
-
}
|
|
133
|
-
function receiverBaseName(node) {
|
|
134
|
-
let cursor = node.object;
|
|
135
|
-
for (;;) {
|
|
136
|
-
if (cursor.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression) {
|
|
137
|
-
if (cursor.object.type === ast_node_types_1.AST_NODE_TYPES.ThisExpression &&
|
|
138
|
-
!cursor.computed &&
|
|
139
|
-
cursor.property.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
|
|
140
|
-
return cursor.property.name;
|
|
141
|
-
}
|
|
142
|
-
cursor = cursor.object;
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
if (cursor.type === ast_node_types_1.AST_NODE_TYPES.CallExpression) {
|
|
146
|
-
cursor = cursor.callee;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
return cursor.type === ast_node_types_1.AST_NODE_TYPES.Identifier ? cursor.name : undefined;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
function createUnscopedMutationRule(config) {
|
|
153
|
-
const sinks = new Set(config.methods);
|
|
154
|
-
const scopeKeys = config.scopeKeys ?? DEFAULT_SCOPE_KEYS;
|
|
155
|
-
const scopeMethods = config.scopeMethods ?? DEFAULT_SCOPE_METHODS;
|
|
156
|
-
const truncateKeys = config.truncateKeys ?? [];
|
|
157
|
-
const argumentRole = config.argumentRole ?? 'options';
|
|
158
|
-
return {
|
|
159
|
-
meta: {
|
|
160
|
-
type: config.meta.type,
|
|
161
|
-
docs: { ...config.meta.docs },
|
|
162
|
-
messages: {
|
|
163
|
-
unscopedMutation: (0, messaging_1.formatLLMMessage)({
|
|
164
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
165
|
-
issueName: 'Unscoped Bulk Mutation',
|
|
166
|
-
description: 'Bulk mutation with no filter detected. This rewrites or deletes every row in the table.',
|
|
167
|
-
severity: 'HIGH',
|
|
168
|
-
cwe: config.meta.docs.cwe,
|
|
169
|
-
owasp: 'A01:2021',
|
|
170
|
-
compliance: ['SOC2', 'NIST-CSF'],
|
|
171
|
-
effort: 'low',
|
|
172
|
-
fix: config.fix,
|
|
173
|
-
documentationLink: config.documentationLink,
|
|
174
|
-
}),
|
|
175
|
-
explicitTruncate: (0, messaging_1.formatLLMMessage)({
|
|
176
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
177
|
-
issueName: 'Unscoped Bulk Mutation',
|
|
178
|
-
description: 'Table truncation detected. This ignores any filter and empties the whole table.',
|
|
179
|
-
severity: 'HIGH',
|
|
180
|
-
cwe: config.meta.docs.cwe,
|
|
181
|
-
owasp: 'A01:2021',
|
|
182
|
-
compliance: ['SOC2', 'NIST-CSF'],
|
|
183
|
-
effort: 'low',
|
|
184
|
-
fix: 'Remove the truncate flag and delete by filter, or move the truncation to an explicit maintenance script.',
|
|
185
|
-
documentationLink: config.documentationLink,
|
|
186
|
-
}),
|
|
187
|
-
},
|
|
188
|
-
schema: [],
|
|
189
|
-
},
|
|
190
|
-
defaultOptions: [],
|
|
191
|
-
create(context) {
|
|
192
|
-
let bindings = new Set();
|
|
193
|
-
return {
|
|
194
|
-
Program(program) {
|
|
195
|
-
bindings = driverBindings(program, config.modules);
|
|
196
|
-
},
|
|
197
|
-
CallExpression(node) {
|
|
198
|
-
if (bindings.size === 0)
|
|
199
|
-
return;
|
|
200
|
-
if (node.callee.type !== ast_node_types_1.AST_NODE_TYPES.MemberExpression ||
|
|
201
|
-
node.callee.computed ||
|
|
202
|
-
node.callee.property.type !== ast_node_types_1.AST_NODE_TYPES.Identifier ||
|
|
203
|
-
!sinks.has(node.callee.property.name)) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
const base = receiverBaseName(node.callee);
|
|
207
|
-
if (base === undefined)
|
|
208
|
-
return;
|
|
209
|
-
if (!bindings.has(base) && !config.receiverPattern.test(base))
|
|
210
|
-
return;
|
|
211
|
-
if (truncateKeys.length > 0) {
|
|
212
|
-
const truncating = node.arguments.some((arg) => arg.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression && hasTruthyKey(arg, truncateKeys));
|
|
213
|
-
if (truncating) {
|
|
214
|
-
context.report({ node, messageId: 'explicitTruncate' });
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if (argumentRole === 'options') {
|
|
219
|
-
if (config.requireOptionsObject === true &&
|
|
220
|
-
!node.arguments.some((arg) => arg.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression)) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
if (hasArgumentScope(node.arguments, scopeKeys))
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
const chained = chainMethodNames(node);
|
|
227
|
-
if (scopeMethods.some((name) => chained.has(name)))
|
|
228
|
-
return;
|
|
229
|
-
context.report({ node, messageId: 'unscopedMutation' });
|
|
230
|
-
},
|
|
231
|
-
};
|
|
232
|
-
},
|
|
233
|
-
};
|
|
234
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.propertyKeyName=propertyKeyName;exports.hasTruthyKey=hasTruthyKey;exports.hasArgumentScope=hasArgumentScope;exports.chainMethodNames=chainMethodNames;exports.driverBindings=driverBindings;exports.receiverBaseName=receiverBaseName;exports.createUnscopedMutationRule=createUnscopedMutationRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const DEFAULT_SCOPE_KEYS=["where"];const DEFAULT_SCOPE_METHODS=["where"];function propertyKeyName(prop){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)return void 0;if(prop.computed)return void 0;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return prop.key.name;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"){return prop.key.value}return void 0}function hasTruthyKey(obj,keys){return obj.properties.some(prop=>{if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)return false;const name=propertyKeyName(prop);if(name===void 0||!keys.includes(name))return false;return!(prop.value.type===ast_node_types_1.AST_NODE_TYPES.Literal&&!prop.value.value)})}function hasArgumentScope(args,scopeKeys){return args.some(arg=>{if(arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){return arg.properties.some(prop=>{if(prop.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)return true;const name=propertyKeyName(prop);if(name===void 0)return true;if(!scopeKeys.includes(name))return false;if(prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&prop.value.properties.length===0){return false}return true})}return arg.type!==ast_node_types_1.AST_NODE_TYPES.Literal})}function chainMethodNames(node){let root=node;for(;;){const parent=root.parent;if(!parent)break;if(parent.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&parent.object===root){root=parent;continue}if(parent.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&parent.callee===root){root=parent;continue}break}const names=new Set;let cursor=root;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){cursor=cursor.callee;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){if(!cursor.computed&&cursor.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier){names.add(cursor.property.name)}cursor=cursor.object;continue}break}return names}function driverBindings(program,modules){const names=new Set;const isDriver=source=>modules.some(m=>source===m||source.startsWith(`${m}/`));for(const stmt of program.body){if(stmt.type===ast_node_types_1.AST_NODE_TYPES.ImportDeclaration){if(typeof stmt.source.value!=="string"||!isDriver(stmt.source.value))continue;for(const spec of stmt.specifiers)names.add(spec.local.name);continue}if(stmt.type!==ast_node_types_1.AST_NODE_TYPES.VariableDeclaration)continue;for(const decl of stmt.declarations){const init=decl.init;if(!init||init.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression||init.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||init.callee.name!=="require"){continue}const arg=init.arguments[0];if(!arg||arg.type!==ast_node_types_1.AST_NODE_TYPES.Literal||typeof arg.value!=="string"||!isDriver(arg.value)){continue}if(decl.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier)names.add(decl.id.name);if(decl.id.type===ast_node_types_1.AST_NODE_TYPES.ObjectPattern){for(const prop of decl.id.properties){if(prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&prop.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier){names.add(prop.value.name)}}}}}return names}function receiverBaseName(node){let cursor=node.object;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){if(cursor.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&!cursor.computed&&cursor.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return cursor.property.name}cursor=cursor.object;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){cursor=cursor.callee;continue}return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor.name:void 0}}function createUnscopedMutationRule(config){const sinks=new Set(config.methods);const scopeKeys=config.scopeKeys??DEFAULT_SCOPE_KEYS;const scopeMethods=config.scopeMethods??DEFAULT_SCOPE_METHODS;const truncateKeys=config.truncateKeys??[];const argumentRole=config.argumentRole??"options";return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{unscopedMutation:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unscoped Bulk Mutation",description:"Bulk mutation with no filter detected. This rewrites or deletes every row in the table.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A01:2021",compliance:["SOC2","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),explicitTruncate:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unscoped Bulk Mutation",description:"Table truncation detected. This ignores any filter and empties the whole table.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A01:2021",compliance:["SOC2","NIST-CSF"],effort:"low",fix:"Remove the truncate flag and delete by filter, or move the truncation to an explicit maintenance script.",documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let bindings=new Set;return{Program(program){bindings=driverBindings(program,config.modules)},CallExpression(node){if(bindings.size===0)return;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression||node.callee.computed||node.callee.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||!sinks.has(node.callee.property.name)){return}const base=receiverBaseName(node.callee);if(base===void 0)return;if(!bindings.has(base)&&!config.receiverPattern.test(base))return;if(truncateKeys.length>0){const truncating=node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&hasTruthyKey(arg,truncateKeys));if(truncating){context.report({node,messageId:"explicitTruncate"});return}}if(argumentRole==="options"){if(config.requireOptionsObject===true&&!node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression)){return}if(hasArgumentScope(node.arguments,scopeKeys))return}const chained=chainMethodNames(node);if(scopeMethods.some(name=>chained.has(name)))return;context.report({node,messageId:"unscopedMutation"})}}}}}
|
package/src/security/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./security-utils"), 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("./security-utils"),exports);
|