Use safeSpawn

This commit is contained in:
Sander Declerck 2026-02-05 10:24:28 +01:00
parent 3e90c0abd1
commit aa461b27c3
No known key found for this signature in database
7 changed files with 62 additions and 63 deletions

View file

@ -26,27 +26,28 @@ function teardown(tools) {
// Remove any existing alias for the tool
removeLinesMatchingPattern(
startupFile,
new RegExp(`^Set-Alias\\s+${tool}\\s+`)
new RegExp(`^Set-Alias\\s+${tool}\\s+`),
);
}
// Remove the line that sources the safe-chain PowerShell initialization script
removeLinesMatchingPattern(
startupFile,
/^\.\s+["']?\$HOME[/\\].safe-chain[/\\]scripts[/\\]init-pwsh\.ps1["']?/
/^\.\s+["']?\$HOME[/\\].safe-chain[/\\]scripts[/\\]init-pwsh\.ps1["']?/,
);
return true;
}
function setup() {
async function setup() {
// Check execution policy
const { isValid, policy } = validatePowerShellExecutionPolicy(executableName);
const { isValid, policy } =
await validatePowerShellExecutionPolicy(executableName);
if (!isValid) {
throw new Error(
`PowerShell execution policy is set to '${policy}', which prevents safe-chain from running. ` +
`To fix this, open PowerShell as Administrator and run: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned. ` +
`For more information, see: https://github.com/AikidoSec/safe-chain/blob/main/docs/troubleshooting.md#powershell-execution-policy-blocks-scripts-windows`
`For more information, see: https://github.com/AikidoSec/safe-chain/blob/main/docs/troubleshooting.md#powershell-execution-policy-blocks-scripts-windows`,
);
}
@ -54,7 +55,7 @@ function setup() {
addLineToFile(
startupFile,
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`,
);
return true;
@ -68,7 +69,7 @@ function getStartupFile() {
}).trim();
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
`Command failed: ${startupFileCommand}. Error: ${error.message}`,
);
}
}

View file

@ -76,8 +76,8 @@ describe("PowerShell Core shell integration", () => {
});
describe("setup", () => {
it("should add init-pwsh.ps1 source line", () => {
const result = powershell.setup();
it("should add init-pwsh.ps1 source line", async () => {
const result = await powershell.setup();
assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -175,9 +175,9 @@ describe("PowerShell Core shell integration", () => {
});
describe("integration tests", () => {
it("should handle complete setup and teardown cycle", () => {
it("should handle complete setup and teardown cycle", async () => {
// Setup
powershell.setup();
await powershell.setup();
let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(
content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'),
@ -191,10 +191,10 @@ describe("PowerShell Core shell integration", () => {
);
});
it("should handle multiple setup calls", () => {
powershell.setup();
it("should handle multiple setup calls", async () => {
await powershell.setup();
powershell.teardown(knownAikidoTools);
powershell.setup();
await powershell.setup();
const content = fs.readFileSync(mockStartupFile, "utf-8");
const sourceMatches = (
@ -206,13 +206,13 @@ describe("PowerShell Core shell integration", () => {
});
describe("execution policy", () => {
it(`should throw for restricted policies`, () => {
it(`should throw for restricted policies`, async () => {
executionPolicyResult = {
isValid: false,
policy: "Restricted",
};
assert.throws(
await assert.rejects(
() => powershell.setup(),
(err) =>
err.message.startsWith(

View file

@ -26,27 +26,28 @@ function teardown(tools) {
// Remove any existing alias for the tool
removeLinesMatchingPattern(
startupFile,
new RegExp(`^Set-Alias\\s+${tool}\\s+`)
new RegExp(`^Set-Alias\\s+${tool}\\s+`),
);
}
// Remove the line that sources the safe-chain PowerShell initialization script
removeLinesMatchingPattern(
startupFile,
/^\.\s+["']?\$HOME[/\\].safe-chain[/\\]scripts[/\\]init-pwsh\.ps1["']?/
/^\.\s+["']?\$HOME[/\\].safe-chain[/\\]scripts[/\\]init-pwsh\.ps1["']?/,
);
return true;
}
function setup() {
async function setup() {
// Check execution policy
const { isValid, policy } = validatePowerShellExecutionPolicy(executableName);
const { isValid, policy } =
await validatePowerShellExecutionPolicy(executableName);
if (!isValid) {
throw new Error(
`PowerShell execution policy is set to '${policy}', which prevents safe-chain from running. ` +
`To fix this, open PowerShell as Administrator and run: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned. ` +
`For more information, see: https://github.com/AikidoSec/safe-chain/blob/main/docs/troubleshooting.md#powershell-execution-policy-blocks-scripts-windows`
`For more information, see: https://github.com/AikidoSec/safe-chain/blob/main/docs/troubleshooting.md#powershell-execution-policy-blocks-scripts-windows`,
);
}
@ -54,7 +55,7 @@ function setup() {
addLineToFile(
startupFile,
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`,
);
return true;
@ -68,7 +69,7 @@ function getStartupFile() {
}).trim();
} catch (/** @type {any} */ error) {
throw new Error(
`Command failed: ${startupFileCommand}. Error: ${error.message}`
`Command failed: ${startupFileCommand}. Error: ${error.message}`,
);
}
}

View file

@ -76,8 +76,8 @@ describe("Windows PowerShell shell integration", () => {
});
describe("setup", () => {
it("should add init-pwsh.ps1 source line", () => {
const result = windowsPowershell.setup();
it("should add init-pwsh.ps1 source line", async () => {
const result = await windowsPowershell.setup();
assert.strictEqual(result, true);
const content = fs.readFileSync(mockStartupFile, "utf-8");
@ -175,9 +175,9 @@ describe("Windows PowerShell shell integration", () => {
});
describe("integration tests", () => {
it("should handle complete setup and teardown cycle", () => {
it("should handle complete setup and teardown cycle", async () => {
// Setup
windowsPowershell.setup();
await windowsPowershell.setup();
let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(
content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'),
@ -191,10 +191,10 @@ describe("Windows PowerShell shell integration", () => {
);
});
it("should handle multiple setup calls", () => {
windowsPowershell.setup();
it("should handle multiple setup calls", async () => {
await windowsPowershell.setup();
windowsPowershell.teardown(knownAikidoTools);
windowsPowershell.setup();
await windowsPowershell.setup();
const content = fs.readFileSync(mockStartupFile, "utf-8");
const sourceMatches = (
@ -206,13 +206,13 @@ describe("Windows PowerShell shell integration", () => {
});
describe("execution policy", () => {
it(`should throw for restricted policies`, () => {
it(`should throw for restricted policies`, async () => {
executionPolicyResult = {
isValid: false,
policy: "Restricted",
};
assert.throws(
await assert.rejects(
() => windowsPowershell.setup(),
(err) =>
err.message.startsWith(