@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.
Files changed (46) hide show
  1. package/README.md +1 -1
  2. package/package.json +2 -2
  3. package/src/aria-definitions.js +1 -990
  4. package/src/ast/ast-utils.js +1 -133
  5. package/src/ast/index.js +1 -17
  6. package/src/ast-node-types.js +1 -174
  7. package/src/index.js +1 -43
  8. package/src/messaging/config.js +1 -98
  9. package/src/messaging/constants.js +1 -678
  10. package/src/messaging/formatters-ai.js +2 -176
  11. package/src/messaging/formatters-v2.js +10 -212
  12. package/src/messaging/formatters.js +3 -186
  13. package/src/messaging/index.js +1 -21
  14. package/src/messaging/types-v2.js +1 -2
  15. package/src/messaging/types.js +1 -2
  16. package/src/node/fs.js +1 -104
  17. package/src/node/index.js +1 -19
  18. package/src/node/os.js +1 -52
  19. package/src/node/path.js +1 -143
  20. package/src/resolver/dependency-analysis.js +1 -740
  21. package/src/resolver/index.js +1 -20
  22. package/src/resolver/resolver-adapter.js +1 -92
  23. package/src/resolver/resolver-detection.js +1 -189
  24. package/src/resolver/resolver.js +1 -206
  25. package/src/resolver.js +1 -17
  26. package/src/rule-creation/browser-escape-hatch-rule.js +1 -100
  27. package/src/rule-creation/hardcoded-credentials-rule.js +1 -143
  28. package/src/rule-creation/index.d.ts +2 -0
  29. package/src/rule-creation/index.js +1 -26
  30. package/src/rule-creation/mass-assignment-rule.js +1 -121
  31. package/src/rule-creation/message-source.d.ts +95 -0
  32. package/src/rule-creation/message-source.js +1 -0
  33. package/src/rule-creation/mock-context.js +1 -39
  34. package/src/rule-creation/raw-identifier-rule.js +1 -140
  35. package/src/rule-creation/require-tls-rule.js +1 -190
  36. package/src/rule-creation/rule-creator.js +1 -70
  37. package/src/rule-creation/sdk-api-key-rule.js +1 -121
  38. package/src/rule-creation/sql-injection-rule.js +1 -207
  39. package/src/rule-creation/system-prompt-injection-rule.d.ts +78 -0
  40. package/src/rule-creation/system-prompt-injection-rule.js +1 -0
  41. package/src/rule-creation/unscoped-mutation-rule.js +1 -234
  42. package/src/security/index.js +1 -17
  43. package/src/security/security-utils.js +1 -461
  44. package/src/types/index.js +1 -18
  45. package/src/types/meta-augmentation.js +1 -2
  46. package/src/types/type-utils.js +1 -222
