Files
pruefwerk/compliance-agent/rules/cra_semgrep_csharp.yaml
sharangandClaude Opus 5.5 8926255d6f
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Dashboard (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Successful in 1m10s
CI / Deploy Agent (push) Successful in 4m42s
feat(dotnet): .NET detection, NuGet SBOM, C# rules and config checks (#447)
Minimal .NET/C# support for epic #445 (N1-N5): detection of solutions/projects and target frameworks, NuGet SBOM without build or restore (pkg:nuget purls for OSV, unresolved-dependency warning, end-of-support frameworks), own C# semgrep rule pack, .NET configuration and key-file checks, .NET build output and restored packages not counted as own code. Fixture fixtures/dotnet/framework48, docs .NET-Projekte (DE/EN).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-10-05 14:38:13 +00:00

267 lines
12 KiB
YAML

# Prüfwerk's own semgrep rules for C# (.NET Framework and .NET), #445. Written
# for Prüfwerk, not adapted from any third-party rule set (the Semgrep registry
# rules may not be used for a service offered to others).
# Same conventions as cra_semgrep.yaml: each rule id is `cra-ai-<n>-<slug>`,
# keyed back to its control via the `control-map` LUT, with its CWE and
# control in the metadata. Keep patterns tight: a false positive that the
# judge refutes marks the whole finding a false positive.
# Test cases: cra_semgrep_csharp.cs (`semgrep --test compliance-agent/rules`).
rules:
# --- cra-ai-20: injection and unsafe input handling --------------------------
- id: cra-ai-20-csharp-sql-string
languages: [csharp]
severity: ERROR
message: SQL text is built from strings (concatenation, interpolation or String.Format) and run as a command — SQL injection (CWE-89). Pass the values as parameters (SqlParameter, AddWithValue, FromSqlInterpolated).
metadata:
cwe: ["CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')"]
control: cra-ai-20
pattern-either:
# The SQL text is built right in the call.
- patterns:
- pattern-either: &sql-sinks
- patterns:
- pattern-either:
- pattern: new $CMD($Q, ...)
- pattern: new $CMD($Q)
- metavariable-regex:
metavariable: $CMD
regex: ^(SqlCommand|OleDbCommand|OdbcCommand|OracleCommand|MySqlCommand|NpgsqlCommand|SQLiteCommand|SqliteCommand|SqlDataAdapter|OleDbDataAdapter)$
- pattern: $X.CommandText = $Q
- pattern: $X.ExecuteSqlCommand($Q, ...)
- pattern: $X.ExecuteSqlRaw($Q, ...)
- pattern: $X.ExecuteSqlRawAsync($Q, ...)
- pattern: $X.FromSqlRaw($Q, ...)
- pattern: $X.SqlQuery($Q, ...)
- pattern: $X.SqlQuery<$T>($Q, ...)
- metavariable-pattern:
metavariable: $Q
patterns: &built-string
- pattern-either:
- pattern: '"..." + $E'
- pattern: $E + "..."
- pattern: String.Format(...)
- pattern: string.Format(...)
- pattern-regex: (?s)^(\$@?|@\$)".*\{
- pattern-not: '"..." + "..."'
# The SQL text is built into a variable first.
- patterns:
- pattern-either:
- pattern-inside: |
$TY $Q = $INIT;
...
- pattern-inside: |
var $Q = $INIT;
...
- pattern-inside: |
$Q = $INIT;
...
- pattern-either: *sql-sinks
- metavariable-pattern:
metavariable: $INIT
patterns: *built-string
- id: cra-ai-20-csharp-process-start
languages: [csharp]
severity: WARNING
message: A process is started with a command line built from strings — command injection if any part comes from outside (CWE-78). Start a fixed program with an argument list (ProcessStartInfo.ArgumentList) and validate the values.
metadata:
cwe: ["CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')"]
control: cra-ai-20
patterns:
- pattern-either:
- pattern: Process.Start($ARG, ...)
- pattern: Process.Start($FILE, $ARG, ...)
- pattern: new ProcessStartInfo($ARG, ...)
- pattern: new ProcessStartInfo($FILE, $ARG, ...)
- pattern: $P.Arguments = $ARG
# Only a ProcessStartInfo's: a file dialog's FileName is no command.
- patterns:
- pattern-either:
- pattern-inside: |
$PT $P = new ProcessStartInfo(...);
...
- pattern-inside: |
var $P = new ProcessStartInfo(...);
...
- pattern-inside: |
$P = new ProcessStartInfo(...);
...
- pattern: $P.FileName = $ARG
- metavariable-pattern:
metavariable: $ARG
patterns:
- pattern-either:
- pattern: '"..." + $E'
- pattern: $E + "..."
- pattern: String.Format(...)
- pattern: string.Format(...)
- pattern: String.Concat(...)
- pattern-regex: (?s)^(\$@?|@\$)".*\{
- pattern-not: '"..." + "..."'
- id: cra-ai-20-csharp-unsafe-deserialization
languages: [csharp]
severity: ERROR
message: $TYPE deserializes type information from its input, so data from outside can run code (CWE-502). Use a serializer bound to known types (System.Text.Json, DataContractSerializer, JSON.NET with TypeNameHandling.None).
metadata:
cwe: ["CWE-502: Deserialization of Untrusted Data"]
control: cra-ai-20
pattern-either:
- patterns:
- pattern: new $TYPE(...)
- metavariable-regex:
metavariable: $TYPE
regex: ^(BinaryFormatter|NetDataContractSerializer|LosFormatter|SoapFormatter|ObjectStateFormatter)$
- patterns:
- pattern: TypeNameHandling.$TYPE
- metavariable-regex:
metavariable: $TYPE
regex: ^(All|Auto|Objects|Arrays)$
- id: cra-ai-20-csharp-xxe
languages: [csharp]
severity: WARNING
message: XML is parsed with DTD processing or an external resolver enabled — external entities can read local files or reach internal hosts (XXE, CWE-611). Set XmlResolver = null and DtdProcessing = DtdProcessing.Prohibit (the default only from .NET Framework 4.5.2 on).
metadata:
cwe: ["CWE-611: Improper Restriction of XML External Entity Reference"]
control: cra-ai-20
pattern-either:
- pattern: $X.XmlResolver = new XmlUrlResolver(...)
- pattern: $X.DtdProcessing = DtdProcessing.Parse
- pattern: $X.ProhibitDtd = false
- patterns:
- pattern-either:
- pattern-inside: |
$D = new XmlDocument();
...
- pattern-inside: |
$DT $D = new XmlDocument();
...
- pattern-inside: |
var $D = new XmlDocument();
...
- pattern-inside: |
$D = new XmlTextReader(...);
...
- pattern-inside: |
$DT $D = new XmlTextReader(...);
...
- pattern-inside: |
var $D = new XmlTextReader(...);
...
- pattern-not-inside: |
$D.XmlResolver = null;
...
- pattern-not-inside: |
$D.DtdProcessing = DtdProcessing.Prohibit;
...
- pattern-not-inside: |
var $D = new $T { ..., XmlResolver = null, ... };
...
- pattern-not-inside: |
$DT $D = new $T { ..., XmlResolver = null, ... };
...
- pattern-either:
- pattern: $D.Load(...)
- pattern: $D.LoadXml(...)
- pattern: $D.Read()
- id: cra-ai-20-csharp-path-traversal
languages: [csharp]
severity: WARNING
message: A file path is built from request input — "..\" in it reaches files outside the intended folder (path traversal, CWE-22). Take only the file name (Path.GetFileName) and check the full path stays under the base folder.
metadata:
cwe: ["CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')"]
control: cra-ai-20
patterns:
- pattern-either:
- pattern: File.$M(..., <... Request.$P ...>, ...)
- pattern: File.$M(..., <... Request[$K] ...>, ...)
- pattern: Path.Combine(..., <... Request.$P ...>, ...)
- pattern: Path.Combine(..., <... Request[$K] ...>, ...)
- pattern: new FileStream(<... Request.$P ...>, ...)
- pattern: new FileStream(<... Request[$K] ...>, ...)
- pattern-not: File.$M(..., <... Path.GetFileName(...) ...>, ...)
- pattern-not: Path.Combine(..., <... Path.GetFileName(...) ...>, ...)
# --- cra-ai-13: Verschlüsselung sensibler Daten ------------------------------
- id: cra-ai-13-csharp-broken-hash-or-cipher
languages: [csharp]
severity: WARNING
message: $ALG is a broken algorithm (MD5, SHA-1, DES, 3DES or RC2) that protects neither integrity nor confidentiality (CWE-327). Use SHA-256 or better, and AES-GCM.
metadata:
cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
control: cra-ai-13
pattern-either:
- patterns:
- pattern: $ALG.Create(...)
- metavariable-regex:
metavariable: $ALG
regex: ^(MD5|SHA1|DES|TripleDES|RC2)$
- patterns:
- pattern: new $ALG(...)
- metavariable-regex:
metavariable: $ALG
regex: ^(MD5CryptoServiceProvider|MD5Cng|SHA1CryptoServiceProvider|SHA1Managed|SHA1Cng|DESCryptoServiceProvider|TripleDESCryptoServiceProvider|TripleDESCng|RC2CryptoServiceProvider|HMACMD5|HMACSHA1)$
- id: cra-ai-13-csharp-ecb-mode
languages: [csharp]
severity: WARNING
message: ECB mode encrypts equal blocks to equal ciphertext, so the structure of the data shows through (CWE-327). Use AES-GCM, or CBC with a random IV and a MAC.
metadata:
cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
control: cra-ai-13
pattern: CipherMode.ECB
# --- cra-ai-15: Transport-Schutz ---------------------------------------------
- id: cra-ai-15-csharp-cert-validation-disabled
languages: [csharp]
severity: ERROR
message: TLS certificate validation is switched off (the callback accepts every certificate), so anyone on the network path can read and change the traffic (CWE-295). Remove the callback, or pin the expected certificate.
metadata:
cwe: ["CWE-295: Improper Certificate Validation"]
control: cra-ai-15
pattern-either:
- pattern: HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
- patterns:
- pattern-either:
- pattern: $X.ServerCertificateValidationCallback = $F
- pattern: $X.ServerCertificateValidationCallback += $F
- pattern: $X.ServerCertificateCustomValidationCallback = $F
- pattern: $X.RemoteCertificateValidationCallback = $F
- metavariable-pattern:
metavariable: $F
pattern-either:
- pattern: (...) => true
- pattern: |
delegate { return true; }
- pattern: |
delegate (...) { return true; }
- pattern: |
(...) => { return true; }
# --- cra-ai-8: Keine Default-Passwörter --------------------------------------
- id: cra-ai-8-csharp-hardcoded-credential
languages: [csharp]
severity: ERROR
message: A credential ($NAME) is written into the source — every installation shares it, and it can be read out of the assembly (CWE-798). Load it from protected configuration or a secret store.
metadata:
cwe: ["CWE-798: Use of Hard-coded Credentials"]
control: cra-ai-8
patterns:
- pattern-either:
- pattern: $TYPE $NAME = "$VALUE";
- pattern: var $NAME = "$VALUE";
- pattern: $NAME = "$VALUE"
- pattern: const string $NAME = "$VALUE";
- metavariable-regex:
metavariable: $NAME
regex: (?i)^(.*\.)?\w*(password|passwd|pwd|secret|apikey|api_key|privatekey|private_key|accesskey|access_key|connectionpassword|encryptionkey|aeskey|secretkey|clientsecret)\w*$
- metavariable-regex:
metavariable: $NAME
regex: (?i)^(?!.*(hint|label|prompt|field|name|param|header|policy|length|regex|pattern|format|path|file|url|uri|key(name|id)|placeholder)).*$
# Six characters or more, and not a placeholder left for the installer.
- metavariable-regex:
metavariable: $VALUE
regex: ^(?!(?i:changeme|change_me|password|secret|admin|x{3,}|your_\w*|<[^>]*>|\*+|\{\d+\}|\$\{.*\}|%\w+%)$).{6,}$