Merge pull request #85 from AikidoSec/powerrshell-line-explosion-fix

Fix line explosion on Windows PowerShell
This commit is contained in:
Sander Declerck 2025-10-08 15:49:10 +02:00 committed by GitHub
commit 361b56a715
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 104 additions and 33 deletions

View file

@ -18,15 +18,15 @@ export const knownAikidoTools = [
* Example: "npm, npx, yarn, pnpm, and pnpx commands" * Example: "npm, npx, yarn, pnpm, and pnpx commands"
*/ */
export function getPackageManagerList() { export function getPackageManagerList() {
const tools = knownAikidoTools.map(t => t.tool); const tools = knownAikidoTools.map((t) => t.tool);
if (tools.length <= 1) { if (tools.length <= 1) {
return `${tools[0] || ''} commands`; return `${tools[0] || ""} commands`;
} }
if (tools.length === 2) { if (tools.length === 2) {
return `${tools[0]} and ${tools[1]} commands`; return `${tools[0]} and ${tools[1]} commands`;
} }
const lastTool = tools.pop(); const lastTool = tools.pop();
return `${tools.join(', ')}, and ${lastTool} commands`; return `${tools.join(", ")}, and ${lastTool} commands`;
} }
export function doesExecutableExistOnSystem(executableName) { export function doesExecutableExistOnSystem(executableName) {
@ -47,7 +47,7 @@ export function removeLinesMatchingPattern(filePath, pattern, eol) {
eol = eol || os.EOL; eol = eol || os.EOL;
const fileContent = fs.readFileSync(filePath, "utf-8"); const fileContent = fs.readFileSync(filePath, "utf-8");
const lines = fileContent.split(/[\r\n\u2028\u2029]/); const lines = fileContent.split(/\r?\n|\r|\u2028|\u2029/);
const updatedLines = lines.filter((line) => !shouldRemoveLine(line, pattern)); const updatedLines = lines.filter((line) => !shouldRemoveLine(line, pattern));
fs.writeFileSync(filePath, updatedLines.join(eol), "utf-8"); fs.writeFileSync(filePath, updatedLines.join(eol), "utf-8");
} }

View file

@ -16,8 +16,8 @@ describe("removeLinesMatchingPatternTests", () => {
namedExports: { namedExports: {
EOL: "\r\n", // Simulate Windows line endings EOL: "\r\n", // Simulate Windows line endings
tmpdir: tmpdir, tmpdir: tmpdir,
platform: () => "linux" platform: () => "linux",
} },
}); });
}); });
@ -31,7 +31,6 @@ describe("removeLinesMatchingPatternTests", () => {
mock.reset(); mock.reset();
}); });
it("should handle mixed line endings without wiping entire file", async () => { it("should handle mixed line endings without wiping entire file", async () => {
// Import helpers after setting up the mock // Import helpers after setting up the mock
const { removeLinesMatchingPattern } = await import("./helpers.js"); const { removeLinesMatchingPattern } = await import("./helpers.js");
@ -42,7 +41,7 @@ describe("removeLinesMatchingPatternTests", () => {
"alias npm='remove-this'", "alias npm='remove-this'",
"# keep this line too", "# keep this line too",
"alias yarn='remove-this-too'", "alias yarn='remove-this-too'",
"# final line to keep" "# final line to keep",
].join("\n"); // File has Unix line endings ].join("\n"); // File has Unix line endings
fs.writeFileSync(testFile, fileContent, "utf-8"); fs.writeFileSync(testFile, fileContent, "utf-8");
@ -55,8 +54,14 @@ describe("removeLinesMatchingPatternTests", () => {
// This test will fail because the function splits on '\r\n' but file uses '\n' // This test will fail because the function splits on '\r\n' but file uses '\n'
// So it treats the entire content as one line and if any part matches, removes everything // So it treats the entire content as one line and if any part matches, removes everything
assert.ok(result.includes("keep this line"), "Should preserve non-matching lines"); assert.ok(
assert.ok(result.includes("final line to keep"), "Should preserve final line"); result.includes("keep this line"),
"Should preserve non-matching lines"
);
assert.ok(
result.includes("final line to keep"),
"Should preserve final line"
);
}); });
it("should handle mixed line endings with short matching content", async () => { it("should handle mixed line endings with short matching content", async () => {
@ -68,7 +73,7 @@ describe("removeLinesMatchingPatternTests", () => {
const fileContent = [ const fileContent = [
"# keep1", "# keep1",
"alias x=y", // Short alias line that should be removed "alias x=y", // Short alias line that should be removed
"# keep2" "# keep2",
].join("\n"); // File has Unix line endings, total length < 100 chars ].join("\n"); // File has Unix line endings, total length < 100 chars
fs.writeFileSync(testFile, fileContent, "utf-8"); fs.writeFileSync(testFile, fileContent, "utf-8");
@ -90,11 +95,9 @@ describe("removeLinesMatchingPatternTests", () => {
// Use Unicode line separator (U+2028) and paragraph separator (U+2029) // Use Unicode line separator (U+2028) and paragraph separator (U+2029)
// These are considered line breaks but aren't \n or \r // These are considered line breaks but aren't \n or \r
const fileContent = [ const fileContent = ["keep this", "alias test=value", "keep that"].join(
"keep this", "\u2028"
"alias test=value", ); // Unicode line separator
"keep that"
].join("\u2028"); // Unicode line separator
fs.writeFileSync(testFile, fileContent, "utf-8"); fs.writeFileSync(testFile, fileContent, "utf-8");
@ -110,4 +113,72 @@ describe("removeLinesMatchingPatternTests", () => {
assert.ok(result.includes("keep that"), "Should preserve last part"); assert.ok(result.includes("keep that"), "Should preserve last part");
}); });
it("should handle Windows CRLF line endings without creating empty lines", async () => {
// Import helpers after setting up the mock
const { removeLinesMatchingPattern } = await import("./helpers.js");
// Create a file with Windows CRLF line endings
const fileContent = [
"# comment 1",
"alias npm='aikido-npm'",
"# comment 2",
"export PATH=$PATH:/usr/local/bin",
"",
"# comment 3",
].join("\r\n"); // Windows line endings
fs.writeFileSync(testFile, fileContent, "utf-8");
// Try to remove lines containing 'alias'
const pattern = /alias/;
removeLinesMatchingPattern(testFile, pattern, "\r\n");
const result = fs.readFileSync(testFile, "utf-8");
// Should preserve non-matching lines without adding empty lines
assert.ok(result.includes("# comment 1"), "Should preserve first comment");
assert.ok(result.includes("# comment 2"), "Should preserve second comment");
assert.ok(result.includes("# comment 3"), "Should preserve third comment");
assert.ok(result.includes("export PATH"), "Should preserve export line");
assert.ok(!result.includes("alias npm"), "Should remove alias line");
// The key test: when we split on \r\n, we should get exactly 4 lines
// Bug: if split(/[\r\n]/) was used, it creates empty lines between each real line
// because \r\n becomes two separators, resulting in: ["# comment 1", "", "# comment 2", "", "export...", "", "# comment 3", ""]
const resultLines = result.split("\r\n");
assert.strictEqual(resultLines.length, 5, "Should have exactly 5 lines");
});
it("should not remove empty lines on unix line endings", async () => {
// Import helpers after setting up the mock
const { removeLinesMatchingPattern } = await import("./helpers.js");
// Create a file with Unix line endings and empty lines
const fileContent = [
"# comment 1",
"alias npm='aikido-npm'",
"# comment 2",
"export PATH=$PATH:/usr/local/bin",
"",
"# comment 3",
].join("\n"); // Unix line endings
fs.writeFileSync(testFile, fileContent, "utf-8");
// Try to remove lines containing 'alias'
const pattern = /alias/;
removeLinesMatchingPattern(testFile, pattern, "\n");
const result = fs.readFileSync(testFile, "utf-8");
// Should preserve non-matching lines including empty lines
assert.ok(result.includes("# comment 1"), "Should preserve first comment");
assert.ok(result.includes("# comment 2"), "Should preserve second comment");
assert.ok(result.includes("# comment 3"), "Should preserve third comment");
assert.ok(result.includes("export PATH"), "Should preserve export line");
assert.ok(!result.includes("alias npm"), "Should remove alias line");
const resultLines = result.split("\n");
assert.strictEqual(resultLines.length, 5, "Should have exactly 5 lines");
});
}); });