@interlace/eslint-devkit 1.9.0 → 1.11.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/README.md +1 -1
- package/package.json +2 -2
- 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 +2 -0
- package/src/rule-creation/index.js +1 -26
- package/src/rule-creation/mass-assignment-rule.js +1 -121
- package/src/rule-creation/message-source.d.ts +95 -0
- package/src/rule-creation/message-source.js +1 -0
- 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
|
@@ -1,143 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.urlEmbedsCredentials = urlEmbedsCredentials;
|
|
4
|
-
exports.staticStringValue = staticStringValue;
|
|
5
|
-
exports.isLiteralSecret = isLiteralSecret;
|
|
6
|
-
exports.createHardcodedCredentialsRule = createHardcodedCredentialsRule;
|
|
7
|
-
const ast_node_types_1 = require("../ast-node-types");
|
|
8
|
-
const messaging_1 = require("../messaging");
|
|
9
|
-
const unscoped_mutation_rule_1 = require("./unscoped-mutation-rule");
|
|
10
|
-
const require_tls_rule_1 = require("./require-tls-rule");
|
|
11
|
-
const CREDENTIAL_KEYS = ['password', 'pass', 'pwd', 'secret', 'passwd', 'auth'];
|
|
12
|
-
const DEFAULT_CONNECTION_KEYS = [
|
|
13
|
-
'host',
|
|
14
|
-
'hostname',
|
|
15
|
-
'port',
|
|
16
|
-
'database',
|
|
17
|
-
'connectionString',
|
|
18
|
-
'socketPath',
|
|
19
|
-
'uri',
|
|
20
|
-
'url',
|
|
21
|
-
'connection',
|
|
22
|
-
'pool',
|
|
23
|
-
'schema',
|
|
24
|
-
];
|
|
25
|
-
function urlEmbedsCredentials(value, schemes) {
|
|
26
|
-
const match = /^([a-z][a-z0-9+.-]*):\/\/([^/@\s:]*:[^/@\s]+)@/i.exec(value);
|
|
27
|
-
if (match === null)
|
|
28
|
-
return false;
|
|
29
|
-
return schemes.some((scheme) => scheme.toLowerCase() === match[1].toLowerCase());
|
|
30
|
-
}
|
|
31
|
-
function staticStringValue(node) {
|
|
32
|
-
if (node.type === ast_node_types_1.AST_NODE_TYPES.Literal) {
|
|
33
|
-
return typeof node.value === 'string' ? node.value : undefined;
|
|
34
|
-
}
|
|
35
|
-
if (node.type === ast_node_types_1.AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
|
|
36
|
-
return node.quasis[0].value.cooked;
|
|
37
|
-
}
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
function isLiteralSecret(node) {
|
|
41
|
-
const value = staticStringValue(node);
|
|
42
|
-
return value !== undefined && value.length > 0;
|
|
43
|
-
}
|
|
44
|
-
function createHardcodedCredentialsRule(config) {
|
|
45
|
-
const connectionKeys = [...DEFAULT_CONNECTION_KEYS, ...(config.connectionKeys ?? [])];
|
|
46
|
-
return {
|
|
47
|
-
meta: {
|
|
48
|
-
type: config.meta.type,
|
|
49
|
-
docs: { ...config.meta.docs },
|
|
50
|
-
messages: {
|
|
51
|
-
hardcodedPassword: (0, messaging_1.formatLLMMessage)({
|
|
52
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
53
|
-
issueName: 'Hardcoded Credentials',
|
|
54
|
-
description: 'A database password is written as a literal. It is in git history, every fork, and the built image — rotating it means rewriting history, so it never gets rotated.',
|
|
55
|
-
severity: 'CRITICAL',
|
|
56
|
-
cwe: config.meta.docs.cwe,
|
|
57
|
-
owasp: 'A07:2021',
|
|
58
|
-
compliance: ['SOC2', 'PCI-DSS', 'ISO27001', 'NIST-CSF'],
|
|
59
|
-
effort: 'low',
|
|
60
|
-
fix: config.fix,
|
|
61
|
-
documentationLink: config.documentationLink,
|
|
62
|
-
}),
|
|
63
|
-
credentialsInUrl: (0, messaging_1.formatLLMMessage)({
|
|
64
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
65
|
-
issueName: 'Hardcoded Credentials',
|
|
66
|
-
description: 'A connection URL embeds a password in its userinfo section, so the secret is committed with the string.',
|
|
67
|
-
severity: 'CRITICAL',
|
|
68
|
-
cwe: config.meta.docs.cwe,
|
|
69
|
-
owasp: 'A07:2021',
|
|
70
|
-
compliance: ['SOC2', 'PCI-DSS', 'ISO27001', 'NIST-CSF'],
|
|
71
|
-
effort: 'low',
|
|
72
|
-
fix: config.urlFix,
|
|
73
|
-
documentationLink: config.documentationLink,
|
|
74
|
-
}),
|
|
75
|
-
},
|
|
76
|
-
schema: [],
|
|
77
|
-
},
|
|
78
|
-
defaultOptions: [],
|
|
79
|
-
create(context) {
|
|
80
|
-
let bindings = new Set();
|
|
81
|
-
let reported = new WeakSet();
|
|
82
|
-
let scanned = new WeakSet();
|
|
83
|
-
const report = (node, messageId) => {
|
|
84
|
-
if (reported.has(node))
|
|
85
|
-
return;
|
|
86
|
-
reported.add(node);
|
|
87
|
-
context.report({ node, messageId });
|
|
88
|
-
};
|
|
89
|
-
const scanConfig = (obj) => {
|
|
90
|
-
if (scanned.has(obj))
|
|
91
|
-
return;
|
|
92
|
-
scanned.add(obj);
|
|
93
|
-
for (const prop of obj.properties) {
|
|
94
|
-
if (prop.type !== ast_node_types_1.AST_NODE_TYPES.Property)
|
|
95
|
-
continue;
|
|
96
|
-
const name = (0, unscoped_mutation_rule_1.propertyKeyName)(prop);
|
|
97
|
-
if (name === undefined)
|
|
98
|
-
continue;
|
|
99
|
-
if (CREDENTIAL_KEYS.includes(name.toLowerCase())) {
|
|
100
|
-
if (isLiteralSecret(prop.value))
|
|
101
|
-
report(prop.value, 'hardcodedPassword');
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
const url = staticStringValue(prop.value);
|
|
105
|
-
if (url !== undefined && urlEmbedsCredentials(url, config.urlSchemes)) {
|
|
106
|
-
report(prop.value, 'credentialsInUrl');
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
if (prop.value.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression &&
|
|
110
|
-
connectionKeys.includes(name)) {
|
|
111
|
-
scanConfig(prop.value);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
return {
|
|
116
|
-
Program(program) {
|
|
117
|
-
bindings = (0, unscoped_mutation_rule_1.driverBindings)(program, config.modules);
|
|
118
|
-
reported = new WeakSet();
|
|
119
|
-
scanned = new WeakSet();
|
|
120
|
-
},
|
|
121
|
-
ObjectExpression(node) {
|
|
122
|
-
if (bindings.size === 0)
|
|
123
|
-
return;
|
|
124
|
-
if (!(0, require_tls_rule_1.looksLikeConnectionConfig)(node, connectionKeys))
|
|
125
|
-
return;
|
|
126
|
-
scanConfig(node);
|
|
127
|
-
},
|
|
128
|
-
'Literal, TemplateLiteral'(node) {
|
|
129
|
-
if (bindings.size === 0)
|
|
130
|
-
return;
|
|
131
|
-
const value = staticStringValue(node);
|
|
132
|
-
if (value === undefined)
|
|
133
|
-
return;
|
|
134
|
-
if (!urlEmbedsCredentials(value, config.urlSchemes))
|
|
135
|
-
return;
|
|
136
|
-
if (!(0, require_tls_rule_1.inConnectionPosition)(node, connectionKeys, bindings))
|
|
137
|
-
return;
|
|
138
|
-
report(node, 'credentialsInUrl');
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.urlEmbedsCredentials=urlEmbedsCredentials;exports.staticStringValue=staticStringValue;exports.isLiteralSecret=isLiteralSecret;exports.createHardcodedCredentialsRule=createHardcodedCredentialsRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const require_tls_rule_1=require("./require-tls-rule");const CREDENTIAL_KEYS=["password","pass","pwd","secret","passwd","auth"];const DEFAULT_CONNECTION_KEYS=["host","hostname","port","database","connectionString","socketPath","uri","url","connection","pool","schema"];function urlEmbedsCredentials(value,schemes){const match=/^([a-z][a-z0-9+.-]*):\/\/([^/@\s:]*:[^/@\s]+)@/i.exec(value);if(match===null)return false;return schemes.some(scheme=>scheme.toLowerCase()===match[1].toLowerCase())}function staticStringValue(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:void 0}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length===0){return node.quasis[0].value.cooked}return void 0}function isLiteralSecret(node){const value=staticStringValue(node);return value!==void 0&&value.length>0}function createHardcodedCredentialsRule(config){const connectionKeys=[...DEFAULT_CONNECTION_KEYS,...config.connectionKeys??[]];return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{hardcodedPassword:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Hardcoded Credentials",description:"A database password is written as a literal. It is in git history, every fork, and the built image \u2014 rotating it means rewriting history, so it never gets rotated.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A07:2021",compliance:["SOC2","PCI-DSS","ISO27001","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),credentialsInUrl:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Hardcoded Credentials",description:"A connection URL embeds a password in its userinfo section, so the secret is committed with the string.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A07:2021",compliance:["SOC2","PCI-DSS","ISO27001","NIST-CSF"],effort:"low",fix:config.urlFix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let bindings=new Set;let reported=new WeakSet;let scanned=new WeakSet;const report=(node,messageId)=>{if(reported.has(node))return;reported.add(node);context.report({node,messageId})};const scanConfig=obj=>{if(scanned.has(obj))return;scanned.add(obj);for(const prop of obj.properties){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;const name=(0,unscoped_mutation_rule_1.propertyKeyName)(prop);if(name===void 0)continue;if(CREDENTIAL_KEYS.includes(name.toLowerCase())){if(isLiteralSecret(prop.value))report(prop.value,"hardcodedPassword");continue}const url=staticStringValue(prop.value);if(url!==void 0&&urlEmbedsCredentials(url,config.urlSchemes)){report(prop.value,"credentialsInUrl");continue}if(prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&connectionKeys.includes(name)){scanConfig(prop.value)}}};return{Program(program){bindings=(0,unscoped_mutation_rule_1.driverBindings)(program,config.modules);reported=new WeakSet;scanned=new WeakSet},ObjectExpression(node){if(bindings.size===0)return;if(!(0,require_tls_rule_1.looksLikeConnectionConfig)(node,connectionKeys))return;scanConfig(node)},"Literal, TemplateLiteral"(node){if(bindings.size===0)return;const value=staticStringValue(node);if(value===void 0)return;if(!urlEmbedsCredentials(value,config.urlSchemes))return;if(!(0,require_tls_rule_1.inConnectionPosition)(node,connectionKeys,bindings))return;report(node,"credentialsInUrl")}}}}}
|
|
@@ -12,4 +12,6 @@ export * from './mass-assignment-rule';
|
|
|
12
12
|
export * from './hardcoded-credentials-rule';
|
|
13
13
|
export * from './sdk-api-key-rule';
|
|
14
14
|
export * from './browser-escape-hatch-rule';
|
|
15
|
+
export * from './system-prompt-injection-rule';
|
|
16
|
+
export * from './message-source';
|
|
15
17
|
export * from './raw-identifier-rule';
|
|
@@ -1,26 +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("./rule-creator"), exports);
|
|
18
|
-
__exportStar(require("./mock-context"), exports);
|
|
19
|
-
__exportStar(require("./sql-injection-rule"), exports);
|
|
20
|
-
__exportStar(require("./unscoped-mutation-rule"), exports);
|
|
21
|
-
__exportStar(require("./require-tls-rule"), exports);
|
|
22
|
-
__exportStar(require("./mass-assignment-rule"), exports);
|
|
23
|
-
__exportStar(require("./hardcoded-credentials-rule"), exports);
|
|
24
|
-
__exportStar(require("./sdk-api-key-rule"), exports);
|
|
25
|
-
__exportStar(require("./browser-escape-hatch-rule"), exports);
|
|
26
|
-
__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);
|
|
@@ -1,121 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isUntrustedSource = isUntrustedSource;
|
|
4
|
-
exports.classifyPayload = classifyPayload;
|
|
5
|
-
exports.createMassAssignmentRule = createMassAssignmentRule;
|
|
6
|
-
const ast_node_types_1 = require("../ast-node-types");
|
|
7
|
-
const messaging_1 = require("../messaging");
|
|
8
|
-
const unscoped_mutation_rule_1 = require("./unscoped-mutation-rule");
|
|
9
|
-
const REQUEST_OBJECTS = ['req', 'request', 'ctx', 'context', 'event'];
|
|
10
|
-
const UNTRUSTED_PROPS = ['body', 'query', 'params', 'payload'];
|
|
11
|
-
function isUntrustedSource(node) {
|
|
12
|
-
if (node.type !== ast_node_types_1.AST_NODE_TYPES.MemberExpression || node.computed)
|
|
13
|
-
return false;
|
|
14
|
-
if (node.property.type !== ast_node_types_1.AST_NODE_TYPES.Identifier)
|
|
15
|
-
return false;
|
|
16
|
-
if (!UNTRUSTED_PROPS.includes(node.property.name)) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
return baseIsRequest(node.object);
|
|
20
|
-
}
|
|
21
|
-
function baseIsRequest(node) {
|
|
22
|
-
let cursor = node;
|
|
23
|
-
while (cursor.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression)
|
|
24
|
-
cursor = cursor.object;
|
|
25
|
-
return (cursor.type === ast_node_types_1.AST_NODE_TYPES.Identifier &&
|
|
26
|
-
REQUEST_OBJECTS.includes(cursor.name));
|
|
27
|
-
}
|
|
28
|
-
function classifyPayload(node) {
|
|
29
|
-
if (isUntrustedSource(node))
|
|
30
|
-
return 'untrustedPayload';
|
|
31
|
-
if (node.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression) {
|
|
32
|
-
for (const prop of node.properties) {
|
|
33
|
-
if (prop.type !== ast_node_types_1.AST_NODE_TYPES.SpreadElement)
|
|
34
|
-
continue;
|
|
35
|
-
if (isUntrustedSource(prop.argument))
|
|
36
|
-
return 'untrustedSpread';
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
function createMassAssignmentRule(config) {
|
|
42
|
-
const methods = new Set(config.methods);
|
|
43
|
-
const payloadKeys = new Set(config.payloadKeys ?? []);
|
|
44
|
-
return {
|
|
45
|
-
meta: {
|
|
46
|
-
type: config.meta.type,
|
|
47
|
-
docs: { ...config.meta.docs },
|
|
48
|
-
messages: {
|
|
49
|
-
untrustedPayload: (0, messaging_1.formatLLMMessage)({
|
|
50
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
51
|
-
issueName: 'Mass Assignment',
|
|
52
|
-
description: 'The request object is written straight to the database, so every column the model exposes is settable by the caller — including ones this code never mentions.',
|
|
53
|
-
severity: 'HIGH',
|
|
54
|
-
cwe: config.meta.docs.cwe,
|
|
55
|
-
owasp: 'A04:2021',
|
|
56
|
-
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
57
|
-
effort: 'medium',
|
|
58
|
-
fix: config.fix,
|
|
59
|
-
documentationLink: config.documentationLink,
|
|
60
|
-
}),
|
|
61
|
-
untrustedSpread: (0, messaging_1.formatLLMMessage)({
|
|
62
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
63
|
-
issueName: 'Mass Assignment',
|
|
64
|
-
description: 'Spreading the request into the payload carries every key it happens to hold, not just the ones written beside it.',
|
|
65
|
-
severity: 'HIGH',
|
|
66
|
-
cwe: config.meta.docs.cwe,
|
|
67
|
-
owasp: 'A04:2021',
|
|
68
|
-
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
69
|
-
effort: 'medium',
|
|
70
|
-
fix: config.fix,
|
|
71
|
-
documentationLink: config.documentationLink,
|
|
72
|
-
}),
|
|
73
|
-
},
|
|
74
|
-
schema: [],
|
|
75
|
-
},
|
|
76
|
-
defaultOptions: [],
|
|
77
|
-
create(context) {
|
|
78
|
-
let bindings = new Set();
|
|
79
|
-
return {
|
|
80
|
-
Program(program) {
|
|
81
|
-
bindings = (0, unscoped_mutation_rule_1.driverBindings)(program, config.modules);
|
|
82
|
-
},
|
|
83
|
-
CallExpression(node) {
|
|
84
|
-
if (bindings.size === 0)
|
|
85
|
-
return;
|
|
86
|
-
if (node.callee.type !== ast_node_types_1.AST_NODE_TYPES.MemberExpression ||
|
|
87
|
-
node.callee.property.type !== ast_node_types_1.AST_NODE_TYPES.Identifier ||
|
|
88
|
-
!methods.has(node.callee.property.name)) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
const base = (0, unscoped_mutation_rule_1.receiverBaseName)(node.callee);
|
|
92
|
-
if (base === undefined)
|
|
93
|
-
return;
|
|
94
|
-
if (!bindings.has(base) && !config.receiverPattern.test(base))
|
|
95
|
-
return;
|
|
96
|
-
for (const arg of node.arguments) {
|
|
97
|
-
if (arg.type === ast_node_types_1.AST_NODE_TYPES.SpreadElement)
|
|
98
|
-
continue;
|
|
99
|
-
const direct = classifyPayload(arg);
|
|
100
|
-
if (direct) {
|
|
101
|
-
context.report({ node: arg, messageId: direct });
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
if (arg.type !== ast_node_types_1.AST_NODE_TYPES.ObjectExpression)
|
|
105
|
-
continue;
|
|
106
|
-
for (const prop of arg.properties) {
|
|
107
|
-
if (prop.type !== ast_node_types_1.AST_NODE_TYPES.Property)
|
|
108
|
-
continue;
|
|
109
|
-
const name = (0, unscoped_mutation_rule_1.propertyKeyName)(prop);
|
|
110
|
-
if (name === undefined || !payloadKeys.has(name))
|
|
111
|
-
continue;
|
|
112
|
-
const nested = classifyPayload(prop.value);
|
|
113
|
-
if (nested)
|
|
114
|
-
context.report({ node: prop.value, messageId: nested });
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isUntrustedSource=isUntrustedSource;exports.classifyPayload=classifyPayload;exports.createMassAssignmentRule=createMassAssignmentRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const REQUEST_OBJECTS=["req","request","ctx","context","event"];const UNTRUSTED_PROPS=["body","query","params","payload"];function isUntrustedSource(node){if(node.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression||node.computed)return false;if(node.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return false;if(!UNTRUSTED_PROPS.includes(node.property.name)){return false}return baseIsRequest(node.object)}function baseIsRequest(node){let cursor=node;while(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression)cursor=cursor.object;return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&REQUEST_OBJECTS.includes(cursor.name)}function classifyPayload(node){if(isUntrustedSource(node))return"untrustedPayload";if(node.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){for(const prop of node.properties){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;if(isUntrustedSource(prop.argument))return"untrustedSpread"}}return false}function createMassAssignmentRule(config){const methods=new Set(config.methods);const payloadKeys=new Set(config.payloadKeys??[]);return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{untrustedPayload:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Mass Assignment",description:"The request object is written straight to the database, so every column the model exposes is settable by the caller \u2014 including ones this code never mentions.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A04:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink}),untrustedSpread:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Mass Assignment",description:"Spreading the request into the payload carries every key it happens to hold, not just the ones written beside it.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A04:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let bindings=new Set;return{Program(program){bindings=(0,unscoped_mutation_rule_1.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.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||!methods.has(node.callee.property.name)){return}const base=(0,unscoped_mutation_rule_1.receiverBaseName)(node.callee);if(base===void 0)return;if(!bindings.has(base)&&!config.receiverPattern.test(base))return;for(const arg of node.arguments){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;const direct=classifyPayload(arg);if(direct){context.report({node:arg,messageId:direct});continue}if(arg.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)continue;for(const prop of arg.properties){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;const name=(0,unscoped_mutation_rule_1.propertyKeyName)(prop);if(name===void 0||!payloadKeys.has(name))continue;const nested=classifyPayload(prop.value);if(nested)context.report({node:prop.value,messageId:nested})}}}}}}}
|
|
@@ -0,0 +1,95 @@
|
|
|
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 { TSESLint, TSESTree } from '@typescript-eslint/utils';
|
|
7
|
+
/** A source whose messages are attacker-influenced, and which owns its own rule. */
|
|
8
|
+
export type MessageSource = 'websocket' | 'worker' | 'filereader' | 'postmessage';
|
|
9
|
+
/**
|
|
10
|
+
* Resolve a receiver identifier to the source it was constructed from.
|
|
11
|
+
*
|
|
12
|
+
* Lexical, not name-keyed. A file-wide name map cannot see shadowing: with
|
|
13
|
+
* `const ws = new WebSocket()` at module scope, an inner `const ws = payload`
|
|
14
|
+
* was still resolved as a WebSocket — so an attacker-controlled `payload`
|
|
15
|
+
* got reported as "WebSocket message data" while the generic rule, believing
|
|
16
|
+
* the line was owned, said nothing. Measured before this change.
|
|
17
|
+
*
|
|
18
|
+
* Resolution walks the scope chain from the reference itself, so the binding
|
|
19
|
+
* that is actually in scope is the one that decides.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* `SHADOWED` — the name resolves to a binding that is not a known source.
|
|
23
|
+
*
|
|
24
|
+
* Distinct from `undefined`, which means no binding at all, i.e. a genuine
|
|
25
|
+
* global. The difference matters for `window` / `self` / `parent`: those are
|
|
26
|
+
* the postMessage receivers only when nothing shadows them, and
|
|
27
|
+
* `function f(window) { window.addEventListener('message', …) }` is a
|
|
28
|
+
* parameter, not the global.
|
|
29
|
+
*/
|
|
30
|
+
export declare const SHADOWED: unique symbol;
|
|
31
|
+
export type ReceiverResolver = (node: TSESTree.Identifier) => MessageSource | typeof SHADOWED | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Build the resolver for a file from its scope manager.
|
|
34
|
+
*
|
|
35
|
+
* A variable with more than one definition is not resolved: `let ws` reassigned
|
|
36
|
+
* later could be anything at the point of use, and guessing is what this module
|
|
37
|
+
* exists to stop.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createReceiverResolver(sourceCode: TSESLint.SourceCode): ReceiverResolver;
|
|
40
|
+
/** The source a `new X()` expression constructs, if X is one we own a rule for. */
|
|
41
|
+
export declare function constructedSource(node: TSESTree.Node | null | undefined): MessageSource | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The source of a receiver expression — `ws` in `ws.onmessage = …`.
|
|
44
|
+
*
|
|
45
|
+
* Resolves an identifier through the scope chain, and an inline
|
|
46
|
+
* `new WebSocket().onmessage = …` directly. Anything else is unknown.
|
|
47
|
+
*/
|
|
48
|
+
export declare function receiverSource(receiver: TSESTree.Node, resolve: ReceiverResolver): MessageSource | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The source whose handler this node is, if it is a message-handler attachment.
|
|
51
|
+
*
|
|
52
|
+
* Covers both attachment shapes:
|
|
53
|
+
* `ws.onmessage = (e) => …` (AssignmentExpression)
|
|
54
|
+
* `ws.addEventListener('message', fn)` (CallExpression)
|
|
55
|
+
* `reader.onload = (e) => …` (FileReader's event name)
|
|
56
|
+
*
|
|
57
|
+
* Returns the source, the handler's first parameter name (what the sink rules
|
|
58
|
+
* match `event.data` against) and the handler function itself, whose range is
|
|
59
|
+
* the region the payload is in scope for. `undefined` means "not a handler, or
|
|
60
|
+
* a handler on a receiver this file cannot identify".
|
|
61
|
+
*/
|
|
62
|
+
export declare function handlerSource(node: TSESTree.Node, resolve: ReceiverResolver): {
|
|
63
|
+
source: MessageSource;
|
|
64
|
+
eventParam: string;
|
|
65
|
+
handler: TSESTree.Node;
|
|
66
|
+
} | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Does this expression read the payload off `eventParam`?
|
|
69
|
+
*
|
|
70
|
+
* Matches `event.data`, `event.target.result` and deeper reads such as
|
|
71
|
+
* `event.data.html`, so that a sink fed a *property of* the payload is still
|
|
72
|
+
* attributed to the source rather than falling through to the generic rule.
|
|
73
|
+
*/
|
|
74
|
+
export declare function readsEventPayload(node: TSESTree.Node, eventParam: string): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* The identifier a member chain is rooted at — `event` in `event.target.result`.
|
|
77
|
+
*
|
|
78
|
+
* Exposed so callers can resolve that identifier to a *binding*. Matching on the
|
|
79
|
+
* name alone is not enough: a nested function whose parameter shares the
|
|
80
|
+
* handler's parameter name reads an entirely different value.
|
|
81
|
+
*/
|
|
82
|
+
export declare function payloadRoot(node: TSESTree.Node): TSESTree.Identifier | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Build the file's payload resolver: "which source does this value come from?"
|
|
85
|
+
*
|
|
86
|
+
* This is the single question every sink rule in the family needs, asked from
|
|
87
|
+
* both directions:
|
|
88
|
+
*
|
|
89
|
+
* source rule `payloadSource(value) === 'websocket'` — mine, report it
|
|
90
|
+
* generic rule `payloadSource(value) === undefined` — nobody's, report it
|
|
91
|
+
*
|
|
92
|
+
* Because the two tests are complements, exactly one rule reports any given
|
|
93
|
+
* value. That is the whole fix.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createPayloadResolver(sourceCode: TSESLint.SourceCode): (node: TSESTree.Node) => MessageSource | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.SHADOWED=void 0;exports.createReceiverResolver=createReceiverResolver;exports.constructedSource=constructedSource;exports.receiverSource=receiverSource;exports.handlerSource=handlerSource;exports.readsEventPayload=readsEventPayload;exports.payloadRoot=payloadRoot;exports.createPayloadResolver=createPayloadResolver;const ast_node_types_1=require("../ast-node-types");const CONSTRUCTORS=new Map([["WebSocket","websocket"],["Worker","worker"],["SharedWorker","worker"],["FileReader","filereader"]]);const POSTMESSAGE_RECEIVERS=new Set(["window","self","globalThis","parent","top"]);exports.SHADOWED=Symbol("shadowed");function findVariable(scope,name){for(let current=scope;current!==null;current=current.upper){const found=current.variables.find(variable=>variable.name===name);if(found!==void 0)return found}return void 0}function createReceiverResolver(sourceCode){return node=>{const scope=sourceCode.getScope(node);const variable=findVariable(scope,node.name);if(variable===void 0||variable.defs.length===0)return void 0;if(variable.defs.length!==1)return exports.SHADOWED;if(variable.references.filter(reference=>reference.isWrite()).length>1){return exports.SHADOWED}const declarator=variable.defs.find(definition=>definition.node.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator);if(declarator===void 0)return exports.SHADOWED;return constructedSource(declarator.node.init)??exports.SHADOWED}}function constructedSource(node){if(node==null||node.type!==ast_node_types_1.AST_NODE_TYPES.NewExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;return CONSTRUCTORS.get(node.callee.name)}function receiverSource(receiver,resolve){if(receiver.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const resolved=resolve(receiver);if(resolved===void 0){return POSTMESSAGE_RECEIVERS.has(receiver.name)?"postmessage":void 0}return resolved===exports.SHADOWED?void 0:resolved}return constructedSource(receiver)}function handlerSource(node,resolve){if(node.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression){if(node.left.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return void 0;if(node.left.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;const source=receiverSource(node.left.object,resolve);if(source===void 0)return void 0;if(!HANDLER_PROPS[source].has(node.left.property.name))return void 0;return withHandler(source,node.right)}if(node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return void 0;if(node.callee.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;if(node.callee.property.name!=="addEventListener")return void 0;const[eventType,handler]=node.arguments;if(eventType?.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return void 0;if(typeof eventType.value!=="string")return void 0;const source=receiverSource(node.callee.object,resolve);if(source===void 0)return void 0;if(!HANDLER_EVENTS[source].has(eventType.value))return void 0;return withHandler(source,handler)}return void 0}function withHandler(source,handler){if(handler===void 0)return void 0;if(handler.type!==ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression&&handler.type!==ast_node_types_1.AST_NODE_TYPES.FunctionExpression){return void 0}const first=handler.params[0];if(first?.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;return{source,eventParam:first.name,handler}}const HANDLER_PROPS={websocket:new Set(["onmessage"]),worker:new Set(["onmessage"]),filereader:new Set(["onload","onloadend"]),postmessage:new Set(["onmessage"])};const HANDLER_EVENTS={websocket:new Set(["message"]),worker:new Set(["message"]),filereader:new Set(["load","loadend"]),postmessage:new Set(["message"])};function readsEventPayload(node,eventParam){const root=payloadRoot(node);return root!==void 0&&root.name===eventParam}function payloadRoot(node){let current=node;while(current.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){current=current.object}return current.type===ast_node_types_1.AST_NODE_TYPES.Identifier?current:void 0}function createPayloadResolver(sourceCode){const resolve=createReceiverResolver(sourceCode);const program=sourceCode.ast;const scopes=[];walk(program,node=>{const handler=handlerSource(node,resolve);if(handler===void 0)return;scopes.push({source:handler.source,handler:handler.handler,range:handler.handler.range})});scopes.sort((a,b)=>a.range[1]-a.range[0]-(b.range[1]-b.range[0]));return node=>scopes.find(scope=>node.range[0]>=scope.range[0]&&node.range[1]<=scope.range[1]&&readsPayloadOf(node,scope.handler,sourceCode))?.source}function readsPayloadOf(node,handler,sourceCode){const root=payloadRoot(node);if(root===void 0)return false;const variable=findVariable(sourceCode.getScope(root),root.name);if(variable===void 0)return false;return variable.defs.some(definition=>definition.type==="Parameter"&&definition.node===handler)}function walk(node,visit){visit(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(isNode(child))walk(child,visit)}}else if(isNode(value)){walk(value,visit)}}}function isNode(value){return typeof value==="object"&&value!==null&&typeof value.type==="string"}
|
|
@@ -1,39 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createWithMockContext = createWithMockContext;
|
|
4
|
-
function createWithMockContext(rule, opts = {}) {
|
|
5
|
-
const reports = [];
|
|
6
|
-
const filename = opts.filename ?? 'mock.ts';
|
|
7
|
-
const emptyScope = { variables: [], references: [], childScopes: [] };
|
|
8
|
-
const sourceCode = {
|
|
9
|
-
ast: opts.ast ?? { type: 'Program', body: [], tokens: [], comments: [] },
|
|
10
|
-
text: opts.sourceText ?? '',
|
|
11
|
-
getText: () => opts.sourceText ?? '',
|
|
12
|
-
getScope: () => emptyScope,
|
|
13
|
-
getAncestors: () => [],
|
|
14
|
-
getCommentsBefore: () => [],
|
|
15
|
-
getDeclaredVariables: () => [],
|
|
16
|
-
};
|
|
17
|
-
const context = {
|
|
18
|
-
id: 'mock-rule',
|
|
19
|
-
filename,
|
|
20
|
-
physicalFilename: filename,
|
|
21
|
-
cwd: '/',
|
|
22
|
-
options: opts.options ?? rule.defaultOptions ?? [],
|
|
23
|
-
settings: opts.settings ?? {},
|
|
24
|
-
parserOptions: {},
|
|
25
|
-
languageOptions: {},
|
|
26
|
-
sourceCode,
|
|
27
|
-
getFilename: () => filename,
|
|
28
|
-
getPhysicalFilename: () => filename,
|
|
29
|
-
getCwd: () => '/',
|
|
30
|
-
getSourceCode: () => sourceCode,
|
|
31
|
-
getScope: () => emptyScope,
|
|
32
|
-
getAncestors: () => [],
|
|
33
|
-
report: (descriptor) => {
|
|
34
|
-
reports.push(descriptor);
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
const listeners = rule.create(context);
|
|
38
|
-
return { listeners, reports, context };
|
|
39
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createWithMockContext=createWithMockContext;function createWithMockContext(rule,opts={}){const reports=[];const filename=opts.filename??"mock.ts";const emptyScope={variables:[],references:[],childScopes:[]};const sourceCode={ast:opts.ast??{type:"Program",body:[],tokens:[],comments:[]},text:opts.sourceText??"",getText:()=>opts.sourceText??"",getScope:()=>emptyScope,getAncestors:()=>[],getCommentsBefore:()=>[],getDeclaredVariables:()=>[]};const context={id:"mock-rule",filename,physicalFilename:filename,cwd:"/",options:opts.options??rule.defaultOptions??[],settings:opts.settings??{},parserOptions:{},languageOptions:{},sourceCode,getFilename:()=>filename,getPhysicalFilename:()=>filename,getCwd:()=>"/",getSourceCode:()=>sourceCode,getScope:()=>emptyScope,getAncestors:()=>[],report:descriptor=>{reports.push(descriptor)}};const listeners=rule.create(context);return{listeners,reports,context}}
|
|
@@ -1,140 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stripIdentifierGap = stripIdentifierGap;
|
|
4
|
-
exports.lastClause = lastClause;
|
|
5
|
-
exports.identifierPosition = identifierPosition;
|
|
6
|
-
exports.calleeText = calleeText;
|
|
7
|
-
exports.isExemptExpression = isExemptExpression;
|
|
8
|
-
exports.tagName = tagName;
|
|
9
|
-
exports.createRawIdentifierRule = createRawIdentifierRule;
|
|
10
|
-
const ast_node_types_1 = require("../ast-node-types");
|
|
11
|
-
const messaging_1 = require("../messaging");
|
|
12
|
-
const unscoped_mutation_rule_1 = require("./unscoped-mutation-rule");
|
|
13
|
-
const IDENTIFIER_KEYWORDS = /\b(?:from|join|into|update|table|order\s+by|group\s+by|select|distinct\s+on)$/i;
|
|
14
|
-
const GAP_CHAR = /[\s"'`[]/;
|
|
15
|
-
function stripIdentifierGap(text) {
|
|
16
|
-
let end = text.length;
|
|
17
|
-
while (end > 0 && GAP_CHAR.test(text[end - 1]))
|
|
18
|
-
end--;
|
|
19
|
-
return text.slice(0, end);
|
|
20
|
-
}
|
|
21
|
-
const CLAUSE_KEYWORDS = /\b(select|from|where|group\s+by|having|order\s+by|limit|offset|union|returning|values|set)\b/gi;
|
|
22
|
-
function lastClause(text) {
|
|
23
|
-
const matches = [...text.matchAll(CLAUSE_KEYWORDS)];
|
|
24
|
-
const last = matches.at(-1);
|
|
25
|
-
return last ? last[1].toLowerCase().replace(/\s+/g, ' ') : '';
|
|
26
|
-
}
|
|
27
|
-
function identifierPosition(precedingText) {
|
|
28
|
-
if (IDENTIFIER_KEYWORDS.test(stripIdentifierGap(precedingText)))
|
|
29
|
-
return 'identifier';
|
|
30
|
-
if (lastClause(precedingText) === 'order by')
|
|
31
|
-
return 'sortDirection';
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
function calleeText(node) {
|
|
35
|
-
if (node.type === ast_node_types_1.AST_NODE_TYPES.Identifier)
|
|
36
|
-
return node.name;
|
|
37
|
-
if (node.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression && !node.computed) {
|
|
38
|
-
const object = calleeText(node.object);
|
|
39
|
-
const property = calleeText(node.property);
|
|
40
|
-
return object && property ? `${object}.${property}` : '';
|
|
41
|
-
}
|
|
42
|
-
return '';
|
|
43
|
-
}
|
|
44
|
-
function isExemptExpression(expr, identifierHelpers, tags) {
|
|
45
|
-
if (expr.type === ast_node_types_1.AST_NODE_TYPES.Literal)
|
|
46
|
-
return true;
|
|
47
|
-
if (expr.type === ast_node_types_1.AST_NODE_TYPES.CallExpression) {
|
|
48
|
-
if (identifierHelpers.includes(calleeText(expr.callee)))
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
if (expr.type === ast_node_types_1.AST_NODE_TYPES.TaggedTemplateExpression) {
|
|
52
|
-
const nested = tagName(expr.tag);
|
|
53
|
-
return nested !== undefined && tags.includes(nested);
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
function tagName(tag) {
|
|
58
|
-
if (tag.type === ast_node_types_1.AST_NODE_TYPES.Identifier)
|
|
59
|
-
return tag.name;
|
|
60
|
-
if (tag.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression &&
|
|
61
|
-
!tag.computed &&
|
|
62
|
-
tag.property.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
|
|
63
|
-
return tag.property.name;
|
|
64
|
-
}
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
function createRawIdentifierRule(config) {
|
|
68
|
-
const tags = new Set(config.tags);
|
|
69
|
-
return {
|
|
70
|
-
meta: {
|
|
71
|
-
type: config.meta.type,
|
|
72
|
-
docs: { ...config.meta.docs },
|
|
73
|
-
messages: {
|
|
74
|
-
identifierInterpolation: (0, messaging_1.formatLLMMessage)({
|
|
75
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
76
|
-
issueName: 'SQL Injection Risk',
|
|
77
|
-
description: 'A table or column name is interpolated into a parameterized query. Bind parameters only ever substitute values, so this identifier is spliced into the SQL as text.',
|
|
78
|
-
severity: 'CRITICAL',
|
|
79
|
-
cwe: config.meta.docs.cwe,
|
|
80
|
-
owasp: 'A03:2021',
|
|
81
|
-
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
82
|
-
effort: 'medium',
|
|
83
|
-
fix: config.fix,
|
|
84
|
-
documentationLink: config.documentationLink,
|
|
85
|
-
}),
|
|
86
|
-
sortDirectionInterpolation: (0, messaging_1.formatLLMMessage)({
|
|
87
|
-
icon: messaging_1.MessageIcons.SECURITY,
|
|
88
|
-
issueName: 'SQL Injection Risk',
|
|
89
|
-
description: 'A value is interpolated inside an ORDER BY clause, where a bind parameter cannot reach.',
|
|
90
|
-
severity: 'HIGH',
|
|
91
|
-
cwe: config.meta.docs.cwe,
|
|
92
|
-
owasp: 'A03:2021',
|
|
93
|
-
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
94
|
-
effort: 'low',
|
|
95
|
-
fix: config.sortDirectionFix,
|
|
96
|
-
documentationLink: config.documentationLink,
|
|
97
|
-
}),
|
|
98
|
-
},
|
|
99
|
-
schema: [],
|
|
100
|
-
},
|
|
101
|
-
defaultOptions: [],
|
|
102
|
-
create(context) {
|
|
103
|
-
let bindings = new Set();
|
|
104
|
-
return {
|
|
105
|
-
Program(program) {
|
|
106
|
-
bindings = (0, unscoped_mutation_rule_1.driverBindings)(program, config.modules);
|
|
107
|
-
},
|
|
108
|
-
TaggedTemplateExpression(node) {
|
|
109
|
-
const name = tagName(node.tag);
|
|
110
|
-
if (name === undefined || !tags.has(name))
|
|
111
|
-
return;
|
|
112
|
-
if (config.requireImport) {
|
|
113
|
-
if (node.tag.type !== ast_node_types_1.AST_NODE_TYPES.Identifier)
|
|
114
|
-
return;
|
|
115
|
-
if (!bindings.has(name))
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
const { quasis, expressions } = node.quasi;
|
|
119
|
-
expressions.forEach((expr, index) => {
|
|
120
|
-
if (isExemptExpression(expr, config.identifierHelpers, config.tags))
|
|
121
|
-
return;
|
|
122
|
-
const preceding = quasis
|
|
123
|
-
.slice(0, index + 1)
|
|
124
|
-
.map((q) => q.value.raw)
|
|
125
|
-
.join(' ');
|
|
126
|
-
const position = identifierPosition(preceding);
|
|
127
|
-
if (position === false)
|
|
128
|
-
return;
|
|
129
|
-
context.report({
|
|
130
|
-
node: expr,
|
|
131
|
-
messageId: position === 'identifier'
|
|
132
|
-
? 'identifierInterpolation'
|
|
133
|
-
: 'sortDirectionInterpolation',
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
},
|
|
137
|
-
};
|
|
138
|
-
},
|
|
139
|
-
};
|
|
140
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.stripIdentifierGap=stripIdentifierGap;exports.lastClause=lastClause;exports.identifierPosition=identifierPosition;exports.calleeText=calleeText;exports.isExemptExpression=isExemptExpression;exports.tagName=tagName;exports.createRawIdentifierRule=createRawIdentifierRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const IDENTIFIER_KEYWORDS=/\b(?:from|join|into|update|table|order\s+by|group\s+by|select|distinct\s+on)$/i;const GAP_CHAR=/[\s"'`[]/;function stripIdentifierGap(text){let end=text.length;while(end>0&&GAP_CHAR.test(text[end-1]))end--;return text.slice(0,end)}const CLAUSE_KEYWORDS=/\b(select|from|where|group\s+by|having|order\s+by|limit|offset|union|returning|values|set)\b/gi;function lastClause(text){const matches=[...text.matchAll(CLAUSE_KEYWORDS)];const last=matches.at(-1);return last?last[1].toLowerCase().replace(/\s+/g," "):""}function identifierPosition(precedingText){if(IDENTIFIER_KEYWORDS.test(stripIdentifierGap(precedingText)))return"identifier";if(lastClause(precedingText)==="order by")return"sortDirection";return false}function calleeText(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return node.name;if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&!node.computed){const object=calleeText(node.object);const property=calleeText(node.property);return object&&property?`${object}.${property}`:""}return""}function isExemptExpression(expr,identifierHelpers,tags){if(expr.type===ast_node_types_1.AST_NODE_TYPES.Literal)return true;if(expr.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){if(identifierHelpers.includes(calleeText(expr.callee)))return true}if(expr.type===ast_node_types_1.AST_NODE_TYPES.TaggedTemplateExpression){const nested=tagName(expr.tag);return nested!==void 0&&tags.includes(nested)}return false}function tagName(tag){if(tag.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return tag.name;if(tag.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&!tag.computed&&tag.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return tag.property.name}return void 0}function createRawIdentifierRule(config){const tags=new Set(config.tags);return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{identifierInterpolation:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"A table or column name is interpolated into a parameterized query. Bind parameters only ever substitute values, so this identifier is spliced into the SQL as text.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink}),sortDirectionInterpolation:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"A value is interpolated inside an ORDER BY clause, where a bind parameter cannot reach.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"low",fix:config.sortDirectionFix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let bindings=new Set;return{Program(program){bindings=(0,unscoped_mutation_rule_1.driverBindings)(program,config.modules)},TaggedTemplateExpression(node){const name=tagName(node.tag);if(name===void 0||!tags.has(name))return;if(config.requireImport){if(node.tag.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return;if(!bindings.has(name))return}const{quasis,expressions}=node.quasi;expressions.forEach((expr,index)=>{if(isExemptExpression(expr,config.identifierHelpers,config.tags))return;const preceding=quasis.slice(0,index+1).map(q=>q.value.raw).join(" ");const position=identifierPosition(preceding);if(position===false)return;context.report({node:expr,messageId:position==="identifier"?"identifierInterpolation":"sortDirectionInterpolation"})})}}}}}
|