@code-sentinel-cli/code-sentinel-cli 3.0.3 → 3.0.4
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/dist/bin.js +1184 -809
- package/dist/builtin-rules/ai/angular/api-service-contract-risk.prompt.md +71 -0
- package/dist/builtin-rules/ai/angular/api-service-contract-risk.rule.yaml +22 -0
- package/dist/builtin-rules/ai/angular/component-state-flow.prompt.md +79 -0
- package/dist/builtin-rules/ai/angular/component-state-flow.rule.yaml +26 -0
- package/dist/builtin-rules/ai/angular/cross-feature-api-drift.prompt.md +70 -0
- package/dist/builtin-rules/ai/angular/cross-feature-api-drift.rule.yaml +42 -0
- package/dist/builtin-rules/ai/angular/cross-feature-route-permission-drift.prompt.md +73 -0
- package/dist/builtin-rules/ai/angular/cross-feature-route-permission-drift.rule.yaml +50 -0
- package/dist/builtin-rules/ai/angular/reactive-form-flow.prompt.md +82 -0
- package/dist/builtin-rules/ai/angular/reactive-form-flow.rule.yaml +23 -0
- package/dist/builtin-rules/ai/angular/route-permission-flow.prompt.md +80 -0
- package/dist/builtin-rules/ai/angular/route-permission-flow.rule.yaml +23 -0
- package/dist/builtin-rules/ai/angular/rxjs-flow-risk.prompt.md +82 -0
- package/dist/builtin-rules/ai/angular/rxjs-flow-risk.rule.yaml +24 -0
- package/dist/builtin-rules/ai/angular/template-contract-risk.prompt.md +80 -0
- package/dist/builtin-rules/ai/angular/template-contract-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/flutter/dynamic-content-risk.prompt.md +55 -0
- package/dist/builtin-rules/ai/flutter/dynamic-content-risk.rule.yaml +18 -0
- package/dist/builtin-rules/ai/flutter/feature-state-flow-risk.prompt.md +58 -0
- package/dist/builtin-rules/ai/flutter/feature-state-flow-risk.rule.yaml +18 -0
- package/dist/builtin-rules/ai/flutter/native-platform-consistency-risk.prompt.md +56 -0
- package/dist/builtin-rules/ai/flutter/native-platform-consistency-risk.rule.yaml +18 -0
- package/dist/builtin-rules/ai/flutter/network-auth-flow-risk.prompt.md +57 -0
- package/dist/builtin-rules/ai/flutter/network-auth-flow-risk.rule.yaml +19 -0
- package/dist/builtin-rules/ai/flutter/ui-responsive-accessibility-risk.prompt.md +60 -0
- package/dist/builtin-rules/ai/flutter/ui-responsive-accessibility-risk.rule.yaml +18 -0
- package/dist/builtin-rules/ai/general/code-clarity.prompt.md +57 -0
- package/dist/builtin-rules/ai/general/code-clarity.rule.yaml +20 -0
- package/dist/builtin-rules/ai/spring-boot/business-invariant-risk.prompt.md +86 -0
- package/dist/builtin-rules/ai/spring-boot/business-invariant-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/cache-consistency-risk.prompt.md +80 -0
- package/dist/builtin-rules/ai/spring-boot/cache-consistency-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/cross-feature-error-handling-drift.prompt.md +76 -0
- package/dist/builtin-rules/ai/spring-boot/cross-feature-error-handling-drift.rule.yaml +63 -0
- package/dist/builtin-rules/ai/spring-boot/cross-feature-transaction-boundary-drift.prompt.md +85 -0
- package/dist/builtin-rules/ai/spring-boot/cross-feature-transaction-boundary-drift.rule.yaml +68 -0
- package/dist/builtin-rules/ai/spring-boot/error-handling-completeness.prompt.md +121 -0
- package/dist/builtin-rules/ai/spring-boot/error-handling-completeness.rule.yaml +84 -0
- package/dist/builtin-rules/ai/spring-boot/event-publishing-risk.prompt.md +80 -0
- package/dist/builtin-rules/ai/spring-boot/event-publishing-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/io-boundary-risk.prompt.md +78 -0
- package/dist/builtin-rules/ai/spring-boot/io-boundary-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/ownership-scope-risk.prompt.md +83 -0
- package/dist/builtin-rules/ai/spring-boot/ownership-scope-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/pagination-risk.prompt.md +81 -0
- package/dist/builtin-rules/ai/spring-boot/pagination-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/security-authorization-risk.prompt.md +75 -0
- package/dist/builtin-rules/ai/spring-boot/security-authorization-risk.rule.yaml +26 -0
- package/dist/builtin-rules/ai/spring-boot/security-mass-assignment-risk.prompt.md +75 -0
- package/dist/builtin-rules/ai/spring-boot/security-mass-assignment-risk.rule.yaml +24 -0
- package/dist/builtin-rules/ai/spring-boot/security-redirect-risk.prompt.md +74 -0
- package/dist/builtin-rules/ai/spring-boot/security-redirect-risk.rule.yaml +24 -0
- package/dist/builtin-rules/ai/spring-boot/security-sensitive-data-risk.prompt.md +75 -0
- package/dist/builtin-rules/ai/spring-boot/security-sensitive-data-risk.rule.yaml +25 -0
- package/dist/builtin-rules/ai/spring-boot/transaction-loop-risk.prompt.md +78 -0
- package/dist/builtin-rules/ai/spring-boot/transaction-loop-risk.rule.yaml +25 -0
- package/dist/builtin-rules/static/angular/component-prefix.rule.js +78 -0
- package/dist/builtin-rules/static/angular/image-control-alt-required.rule.js +105 -0
- package/dist/builtin-rules/static/angular/img-alt-required.rule.js +96 -0
- package/dist/builtin-rules/static/angular/inject-pattern.rule.js +47 -0
- package/dist/builtin-rules/static/angular/lifecycle-method-required.rule.js +59 -0
- package/dist/builtin-rules/static/angular/no-any-type.rule.yaml +55 -0
- package/dist/builtin-rules/static/angular/no-direct-dom-access.rule.js +191 -0
- package/dist/builtin-rules/static/angular/no-dom-xss.rule.js +48 -0
- package/dist/builtin-rules/static/angular/no-leaky-subscription.rule.js +197 -0
- package/dist/builtin-rules/static/angular/no-ngmodule.rule.yaml +44 -0
- package/dist/builtin-rules/static/angular/no-non-null-asserted-optional-chain.rule.js +53 -0
- package/dist/builtin-rules/static/angular/no-uncalled-signal-in-condition.rule.js +112 -0
- package/dist/builtin-rules/static/angular/observable-naming.rule.js +85 -0
- package/dist/builtin-rules/static/angular/onpush-required.rule.js +70 -0
- package/dist/builtin-rules/static/angular/standalone-component-required.rule.js +65 -0
- package/dist/builtin-rules/static/angular/template-dom-xss.rule.js +112 -0
- package/dist/builtin-rules/static/angular/template-syntax-modern.rule.js +154 -0
- package/dist/builtin-rules/static/angular/trackby-required.rule.js +66 -0
- package/dist/builtin-rules/static/flutter/android-exported-component.rule.js +75 -0
- package/dist/builtin-rules/static/flutter/android-manifest-hardening.rule.js +117 -0
- package/dist/builtin-rules/static/flutter/android-sensitive-permissions.rule.js +87 -0
- package/dist/builtin-rules/static/flutter/debug-tooling-release-risk.rule.js +90 -0
- package/dist/builtin-rules/static/flutter/dynamic-archive-risk.rule.js +96 -0
- package/dist/builtin-rules/static/flutter/hive-sensitive-storage.rule.js +77 -0
- package/dist/builtin-rules/static/flutter/ios-privacy-hardening.rule.js +119 -0
- package/dist/builtin-rules/static/flutter/packaged-env-risk.rule.js +95 -0
- package/dist/builtin-rules/static/flutter/sensitive-log-leak.rule.js +94 -0
- package/dist/builtin-rules/static/flutter/webview-bridge-guard.rule.js +102 -0
- package/dist/builtin-rules/static/general/file-too-long.rule.js +41 -0
- package/dist/builtin-rules/static/general/function-too-long.rule.js +72 -0
- package/dist/builtin-rules/static/general/no-circular-import.rule.js +39 -0
- package/dist/builtin-rules/static/general/no-code-injection.rule.js +48 -0
- package/dist/builtin-rules/static/general/no-console-log.rule.js +110 -0
- package/dist/builtin-rules/static/general/no-empty-catch.rule.yaml +56 -0
- package/dist/builtin-rules/static/general/no-hardcoded-secret.rule.js +517 -0
- package/dist/builtin-rules/static/general/no-magic-number.rule.js +78 -0
- package/dist/builtin-rules/static/general/no-open-redirect.rule.js +46 -0
- package/dist/builtin-rules/static/general/no-todo-comment.rule.yaml +30 -0
- package/dist/builtin-rules/static/general/no-unresolved-import-symbol.rule.js +80 -0
- package/dist/builtin-rules/static/general/no-unused-export.rule.js +140 -0
- package/dist/builtin-rules/static/spring-boot/no-autowired-field.rule.js +162 -0
- package/dist/builtin-rules/static/spring-boot/no-business-logic-in-controller.rule.js +94 -0
- package/dist/builtin-rules/static/spring-boot/no-command-injection.rule.js +47 -0
- package/dist/builtin-rules/static/spring-boot/no-entity-in-controller.rule.yaml +52 -0
- package/dist/builtin-rules/static/spring-boot/no-nplus1-query-in-loop.rule.js +106 -0
- package/dist/builtin-rules/static/spring-boot/no-path-traversal.rule.js +47 -0
- package/dist/builtin-rules/static/spring-boot/no-spel-injection.rule.js +41 -0
- package/dist/builtin-rules/static/spring-boot/no-sql-string-concat.rule.js +194 -0
- package/dist/builtin-rules/static/spring-boot/no-ssrf.rule.js +49 -0
- package/dist/builtin-rules/static/spring-boot/no-transactional-self-invocation.rule.js +142 -0
- package/dist/builtin-rules/static/spring-boot/no-unclosed-resource.rule.js +108 -0
- package/dist/builtin-rules/static/spring-boot/no-unsafe-shared-formatter.rule.js +118 -0
- package/dist/builtin-rules/static/spring-boot/repository-naming.rule.yaml +42 -0
- package/dist/builtin-rules/static/spring-boot/restful-naming.rule.yaml +39 -0
- package/dist/builtin-rules/static/spring-boot/transaction-on-service.rule.yaml +58 -0
- package/dist/builtin-rules/static/spring-boot/validate-input.rule.js +123 -0
- package/dist/builtin-rules/vauthz/static/spring-boot/require-authz-check.rule.js +101 -0
- package/dist/index.d.ts +224 -47
- package/dist/index.js +1183 -808
- package/package.json +3 -2
- package/src/migrations/0000_initial.sql +62 -0
- package/src/migrations/0001_add_confidence.sql +7 -0
- package/src/migrations/0002_add_ai_stats.sql +21 -0
- package/src/migrations/0003_add_fix_recipe.sql +29 -0
- package/src/migrations/0004_add_ast_graph.sql +61 -0
- package/src/migrations/0005_add_project_id.sql +1 -0
- package/src/migrations/0006_add_ai_model.sql +5 -0
- package/src/migrations/0006_add_rule_results.sql +1 -0
- package/src/migrations/0007_add_finding_metadata.sql +4 -0
- package/src/migrations/0008_add_coverage.sql +2 -0
- package/src/migrations/0009_run_provenance.sql +5 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// builtin-rules/static/spring-boot/no-path-traversal.rule.ts
|
|
2
|
+
var PATH_TRAVERSAL_SINK_IDS = /* @__PURE__ */ new Set([
|
|
3
|
+
"spring/path-file",
|
|
4
|
+
"spring/path-resource",
|
|
5
|
+
"spring/minio-object"
|
|
6
|
+
]);
|
|
7
|
+
var SINK_LABEL = {
|
|
8
|
+
"spring/path-file": "new File / FileInputStream / \u2026 (m\u1EDF file theo \u0111\u01B0\u1EDDng d\u1EABn)",
|
|
9
|
+
"spring/path-resource": "getResource / getResourceAsStream(...)",
|
|
10
|
+
"spring/minio-object": "MinIO *ObjectArgs.builder().object(...) (kh\xF3a object-store)"
|
|
11
|
+
};
|
|
12
|
+
var rule = {
|
|
13
|
+
id: "spring-boot/no-path-traversal",
|
|
14
|
+
version: 2,
|
|
15
|
+
kind: "static-ts",
|
|
16
|
+
title: "Kh\xF4ng m\u1EDF file theo \u0111\u01B0\u1EDDng d\u1EABn t\u1EEB d\u1EEF li\u1EC7u ch\u01B0a ki\u1EC3m so\xE1t (Path Traversal)",
|
|
17
|
+
description: "D\u1EEF li\u1EC7u t\u1EEB request (@RequestParam, @PathVariable, @RequestBody, @RequestHeader, @RequestPart, HttpServletRequest, \u2026) ch\u1EA3y th\u1EB3ng v\xE0o \u0111\u01B0\u1EDDng d\u1EABn filesystem (`new File(...)`, `new FileInputStream(...)`, `getResource(...)`) ho\u1EB7c kh\xF3a object-store MinIO (`.object(...)`) l\xE0 l\u1ED7 h\u1ED5ng Path Traversal (CWE-22) \u2014 k\u1EBB t\u1EA5n c\xF4ng d\xF9ng `../../../../etc/passwd` \u0111\u1EC3 \u0111\u1ECDc/ghi file ngo\xE0i th\u01B0 m\u1EE5c d\u1EF1 ki\u1EBFn. Rule d\xF9ng taint analysis (Tier-1 li\xEAn th\u1EE7 t\u1EE5c trong c\xF9ng method) \u0111\u1EC3 truy v\u1EBFt lu\u1ED3ng d\u1EEF li\u1EC7u. Kh\u1EAFc ph\u1EE5c: chu\u1EA9n h\xF3a \u0111\u01B0\u1EDDng d\u1EABn (`getCanonicalPath()`) r\u1ED3i ki\u1EC3m tra n\u1EB1m trong th\u01B0 m\u1EE5c g\u1ED1c cho ph\xE9p (allowlist); kh\xF4ng bao gi\u1EDD gh\xE9p \u0111\u01B0\u1EDDng d\u1EABn tr\u1EF1c ti\u1EBFp t\u1EEB input ng\u01B0\u1EDDi d\xF9ng.",
|
|
18
|
+
severity: "error",
|
|
19
|
+
category: "security",
|
|
20
|
+
languages: ["java"],
|
|
21
|
+
scope: "spring-boot",
|
|
22
|
+
requires: "syntactic",
|
|
23
|
+
usesTaint: true,
|
|
24
|
+
baseConfidence: 80,
|
|
25
|
+
check(ctx) {
|
|
26
|
+
const findings = [];
|
|
27
|
+
for (const tf of ctx.taintFindings ?? []) {
|
|
28
|
+
if (!PATH_TRAVERSAL_SINK_IDS.has(tf.sinkId)) continue;
|
|
29
|
+
const sources = [...new Set(tf.sourceIds)].join(", ");
|
|
30
|
+
const laundered = (tf.path?.length ?? 0) > 0;
|
|
31
|
+
const sinkLabel = SINK_LABEL[tf.sinkId] ?? tf.sinkId;
|
|
32
|
+
const trace = laundered ? " (truy v\u1EBFt li\xEAn th\u1EE7 t\u1EE5c)" : "";
|
|
33
|
+
findings.push({
|
|
34
|
+
message: `\u26A0 Path Traversal: d\u1EEF li\u1EC7u ch\u01B0a ki\u1EC3m so\xE1t (${sources}) ch\u1EA3y v\xE0o ${sinkLabel} \u2014 k\u1EBB t\u1EA5n c\xF4ng c\xF3 th\u1EC3 \u0111\u1ECDc/ghi file ngo\xE0i th\u01B0 m\u1EE5c cho ph\xE9p (vd. ../../etc/passwd).${trace}`,
|
|
35
|
+
file: ctx.file.path,
|
|
36
|
+
range: { line: tf.line, column: tf.column ?? 1 },
|
|
37
|
+
confidence: laundered ? 75 : 85,
|
|
38
|
+
suggestion: "Chu\u1EA9n h\xF3a \u0111\u01B0\u1EDDng d\u1EABn b\u1EB1ng `file.getCanonicalPath()` r\u1ED3i ki\u1EC3m tra n\xF3 b\u1EAFt \u0111\u1EA7u b\u1EB1ng th\u01B0 m\u1EE5c g\u1ED1c cho ph\xE9p (allowlist) tr\u01B0\u1EDBc khi m\u1EDF. T\u1ED1t h\u01A1n: map id \u2192 t\xEAn file qua b\u1EA3ng tra c\u1EE9u ph\xEDa server, kh\xF4ng nh\u1EADn \u0111\u01B0\u1EDDng d\u1EABn th\xF4 t\u1EEB ng\u01B0\u1EDDi d\xF9ng."
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return findings;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { rule };
|
|
46
|
+
//# sourceMappingURL=no-path-traversal.rule.js.map
|
|
47
|
+
//# sourceMappingURL=no-path-traversal.rule.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// builtin-rules/static/spring-boot/no-spel-injection.rule.ts
|
|
2
|
+
var SPEL_SINK_IDS = /* @__PURE__ */ new Set(["spring/spel-parse"]);
|
|
3
|
+
var SINK_LABEL = {
|
|
4
|
+
"spring/spel-parse": "ExpressionParser.parseExpression(...)"
|
|
5
|
+
};
|
|
6
|
+
var rule = {
|
|
7
|
+
id: "spring-boot/no-spel-injection",
|
|
8
|
+
version: 1,
|
|
9
|
+
kind: "static-ts",
|
|
10
|
+
title: "Kh\xF4ng parse SpEL t\u1EEB d\u1EEF li\u1EC7u ch\u01B0a ki\u1EC3m so\xE1t (Expression Language Injection)",
|
|
11
|
+
description: "D\u1EEF li\u1EC7u t\u1EEB request (@RequestParam, @PathVariable, @RequestBody, @RequestHeader, HttpServletRequest, \u2026) ch\u1EA3y th\u1EB3ng v\xE0o `ExpressionParser.parseExpression(...)` l\xE0 l\u1ED7 h\u1ED5ng Expression Language (SpEL) Injection (CWE-917) \u2014 bi\u1EC3u th\u1EE9c SpEL do k\u1EBB t\u1EA5n c\xF4ng ki\u1EC3m so\xE1t c\xF3 th\u1EC3 g\u1ECDi method t\xF9y \xFD (`T(java.lang.Runtime).getRuntime().exec(...)`) \u2192 RCE. Rule d\xF9ng taint analysis (Tier-1 li\xEAn th\u1EE7 t\u1EE5c trong c\xF9ng method) \u0111\u1EC3 truy v\u1EBFt lu\u1ED3ng d\u1EEF li\u1EC7u. Kh\u1EAFc ph\u1EE5c: kh\xF4ng bao gi\u1EDD parse SpEL t\u1EEB input ng\u01B0\u1EDDi d\xF9ng; n\u1EBFu c\u1EA7n bi\u1EC3u th\u1EE9c \u0111\u1ED9ng, d\xF9ng allowlist bi\u1EC3u th\u1EE9c c\u1ED1 \u0111\u1ECBnh, ho\u1EB7c `SimpleEvaluationContext` (ch\u1EB7n type reference / method call nguy hi\u1EC3m) thay cho `StandardEvaluationContext`.",
|
|
12
|
+
severity: "error",
|
|
13
|
+
category: "security",
|
|
14
|
+
languages: ["java"],
|
|
15
|
+
scope: "spring-boot",
|
|
16
|
+
requires: "syntactic",
|
|
17
|
+
usesTaint: true,
|
|
18
|
+
baseConfidence: 80,
|
|
19
|
+
check(ctx) {
|
|
20
|
+
const findings = [];
|
|
21
|
+
for (const tf of ctx.taintFindings ?? []) {
|
|
22
|
+
if (!SPEL_SINK_IDS.has(tf.sinkId)) continue;
|
|
23
|
+
const sources = [...new Set(tf.sourceIds)].join(", ");
|
|
24
|
+
const laundered = (tf.path?.length ?? 0) > 0;
|
|
25
|
+
const sinkLabel = SINK_LABEL[tf.sinkId] ?? tf.sinkId;
|
|
26
|
+
const trace = laundered ? " (truy v\u1EBFt li\xEAn th\u1EE7 t\u1EE5c)" : "";
|
|
27
|
+
findings.push({
|
|
28
|
+
message: `\u26A0 SpEL Injection: d\u1EEF li\u1EC7u ch\u01B0a ki\u1EC3m so\xE1t (${sources}) ch\u1EA3y v\xE0o ${sinkLabel} \u2014 k\u1EBB t\u1EA5n c\xF4ng c\xF3 th\u1EC3 ch\u1EA1y m\xE3 t\xF9y \xFD qua bi\u1EC3u th\u1EE9c SpEL (RCE).${trace}`,
|
|
29
|
+
file: ctx.file.path,
|
|
30
|
+
range: { line: tf.line, column: tf.column ?? 1 },
|
|
31
|
+
confidence: laundered ? 75 : 85,
|
|
32
|
+
suggestion: "Kh\xF4ng parse SpEL t\u1EEB input ng\u01B0\u1EDDi d\xF9ng. N\u1EBFu c\u1EA7n bi\u1EC3u th\u1EE9c \u0111\u1ED9ng: d\xF9ng allowlist bi\u1EC3u th\u1EE9c c\u1ED1 \u0111\u1ECBnh, ho\u1EB7c \u0111\xE1nh gi\xE1 v\u1EDBi `SimpleEvaluationContext` (ch\u1EB7n type reference / g\u1ECDi method nguy hi\u1EC3m) thay cho `StandardEvaluationContext`. Tuy\u1EC7t \u0111\u1ED1i kh\xF4ng d\u1EF1ng bi\u1EC3u th\u1EE9c t\u1EEB gi\xE1 tr\u1ECB do ng\u01B0\u1EDDi d\xF9ng nh\u1EADp."
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return findings;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { rule };
|
|
40
|
+
//# sourceMappingURL=no-spel-injection.rule.js.map
|
|
41
|
+
//# sourceMappingURL=no-spel-injection.rule.js.map
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { asTreeSitterTree } from '@code-sentinel/parsers';
|
|
2
|
+
|
|
3
|
+
// builtin-rules/static/spring-boot/no-sql-string-concat.rule.ts
|
|
4
|
+
var SQL_KEYWORD = /\b(SELECT|INSERT|UPDATE|DELETE|FROM|WHERE)\b/;
|
|
5
|
+
var SQL_SINK_CALLEES = /* @__PURE__ */ new Set([
|
|
6
|
+
"createQuery",
|
|
7
|
+
"createNativeQuery",
|
|
8
|
+
"query",
|
|
9
|
+
"queryForObject",
|
|
10
|
+
"queryForList",
|
|
11
|
+
"queryForMap",
|
|
12
|
+
"queryForRowSet",
|
|
13
|
+
"update",
|
|
14
|
+
"execute",
|
|
15
|
+
"batchUpdate"
|
|
16
|
+
]);
|
|
17
|
+
var SANITIZER_CALLEES = /* @__PURE__ */ new Set([
|
|
18
|
+
"parseLong",
|
|
19
|
+
"parseInt",
|
|
20
|
+
"parseDouble",
|
|
21
|
+
"parseFloat",
|
|
22
|
+
"parseShort",
|
|
23
|
+
"parseByte",
|
|
24
|
+
"parseBoolean"
|
|
25
|
+
]);
|
|
26
|
+
var LITERAL_TYPES = /* @__PURE__ */ new Set([
|
|
27
|
+
"string_literal",
|
|
28
|
+
"character_literal",
|
|
29
|
+
"decimal_integer_literal",
|
|
30
|
+
"hex_integer_literal",
|
|
31
|
+
"octal_integer_literal",
|
|
32
|
+
"binary_integer_literal",
|
|
33
|
+
"decimal_floating_point_literal",
|
|
34
|
+
"hex_floating_point_literal",
|
|
35
|
+
"true",
|
|
36
|
+
"false",
|
|
37
|
+
"null_literal"
|
|
38
|
+
]);
|
|
39
|
+
function* walk(node, type) {
|
|
40
|
+
const stack = [node];
|
|
41
|
+
while (stack.length > 0) {
|
|
42
|
+
const cur = stack.pop();
|
|
43
|
+
if (!cur) continue;
|
|
44
|
+
if (cur.type === type) yield cur;
|
|
45
|
+
for (let i = cur.namedChildCount - 1; i >= 0; i--) {
|
|
46
|
+
const child = cur.namedChild(i);
|
|
47
|
+
if (child) stack.push(child);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var operatorText = (node) => node.childForFieldName("operator")?.text;
|
|
52
|
+
var isPlusConcat = (node) => node.type === "binary_expression" && operatorText(node) === "+";
|
|
53
|
+
var declaredName = (decl) => {
|
|
54
|
+
const nameNode = decl.childForFieldName("name");
|
|
55
|
+
if (nameNode?.type === "identifier") return nameNode.text;
|
|
56
|
+
for (const child of namedChildren(decl)) {
|
|
57
|
+
if (child.type === "identifier") return child.text;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
};
|
|
61
|
+
function namedChildren(node) {
|
|
62
|
+
const out = [];
|
|
63
|
+
for (let i = 0; i < node.namedChildCount; i++) {
|
|
64
|
+
const child = node.namedChild(i);
|
|
65
|
+
if (child) out.push(child);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
var collectStaticFinalFields = (root) => {
|
|
70
|
+
const names = /* @__PURE__ */ new Set();
|
|
71
|
+
for (const field of walk(root, "field_declaration")) {
|
|
72
|
+
let isConst = false;
|
|
73
|
+
for (const child of namedChildren(field)) {
|
|
74
|
+
if (child.type !== "modifiers") continue;
|
|
75
|
+
const text = child.text;
|
|
76
|
+
if (/\bstatic\b/.test(text) && /\bfinal\b/.test(text)) isConst = true;
|
|
77
|
+
}
|
|
78
|
+
if (!isConst) continue;
|
|
79
|
+
for (const decl of namedChildren(field)) {
|
|
80
|
+
if (decl.type !== "variable_declarator") continue;
|
|
81
|
+
const name = declaredName(decl);
|
|
82
|
+
if (name) names.add(name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return names;
|
|
86
|
+
};
|
|
87
|
+
var flattenConcat = (node) => {
|
|
88
|
+
if (isPlusConcat(node)) {
|
|
89
|
+
const left = node.childForFieldName("left");
|
|
90
|
+
const right = node.childForFieldName("right");
|
|
91
|
+
return [...left ? flattenConcat(left) : [], ...right ? flattenConcat(right) : []];
|
|
92
|
+
}
|
|
93
|
+
if (node.type === "parenthesized_expression") {
|
|
94
|
+
const inner = node.namedChild(0);
|
|
95
|
+
return inner ? flattenConcat(inner) : [node];
|
|
96
|
+
}
|
|
97
|
+
return [node];
|
|
98
|
+
};
|
|
99
|
+
var isDynamicOperand = (op, staticFinals) => {
|
|
100
|
+
if (LITERAL_TYPES.has(op.type)) return false;
|
|
101
|
+
if (op.type === "parenthesized_expression") {
|
|
102
|
+
const inner = op.namedChild(0);
|
|
103
|
+
return inner ? isDynamicOperand(inner, staticFinals) : false;
|
|
104
|
+
}
|
|
105
|
+
if (op.type === "identifier") return !staticFinals.has(op.text);
|
|
106
|
+
if (op.type === "field_access") {
|
|
107
|
+
const field = op.childForFieldName("field")?.text;
|
|
108
|
+
return !(field && staticFinals.has(field));
|
|
109
|
+
}
|
|
110
|
+
if (op.type === "method_invocation") {
|
|
111
|
+
const name = op.childForFieldName("name")?.text;
|
|
112
|
+
return !(name && SANITIZER_CALLEES.has(name));
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
};
|
|
116
|
+
var enclosingSqlSinkLine = (node) => {
|
|
117
|
+
let cur = node.parent;
|
|
118
|
+
while (cur) {
|
|
119
|
+
if (cur.type === "method_invocation") {
|
|
120
|
+
const name = cur.childForFieldName("name")?.text;
|
|
121
|
+
if (name && SQL_SINK_CALLEES.has(name)) return cur.startPosition.row + 1;
|
|
122
|
+
}
|
|
123
|
+
cur = cur.parent;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
};
|
|
127
|
+
var isConcatRoot = (node) => {
|
|
128
|
+
let parent = node.parent;
|
|
129
|
+
while (parent && parent.type === "parenthesized_expression") parent = parent.parent;
|
|
130
|
+
return !(parent && isPlusConcat(parent));
|
|
131
|
+
};
|
|
132
|
+
var rule = {
|
|
133
|
+
id: "spring-boot/no-sql-string-concat",
|
|
134
|
+
version: 2,
|
|
135
|
+
kind: "static-ts",
|
|
136
|
+
title: "Kh\xF4ng n\u1ED1i SQL b\u1EB1ng `+` \u2014 d\xF9ng prepared statement / named parameter",
|
|
137
|
+
description: "N\u1ED1i chu\u1ED7i SQL b\u1EB1ng `+` l\xE0 pattern \u0111i\u1EC3n h\xECnh c\u1EE7a SQL Injection. v2 d\xF9ng taint analysis: n\u1ED1i c\xF3 gi\xE1 tr\u1ECB t\u1EEB request (taint-confirmed) \u2192 error tin c\u1EADy cao; n\u1ED1i to\xE0n h\u1EB1ng s\u1ED1 (literal / `static final`) \u2192 b\u1ECF qua; c\xF2n l\u1EA1i gi\u1EEF c\u1EA3nh b\xE1o nh\u01B0 v1. D\xF9ng JdbcTemplate `?` + args, JPQL `:param` + `setParameter(...)`, ho\u1EB7c Spring Data query methods.",
|
|
138
|
+
severity: "error",
|
|
139
|
+
category: "security",
|
|
140
|
+
languages: ["java"],
|
|
141
|
+
scope: "spring-boot",
|
|
142
|
+
requires: "syntactic",
|
|
143
|
+
usesTaint: true,
|
|
144
|
+
baseConfidence: 70,
|
|
145
|
+
check(ctx) {
|
|
146
|
+
const tree = asTreeSitterTree(ctx.parsed);
|
|
147
|
+
const root = tree.rootNode;
|
|
148
|
+
const staticFinals = collectStaticFinalFields(root);
|
|
149
|
+
const confirmedSinkLines = new Set((ctx.taintFindings ?? []).map((f) => f.line));
|
|
150
|
+
const findings = [];
|
|
151
|
+
for (const concat of walk(root, "binary_expression")) {
|
|
152
|
+
if (!isPlusConcat(concat) || !isConcatRoot(concat)) continue;
|
|
153
|
+
const operands = flattenConcat(concat);
|
|
154
|
+
const hasSqlKeyword = operands.some(
|
|
155
|
+
(op) => op.type === "string_literal" && SQL_KEYWORD.test(op.text)
|
|
156
|
+
);
|
|
157
|
+
if (!hasSqlKeyword) continue;
|
|
158
|
+
const dynamicOperands = operands.filter((op) => isDynamicOperand(op, staticFinals));
|
|
159
|
+
if (dynamicOperands.length === 0) continue;
|
|
160
|
+
const range = {
|
|
161
|
+
line: concat.startPosition.row + 1,
|
|
162
|
+
column: concat.startPosition.column + 1
|
|
163
|
+
};
|
|
164
|
+
const sinkLine = enclosingSqlSinkLine(concat);
|
|
165
|
+
const confirmed = sinkLine !== null && confirmedSinkLines.has(sinkLine);
|
|
166
|
+
if (confirmed) {
|
|
167
|
+
const sources = [
|
|
168
|
+
...new Set(
|
|
169
|
+
(ctx.taintFindings ?? []).filter((f) => f.line === sinkLine).flatMap((f) => f.sourceIds)
|
|
170
|
+
)
|
|
171
|
+
].join(", ");
|
|
172
|
+
findings.push({
|
|
173
|
+
message: `\u26A0 SQL Injection x\xE1c nh\u1EADn: gi\xE1 tr\u1ECB t\u1EEB request (${sources}) n\u1ED1i th\u1EB3ng v\xE0o c\xE2u SQL \u2014 d\xF9ng prepared statement / named parameter.`,
|
|
174
|
+
file: ctx.file.path,
|
|
175
|
+
range,
|
|
176
|
+
confidence: 90,
|
|
177
|
+
suggestion: "Bind tham s\u1ED1: JdbcTemplate `?` + args, ho\u1EB7c JPQL `:param` + `setParameter(...)`."
|
|
178
|
+
});
|
|
179
|
+
} else {
|
|
180
|
+
findings.push({
|
|
181
|
+
message: "C\xF3 d\u1EA5u hi\u1EC7u n\u1ED1i chu\u1ED7i SQL \u2014 risk SQL Injection. D\xF9ng prepared statement / named parameter.",
|
|
182
|
+
file: ctx.file.path,
|
|
183
|
+
range,
|
|
184
|
+
suggestion: "JdbcTemplate `?` placeholders + args, ho\u1EB7c JPQL `:param` + `setParameter(...)`, ho\u1EB7c Spring Data query methods."
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return findings;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export { rule };
|
|
193
|
+
//# sourceMappingURL=no-sql-string-concat.rule.js.map
|
|
194
|
+
//# sourceMappingURL=no-sql-string-concat.rule.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// builtin-rules/static/spring-boot/no-ssrf.rule.ts
|
|
2
|
+
var SSRF_SINK_IDS = /* @__PURE__ */ new Set([
|
|
3
|
+
"spring/rest-template-ssrf",
|
|
4
|
+
"spring/web-client-ssrf",
|
|
5
|
+
"spring/url-ssrf",
|
|
6
|
+
"spring/okhttp-url"
|
|
7
|
+
]);
|
|
8
|
+
var SINK_LABEL = {
|
|
9
|
+
"spring/rest-template-ssrf": "RestTemplate (getForObject / exchange / \u2026)",
|
|
10
|
+
"spring/web-client-ssrf": "WebClient.uri(...)",
|
|
11
|
+
"spring/url-ssrf": "new URL(...) / new URI(...)",
|
|
12
|
+
"spring/okhttp-url": "OkHttp Request.Builder.url(...)"
|
|
13
|
+
};
|
|
14
|
+
var rule = {
|
|
15
|
+
id: "spring-boot/no-ssrf",
|
|
16
|
+
version: 3,
|
|
17
|
+
kind: "static-ts",
|
|
18
|
+
title: "Kh\xF4ng g\u1ECDi HTTP ra ngo\xE0i b\u1EB1ng URL t\u1EEB d\u1EEF li\u1EC7u ch\u01B0a ki\u1EC3m so\xE1t (SSRF)",
|
|
19
|
+
description: "D\u1EEF li\u1EC7u t\u1EEB request (@RequestParam, @PathVariable, @RequestBody, @RequestHeader, HttpServletRequest, \u2026) ch\u1EA3y th\u1EB3ng v\xE0o URL c\u1EE7a m\u1ED9t l\u1EDDi g\u1ECDi HTTP ra ngo\xE0i (RestTemplate `getForObject`/`exchange`/\u2026, WebClient `.uri(...)`, ho\u1EB7c OkHttp `Request.Builder.url(...)`) l\xE0 l\u1ED7 h\u1ED5ng Server-Side Request Forgery (CWE-918) \u2014 k\u1EBB t\u1EA5n c\xF4ng \u0111i\u1EC1u khi\u1EC3n server g\u1ECDi t\u1EDBi \u0111\u1ECBa ch\u1EC9 n\u1ED9i b\u1ED9 / metadata cloud (vd. 169.254.169.254) ho\u1EB7c d\u1ECBch v\u1EE5 n\u1ED9i b\u1ED9 kh\xF4ng \u0111\u01B0\u1EE3c ph\xE9p truy c\u1EADp t\u1EEB b\xEAn ngo\xE0i. Rule d\xF9ng taint analysis (Tier-1 li\xEAn th\u1EE7 t\u1EE5c trong c\xF9ng method) \u0111\u1EC3 truy v\u1EBFt lu\u1ED3ng d\u1EEF li\u1EC7u. Kh\u1EAFc ph\u1EE5c: validate URL b\u1EB1ng allowlist host/scheme c\u1ED1 \u0111\u1ECBnh; kh\xF4ng bao gi\u1EDD gh\xE9p URL \u0111\xEDch tr\u1EF1c ti\u1EBFp t\u1EEB gi\xE1 tr\u1ECB do ng\u01B0\u1EDDi d\xF9ng nh\u1EADp.",
|
|
20
|
+
severity: "error",
|
|
21
|
+
category: "security",
|
|
22
|
+
languages: ["java"],
|
|
23
|
+
scope: "spring-boot",
|
|
24
|
+
requires: "syntactic",
|
|
25
|
+
usesTaint: true,
|
|
26
|
+
baseConfidence: 80,
|
|
27
|
+
check(ctx) {
|
|
28
|
+
const findings = [];
|
|
29
|
+
for (const tf of ctx.taintFindings ?? []) {
|
|
30
|
+
if (!SSRF_SINK_IDS.has(tf.sinkId)) continue;
|
|
31
|
+
const sources = [...new Set(tf.sourceIds)].join(", ");
|
|
32
|
+
const laundered = (tf.path?.length ?? 0) > 0;
|
|
33
|
+
const sinkLabel = SINK_LABEL[tf.sinkId] ?? tf.sinkId;
|
|
34
|
+
const trace = laundered ? " (truy v\u1EBFt li\xEAn th\u1EE7 t\u1EE5c)" : "";
|
|
35
|
+
findings.push({
|
|
36
|
+
message: `\u26A0 SSRF: d\u1EEF li\u1EC7u ch\u01B0a ki\u1EC3m so\xE1t (${sources}) ch\u1EA3y v\xE0o URL c\u1EE7a ${sinkLabel} \u2014 k\u1EBB t\u1EA5n c\xF4ng c\xF3 th\u1EC3 \xE9p server g\u1ECDi t\u1EDBi \u0111\u1ECBa ch\u1EC9 n\u1ED9i b\u1ED9 / metadata cloud.${trace}`,
|
|
37
|
+
file: ctx.file.path,
|
|
38
|
+
range: { line: tf.line, column: tf.column ?? 1 },
|
|
39
|
+
confidence: laundered ? 75 : 85,
|
|
40
|
+
suggestion: "Validate URL \u0111\xEDch b\u1EB1ng allowlist host/scheme c\u1ED1 \u0111\u1ECBnh tr\u01B0\u1EDBc khi g\u1ECDi (vd. ch\u1EC9 cho ph\xE9p domain n\u1ED9i b\u1ED9 \u0111\xE3 duy\u1EC7t). Tuy\u1EC7t \u0111\u1ED1i kh\xF4ng d\u1EF1ng URL \u0111\xEDch t\u1EEB gi\xE1 tr\u1ECB do ng\u01B0\u1EDDi d\xF9ng nh\u1EADp; n\u1EBFu c\u1EA7n proxy theo id, map id \u2192 URL qua m\u1ED9t b\u1EA3ng tra c\u1EE9u ph\xEDa server."
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return findings;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { rule };
|
|
48
|
+
//# sourceMappingURL=no-ssrf.rule.js.map
|
|
49
|
+
//# sourceMappingURL=no-ssrf.rule.js.map
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { asTreeSitterTree } from '@code-sentinel/parsers';
|
|
2
|
+
|
|
3
|
+
// builtin-rules/static/spring-boot/no-transactional-self-invocation.rule.ts
|
|
4
|
+
var TX_ANNOTATION = "Transactional";
|
|
5
|
+
var SCOPE_BOUNDARIES = /* @__PURE__ */ new Set([
|
|
6
|
+
"class_declaration",
|
|
7
|
+
"interface_declaration",
|
|
8
|
+
"enum_declaration",
|
|
9
|
+
"record_declaration",
|
|
10
|
+
"annotation_type_declaration",
|
|
11
|
+
"class_body"
|
|
12
|
+
// anonymous class body (direct child of object_creation_expression)
|
|
13
|
+
]);
|
|
14
|
+
function* walk(node, type) {
|
|
15
|
+
const stack = [node];
|
|
16
|
+
while (stack.length > 0) {
|
|
17
|
+
const cur = stack.pop();
|
|
18
|
+
if (!cur) continue;
|
|
19
|
+
if (cur.type === type) yield cur;
|
|
20
|
+
for (let i = cur.namedChildCount - 1; i >= 0; i--) {
|
|
21
|
+
const child = cur.namedChild(i);
|
|
22
|
+
if (child) stack.push(child);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function modifiersOf(decl) {
|
|
27
|
+
for (let i = 0; i < decl.namedChildCount; i++) {
|
|
28
|
+
const c = decl.namedChild(i);
|
|
29
|
+
if (c?.type === "modifiers") return c;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
function annotationSimpleName(ann) {
|
|
34
|
+
const name = ann.childForFieldName("name");
|
|
35
|
+
if (!name) return null;
|
|
36
|
+
const text = name.text;
|
|
37
|
+
const dot = text.lastIndexOf(".");
|
|
38
|
+
return dot === -1 ? text : text.slice(dot + 1);
|
|
39
|
+
}
|
|
40
|
+
function hasTransactional(decl) {
|
|
41
|
+
const mods = modifiersOf(decl);
|
|
42
|
+
if (!mods) return false;
|
|
43
|
+
for (let i = 0; i < mods.namedChildCount; i++) {
|
|
44
|
+
const m = mods.namedChild(i);
|
|
45
|
+
if (m && (m.type === "annotation" || m.type === "marker_annotation")) {
|
|
46
|
+
if (annotationSimpleName(m) === TX_ANNOTATION) return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
function classBodyOf(classDecl) {
|
|
52
|
+
const byField = classDecl.childForFieldName("body");
|
|
53
|
+
if (byField) return byField;
|
|
54
|
+
for (let i = 0; i < classDecl.namedChildCount; i++) {
|
|
55
|
+
const c = classDecl.namedChild(i);
|
|
56
|
+
if (c?.type === "class_body") return c;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
function directMethods(classBody) {
|
|
61
|
+
const out = [];
|
|
62
|
+
for (let i = 0; i < classBody.namedChildCount; i++) {
|
|
63
|
+
const c = classBody.namedChild(i);
|
|
64
|
+
if (c?.type === "method_declaration") out.push(c);
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function isSelfInvocation(call) {
|
|
69
|
+
const obj = call.childForFieldName("object");
|
|
70
|
+
if (!obj) return true;
|
|
71
|
+
return obj.type === "this";
|
|
72
|
+
}
|
|
73
|
+
function collectSelfCalls(body, txMethods) {
|
|
74
|
+
const hits = [];
|
|
75
|
+
const stack = [body];
|
|
76
|
+
while (stack.length > 0) {
|
|
77
|
+
const cur = stack.pop();
|
|
78
|
+
if (!cur) continue;
|
|
79
|
+
if (cur.type === "method_invocation" && isSelfInvocation(cur)) {
|
|
80
|
+
const name = cur.childForFieldName("name")?.text;
|
|
81
|
+
if (name && txMethods.has(name)) hits.push(cur);
|
|
82
|
+
}
|
|
83
|
+
for (let i = cur.namedChildCount - 1; i >= 0; i--) {
|
|
84
|
+
const child = cur.namedChild(i);
|
|
85
|
+
if (child && !SCOPE_BOUNDARIES.has(child.type)) stack.push(child);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return hits;
|
|
89
|
+
}
|
|
90
|
+
var rule = {
|
|
91
|
+
id: "spring-boot/no-transactional-self-invocation",
|
|
92
|
+
version: 1,
|
|
93
|
+
kind: "static-ts",
|
|
94
|
+
title: "G\u1ECDi @Transactional c\xF9ng l\u1EDBp \u2014 proxy AOP b\u1ECB b\u1ECF qua",
|
|
95
|
+
description: "G\u1ECDi m\u1ED9t ph\u01B0\u01A1ng th\u1EE9c `@Transactional` t\u1EEB ch\xEDnh trong l\u1EDBp \u0111\xF3 (self-invocation: `tx()` ho\u1EB7c `this.tx()`) kh\xF4ng \u0111i qua proxy AOP c\u1EE7a Spring, n\xEAn annotation b\u1ECB b\u1ECF qua \xE2m th\u1EA7m: kh\xF4ng m\u1EDF giao d\u1ECBch, kh\xF4ng rollback khi l\u1ED7i. T\xE1ch ph\u01B0\u01A1ng th\u1EE9c giao d\u1ECBch sang m\u1ED9t bean kh\xE1c r\u1ED3i g\u1ECDi qua bean \u0111\xF3, ho\u1EB7c self-inject proxy (`@Autowired private OrderService self;` r\u1ED3i `self.tx()`), ho\u1EB7c d\xF9ng `TransactionTemplate` cho kh\u1ED1i c\u1EA7n giao d\u1ECBch.",
|
|
96
|
+
severity: "warning",
|
|
97
|
+
category: "correctness",
|
|
98
|
+
languages: ["java"],
|
|
99
|
+
scope: "spring-boot",
|
|
100
|
+
requires: "syntactic",
|
|
101
|
+
baseConfidence: 80,
|
|
102
|
+
check(ctx) {
|
|
103
|
+
const tree = asTreeSitterTree(ctx.parsed);
|
|
104
|
+
const findings = [];
|
|
105
|
+
for (const classDecl of walk(tree.rootNode, "class_declaration")) {
|
|
106
|
+
if (hasTransactional(classDecl)) continue;
|
|
107
|
+
const body = classBodyOf(classDecl);
|
|
108
|
+
if (!body) continue;
|
|
109
|
+
const methods = directMethods(body);
|
|
110
|
+
const txMethods = /* @__PURE__ */ new Set();
|
|
111
|
+
for (const m of methods) {
|
|
112
|
+
if (hasTransactional(m)) {
|
|
113
|
+
const name = m.childForFieldName("name")?.text;
|
|
114
|
+
if (name) txMethods.add(name);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (txMethods.size === 0) continue;
|
|
118
|
+
for (const caller of methods) {
|
|
119
|
+
if (hasTransactional(caller)) continue;
|
|
120
|
+
const callerBody = caller.childForFieldName("body");
|
|
121
|
+
if (!callerBody) continue;
|
|
122
|
+
for (const call of collectSelfCalls(callerBody, txMethods)) {
|
|
123
|
+
const method = call.childForFieldName("name")?.text ?? "";
|
|
124
|
+
findings.push({
|
|
125
|
+
message: `G\u1ECDi \`${method}(...)\` l\xE0 self-invocation ph\u01B0\u01A1ng th\u1EE9c \`@Transactional\` c\xF9ng l\u1EDBp \u2014 proxy AOP b\u1ECB b\u1ECF qua n\xEAn giao d\u1ECBch kh\xF4ng \u0111\u01B0\u1EE3c m\u1EDF (kh\xF4ng rollback khi l\u1ED7i).`,
|
|
126
|
+
file: ctx.file.path,
|
|
127
|
+
range: {
|
|
128
|
+
line: call.startPosition.row + 1,
|
|
129
|
+
column: call.startPosition.column + 1
|
|
130
|
+
},
|
|
131
|
+
suggestion: "T\xE1ch ph\u01B0\u01A1ng th\u1EE9c giao d\u1ECBch sang bean kh\xE1c r\u1ED3i g\u1ECDi qua bean \u0111\xF3, ho\u1EB7c self-inject proxy (`self.tx()`), ho\u1EB7c d\xF9ng `TransactionTemplate`."
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return findings;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export { rule };
|
|
141
|
+
//# sourceMappingURL=no-transactional-self-invocation.rule.js.map
|
|
142
|
+
//# sourceMappingURL=no-transactional-self-invocation.rule.js.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { asTreeSitterTree } from '@code-sentinel/parsers';
|
|
2
|
+
|
|
3
|
+
// builtin-rules/static/spring-boot/no-unclosed-resource.rule.ts
|
|
4
|
+
var RESOURCE_TYPES = /* @__PURE__ */ new Set([
|
|
5
|
+
"FileInputStream",
|
|
6
|
+
"FileOutputStream",
|
|
7
|
+
"FileReader",
|
|
8
|
+
"FileWriter",
|
|
9
|
+
"RandomAccessFile",
|
|
10
|
+
"Socket",
|
|
11
|
+
"ServerSocket",
|
|
12
|
+
"DatagramSocket",
|
|
13
|
+
"ZipFile",
|
|
14
|
+
"JarFile"
|
|
15
|
+
]);
|
|
16
|
+
var METHOD_SCOPES = /* @__PURE__ */ new Set([
|
|
17
|
+
"method_declaration",
|
|
18
|
+
"constructor_declaration"
|
|
19
|
+
]);
|
|
20
|
+
function* walk(node, type) {
|
|
21
|
+
const stack = [node];
|
|
22
|
+
while (stack.length > 0) {
|
|
23
|
+
const cur = stack.pop();
|
|
24
|
+
if (!cur) continue;
|
|
25
|
+
if (cur.type === type) yield cur;
|
|
26
|
+
for (let i = cur.namedChildCount - 1; i >= 0; i--) {
|
|
27
|
+
const child = cur.namedChild(i);
|
|
28
|
+
if (child) stack.push(child);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
var sameNode = (a, b) => a !== null && b !== null && a.startIndex === b.startIndex && a.endIndex === b.endIndex;
|
|
33
|
+
function simpleTypeName(text) {
|
|
34
|
+
const noGeneric = text.split("<")[0] ?? text;
|
|
35
|
+
const segments = noGeneric.split(".");
|
|
36
|
+
return (segments[segments.length - 1] ?? noGeneric).trim();
|
|
37
|
+
}
|
|
38
|
+
function enclosingMethod(node) {
|
|
39
|
+
let cur = node.parent;
|
|
40
|
+
while (cur) {
|
|
41
|
+
if (METHOD_SCOPES.has(cur.type)) return cur;
|
|
42
|
+
cur = cur.parent;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
function isManagedUsage(id) {
|
|
47
|
+
const p = id.parent;
|
|
48
|
+
if (!p) return false;
|
|
49
|
+
if (p.type === "return_statement") return true;
|
|
50
|
+
if (p.type === "argument_list") return true;
|
|
51
|
+
if (p.type === "assignment_expression") return true;
|
|
52
|
+
if (p.type === "variable_declarator" && sameNode(p.childForFieldName("value"), id)) return true;
|
|
53
|
+
if (p.type === "method_invocation" && sameNode(p.childForFieldName("object"), id) && p.childForFieldName("name")?.text === "close") {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function isManagedInMethod(method, name) {
|
|
59
|
+
for (const id of walk(method, "identifier")) {
|
|
60
|
+
if (id.text === name && isManagedUsage(id)) return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
var rule = {
|
|
65
|
+
id: "spring-boot/no-unclosed-resource",
|
|
66
|
+
version: 1,
|
|
67
|
+
kind: "static-ts",
|
|
68
|
+
title: "Resource m\u1EDF nh\u01B0ng kh\xF4ng \u0111\xF3ng \u2014 r\xF2 r\u1EC9 file handle/socket",
|
|
69
|
+
description: "M\u1ED9t resource gi\u1EEF handle OS/m\u1EA1ng (`new FileInputStream`/`new Socket`/\u2026) \u0111\u01B0\u1EE3c g\xE1n v\xE0o bi\u1EBFn c\u1EE5c b\u1ED9 nh\u01B0ng kh\xF4ng \u0111\u01B0\u1EE3c \u0111\xF3ng, kh\xF4ng return, kh\xF4ng truy\u1EC1n \u0111i \u2192 r\xF2 r\u1EC9 file descriptor / socket. D\u01B0\u1EDBi t\u1EA3i cao (endpoint b\u1EADn, batch job) s\u1EBD c\u1EA1n file descriptor / connection pool v\xE0 l\xE0m s\u1EADp service. D\xF9ng try-with-resources \u0111\u1EC3 \u0111\xF3ng t\u1EF1 \u0111\u1ED9ng k\u1EC3 c\u1EA3 khi c\xF3 exception: `try (var in = new FileInputStream(f)) { ... }`.",
|
|
70
|
+
severity: "warning",
|
|
71
|
+
category: "correctness",
|
|
72
|
+
languages: ["java"],
|
|
73
|
+
scope: "spring-boot",
|
|
74
|
+
requires: "syntactic",
|
|
75
|
+
baseConfidence: 75,
|
|
76
|
+
check(ctx) {
|
|
77
|
+
const tree = asTreeSitterTree(ctx.parsed);
|
|
78
|
+
const findings = [];
|
|
79
|
+
for (const declarator of walk(tree.rootNode, "variable_declarator")) {
|
|
80
|
+
if (declarator.parent?.type !== "local_variable_declaration") continue;
|
|
81
|
+
const value = declarator.childForFieldName("value");
|
|
82
|
+
if (!value || value.type !== "object_creation_expression") continue;
|
|
83
|
+
const typeText = value.childForFieldName("type")?.text;
|
|
84
|
+
if (!typeText) continue;
|
|
85
|
+
const typeName = simpleTypeName(typeText);
|
|
86
|
+
if (!RESOURCE_TYPES.has(typeName)) continue;
|
|
87
|
+
const name = declarator.childForFieldName("name")?.text;
|
|
88
|
+
if (!name) continue;
|
|
89
|
+
const method = enclosingMethod(declarator);
|
|
90
|
+
if (!method) continue;
|
|
91
|
+
if (isManagedInMethod(method, name)) continue;
|
|
92
|
+
findings.push({
|
|
93
|
+
message: `Resource \`${name}\` (\`${typeName}\`) \u0111\u01B0\u1EE3c m\u1EDF nh\u01B0ng kh\xF4ng th\u1EA5y \u0111\xF3ng/return/truy\u1EC1n \u0111i trong method \u2014 r\xF2 r\u1EC9 file handle/socket.`,
|
|
94
|
+
file: ctx.file.path,
|
|
95
|
+
range: {
|
|
96
|
+
line: value.startPosition.row + 1,
|
|
97
|
+
column: value.startPosition.column + 1
|
|
98
|
+
},
|
|
99
|
+
suggestion: `D\xF9ng try-with-resources \u0111\u1EC3 resource t\u1EF1 \u0111\xF3ng k\u1EC3 c\u1EA3 khi c\xF3 exception: \`try (${typeName} ${name} = new ...) { ... }\`, ho\u1EB7c \u0111\xF3ng trong \`finally\`.`
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return findings;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export { rule };
|
|
107
|
+
//# sourceMappingURL=no-unclosed-resource.rule.js.map
|
|
108
|
+
//# sourceMappingURL=no-unclosed-resource.rule.js.map
|