@hasna/hooks 0.15.1 → 0.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schema": 1,
|
|
3
3
|
"package": "@hasna/hooks",
|
|
4
|
-
"version": "0.15.
|
|
4
|
+
"version": "0.15.2",
|
|
5
5
|
"hooks": {
|
|
6
6
|
"affected-tests": {
|
|
7
7
|
"version": "0.1.0",
|
|
@@ -176,7 +176,7 @@
|
|
|
176
176
|
"pre-bash": {
|
|
177
177
|
"version": "0.1.0",
|
|
178
178
|
"script": "hooks/pre-bash/src/hook.ts",
|
|
179
|
-
"sha256": "
|
|
179
|
+
"sha256": "76bd1336efd61eaf8497f93df017236f4382fba664fdb921699c2bf51a05d196"
|
|
180
180
|
},
|
|
181
181
|
"precompact": {
|
|
182
182
|
"version": "0.1.0",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, type Stats, writeFileSync, writeSync } from "node:fs";
|
|
1
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, statSync, type Stats, writeFileSync, writeSync } from "node:fs";
|
|
2
2
|
import { basename, dirname, isAbsolute, join, parse, relative, resolve, sep } from "node:path";
|
|
3
3
|
import { homedir, tmpdir } from "node:os";
|
|
4
4
|
|
|
@@ -149,8 +149,11 @@ export interface GitCommandInfo {
|
|
|
149
149
|
function splitShellSegmentsPass(
|
|
150
150
|
command: string,
|
|
151
151
|
atomicSubstitutions: boolean
|
|
152
|
-
): { segments: string[]; isolation: boolean[]; depths: number[]; groups: number[]; piped: boolean[]; shortCircuit: boolean[]; unterminated: boolean } {
|
|
152
|
+
): { segments: string[]; isolation: boolean[]; depths: number[]; groups: number[]; piped: boolean[]; shortCircuit: boolean[]; followedBy: string[]; unterminated: boolean } {
|
|
153
153
|
const segments: string[] = [];
|
|
154
|
+
// The separator after each segment: ";", "&", "&&", "||", "|", "(", ")", "\n" or "end".
|
|
155
|
+
// The `&` of a redirection such as `2>&1` is not a separator and stays in the segment.
|
|
156
|
+
const followedBy: string[] = [];
|
|
154
157
|
const isolation: boolean[] = [];
|
|
155
158
|
const depths: number[] = [];
|
|
156
159
|
const groups: number[] = [];
|
|
@@ -170,9 +173,10 @@ function splitShellSegmentsPass(
|
|
|
170
173
|
let groupCounter = 0;
|
|
171
174
|
const groupStack: number[] = [0];
|
|
172
175
|
|
|
173
|
-
const flush = (nextSeparator: string | null) => {
|
|
176
|
+
const flush = (nextSeparator: string | null, separatorToken: string = nextSeparator ?? "end") => {
|
|
174
177
|
if (current.trim()) {
|
|
175
178
|
segments.push(current.trim());
|
|
179
|
+
followedBy.push(separatorToken);
|
|
176
180
|
// A stage of a pipeline runs in its own process, as does anything inside `( … )`.
|
|
177
181
|
isolation.push(parenDepth > 0 || pipedFromPrevious || nextSeparator === "|");
|
|
178
182
|
depths.push(parenDepth);
|
|
@@ -235,8 +239,16 @@ function splitShellSegmentsPass(
|
|
|
235
239
|
}
|
|
236
240
|
if (ch === ";" || ch === "|" || ch === "&" || ch === "(" || ch === ")" || ch === "\n") {
|
|
237
241
|
const doubled = (ch === "|" || ch === "&") && command[i + 1] === ch;
|
|
242
|
+
// A single `&` inside a redirection (`2>&1`, `>&2`, `<&0`, `&>file`, `&>>file`) is not a
|
|
243
|
+
// separator at all: it stays inside the segment, so a real background `&` after it
|
|
244
|
+
// (`cd /tmp >/dev/null 2>&1 & rm -rf x`) still ends the cd's own segment (HAA-00113).
|
|
245
|
+
// An escaped `\>` is a literal character, not a redirection.
|
|
246
|
+
if (ch === "&" && !doubled && (/(^|[^\\])(\\\\)*[<>]$/.test(current) || command[i + 1] === ">")) {
|
|
247
|
+
current += ch;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
238
250
|
// `||` and `&&` are sequencing, not a pipe.
|
|
239
|
-
flush(ch === "|" && !doubled ? "|" : null);
|
|
251
|
+
flush(ch === "|" && !doubled ? "|" : null, doubled ? ch + ch : ch);
|
|
240
252
|
// `a && X=1` and `a || X=1` run X= only if the left side decided so.
|
|
241
253
|
precededByShortCircuit = doubled && (ch === "|" || ch === "&");
|
|
242
254
|
if (ch === "(") {
|
|
@@ -254,7 +266,7 @@ function splitShellSegmentsPass(
|
|
|
254
266
|
}
|
|
255
267
|
|
|
256
268
|
flush(null);
|
|
257
|
-
return { segments, isolation, depths, groups, piped: pipedFlags, shortCircuit: shortCircuitFlags, unterminated: substitutionDepth > 0 || inBacktick };
|
|
269
|
+
return { segments, isolation, depths, groups, piped: pipedFlags, shortCircuit: shortCircuitFlags, followedBy, unterminated: substitutionDepth > 0 || inBacktick };
|
|
258
270
|
}
|
|
259
271
|
|
|
260
272
|
function splitShellSegments(command: string): string[] {
|
|
@@ -272,6 +284,12 @@ interface ShellSegment {
|
|
|
272
284
|
piped: boolean;
|
|
273
285
|
/** Reached only via `&&` / `||`, so whether it ran depends on the previous command. */
|
|
274
286
|
shortCircuit: boolean;
|
|
287
|
+
/**
|
|
288
|
+
* The separator after this segment: ";", "&", "&&", "||", "|", "(", ")", "\n" or "end".
|
|
289
|
+
* A `cd` followed by a single "&" runs in the background and moves nothing; one followed
|
|
290
|
+
* by anything but "&&" may have failed while the next command still runs.
|
|
291
|
+
*/
|
|
292
|
+
followedBy: string;
|
|
275
293
|
/**
|
|
276
294
|
* True when the segment runs in a subshell `( … )` or as a stage of a pipeline. A `cd`
|
|
277
295
|
* there affects only that child process, so treating it as persistent silently moves the
|
|
@@ -304,6 +322,7 @@ function splitShellSegmentsUncached(command: string): ShellSegment[] {
|
|
|
304
322
|
group: chosen.groups[index] ?? 0,
|
|
305
323
|
piped: chosen.piped[index] ?? false,
|
|
306
324
|
shortCircuit: chosen.shortCircuit[index] ?? false,
|
|
325
|
+
followedBy: chosen.followedBy[index] ?? "end",
|
|
307
326
|
isolated: chosen.isolation[index] ?? false,
|
|
308
327
|
}));
|
|
309
328
|
}
|
|
@@ -1587,7 +1606,14 @@ export function ownScratchCommandAllowed(command: string): boolean {
|
|
|
1587
1606
|
const pieces = command.split(/[;&|\n\r]+/).map((piece) => piece.trim()).filter(Boolean);
|
|
1588
1607
|
if (pieces.length > 0 && LEADING_CD.test(pieces[0])) pieces.shift();
|
|
1589
1608
|
if (pieces.length === 0) return false;
|
|
1590
|
-
return pieces.every((piece) =>
|
|
1609
|
+
return pieces.every((piece) => {
|
|
1610
|
+
const words = piece.split(/\s+/);
|
|
1611
|
+
if (!DELETE_ONLY_WORD.test(words[0])) return false;
|
|
1612
|
+
// `rmdir -p <own>/b` removes <own>/b and then every empty parent, the session folder
|
|
1613
|
+
// itself included, which the exception refuses (HAA-00114).
|
|
1614
|
+
if (words[0].endsWith("rmdir") && words.slice(1).some((word) => word === "--parents" || /^-[A-Za-z]*p[A-Za-z]*$/.test(word))) return false;
|
|
1615
|
+
return true;
|
|
1616
|
+
});
|
|
1591
1617
|
}
|
|
1592
1618
|
|
|
1593
1619
|
/**
|
|
@@ -1713,6 +1739,45 @@ function realpathThroughExisting(path: string): string | null {
|
|
|
1713
1739
|
}
|
|
1714
1740
|
}
|
|
1715
1741
|
|
|
1742
|
+
/** Uncertain cds (that may have failed) tracked before the analysis fails closed. */
|
|
1743
|
+
const MAX_UNCERTAIN_CDS = 8;
|
|
1744
|
+
|
|
1745
|
+
/**
|
|
1746
|
+
* A real location re-spelled under a protected root as the rules write it: when the root
|
|
1747
|
+
* itself sits behind a link (macOS /tmp is /private/tmp), a real path inside the root's real
|
|
1748
|
+
* location is mapped back under the root's own spelling, so the rule still recognises it.
|
|
1749
|
+
*/
|
|
1750
|
+
function inRuleSpelling(realPath: string, root: string): string {
|
|
1751
|
+
const realRoot = realpathThroughExisting(root);
|
|
1752
|
+
if (!realRoot || realRoot === resolve(root)) return realPath;
|
|
1753
|
+
if (realPath === realRoot) return resolve(root);
|
|
1754
|
+
return isInsidePath(realPath, realRoot) ? join(resolve(root), relative(realRoot, realPath)) : realPath;
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
/** True only for a path that exists now and is a directory (links followed, as cd does). */
|
|
1758
|
+
function isExistingDirectory(path: string): boolean {
|
|
1759
|
+
try {
|
|
1760
|
+
return statSync(path).isDirectory();
|
|
1761
|
+
} catch {
|
|
1762
|
+
return false;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
/**
|
|
1767
|
+
* Where an `rm` of `path` really acts: the real location of its parent folder (every link
|
|
1768
|
+
* there followed, as the kernel does) plus its own last name, which rm removes without
|
|
1769
|
+
* following. Null when the parent cannot be resolved, or when it equals `path`.
|
|
1770
|
+
*/
|
|
1771
|
+
function realParentLocation(path: string): string | null {
|
|
1772
|
+
const resolved = resolve(path);
|
|
1773
|
+
const parent = dirname(resolved);
|
|
1774
|
+
if (parent === resolved) return null;
|
|
1775
|
+
const realParent = realpathThroughExisting(parent);
|
|
1776
|
+
if (!realParent) return null;
|
|
1777
|
+
const real = join(realParent, basename(resolved));
|
|
1778
|
+
return real === resolved ? null : real;
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1716
1781
|
function isMissingPathError(error: unknown): boolean {
|
|
1717
1782
|
return error instanceof Error && "code" in error && (error as NodeJS.ErrnoException).code === "ENOENT";
|
|
1718
1783
|
}
|
|
@@ -2467,6 +2532,9 @@ function cwdTrackedSegments(command: string, baseCwd: string, nonEmptyNames: Rea
|
|
|
2467
2532
|
// outright left `(cd / && rm -rf *)`, the standard "cd without moving my shell" idiom,
|
|
2468
2533
|
// completely unguarded. Depth 0 is the parent shell.
|
|
2469
2534
|
let cdOperations = 0;
|
|
2535
|
+
// cds that may have failed widen the set of folders a later relative delete is judged in;
|
|
2536
|
+
// past this many the set is not tracked further and the analysis fails closed instead.
|
|
2537
|
+
let uncertainCds = 0;
|
|
2470
2538
|
let stack: Array<{ group: number; cwds: string[]; previous: string[]; dirStack: string[][]; explicit: boolean }> = [
|
|
2471
2539
|
{ group: 0, cwds: [baseCwd], previous: [baseCwd], dirStack: [], explicit: false },
|
|
2472
2540
|
];
|
|
@@ -2489,7 +2557,7 @@ function cwdTrackedSegments(command: string, baseCwd: string, nonEmptyNames: Rea
|
|
|
2489
2557
|
return stack[depth];
|
|
2490
2558
|
};
|
|
2491
2559
|
|
|
2492
|
-
splitShellSegmentsDetailed(command).forEach(({ text: segment, depth, group, piped, shortCircuit }, segmentIndex) => {
|
|
2560
|
+
splitShellSegmentsDetailed(command).forEach(({ text: segment, depth, group, piped, shortCircuit, followedBy }, segmentIndex) => {
|
|
2493
2561
|
const frame = frameFor(depth, group);
|
|
2494
2562
|
// A leading `{` from a brace group is not part of the command.
|
|
2495
2563
|
const tokens = shellWords(segment).filter((token, index) => !(index === 0 && (token === "{" || token === "}")));
|
|
@@ -2514,6 +2582,9 @@ function cwdTrackedSegments(command: string, baseCwd: string, nonEmptyNames: Rea
|
|
|
2514
2582
|
// reached via `&&`/`||` may not run at all: `cd /home/hasna; false && cd /tmp;
|
|
2515
2583
|
// rm -rf *` left the guard in /tmp while bash stayed in the home directory.
|
|
2516
2584
|
if (piped) return;
|
|
2585
|
+
// `cd /tmp & rm -rf x` runs the cd in a background job: the shell that runs the rm
|
|
2586
|
+
// never moved, so the tracked cwd must not move either (HAA-00113).
|
|
2587
|
+
if (followedBy === "&") return;
|
|
2517
2588
|
if (shortCircuit) {
|
|
2518
2589
|
analysis.degraded = true;
|
|
2519
2590
|
return;
|
|
@@ -2583,7 +2654,21 @@ function cwdTrackedSegments(command: string, baseCwd: string, nonEmptyNames: Rea
|
|
|
2583
2654
|
next.add(resolveFrom(current, operand));
|
|
2584
2655
|
if (collapsed !== null) next.add(resolveFrom(current, collapsed));
|
|
2585
2656
|
}
|
|
2586
|
-
|
|
2657
|
+
// A cd may fail (a missing folder, no permission) and the shell runs the next
|
|
2658
|
+
// command anyway unless the two are joined by `&&`: `cd /nonexistent; rm -rf state`
|
|
2659
|
+
// deletes `state` in the folder the shell was already in. Unless the cd is followed
|
|
2660
|
+
// by `&&`, or every folder it can land in exists as a directory right now, the
|
|
2661
|
+
// folder before it stays a candidate too (HAA-00113).
|
|
2662
|
+
const landed = [...next];
|
|
2663
|
+
const mayFail = followedBy !== "&&" && uncertainCds <= MAX_UNCERTAIN_CDS && !landed.every((dir) => isExistingDirectory(dir));
|
|
2664
|
+
let candidates = landed;
|
|
2665
|
+
if (mayFail) {
|
|
2666
|
+
uncertainCds += 1;
|
|
2667
|
+
if (uncertainCds > MAX_UNCERTAIN_CDS) analysis.degraded = true;
|
|
2668
|
+
else candidates = [...new Set([...landed, ...priorCwds])];
|
|
2669
|
+
}
|
|
2670
|
+
if (candidates.length > MAX_CWD_VARIANTS) analysis.degraded = true;
|
|
2671
|
+
frame.cwds = candidates.slice(0, MAX_CWD_VARIANTS);
|
|
2587
2672
|
if (isAbsolute(expandHome(operand)) || collapsed !== null) frame.explicit = true;
|
|
2588
2673
|
}
|
|
2589
2674
|
frame.previous = priorCwds;
|
|
@@ -2796,27 +2881,42 @@ export async function classifyDangerousOperation(input: CodewithHookInput): Prom
|
|
|
2796
2881
|
return extraRule ? [...rules, extraRule] : rules;
|
|
2797
2882
|
};
|
|
2798
2883
|
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2884
|
+
// Judge the target where the command names it, and also where it really acts: a link
|
|
2885
|
+
// in a parent folder (`cd ~/link && rm -rf state`, `rm -rf ~/link/state`, with
|
|
2886
|
+
// ~/link -> ~/.hasna) moves the delete into a protected root the text never names
|
|
2887
|
+
// (HAA-00113). rm removes the last name itself without following it.
|
|
2888
|
+
const realLocation = realParentLocation(targetPath);
|
|
2889
|
+
// A trailing slash (`rm -rf ~/link/`) makes rm follow the last name too, so the delete
|
|
2890
|
+
// acts inside wherever that link finally points (HAA-00113 review).
|
|
2891
|
+
const realTarget = /[\\/]\.?$/.test(target.path) ? realpathThroughExisting(targetPath) : null;
|
|
2892
|
+
const judgedLocations = [targetPath];
|
|
2893
|
+
for (const location of [realLocation, realTarget]) {
|
|
2894
|
+
if (location && !judgedLocations.includes(location)) judgedLocations.push(location);
|
|
2895
|
+
}
|
|
2896
|
+
for (const judged of judgedLocations) {
|
|
2897
|
+
for (const rule of rulesFor(judged)) {
|
|
2898
|
+
const ruleJudged = judged === targetPath ? judged : inRuleSpelling(judged, rule.root);
|
|
2899
|
+
if (threatensRule(ruleJudged, rule, currentManagedRepoRoot)) {
|
|
2900
|
+
// A session removing content of its own scratch folder (session id from the
|
|
2901
|
+
// harness, not the command) is not a threat to the ~/.hasna tree.
|
|
2902
|
+
// The target must be written as an absolute path (the same wherever the command
|
|
2903
|
+
// runs), so no `cd`, `pushd` or `cd -P` before it can move what it means.
|
|
2904
|
+
if (
|
|
2905
|
+
rule.label === "Hasna state root ~/.hasna" &&
|
|
2906
|
+
ownScratchCommandAllowed(command) &&
|
|
2907
|
+
isAbsolute(expandHome(target.path)) &&
|
|
2908
|
+
!target.path.split(/[\\/]/).some((part) => part === ".." || part === ".") &&
|
|
2909
|
+
isOwnScratchContent(ruleJudged, rule.root, input.session_id)
|
|
2910
|
+
) continue;
|
|
2911
|
+
return {
|
|
2912
|
+
block: true,
|
|
2913
|
+
targetPath: ruleJudged,
|
|
2914
|
+
protectedPath: rule.root,
|
|
2915
|
+
protectedLabel: rule.label,
|
|
2916
|
+
operation: target.operation,
|
|
2917
|
+
reason: scopedBlockReason(target.operation, ruleJudged, rule, target.remote) + ownScratchHint(ruleJudged, rule),
|
|
2918
|
+
};
|
|
2919
|
+
}
|
|
2820
2920
|
}
|
|
2821
2921
|
}
|
|
2822
2922
|
|
|
@@ -150,7 +150,7 @@ async function runStagedSecretsScan(targetCwd: string, targetCertain: boolean):
|
|
|
150
150
|
status: "skipped",
|
|
151
151
|
scanned: false,
|
|
152
152
|
code: "gitleaks-inconclusive",
|
|
153
|
-
detail: `git could not resolve a work tree for ${targetCwd}${top.exitCode ? ` (git rev-parse exited ${top.exitCode})` : ""}, so this git command was NOT scanned for staged secrets
|
|
153
|
+
detail: `git could not resolve a work tree for ${targetCwd}${top.exitCode ? ` (git rev-parse exited ${top.exitCode})` : ""}, so this git command was NOT scanned for staged secrets.${/\$/.test(targetCwd) ? " The repository path holds a shell variable the scan cannot expand: write the path literally (git -C /full/path/to/repo …)." : ""}`,
|
|
154
154
|
};
|
|
155
155
|
}
|
|
156
156
|
|
package/package.json
CHANGED