Skip to content

Commit 4e6f463

Browse files
authored
Merge pull request #174 from microsoft/revert-ps-module-extraction
PS: Revert extraction of code found via `PSModulePath`
2 parents 0628e49 + abc23e0 commit 4e6f463

File tree

14 files changed

+3318
-106
lines changed

14 files changed

+3318
-106
lines changed

misc/scripts/prepare-db-upgrade.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ EOF
3333
# default for prev_hash: the main branch of the remote for 'github/codeql'.
3434
# This works out as a dynamic lookup of the hash of the file in the main branch
3535
# of the repo.
36-
prev_hash=$(git remote -v | grep 'github/codeql\.git (fetch)$' | cut -f1)/main
36+
prev_hash=$(git remote -v | grep 'microsoft/codeql\.git (fetch)$' | cut -f1)/main
3737

3838
while [ $# -gt 0 ]; do
3939
case "$1" in

powershell/codeql-extractor.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,4 @@ file_types:
1010
display_name: powershellscripts
1111
extensions:
1212
- .ps1
13-
- .psd1
14-
options:
15-
skip_psmodulepath_files:
16-
title: Skip PSModulePath files.
17-
description: Whether to avoid extracting source files in paths specified by the PSModulePath environment variable.
18-
type: string
19-
pattern: "^(false|true)$"
13+
- .psd1

powershell/downgrades/802d5b9f407fb0dac894df1c0b4584f2215e1512/semmlecode.powershell.dbscheme

Lines changed: 5 additions & 1 deletion
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,
@@ -82,7 +86,7 @@ parent(
8286
// https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.language.commandbaseast?view=powershellsdk-7.3.0
8387
@command_base = @command | @command_expression;
8488
// https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.language.chainableast?view=powershellsdk-7.3.0
85-
@chainable = @pipeline | @pipeline_chain;
89+
@chainable = @command_base | @pipeline | @pipeline_chain;
8690
//https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.language.pipelinebaseast?view=powershellsdk-7.3.0
8791
@pipeline_base = @chainable | @error_statement | @assignment_statement;
8892
// https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.language.statementast?view=powershellsdk-7.3.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
description: Remove Pipelines with one element.
1+
description: Remove psmodule file extraction
22
compatibility: partial

powershell/extractor/Semmle.Extraction.PowerShell.Standalone/Options.cs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
@@ -25,9 +24,6 @@ public override bool HandleFlag(string key, bool value)
2524
case "dry-run":
2625
SkipExtraction = value;
2726
return true;
28-
case "skip-psmodulepath-files":
29-
SkipPSModulePathFiles = value;
30-
return true;
3127
default:
3228
return base.HandleFlag(key, value);
3329
}
@@ -78,7 +74,7 @@ public override void InvalidArgument(string argument)
7874
/// <summary>
7975
/// List of extensions to include.
8076
/// </summary>
81-
public IList<string> Extensions { get; } = new List<string>() { ".ps1", ".psd1" };
77+
public IList<string> Extensions { get; } = new List<string>() { ".ps1" };
8278

8379
/// <summary>
8480
/// Files/patterns to exclude.
@@ -121,24 +117,6 @@ private static FileInfo[] GetDefaultFiles()
121117
}
122118
}
123119

124-
/// <summary>
125-
/// Returns true if the extractor should skip files in the PSModulePath because the
126-
/// environment variable CODEQL_EXTRACTOR_POWERSHELL_OPTION_SKIP_PSMODULEPATH_FILES
127-
/// is set to a truthy value.
128-
/// </summary>
129-
private static bool GetDefaultSkipPSModulePathFiles()
130-
{
131-
var skip = System.Environment.GetEnvironmentVariable(
132-
"CODEQL_EXTRACTOR_POWERSHELL_OPTION_SKIP_PSMODULEPATH_FILES"
133-
);
134-
bool b = skip != null && skip.ToLower() != "false";
135-
if (b)
136-
{
137-
System.Console.WriteLine("Skipping files in PSModulePath");
138-
}
139-
return b;
140-
}
141-
142120
/// <summary>
143121
/// The directory or file containing the source code;
144122
/// </summary>
@@ -149,12 +127,6 @@ private static bool GetDefaultSkipPSModulePathFiles()
149127
/// </summary>
150128
public bool SkipExtraction { get; private set; } = false;
151129

