Skip to content

Commit 978e5ea

Browse files
committed
Code Securty QL
1 parent e17ecb5 commit 978e5ea

2 files changed

Lines changed: 12 additions & 45 deletions

File tree

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
/**
2-
* @name Find hardcoded secrets in C#
3-
* @description Detects hardcoded string literals assigned to fields with secret-related names
2+
* @name Hardcoded secrets in C# code
3+
* @description Finds string literals that may contain hardcoded secrets.
44
* @kind problem
55
* @problem.severity warning
66
* @security-severity 8.0
77
* @id cs/hardcoded-secrets
8-
* @tags security
8+
* @tags security, external/cwe/cwe-798
99
*/
1010

1111
import csharp
1212

13-
predicate isSecretField(Field f) {
14-
f.getName().regexpMatch("(?i).*(apiKey|token|secret|password|auth)")
15-
}
16-
17-
predicate isSecretValue(string value) {
18-
value.regexpMatch("(?i)^(sk_.*|token_.*|apikey_.*|[a-zA-Z0-9+/=]{32,})")
19-
}
20-
21-
from Field f, StringLiteral lit
22-
where
23-
isSecretField(f) and
24-
f.getInitializer() = lit and
25-
isSecretValue(lit.getValue())
26-
select lit, "Hardcoded secret detected: '" + lit.getValue() + "' assigned to field '" + f.getName() + "'"
27-
from Field f, StringLiteral lit
13+
from StringLiteral s
2814
where
29-
isSecretField(f) and
30-
f.getInitializer() = lit and
31-
isSecretValue(lit.getValue())
32-
select lit, "Hardcoded secret detected: '" + lit.getValue() + "' assigned to field '" + f.getName() + "'"
15+
s.getValue().matchesRegex("(?i)(sk_[a-z0-9]{10,}|api[_-]?key|token|secret|[A-Za-z0-9+/=]{32,})")
16+
select s, "🔒 Possible hardcoded secret: '" + s.getValue() + "'"

README.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -486,38 +486,21 @@ paths-ignore:
486486

487487
```ql
488488
/**
489-
* @name Find hardcoded secrets in C#
490-
* @description Detects hardcoded string literals assigned to fields with secret-related names
489+
* @name Hardcoded secrets in C# code
490+
* @description Finds string literals that may contain hardcoded secrets.
491491
* @kind problem
492492
* @problem.severity warning
493493
* @security-severity 8.0
494494
* @id cs/hardcoded-secrets
495-
* @tags security
495+
* @tags security, external/cwe/cwe-798
496496
*/
497497
498498
import csharp
499499
500-
predicate isSecretField(Field f) {
501-
f.getName().regexpMatch("(?i).*(apiKey|token|secret|password|auth)")
502-
}
503-
504-
predicate isSecretValue(string value) {
505-
value.regexpMatch("(?i)^(sk_.*|token_.*|apikey_.*|[a-zA-Z0-9+/=]{32,})")
506-
}
507-
508-
from Field f, StringLiteral lit
509-
where
510-
isSecretField(f) and
511-
f.getInitializer() = lit and
512-
isSecretValue(lit.getValue())
513-
select lit, "Hardcoded secret detected: '" + lit.getValue() + "' assigned to field '" + f.getName() + "'"
514-
from Field f, StringLiteral lit
500+
from StringLiteral s
515501
where
516-
isSecretField(f) and
517-
f.getInitializer() = lit and
518-
isSecretValue(lit.getValue())
519-
select lit, "Hardcoded secret detected: '" + lit.getValue() + "' assigned to field '" + f.getName() + "'"
520-
502+
s.getValue().matchesRegex("(?i)(sk_[a-z0-9]{10,}|api[_-]?key|token|secret|[A-Za-z0-9+/=]{32,})")
503+
select s, "🔒 Possible hardcoded secret: '" + s.getValue() + "'"
521504
```
522505

523506
### 🔍 Purpose of `FindHardcodedSecrets.ql` Query

0 commit comments

Comments
 (0)