@@ -475,43 +475,32 @@ 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.
481- > - Use `Field` and `string_literal ` from the `csharp` CodeQL library.
481+ > - Use `Field` and `Literal ` from the `csharp` CodeQL library.
482482> - Return the matched string literal and a message indicating a hardcoded secret.
483483> - Include standard CodeQL metadata: `@name`, `@description`, `@id`, `@tags`, `@problem.severity`, and `@security-severity`.
484484
485485# ## **✅ Expected Outcome:**
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
498498import csharp
499499
500- predicate isSecretField(Field f) {
501- f.getName().regexpMatch("(?i).*(apiKey|token|secret|password|auth)")
502- }
503-
504- predicate isSecretValue(string_literal s) {
505- s.getValue().regexpMatch("(?i)^(sk_.*|token_.*|apikey_.*|[a-zA-Z0-9+/=]{32,})")
506- }
507-
508- from Field f, string_literal s
500+ from StringLiteral s
509501where
510- isSecretField(f) and
511- f.getInitializer() = s and
512- isSecretValue(s)
513- select s, "Hardcoded secret detected: '" + s.getValue() + "' assigned to field '" + f.getName() + "'"
514-
502+ s.getValue().regexpMatch("(?i)(sk_[a-z0-9]{10,}|api[_-]?key|token|secret|[A-Za-z0-9+/=]{32,})")
503+ select s, "🔒 Possible hardcoded secret: '" + s.getValue() + "'"
515504` ` `
516505
517506# ## 🔍 Purpose of `FindHardcodedSecrets.ql` Query
0 commit comments