@@ -1,190 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isLiteralBoolean = isLiteralBoolean;
4
- exports.looksLikeConnectionConfig = looksLikeConnectionConfig;
5
- exports.urlDisablesTls = urlDisablesTls;
6
- exports.inConnectionPosition = inConnectionPosition;
7
- exports.createRequireTlsRule = createRequireTlsRule;
8
- const ast_node_types_1 = require("../ast-node-types");
9
- const messaging_1 = require("../messaging");
10
- const unscoped_mutation_rule_1 = require("./unscoped-mutation-rule");
11
- const DEFAULT_CONNECTION_KEYS = [
12
- 'host',
13
- 'hostname',
14
- 'port',
15
- 'user',
16
- 'username',
17
- 'password',
18
- 'database',
19
- 'connectionString',
20
- 'socketPath',
21
- 'uri',
22
- 'url',
23
- 'connection',
24
- 'pool',
25
- 'schema',
26
- ];
27
- const TLS_OFF_KEYS = ['ssl', 'secure', 'tls', 'encrypt', 'useSSL', 'requireSSL'];
28
- const VERIFY_OFF_KEYS = [
29
- 'rejectUnauthorized',
30
- 'verifyServerCertificate',
31
- 'checkServerIdentity',
32
- 'trustServerCertificate',
33
- ];
34
- function isLiteralBoolean(value, expect) {
35
- if (value.type !== ast_node_types_1.AST_NODE_TYPES.Literal)
36
- return false;
37
- if (typeof value.value !== 'boolean') {
38
- return expect === false && !value.value;
39
- }
40
- return value.value === expect;
41
- }
42
- function looksLikeConnectionConfig(obj, connectionKeys) {
43
- return obj.properties.some((prop) => {
44
- const name = (0, unscoped_mutation_rule_1.propertyKeyName)(prop);
45
- return name !== undefined && connectionKeys.includes(name);
46
- });
47
- }
48
- function urlDisablesTls(value, schemes) {
49
- if (!schemes.some((scheme) => value.startsWith(`${scheme}://`)))
50
- return false;
51
- const query = value.split('#')[0];
52
- return /[?&](?:sslmode=disable|ssl=(?:false|0))(?:&|$)/i.test(query);
53
- }
54
- function inConnectionPosition(node, connectionKeys, bindings) {
55
- const parent = node.parent;
56
- if (!parent)
57
- return false;
58
- if (parent.type === ast_node_types_1.AST_NODE_TYPES.Property && parent.value === node) {
59
- const name = (0, unscoped_mutation_rule_1.propertyKeyName)(parent);
60
- return name !== undefined && connectionKeys.includes(name);
61
- }
62
- if (parent.type === ast_node_types_1.AST_NODE_TYPES.CallExpression ||
63
- parent.type === ast_node_types_1.AST_NODE_TYPES.NewExpression) {
64
- if (!parent.arguments.includes(node))
65
- return false;
66
- return calleeBaseIsDriver(parent.callee, bindings);
67
- }
68
- return false;
69
- }
70
- function calleeBaseIsDriver(callee, bindings) {
71
- let cursor = callee;
72
- for (;;) {
73
- if (cursor.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression) {
74
- cursor = cursor.object;
75
- continue;
76
- }
77
- if (cursor.type === ast_node_types_1.AST_NODE_TYPES.CallExpression) {
78
- cursor = cursor.callee;
79
- continue;
80
- }
81
- return cursor.type === ast_node_types_1.AST_NODE_TYPES.Identifier && bindings.has(cursor.name);
82
- }
83
- }
84
- function createRequireTlsRule(config) {
85
- const connectionKeys = [...DEFAULT_CONNECTION_KEYS, ...(config.connectionKeys ?? [])];
86
- const urlSchemes = config.urlSchemes ?? [];
87
- return {
88
- meta: {
89
- type: config.meta.type,
90
- docs: { ...config.meta.docs },
91
- messages: {
92
- tlsDisabled: (0, messaging_1.formatLLMMessage)({
93
- icon: messaging_1.MessageIcons.SECURITY,
94
- issueName: 'Cleartext Database Connection',
95
- description: 'TLS is explicitly disabled on this database connection. Queries, result rows and the credentials that open the session all cross the network in cleartext.',
96
- severity: 'HIGH',
97
- cwe: config.meta.docs.cwe,
98
- owasp: 'A02:2021',
99
- compliance: ['SOC2', 'PCI-DSS', 'HIPAA', 'NIST-CSF'],
100
- effort: 'low',
101
- fix: config.fix,
102
- documentationLink: config.documentationLink,
103
- }),
104
- certificateValidationDisabled: (0, messaging_1.formatLLMMessage)({
105
- icon: messaging_1.MessageIcons.SECURITY,
106
- issueName: 'Unverified Database TLS Peer',
107
- description: 'Certificate validation is disabled on this database connection. The traffic is encrypted but the server is never authenticated, so it encrypts just as willingly to whoever answered in its place.',
108
- severity: 'HIGH',
109
- cwe: config.meta.docs.cwe,
110
- owasp: 'A02:2021',
111
- compliance: ['SOC2', 'PCI-DSS', 'HIPAA', 'NIST-CSF'],
112
- effort: 'medium',
113
- fix: 'Supply the server CA instead of switching verification off — `ssl: { ca: fs.readFileSync(caPath) }`. If the certificate is self-signed, add that CA to the trust store rather than trusting everything.',
114
- documentationLink: config.documentationLink,
115
- }),
116
- },
117
- schema: [],
118
- },
119
- defaultOptions: [],
120
- create(context) {
121
- let bindings = new Set();
122
- let reported = new WeakSet();
123
- const report = (prop, messageId) => {
124
- if (reported.has(prop))
125
- return;
126
- reported.add(prop);
127
- context.report({ node: prop, messageId });
128
- };
129
- const reportVerifyOffProperty = (prop, name) => {
130
- if (!VERIFY_OFF_KEYS.includes(name))
131
- return;
132
- const inverted = name === 'trustServerCertificate';
133
- if (!isLiteralBoolean(prop.value, inverted))
134
- return;
135
- report(prop, 'certificateValidationDisabled');
136
- };
137
- const scanTlsKeys = (obj) => {
138
- for (const prop of obj.properties) {
139
- if (prop.type !== ast_node_types_1.AST_NODE_TYPES.Property)
140
- continue;
141
- const name = (0, unscoped_mutation_rule_1.propertyKeyName)(prop);
142
- if (name === undefined)
143
- continue;
144
- if (TLS_OFF_KEYS.includes(name)) {
145
- if (isLiteralBoolean(prop.value, false)) {
146
- report(prop, 'tlsDisabled');
147
- continue;
148
- }
149
- if (prop.value.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression) {
150
- scanTlsKeys(prop.value);
151
- }
152
- continue;
153
- }
154
- reportVerifyOffProperty(prop, name);
155
- if (prop.value.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression &&
156
- connectionKeys.includes(name)) {
157
- scanTlsKeys(prop.value);
158
- }
159
- }
160
- };
161
- const checkObject = (obj) => {
162
- if (!looksLikeConnectionConfig(obj, connectionKeys))
163
- return;
164
- scanTlsKeys(obj);
165
- };
166
- return {
167
- Program(program) {
168
- bindings = (0, unscoped_mutation_rule_1.driverBindings)(program, config.modules);
169
- reported = new WeakSet();
170
- },
171
- ObjectExpression(node) {
172
- if (bindings.size === 0)
173
- return;
174
- checkObject(node);
175
- },
176
- Literal(node) {
177
- if (bindings.size === 0)
178
- return;
179
- if (typeof node.value !== 'string')
180
- return;
181
- if (!urlDisablesTls(node.value, urlSchemes))
182
- return;
183
- if (!inConnectionPosition(node, connectionKeys, bindings))
184
- return;
185
- context.report({ node, messageId: 'tlsDisabled' });
186
- },
187
- };
188
- },
189
- };
190
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isLiteralBoolean=isLiteralBoolean;exports.looksLikeConnectionConfig=looksLikeConnectionConfig;exports.urlDisablesTls=urlDisablesTls;exports.inConnectionPosition=inConnectionPosition;exports.createRequireTlsRule=createRequireTlsRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const DEFAULT_CONNECTION_KEYS=["host","hostname","port","user","username","password","database","connectionString","socketPath","uri","url","connection","pool","schema"];const TLS_OFF_KEYS=["ssl","secure","tls","encrypt","useSSL","requireSSL"];const VERIFY_OFF_KEYS=["rejectUnauthorized","verifyServerCertificate","checkServerIdentity","trustServerCertificate"];function isLiteralBoolean(value,expect){if(value.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return false;if(typeof value.value!=="boolean"){return expect===false&&!value.value}return value.value===expect}function looksLikeConnectionConfig(obj,connectionKeys){return obj.properties.some(prop=>{const name=(0,unscoped_mutation_rule_1.propertyKeyName)(prop);return name!==void 0&&connectionKeys.includes(name)})}function urlDisablesTls(value,schemes){if(!schemes.some(scheme=>value.startsWith(`${scheme}://`)))return false;const query=value.split("#")[0];return/[?&](?:sslmode=disable|ssl=(?:false|0))(?:&|$)/i.test(query)}function inConnectionPosition(node,connectionKeys,bindings){const parent=node.parent;if(!parent)return false;if(parent.type===ast_node_types_1.AST_NODE_TYPES.Property&&parent.value===node){const name=(0,unscoped_mutation_rule_1.propertyKeyName)(parent);return name!==void 0&&connectionKeys.includes(name)}if(parent.type===ast_node_types_1.AST_NODE_TYPES.CallExpression||parent.type===ast_node_types_1.AST_NODE_TYPES.NewExpression){if(!parent.arguments.includes(node))return false;return calleeBaseIsDriver(parent.callee,bindings)}return false}function calleeBaseIsDriver(callee,bindings){let cursor=callee;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){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&&bindings.has(cursor.name)}}function createRequireTlsRule(config){const connectionKeys=[...DEFAULT_CONNECTION_KEYS,...config.connectionKeys??[]];const urlSchemes=config.urlSchemes??[];return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{tlsDisabled:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Cleartext Database Connection",description:"TLS is explicitly disabled on this database connection. Queries, result rows and the credentials that open the session all cross the network in cleartext.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A02:2021",compliance:["SOC2","PCI-DSS","HIPAA","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),certificateValidationDisabled:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unverified Database TLS Peer",description:"Certificate validation is disabled on this database connection. The traffic is encrypted but the server is never authenticated, so it encrypts just as willingly to whoever answered in its place.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A02:2021",compliance:["SOC2","PCI-DSS","HIPAA","NIST-CSF"],effort:"medium",fix:"Supply the server CA instead of switching verification off \u2014 `ssl: { ca: fs.readFileSync(caPath) }`. If the certificate is self-signed, add that CA to the trust store rather than trusting everything.",documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let bindings=new Set;let reported=new WeakSet;const report=(prop,messageId)=>{if(reported.has(prop))return;reported.add(prop);context.report({node:prop,messageId})};const reportVerifyOffProperty=(prop,name)=>{if(!VERIFY_OFF_KEYS.includes(name))return;const inverted=name==="trustServerCertificate";if(!isLiteralBoolean(prop.value,inverted))return;report(prop,"certificateValidationDisabled")};const scanTlsKeys=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(TLS_OFF_KEYS.includes(name)){if(isLiteralBoolean(prop.value,false)){report(prop,"tlsDisabled");continue}if(prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){scanTlsKeys(prop.value)}continue}reportVerifyOffProperty(prop,name);if(prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&connectionKeys.includes(name)){scanTlsKeys(prop.value)}}};const checkObject=obj=>{if(!looksLikeConnectionConfig(obj,connectionKeys))return;scanTlsKeys(obj)};return{Program(program){bindings=(0,unscoped_mutation_rule_1.driverBindings)(program,config.modules);reported=new WeakSet},ObjectExpression(node){if(bindings.size===0)return;checkObject(node)},Literal(node){if(bindings.size===0)return;if(typeof node.value!=="string")return;if(!urlDisablesTls(node.value,urlSchemes))return;if(!inConnectionPosition(node,connectionKeys,bindings))return;context.report({node,messageId:"tlsDisabled"})}}}}}
@@ -1,70 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ESLintUtils = exports.createRule = void 0;
4
- exports.applyDefault = applyDefault;
5
- exports.RuleCreator = RuleCreator;
6
- exports.createRuleCreator = createRuleCreator;
7
- function isObjectNotArray(obj) {
8
- return typeof obj === 'object' && obj != null && !Array.isArray(obj);
9
- }
10
- function deepMerge(first, second) {
11
- const keys = new Set([...Object.keys(first), ...Object.keys(second)]);
12
- return Object.fromEntries([...keys].map((key) => {
13
- const firstHasKey = key in first;
14
- const secondHasKey = key in second;
15
- const firstValue = first[key];
16
- const secondValue = second[key];
17
- if (firstHasKey && secondHasKey) {
18
- return [
19
- key,
20
- isObjectNotArray(firstValue) && isObjectNotArray(secondValue)
21
- ? deepMerge(firstValue, secondValue)
22
- : secondValue,
23
- ];
24
- }
25
- return [key, firstHasKey ? firstValue : secondValue];
26
- }));
27
- }
28
- function applyDefault(defaultOptions, userOptions) {
29
- const options = structuredClone(defaultOptions);
30
- if (userOptions == null) {
31
- return options;
32
- }
33
- options.forEach((opt, i) => {
34
- if (userOptions[i] !== undefined) {
35
- const userOpt = userOptions[i];
36
- options[i] =
37
- isObjectNotArray(userOpt) && isObjectNotArray(opt)
38
- ? deepMerge(opt, userOpt)
39
- : userOpt;
40
- }
41
- });
42
- return options;
43
- }
44
- function createRuleInternal({ create, defaultOptions, meta, name, }) {
45
- const resolvedDefaultOptions = (defaultOptions ?? []);
46
- return {
47
- create(context) {
48
- return create(context, applyDefault(resolvedDefaultOptions, context.options));
49
- },
50
- defaultOptions: resolvedDefaultOptions,
51
- meta,
52
- name: name,
53
- };
54
- }
55
- function RuleCreator(urlCreator) {
56
- return ({ meta, name, ...rule }) => createRuleInternal({
57
- meta: {
58
- ...meta,
59
- docs: { ...meta.docs, url: urlCreator(name) },
60
- },
61
- name,
62
- ...rule,
63
- });
64
- }
65
- RuleCreator.withoutDocs = (args) => createRuleInternal(args);
66
- function createRuleCreator(urlCreator) {
67
- return RuleCreator(urlCreator);
68
- }
69
- exports.createRule = RuleCreator((name) => `https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin/docs/rules/${name}.md`);
70
- exports.ESLintUtils = { RuleCreator, applyDefault };
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.ESLintUtils=exports.createRule=void 0;exports.applyDefault=applyDefault;exports.RuleCreator=RuleCreator;exports.createRuleCreator=createRuleCreator;function isObjectNotArray(obj){return typeof obj==="object"&&obj!=null&&!Array.isArray(obj)}function deepMerge(first,second){const keys=new Set([...Object.keys(first),...Object.keys(second)]);return Object.fromEntries([...keys].map(key=>{const firstHasKey=key in first;const secondHasKey=key in second;const firstValue=first[key];const secondValue=second[key];if(firstHasKey&&secondHasKey){return[key,isObjectNotArray(firstValue)&&isObjectNotArray(secondValue)?deepMerge(firstValue,secondValue):secondValue]}return[key,firstHasKey?firstValue:secondValue]}))}function applyDefault(defaultOptions,userOptions){const options=structuredClone(defaultOptions);if(userOptions==null){return options}options.forEach((opt,i)=>{if(userOptions[i]!==void 0){const userOpt=userOptions[i];options[i]=isObjectNotArray(userOpt)&&isObjectNotArray(opt)?deepMerge(opt,userOpt):userOpt}});return options}function createRuleInternal({create,defaultOptions,meta,name}){const resolvedDefaultOptions=defaultOptions??[];return{create(context){return create(context,applyDefault(resolvedDefaultOptions,context.options))},defaultOptions:resolvedDefaultOptions,meta,name}}function RuleCreator(urlCreator){return({meta,name,...rule})=>createRuleInternal({meta:{...meta,docs:{...meta.docs,url:urlCreator(name)}},name,...rule})}RuleCreator.withoutDocs=args=>createRuleInternal(args);function createRuleCreator(urlCreator){return RuleCreator(urlCreator)}exports.createRule=RuleCreator(name=>`https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin/docs/rules/${name}.md`);exports.ESLintUtils={RuleCreator,applyDefault};
@@ -1,121 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.POSITIONAL_KEY_LABEL = void 0;
4
- exports.matchesModule = matchesModule;
5
- exports.readCredential = readCredential;
6
- exports.calleeName = calleeName;
7
- exports.createSdkApiKeyRule = createSdkApiKeyRule;
8
- const rule_creator_1 = require("./rule-creator");
9
- const messaging_1 = require("../messaging");
10
- exports.POSITIONAL_KEY_LABEL = 'The first argument';
11
- function matchesModule(source, modules) {
12
- return modules.some((m) => source === m || source.startsWith(`${m}/`));
13
- }
14
- function readCredential(options, keyProps) {
15
- for (const prop of options.properties) {
16
- if (prop.type === 'SpreadElement')
17
- return { kind: 'unreadable' };
18
- if (prop.computed)
19
- continue;
20
- const name = prop.key.type === 'Identifier' ? prop.key.name : String(prop.key.value);
21
- if (!keyProps.has(name))
22
- continue;
23
- if (prop.value.type !== 'Literal')
24
- continue;
25
- if (typeof prop.value.value === 'string' && prop.value.value.length > 0) {
26
- return { kind: 'literal', prop: name };
27
- }
28
- }
29
- return { kind: 'safe' };
30
- }
31
- function calleeName(node) {
32
- if (node.type === 'Identifier')
33
- return node.name;
34
- if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier') {
35
- return node.property.name;
36
- }
37
- return undefined;
38
- }
39
- function createSdkApiKeyRule(config) {
40
- const keyProps = new Set(config.keyProps);
41
- const positional = new Set(config.positionalKeyConstructors ?? []);
42
- return (0, rule_creator_1.createRule)({
43
- name: config.ruleName,
44
- meta: {
45
- type: 'problem',
46
- docs: {
47
- url: config.docsUrl,
48
- description: `Forbid a literal API key in the ${config.vendor} client options`,
49
- cwe: 'CWE-798',
50
- cvss: 9.1,
51
- },
52
- messages: {
53
- hardcodedApiKey: (0, messaging_1.formatLLMMessage)({
54
- icon: messaging_1.MessageIcons.SECURITY,
55
- issueName: `Hardcoded ${config.vendor} Credential`,
56
- cwe: 'CWE-798',
57
- owasp: 'A07:2021',
58
- cvss: 9.1,
59
- description: '{{prop}} is a string literal, so the credential is committed to source control and readable by anyone with repository access',
60
- severity: 'CRITICAL',
61
- compliance: ['SOC2', 'PCI-DSS'],
62
- fix: `Read it from the environment: ${config.fixTemplate}`,
63
- documentationLink: config.documentationLink,
64
- }),
65
- },
66
- schema: [],
67
- },
68
- defaultOptions: [],
69
- create(context) {
70
- let importsSdk = false;
71
- const candidates = [];
72
- function inspect(node, args) {
73
- const first = args[0];
74
- if (first === undefined)
75
- return;
76
- if (first.type === 'Literal') {
77
- const name = calleeName(node.callee);
78
- if (name !== undefined &&
79
- positional.has(name) &&
80
- typeof first.value === 'string' &&
81
- first.value.length > 0) {
82
- candidates.push({ node, prop: exports.POSITIONAL_KEY_LABEL });
83
- }
84
- return;
85
- }
86
- if (first.type !== 'ObjectExpression')
87
- return;
88
- const verdict = readCredential(first, keyProps);
89
- if (verdict.kind === 'literal')
90
- candidates.push({ node, prop: verdict.prop });
91
- }
92
- return {
93
- ImportDeclaration(node) {
94
- if (matchesModule(String(node.source.value), config.modules))
95
- importsSdk = true;
96
- },
97
- NewExpression(node) {
98
- inspect(node, node.arguments);
99
- },
100
- CallExpression(node) {
101
- if (node.callee.type === 'Identifier' &&
102
- node.callee.name === 'require' &&
103
- node.arguments[0]?.type === 'Literal' &&
104
- typeof node.arguments[0].value === 'string' &&
105
- matchesModule(node.arguments[0].value, config.modules)) {
106
- importsSdk = true;
107
- return;
108
- }
109
- inspect(node, node.arguments);
110
- },
111
- 'Program:exit'() {
112
- if (!importsSdk)
113
- return;
114
- for (const { node, prop } of candidates) {
115
- context.report({ node, messageId: 'hardcodedApiKey', data: { prop } });
116
- }
117
- },
118
- };
119
- },
120
- });
121
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.POSITIONAL_KEY_LABEL=void 0;exports.matchesModule=matchesModule;exports.readCredential=readCredential;exports.calleeName=calleeName;exports.createSdkApiKeyRule=createSdkApiKeyRule;const rule_creator_1=require("./rule-creator");const messaging_1=require("../messaging");exports.POSITIONAL_KEY_LABEL="The first argument";function matchesModule(source,modules){return modules.some(m=>source===m||source.startsWith(`${m}/`))}function readCredential(options,keyProps){for(const prop of options.properties){if(prop.type==="SpreadElement")return{kind:"unreadable"};if(prop.computed)continue;const name=prop.key.type==="Identifier"?prop.key.name:String(prop.key.value);if(!keyProps.has(name))continue;if(prop.value.type!=="Literal")continue;if(typeof prop.value.value==="string"&&prop.value.value.length>0){return{kind:"literal",prop:name}}}return{kind:"safe"}}function calleeName(node){if(node.type==="Identifier")return node.name;if(node.type==="MemberExpression"&&!node.computed&&node.property.type==="Identifier"){return node.property.name}return void 0}function createSdkApiKeyRule(config){const keyProps=new Set(config.keyProps);const positional=new Set(config.positionalKeyConstructors??[]);return(0,rule_creator_1.createRule)({name:config.ruleName,meta:{type:"problem",docs:{url:config.docsUrl,description:`Forbid a literal API key in the ${config.vendor} client options`,cwe:"CWE-798",cvss:9.1},messages:{hardcodedApiKey:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:`Hardcoded ${config.vendor} Credential`,cwe:"CWE-798",owasp:"A07:2021",cvss:9.1,description:"{{prop}} is a string literal, so the credential is committed to source control and readable by anyone with repository access",severity:"CRITICAL",compliance:["SOC2","PCI-DSS"],fix:`Read it from the environment: ${config.fixTemplate}`,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let importsSdk=false;const candidates=[];function inspect(node,args){const first=args[0];if(first===void 0)return;if(first.type==="Literal"){const name=calleeName(node.callee);if(name!==void 0&&positional.has(name)&&typeof first.value==="string"&&first.value.length>0){candidates.push({node,prop:exports.POSITIONAL_KEY_LABEL})}return}if(first.type!=="ObjectExpression")return;const verdict=readCredential(first,keyProps);if(verdict.kind==="literal")candidates.push({node,prop:verdict.prop})}return{ImportDeclaration(node){if(matchesModule(String(node.source.value),config.modules))importsSdk=true},NewExpression(node){inspect(node,node.arguments)},CallExpression(node){if(node.callee.type==="Identifier"&&node.callee.name==="require"&&node.arguments[0]?.type==="Literal"&&typeof node.arguments[0].value==="string"&&matchesModule(node.arguments[0].value,config.modules)){importsSdk=true;return}inspect(node,node.arguments)},"Program:exit"(){if(!importsSdk)return;for(const{node,prop}of candidates){context.report({node,messageId:"hardcodedApiKey",data:{prop}})}}}}})}
@@ -1,207 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSqlInjectionRule = createSqlInjectionRule;
4
- const ast_node_types_1 = require("../ast-node-types");
5
- const messaging_1 = require("../messaging");
6
- const SQL_KEYWORDS = /\b(?:select|insert|update|delete|drop|alter|truncate|union|from|where|values|set)\b/i;
7
- const FUNCTION_TYPES = new Set([
8
- ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration,
9
- ast_node_types_1.AST_NODE_TYPES.FunctionExpression,
10
- ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression,
11
- ]);
12
- function enclosingFunction(node) {
13
- for (let n = node.parent; n; n = n.parent) {
14
- if (FUNCTION_TYPES.has(n.type))
15
- return n;
16
- }
17
- return undefined;
18
- }
19
- function callableName(fn) {
20
- if (fn.type === ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration && fn.id)
21
- return fn.id.name;
22
- const parent = fn.parent;
23
- if (parent.type === ast_node_types_1.AST_NODE_TYPES.VariableDeclarator &&
24
- parent.id.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
25
- return parent.id.name;
26
- }
27
- if ((parent.type === ast_node_types_1.AST_NODE_TYPES.MethodDefinition ||
28
- parent.type === ast_node_types_1.AST_NODE_TYPES.PropertyDefinition ||
29
- parent.type === ast_node_types_1.AST_NODE_TYPES.Property) &&
30
- parent.key.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
31
- return parent.key.name;
32
- }
33
- return undefined;
34
- }
35
- function staticText(node) {
36
- if (node.type === ast_node_types_1.AST_NODE_TYPES.TemplateLiteral) {
37
- return node.quasis.map((q) => q.value.raw).join(' ');
38
- }
39
- if (node.type === ast_node_types_1.AST_NODE_TYPES.BinaryExpression) {
40
- return `${staticText(node.left)} ${staticText(node.right)}`;
41
- }
42
- if (node.type === ast_node_types_1.AST_NODE_TYPES.Literal && typeof node.value === 'string') {
43
- return node.value;
44
- }
45
- return '';
46
- }
47
- function classify(node, requireSqlKeywords) {
48
- let kind = false;
49
- if (node.type === ast_node_types_1.AST_NODE_TYPES.BinaryExpression && node.operator === '+') {
50
- kind = 'concat';
51
- }
52
- if (node.type === ast_node_types_1.AST_NODE_TYPES.TemplateLiteral &&
53
- node.expressions.length > 0) {
54
- kind = 'template';
55
- }
56
- if (kind === false)
57
- return false;
58
- if (kind === 'concat' && staticText(node).trim() === '')
59
- return false;
60
- if (requireSqlKeywords && !SQL_KEYWORDS.test(staticText(node)))
61
- return false;
62
- return kind;
63
- }
64
- function createSqlInjectionRule(config) {
65
- const sinks = new Set(config.methods);
66
- return {
67
- meta: {
68
- type: config.meta.type,
69
- docs: { ...config.meta.docs },
70
- messages: {
71
- noUnsafeQuery: (0, messaging_1.formatLLMMessage)({
72
- icon: messaging_1.MessageIcons.SECURITY,
73
- issueName: 'SQL Injection Risk',
74
- description: 'Unsafe SQL query detected. Variable interpolation found.',
75
- severity: 'CRITICAL',
76
- cwe: config.meta.docs.cwe,
77
- owasp: 'A03:2021',
78
- compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
79
- effort: 'high',
80
- fix: config.fix,
81
- documentationLink: config.documentationLink,
82
- }),
83
- unsafeTemplateLiteral: (0, messaging_1.formatLLMMessage)({
84
- icon: messaging_1.MessageIcons.SECURITY,
85
- issueName: 'SQL Injection Risk',
86
- description: 'Unsafe SQL query construction detected (template literal).',
87
- severity: 'CRITICAL',
88
- cwe: config.meta.docs.cwe,
89
- owasp: 'A03:2021',
90
- compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
91
- effort: 'high',
92
- fix: config.fix,
93
- documentationLink: config.documentationLink,
94
- }),
95
- },
96
- schema: [],
97
- },
98
- defaultOptions: [],
99
- create(context) {
100
- const tainted = new Map();
101
- const sqlish = new Set();
102
- const wrappers = new Map();
103
- const definitions = new Map();
104
- const wrapperFns = new Map();
105
- const pending = [];
106
- const report = (node, kind) => {
107
- context.report({
108
- node,
109
- messageId: kind === 'template' ? 'unsafeTemplateLiteral' : 'noUnsafeQuery',
110
- });
111
- };
112
- return {
113
- 'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression'(node) {
114
- const name = callableName(node);
115
- if (name !== undefined)
116
- definitions.set(name, (definitions.get(name) ?? 0) + 1);
117
- },
118
- VariableDeclarator(node) {
119
- if (node.id.type === ast_node_types_1.AST_NODE_TYPES.Identifier && node.init) {
120
- const kind = classify(node.init, config.requireSqlKeywords);
121
- if (kind)
122
- tainted.set(node.id.name, kind);
123
- if (SQL_KEYWORDS.test(staticText(node.init)))
124
- sqlish.add(node.id.name);
125
- }
126
- },
127
- AssignmentExpression(node) {
128
- if (node.operator === '+=' &&
129
- node.left.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
130
- const gate = config.requireSqlKeywords && !sqlish.has(node.left.name);
131
- const kind = classify(node.right, gate);
132
- if (kind)
133
- tainted.set(node.left.name, kind);
134
- if (SQL_KEYWORDS.test(staticText(node.right)))
135
- sqlish.add(node.left.name);
136
- }
137
- },
138
- CallExpression(node) {
139
- const isMember = node.callee.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression &&
140
- node.callee.property.type === ast_node_types_1.AST_NODE_TYPES.Identifier;
141
- const calleeName = isMember
142
- ? node.callee
143
- .property.name
144
- : node.callee.type === ast_node_types_1.AST_NODE_TYPES.Identifier
145
- ? node.callee.name
146
- : undefined;
147
- if (calleeName === undefined)
148
- return;
149
- if (!isMember || !sinks.has(calleeName)) {
150
- for (const [index, arg] of node.arguments.entries()) {
151
- if (arg.type === ast_node_types_1.AST_NODE_TYPES.SpreadElement)
152
- continue;
153
- const kind = classify(arg, true);
154
- if (kind)
155
- pending.push({ name: calleeName, index, arg, kind });
156
- else if (arg.type === ast_node_types_1.AST_NODE_TYPES.Identifier)
157
- pending.push({ name: calleeName, index, arg, kind: undefined });
158
- }
159
- return;
160
- }
161
- const queryArg = node.arguments[0];
162
- if (!queryArg)
163
- return;
164
- if (queryArg.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
165
- const fn = enclosingFunction(node);
166
- const index = fn?.params.findIndex((p) => p.type === ast_node_types_1.AST_NODE_TYPES.Identifier &&
167
- p.name === queryArg.name) ?? -1;
168
- const name = fn && index >= 0 ? callableName(fn) : undefined;
169
- if (name !== undefined && fn) {
170
- wrappers.set(name, index);
171
- wrapperFns.set(fn, name);
172
- }
173
- }
174
- const direct = classify(queryArg, config.requireSqlKeywords);
175
- if (direct) {
176
- report(queryArg, direct);
177
- return;
178
- }
179
- if (queryArg.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
180
- const taint = tainted.get(queryArg.name);
181
- if (taint)
182
- report(queryArg, taint);
183
- }
184
- },
185
- 'Program:exit'() {
186
- const proven = new Map();
187
- for (const name of wrapperFns.values())
188
- proven.set(name, (proven.get(name) ?? 0) + 1);
189
- for (const { name, index, arg, kind } of pending) {
190
- if (wrappers.get(name) !== index)
191
- continue;
192
- if (definitions.get(name) !== proven.get(name))
193
- continue;
194
- if (kind) {
195
- report(arg, kind);
196
- continue;
197
- }
198
- const varName = arg.name;
199
- const taint = tainted.get(varName);
200
- if (taint && sqlish.has(varName))
201
- report(arg, taint);
202
- }
203
- },
204
- };
205
- },
206
- };
207
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createSqlInjectionRule=createSqlInjectionRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const SQL_KEYWORDS=/\b(?:select|insert|update|delete|drop|alter|truncate|union|from|where|values|set)\b/i;const FUNCTION_TYPES=new Set([ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration,ast_node_types_1.AST_NODE_TYPES.FunctionExpression,ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression]);function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if(FUNCTION_TYPES.has(n.type))return n}return void 0}function callableName(fn){if(fn.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.id)return fn.id.name;const parent=fn.parent;if(parent.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.id.name}if((parent.type===ast_node_types_1.AST_NODE_TYPES.MethodDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.Property)&&parent.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.key.name}return void 0}function staticText(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.raw).join(" ")}if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression){return`${staticText(node.left)} ${staticText(node.right)}`}if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof node.value==="string"){return node.value}return""}function classify(node,requireSqlKeywords){let kind=false;if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){kind="concat"}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length>0){kind="template"}if(kind===false)return false;if(kind==="concat"&&staticText(node).trim()==="")return false;if(requireSqlKeywords&&!SQL_KEYWORDS.test(staticText(node)))return false;return kind}function createSqlInjectionRule(config){const sinks=new Set(config.methods);return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{noUnsafeQuery:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink}),unsafeTemplateLiteral:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){const tainted=new Map;const sqlish=new Set;const wrappers=new Map;const definitions=new Map;const wrapperFns=new Map;const pending=[];const report=(node,kind)=>{context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};return{"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression"(node){const name=callableName(node);if(name!==void 0)definitions.set(name,(definitions.get(name)??0)+1)},VariableDeclarator(node){if(node.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.init){const kind=classify(node.init,config.requireSqlKeywords);if(kind)tainted.set(node.id.name,kind);if(SQL_KEYWORDS.test(staticText(node.init)))sqlish.add(node.id.name)}},AssignmentExpression(node){if(node.operator==="+="&&node.left.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const gate=config.requireSqlKeywords&&!sqlish.has(node.left.name);const kind=classify(node.right,gate);if(kind)tainted.set(node.left.name,kind);if(SQL_KEYWORDS.test(staticText(node.right)))sqlish.add(node.left.name)}},CallExpression(node){const isMember=node.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier;const calleeName=isMember?node.callee.property.name:node.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.callee.name:void 0;if(calleeName===void 0)return;if(!isMember||!sinks.has(calleeName)){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;const kind=classify(arg,true);if(kind)pending.push({name:calleeName,index,arg,kind});else if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier)pending.push({name:calleeName,index,arg,kind:void 0})}return}const queryArg=node.arguments[0];if(!queryArg)return;if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const fn=enclosingFunction(node);const index=fn?.params.findIndex(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name===queryArg.name)??-1;const name=fn&&index>=0?callableName(fn):void 0;if(name!==void 0&&fn){wrappers.set(name,index);wrapperFns.set(fn,name)}}const direct=classify(queryArg,config.requireSqlKeywords);if(direct){report(queryArg,direct);return}if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const taint=tainted.get(queryArg.name);if(taint)report(queryArg,taint)}},"Program:exit"(){const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg,kind}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;if(kind){report(arg,kind);continue}const varName=arg.name;const taint=tainted.get(varName);if(taint&&sqlish.has(varName))report(arg,taint)}}}}}}