mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
import {
|
|
addLineToFile,
|
|
doesExecutableExistOnSystem,
|
|
removeLinesMatchingPattern,
|
|
validatePowerShellExecutionPolicy,
|
|
} from "../helpers.js";
|
|
import { execSync } from "child_process";
|
|
|
|
const shellName = "PowerShell Core";
|
|
const executableName = "pwsh";
|
|
const startupFileCommand = "echo $PROFILE";
|
|
|
|
function isInstalled() {
|
|
return doesExecutableExistOnSystem(executableName);
|
|
}
|
|
|
|
/**
|
|
* @param {import("../helpers.js").AikidoTool[]} tools
|
|
*
|
|
* @returns {boolean}
|
|
*/
|
|
function teardown(tools) {
|
|
const startupFile = getStartupFile();
|
|
|
|
for (const { tool } of tools) {
|
|
// Remove any existing alias for the tool
|
|
removeLinesMatchingPattern(
|
|
startupFile,
|
|
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["']?/
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
function setup() {
|
|
// Check execution policy
|
|
const { isValid, policy } = 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`
|
|
);
|
|
}
|
|
|
|
const startupFile = getStartupFile();
|
|
|
|
addLineToFile(
|
|
startupFile,
|
|
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
function getStartupFile() {
|
|
try {
|
|
return execSync(startupFileCommand, {
|
|
encoding: "utf8",
|
|
shell: executableName,
|
|
}).trim();
|
|
} catch (/** @type {any} */ error) {
|
|
throw new Error(
|
|
`Command failed: ${startupFileCommand}. Error: ${error.message}`
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @type {import("../shellDetection.js").Shell}
|
|
*/
|
|
export default {
|
|
name: shellName,
|
|
isInstalled,
|
|
setup,
|
|
teardown,
|
|
};
|