Skip to content

[NEW QUERY] C#: cs/unsafe-reflection (CWE-470) and cs/untrusted-search-path (CWE-426) - #341

Draft
felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/csharp-query-create-ssrf-reflection-injection
Draft

felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/csharp-query-create-ssrf-reflection-injection

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown

📝 Query Information

  • Language: csharp
  • Query ID: cs/unsafe-reflection, cs/untrusted-search-path
  • Category: security
  • Severity: warning (@precision medium; security-severity 6.5 and 6.7)
  • CWE/CVE (if applicable): CWE-470 (cs/unsafe-reflection); CWE-426 and CWE-427 (cs/untrusted-search-path)

🎯 Description

An AI scan found two C# patterns that baseline CodeQL does not report. The first resolves and instantiates a type named by the user. The second starts a program by its bare name, so the OS looks it up through the search path. I reproduced both in test databases. The stock security-extended suite, run with --threat-model local, reports neither pattern. Its only result is an unrelated cs/command-line-injection alert on one of the safe examples. These queries are medium precision, so they are aimed at Community Packs rather than github/codeql.

What This Query Detects

cs/unsafe-reflection (taint tracking)

  • Sources: inputs from the active threat models.
  • Sinks: the type or assembly name argument of Type.GetType, Type.GetTypeFromProgID, Assembly.GetType / Module.GetType, Assembly.CreateInstance, Activator.CreateInstance / CreateInstanceFrom (string overloads) and AppDomain.CreateInstance*.
  • Barriers: allowlist checks, meaning Contains / ContainsKey, or == / != / Equals against a string constant. Dictionary lookups break the flow on their own.
  • Threat model: the original finding comes from a command-line argument, which is a local source. It is reported only when the local threat model is enabled.
  • Alert location: the name-resolution call (Type.GetType(...)), not the later Activator.CreateInstance(type) that the AI scan flagged.

cs/untrusted-search-path (data flow)

  • Sources: string literals that name a program with no directory part. Names with a non-executable extension, such as readme.txt, are excluded. Names ending in .exe, .com, .bat or .cmd are kept.
  • Sinks: writes to ProcessStartInfo.FileName, including object initializers, plus the fileName argument of new ProcessStartInfo(...) and of static Process.Start(...).
  • Path reporting: a value from an if/else (the Juliet Bad() case) or passed through a helper method produces one path per literal.
  • Expected noise: common tools started by name, such as git or dotnet, will be flagged.

Example Vulnerable Code

Type type = Type.GetType(Environment.GetCommandLineArgs()[1]);   // cs/unsafe-reflection
Activator.CreateInstance(type!);

var startInfo = new ProcessStartInfo();
startInfo.FileName = "ls";                                        // cs/untrusted-search-path
Process.Start(startInfo);

Example Safe Code

if (AllowedTypes.Contains(name))
    Activator.CreateInstance(Type.GetType(name));

if (TypeMap.TryGetValue(key, out Type t))
    Activator.CreateInstance(t);

startInfo.FileName = "/bin/ls";
Process.Start(new ProcessStartInfo("readme.txt") { UseShellExecute = true });

🧪 Testing

  • Positive test cases included
  • Negative test cases included
  • Edge cases covered
  • All tests pass

Tests use inline expectations (// $ Alert / // $ Source).

  • UnsafeReflection.ext.yml turns on the local threat model, which covers argv, environment variables and stdin.
  • Each test directory has an options file that adds references the test compiler is missing, such as System.Diagnostics.Process.dll.
  • The test compiler builds a library, so top-level statements fail to compile. The argv repro therefore uses an explicit Main.

📋 Checklist

  • Query compiles without errors
  • Documentation complete (.md and .qhelp): .md only, no .qhelp added
  • Metadata properly set (@name, @id, @kind, etc.)
  • Tests validate query behavior
  • No false positives in test cases

🔗 References


Note: This query was developed using Test-Driven Development methodology.

…WE-426) queries with tests

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new query for SSRF, reflection, and command injection [NEW QUERY] C#: cs/unsafe-reflection (CWE-470) and cs/untrusted-search-path (CWE-426) Oct 31, 2026
Copilot AI requested a review from felickz October 31, 2026 06:23
@felickz
felickz requested a balanced review from Copilot November 2, 2026 18:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The queries can miss real vulnerabilities because method-name-only allowlist matching suppresses unsafe-reflection alerts and the extension filter excludes PATH-resolved Unix executables.

2 open issues
What changed in this PR

Adds two medium-precision C# security queries to the custom CodeQL pack, addressing issue #291’s gaps in detecting user-controlled reflection and process launches through the search path.

Changes:

  • Adds cs/unsafe-reflection with threat-model sources and allowlist barriers.
  • Adds cs/untrusted-search-path to track bare program names into process-launch APIs.
  • Includes documentation, positive and negative test cases, and expected path results.
File Description
languages/​csharp/​custom/​test/​UntrustedSearchPath/​UntrustedSearchPath.qlref Connects the query to inline-expectation tests.
languages/​csharp/​custom/​test/​UntrustedSearchPath/​UntrustedSearchPath.expected Records expected search-path alerts and flow paths.
languages/​csharp/​custom/​test/​UntrustedSearchPath/​Test.cs Covers bare names, qualified paths, and helper calls.
languages/​csharp/​custom/​test/​UntrustedSearchPath/​options Adds process API references for test extraction.
languages/​csharp/​custom/​test/​UnsafeReflection/​UnsafeReflection.qlref Connects the reflection query to inline-expectation tests.
languages/​csharp/​custom/​test/​UnsafeReflection/​UnsafeReflection.ext.yml Enables local threat-model sources in tests.
languages/​csharp/​custom/​test/​UnsafeReflection/​UnsafeReflection.expected Records expected reflection alerts and flow paths.
languages/​csharp/​custom/​test/​UnsafeReflection/​Tests.cs Covers reflection APIs and allowlist examples.
languages/​csharp/​custom/​test/​UnsafeReflection/​Program.cs Reproduces command-line-controlled type selection.
languages/​csharp/​custom/​test/​UnsafeReflection/​options Adds framework references for test extraction.
languages/​csharp/​custom/​src/​UntrustedSearchPath/​UntrustedSearchPath.ql Implements bare-program-name flow analysis.
languages/​csharp/​custom/​src/​UntrustedSearchPath/​UntrustedSearchPath.md Documents search-path risks and safer launches.
languages/​csharp/​custom/​src/​UnsafeReflection/​UnsafeReflection.ql Implements reflection taint tracking and barriers.
languages/​csharp/​custom/​src/​UnsafeReflection/​UnsafeReflection.md Documents reflection risks and safe type mappings.

Review effort: Balanced

Copilot AI and others added 2 commits November 2, 2026 21:27
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Query Create]: CWE-426, CWE-470 MEDIUM - C#: new query for SSRF, reflection and command injection

3 participants