Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CodeQL recommendation against Path.Combine #18865

Merged
merged 7 commits into from
Mar 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add implementation and tests
  • Loading branch information
carldybdahl-microsoft committed Mar 3, 2025
commit 44e6691e6dd29771b167e298b3b36579f79418d7
16 changes: 16 additions & 0 deletions csharp/ql/src/Bad Practices/PathCombine.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p><code>Path.Combine</code> may silently drop its earlier arguments if its later arguments are absolute paths. E.g. <code>Path.Combine("C:\\Users\\Me\\Documents", "C:\\Program Files\\") == "C:\\Program Files"</code>.</p>

</overview>
<recommendation>
<p>Use <code>Path.Join</code> instead.</p>
</recommendation>
<references>


</references>
</qhelp>
7 changes: 7 additions & 0 deletions csharp/ql/src/Bad Practices/PathCombine.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import csharp
import semmle.code.csharp.frameworks.System

from MethodCall call
where call.getTarget().hasFullyQualifiedName("System.IO", "Path", "Combine")
select call, "Path.Combine may silently discard its initial arguments if the latter are absolute paths. Use Path.Join to consistently join them."
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;

class EmptyCatchBlock
{
void bad()
{
Path.Combine(@"C:\Users", @"C:\Program Files");
}

void good()
{
Path.Join(@"C:\Users", @"C:\Program Files");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| PathCombine.cs:7:9:7:55 | catch (...) {...} | Path.Combine may silently discard its initial arguments if the latter are absolute paths. Use Path.Join to consistently join them. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bad Practices/PathCombine.ql

Check warning

Code scanning / CodeQL

Query test without inline test expectations Warning test

Query test does not use inline test expectations.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
semmle-extractor-options: /nostdlib /noconfig
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj