Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(safety): read Remove-Item's -Recurse after an en dash, em dash or horizontal bar

DheerajPiduru52591e782d06d3
diff --git a/server/safety-floor.test.ts b/server/safety-floor.test.ts
index 684f7734d2..d037a3f012 100644
--- a/server/safety-floor.test.ts
+++ b/server/safety-floor.test.ts
@@ -220,6 +220,11 @@ const BLOCKED: Case[] = [
["powershell.exe -NoProfile -Command Remove-Item -Recurse C:\\Users", "delete-system-directory", "win32"],
["rm -r -fo C:\\", "delete-root", "win32"],
["rm -Recurse -Force C:\\Windows", "delete-system-directory", "win32"],
+ ["Remove-Item C:\\ \u2013Recurse \u2013Force", "delete-root", "win32"],
+ ["Remove-Item \u2014Recurse C:\\Windows", "delete-system-directory", "win32"],
+ ["ri \u2015r $HOME", "delete-home", "win32"],
+ ["rm \u2013r \u2013fo C:\\Users", "delete-system-directory", "win32"],
+ ["pwsh -c 'Remove-Item \u2013Recurse -Force /'", "delete-root"],
// Windows: Git Bash and WSL spellings of a drive
["rm -rf /c", "delete-root", "win32"],
["rm -rf /c/Users", "delete-system-directory", "win32"],
@@ -480,6 +485,7 @@ const ALLOWED: Array<[command: string, on?: Platform | "posix"]> = [
["del /s /q C:\\Temp\\*"],
["del C:\\Windows\\Temp\\a.log"],
["Remove-Item -Recurse -Force .\\node_modules"],
+ ["Remove-Item \u2013Recurse \u2013Force .\\node_modules"],
["Remove-Item -Recurse $env:TEMP\\work"],
['Remove-Item -Recurse "$env:USERPROFILE\\Downloads\\old"'],
["Remove-Item -Recurse '$HOME'"],
diff --git a/server/safety-floor.ts b/server/safety-floor.ts
index ed8e0b7774..746e491cb7 100644
--- a/server/safety-floor.ts
+++ b/server/safety-floor.ts
@@ -555,6 +555,11 @@ function windowsTarget(word: string, percent: boolean, host: Host): FloorVerdict
return home && drive && home === `${drive}\\${parts.join("\\")}`.toLowerCase() ? DELETE_HOME : null;
}
+/** PowerShell also takes an en dash, em dash or horizontal bar where a parameter's dash goes. */
+function parameterName(arg: string): string {
+ return arg.toLowerCase().replace(/^[\u2013\u2014\u2015]/, "-");
+}
+
/** `powershell`: PowerShell on macOS or Linux, whose deletes take POSIX paths. */
type Mode = "posix" | "windows" | "powershell";
@@ -636,8 +641,8 @@ function judge(command: Command, source: string, pass: Pass, mode: Mode, depth:
if (mode === "powershell") {
// Remove-Item's aliases off Windows, where `rm` and `rmdir` are the real programs the POSIX reading judges.
if (!["remove-item", "ri", "rd", "del", "erase"].includes(program)) return null;
- const isSwitch = (arg: string) => /^-./.test(arg);
- if (!args.some((arg) => isSwitch(arg) && "-recurse".startsWith(arg.toLowerCase()))) return null;
+ const isSwitch = (arg: string) => /^-./.test(parameterName(arg));
+ if (!args.some((arg) => isSwitch(arg) && "-recurse".startsWith(parameterName(arg)))) return null;
for (const target of args) {
// PowerShell takes either slash in a path.
const verdict = isSwitch(target) ? null : posixTarget(target.replace(/\\/g, "/"), host);
@@ -650,10 +655,10 @@ function judge(command: Command, source: string, pass: Pass, mode: Mode, depth:
const cmdStyle = program === "rd" || program === "rmdir" || program === "del" || program === "erase";
if (cmdStyle || program === "ri" || program === "remove-item" || program === "rm") {
// cmd switches are /s /q or /s/q; PowerShell takes -Recurse or any unambiguous start of it.
- const isSwitch = (arg: string) => /^-./.test(arg) || (cmdStyle && /^\/[A-Za-z?]/.test(arg));
+ const isSwitch = (arg: string) => /^-./.test(parameterName(arg)) || (cmdStyle && /^\/[A-Za-z?]/.test(arg));
const recursive = args.some((arg) => isSwitch(arg) && (arg[0] === "/"
? arg.toLowerCase().split("/").includes("s")
- : "-recurse".startsWith(arg.toLowerCase())));
+ : "-recurse".startsWith(parameterName(arg))));
if (!recursive) return null;
for (const target of args) {
// After `rm` a lone backslash is a POSIX line continuation; after the others it is the drive root.
@@ -665,12 +670,11 @@ function judge(command: Command, source: string, pass: Pass, mode: Mode, depth:
}
if (program === "format") return args.some((arg) => /^[A-Za-z]:\\?$/.test(arg)) ? FORMAT_DISK : null;
// Windows Storage module: Format-Volume formats whatever volume it is given, also one piped in; Clear-Disk erases
- // a disk's data only with -RemoveData, and refuses a disk holding data partitions without it. PowerShell also takes
- // an en dash, em dash or horizontal bar where a parameter's dash goes.
+ // a disk's data only with -RemoveData, and refuses a disk holding data partitions without it.
if (program === "format-volume") return FORMAT_DISK;
if (program === "clear-disk") {
return args.some((arg) => {
- const name = arg.split(":")[0].toLowerCase().replace(/^[\u2013\u2014\u2015]/, "-");
+ const name = parameterName(arg.split(":")[0]);
return name.length >= "-removed".length && "-removedata".startsWith(name);
}) ? FORMAT_DISK : null;
}