152-
/// <summary>
153-
/// Whether to extract files in the paths found in the `PSModulePath`
154-
/// environment variable.
155-
/// </summary>
156-
public bool SkipPSModulePathFiles { get; private set; } = GetDefaultSkipPSModulePathFiles();
157-
158130
/// <summary>
159131
/// Whether errors were encountered parsing the arguments.
160132
/// </summary>
@@ -186,18 +158,13 @@ public static void ShowHelp(System.IO.TextWriter output)
186158
"PowerShell# standalone extractor\n\nExtracts PowerShell scripts in the current directory.\n"
187159
);
188160
output.WriteLine("Additional options:\n");
189-
output.WriteLine(" <path> Use the provided path instead.");
190-
output.WriteLine(
191-
" --exclude:xxx Exclude a file or directory (can be specified multiple times)"
192-
);
193-
output.WriteLine(" --dry-run Stop before extraction");
194-
output.WriteLine(
195-
" --threads:nnn Specify number of threads (default=CPU cores)"
196-
);
197-
output.WriteLine(" --verbose Produce more output");
161+
output.WriteLine(" <path> Use the provided path instead.");
198162
output.WriteLine(
199-
" --skip-psmodulepath-files Avoid extracting source files in paths specified by the PSModulePath environment variable."
163+
" --exclude:xxx Exclude a file or directory (can be specified multiple times)"
200164
);
165+
output.WriteLine(" --dry-run Stop before extraction");
166+
output.WriteLine(" --threads:nnn Specify number of threads (default=CPU cores)");
167+
output.WriteLine(" --verbose Produce more output");
201168
}
202169

203170
private Options() { }

powershell/extractor/Semmle.Extraction.PowerShell.Standalone/Program.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public static int Main(string[] args)
4545

4646
output.Log(Severity.Info, "Running PowerShell standalone extractor");
4747
var sourceFiles = options
48-
.Files.Concat(GetPSModuleFiles(options))
49-
.Where(d =>
48+
.Files.Where(d =>
5049
options.Extensions.Contains(
5150
d.Extension,
5251
StringComparer.InvariantCultureIgnoreCase
@@ -88,30 +87,6 @@ public static int Main(string[] args)
8887
return 0;
8988
}
9089

91-
private static String[] GetPSModulePaths()
92-
{
93-
return Environment.GetEnvironmentVariable("PSModulePath")?.Split(Path.PathSeparator)
94-
?? Array.Empty<string>();
95-
}
96-
97-
private static IEnumerable<FileInfo> GetPSModuleFiles(Options options)
98-
{
99-
if(options.SkipPSModulePathFiles)
100-
{
101-
return Array.Empty<FileInfo>();
102-
}
103-
104-
return GetPSModulePaths()
105-
.Where(d => Directory.Exists(d))
106-
.SelectMany(d =>
107-
{
108-
var di = new DirectoryInfo(d);
109-
return di.Exists
110-
? di.GetFiles("*.*", SearchOption.AllDirectories)
111-
: new FileInfo[] { new(d) };
112-
});
113-
}
114-
11590
private class ExtractionProgress : IProgressMonitor
11691
{
11792
public ExtractionProgress(ILogger output)

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

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

76
namespace Semmle.Extraction.PowerShell.Entities
87
{
@@ -15,18 +14,6 @@ protected File(PowerShellContext cx, string path)
1514
{
1615
}
1716

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-
3017
public override void Populate(TextWriter trapFile)
3118
{
3219
trapFile.files(this, TransformedPath.Value);
@@ -36,11 +23,6 @@ public override void Populate(TextWriter trapFile)
3623
trapFile.containerparent(Extraction.Entities.Folder.Create(PowerShellContext, dir), this);
3724
}
3825

39-
if(PathIsInPSModulePath())
40-
{
41-
trapFile.is_in_psmodule_path(this);
42-
}
43-
4426
try
4527
{
4628
System.Text.Encoding encoding;

powershell/extractor/Semmle.Extraction/Tuples.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,5 @@ 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-
}
3935
}
4036
}

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

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

212-
predicate isInPSModulePath() {
213-
is_in_psmodule_path(this)
214-
}
215-
216212
/** Holds if this file is a library. */
217213
predicate fromLibrary() {
218214
not this.getBaseName() = "" and

powershell/ql/lib/semmlecode.powershell.dbscheme

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

44-
is_in_psmodule_path(
45-
int file: @file ref
46-
);
47-
4844
/* Comments */
4945
comment_entity(
5046
unique int id: @comment_entity,

0 commit comments

Comments
 (0)