Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion careful/bin/check-careful.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CMD_LOWER=$(printf '%s' "$CMD" | tr '[:upper:]' '[:lower:]')
# --- Check for safe exceptions (rm -rf of build artifacts) ---
if printf '%s' "$CMD" | grep -qE 'rm\s+(-[a-zA-Z]*r[a-zA-Z]*\s+|--recursive\s+)' 2>/dev/null; then
SAFE_ONLY=true
RM_ARGS=$(printf '%s' "$CMD" | sed -E 's/.*rm\s+(-[a-zA-Z]+\s+)*//;s/--recursive\s*//')
RM_ARGS=$(printf '%s' "$CMD" | sed -E 's/.*rm[[:space:]]+(-[a-zA-Z]+[[:space:]]+)*//;s/--recursive[[:space:]]*//')
for target in $RM_ARGS; do
case "$target" in
*/node_modules|node_modules|*/\.next|\.next|*/dist|dist|*/__pycache__|__pycache__|*/\.cache|\.cache|*/build|build|*/\.turbo|\.turbo|*/coverage|coverage)
Expand Down
22 changes: 2 additions & 20 deletions test/hook-scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ function withFreezeDir(freezePath: string, fn: (stateDir: string) => void) {
}
}

// Detect whether the safe-rm-targets regex works on this platform.
// macOS sed -E does not support \s, so the safe exception check fails there.
function detectSafeRmWorks(): boolean {
const { output } = runHook(CAREFUL_SCRIPT, carefulInput('rm -rf node_modules'));
return output.permissionDecision === undefined;
}

// ============================================================
// check-careful.sh tests
// ============================================================
Expand All @@ -88,24 +81,13 @@ describe('check-careful.sh', () => {
test('rm -rf node_modules allows (safe exception)', () => {
const { exitCode, output } = runHook(CAREFUL_SCRIPT, carefulInput('rm -rf node_modules'));
expect(exitCode).toBe(0);
if (detectSafeRmWorks()) {
// GNU sed: safe exception triggers, allows through
expect(output.permissionDecision).toBeUndefined();
} else {
// macOS sed: safe exception regex uses \\s which is unsupported,
// so the safe-targets check fails and the command warns
expect(output.permissionDecision).toBe('ask');
}
expect(output.permissionDecision).toBeUndefined();
});

test('rm -rf .next dist allows (multiple safe targets)', () => {
const { exitCode, output } = runHook(CAREFUL_SCRIPT, carefulInput('rm -rf .next dist'));
expect(exitCode).toBe(0);
if (detectSafeRmWorks()) {
expect(output.permissionDecision).toBeUndefined();
} else {
expect(output.permissionDecision).toBe('ask');
}
expect(output.permissionDecision).toBeUndefined();
});

test('rm -rf node_modules /var/data warns (mixed safe+unsafe)', () => {
Expand Down