Skip to content

Commit e17ecb5

Browse files
committed
Code Securty QL
1 parent b6bad4a commit e17ecb5

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

.github/codeql/queries/FindHardcodedSecrets.ql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ predicate isSecretValue(string value) {
1818
value.regexpMatch("(?i)^(sk_.*|token_.*|apikey_.*|[a-zA-Z0-9+/=]{32,})")
1919
}
2020

21-
from Field f, Expr::Literal lit
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() + "'"
2227
from Field f, StringLiteral lit
2328
where
2429
isSecretField(f) and

.github/workflows/codeql.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
name: CodeQL Scan
2+
23
on:
34
push:
45
branches: [main]
56
pull_request:
67
branches: [main]
7-
8+
89
permissions:
910
contents: read
1011
security-events: write
12+
1113
jobs:
1214
analyze:
1315
name: CodeQL Analyze C#
1416
runs-on: ubuntu-latest
1517
steps:
1618
- uses: actions/checkout@v3
19+
1720
- uses: actions/setup-dotnet@v3
1821
with:
1922
dotnet-version: '8.0.x'
23+
2024
- uses: github/codeql-action/init@v3
2125
with:
2226
languages: csharp
2327
config-file: .github/codeql/config.yml
28+
2429
- run: dotnet build UserApp/UserApp.csproj --configuration Release
25-
- uses: github/codeql-action/analyze@v3
30+
31+
- name: Perform CodeQL Analysis
32+
uses: github/codeql-action/analyze@v3
33+
with:
34+
category: "/language:csharp"
35+
output: results
36+
37+
- name: Upload SARIF
38+
uses: actions/upload-artifact@v4
39+
with:
40+
name: codeql-results
41+
path: results/csharp.sarif
42+
retention-days: 5

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ paths-ignore:
475475

476476
> **Copilot Prompt:**
477477
> Create a custom CodeQL query named `FindHardcodedSecrets.ql` for C# to detect hardcoded secrets.
478-
> - Target fields that are initialized with string literals.
478+
> - Target fields that are initialized with StringLiteral.
479479
> - Match field names containing `apiKey`, `token`, `secret`, `password`, or `auth` (case-insensitive).
480480
> - Match values that resemble secrets, such as those starting with `sk_`, `token_`, `apikey_`, or 32+ base64-like characters.
481481
> - Use `Field` and `Literal` from the `csharp` CodeQL library.
@@ -505,7 +505,12 @@ predicate isSecretValue(string value) {
505505
value.regexpMatch("(?i)^(sk_.*|token_.*|apikey_.*|[a-zA-Z0-9+/=]{32,})")
506506
}
507507
508-
from Field f, Expr::Literal lit
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() + "'"
509514
from Field f, StringLiteral lit
510515
where
511516
isSecretField(f) and

0 commit comments

Comments
 (0)