-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18865 from carldybdahl-microsoft/csharp/path-combine
Add CodeQL recommendation against Path.Combine
- Loading branch information
Showing
7 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!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> | ||
|
||
<li>Microsoft Learn, .NET API browser, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-9.0">Path.Combine</a>.</li> | ||
<li>Microsoft Learn, .NET API browser, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.path.join?view=net-9.0">Path.Join</a>.</li> | ||
|
||
</references> | ||
</qhelp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @name Call to System.IO.Path.Combine | ||
* @description Finds calls to System.IO.Path's Combine method | ||
* @kind problem | ||
* @problem.severity recommendation | ||
* @precision very-high | ||
* @id cs/path-combine | ||
* @tags reliability | ||
*/ | ||
|
||
import csharp | ||
import semmle.code.csharp.frameworks.System | ||
|
||
from MethodCall call | ||
where call.getTarget().hasFullyQualifiedName("System.IO", "Path", "Combine") | ||
select call, "Call to 'System.IO.Path.Combine'." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: newQuery | ||
--- | ||
* Added a new query, `csharp/path-combine`, to recommend against the `Path.Combine` method due to it silently discarding its earlier parameters if later parameters are rooted. |
14 changes: 14 additions & 0 deletions
14
csharp/ql/test/query-tests/Bad Practices/Path Combine/PathCombine.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.IO; | ||
|
||
class PathCombine | ||
{ | ||
void bad() | ||
{ | ||
Path.Combine(@"C:\Users", @"C:\Program Files"); | ||
} | ||
|
||
void good() | ||
{ | ||
Path.Join(@"C:\Users", @"C:\Program Files"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
csharp/ql/test/query-tests/Bad Practices/Path Combine/PathCombine.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
| PathCombine.cs:7:9:7:54 | call to method Combine | Call to 'System.IO.Path.Combine'. | |
1 change: 1 addition & 0 deletions
1
csharp/ql/test/query-tests/Bad Practices/Path Combine/PathCombine.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Bad Practices/PathCombine.ql |
2 changes: 2 additions & 0 deletions
2
csharp/ql/test/query-tests/Bad Practices/Path Combine/options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |