-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add CodeQL recommendation against Path.Combine #18865
Open
carldybdahl-microsoft
wants to merge
7
commits into
github:main
Choose a base branch
from
carldybdahl-microsoft:csharp/path-combine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44e6691
Add implementation and tests
carldybdahl-microsoft 35fd4d2
Oops
carldybdahl-microsoft aa6779f
Add changelog
carldybdahl-microsoft d82295c
Add QLDoc
carldybdahl-microsoft d371723
Fix test
carldybdahl-microsoft b344795
Match autoformatting, add QLDoc references
carldybdahl-microsoft 2f7cdf1
Improvements
carldybdahl-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Query test without inline test expectations Warning test