Skip to content

Commit 28ddb9a

Browse files
committed
PS: Mark which files are from the PSModulePath environment variable.
1 parent 261d7d7 commit 28ddb9a

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

powershell/extractor/Semmle.Extraction.PowerShell/Entities/Base/File.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Linq;
56

67
namespace Semmle.Extraction.PowerShell.Entities
78
{
@@ -14,6 +15,18 @@ protected File(PowerShellContext cx, string path)
1415
{
1516
}
1617

18+
private static string[] GetPSModulePaths()
19+
{
20+
return Environment.GetEnvironmentVariable("PSModulePath")?.Split(Path.PathSeparator)
21+
?? Array.Empty<string>();
22+
}
23+
24+
private bool PathIsInPSModulePath()
25+
{
26+
// Check if f's path is inside one of the paths in $Env:PSModulePath
27+
return GetPSModulePaths().Any(originalPath.StartsWith);
28+
}
29+
1730
public override void Populate(TextWriter trapFile)
1831
{
1932
trapFile.files(this, TransformedPath.Value);
@@ -23,6 +36,11 @@ public override void Populate(TextWriter trapFile)
2336
trapFile.containerparent(Extraction.Entities.Folder.Create(PowerShellContext, dir), this);
2437
}
2538

39+
if(PathIsInPSModulePath())
40+
{
41+
trapFile.is_in_psmodule_path(this);
42+
}
43+
2644
try
2745
{
2846
System.Text.Encoding encoding;

powershell/extractor/Semmle.Extraction/Tuples.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ public static void locations_default(this System.IO.TextWriter trapFile, SourceL
3232
{
3333
trapFile.WriteTuple("locations_default", label, file, startLine, startCol, endLine, endCol);
3434
}
35+
36+
public static void is_in_psmodule_path(this System.IO.TextWriter trapFile, Entities.File file) {
37+
trapFile.WriteTuple("is_in_psmodule_path", file);
38+
}
3539
}
3640
}

powershell/ql/lib/semmle/code/powershell/File.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ class File extends Container, @file {
209209
not this.isStub()
210210
}
211211

212+
predicate isInPSModulePath() {
213+
is_in_psmodule_path(this)
214+
}
215+
212216
/** Holds if this file is a library. */
213217
predicate fromLibrary() {
214218
not this.getBaseName() = "" and

powershell/ql/lib/semmlecode.powershell.dbscheme

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ containerparent(
4141
unique int child: @container ref
4242
);
4343

44+
is_in_psmodule_path(
45+
int file: @file ref
46+
);
47+
4448
/* Comments */
4549
comment_entity(
4650
unique int id: @comment_entity,

0 commit comments

Comments
 (0)