@gotgenes/pi-permission-system 7.4.0 → 7.4.1

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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [7.4.1](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v7.4.0...pi-permission-system-v7.4.1) (2026-05-30)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **pi-permission-system:** resolve bash paths against leading cd target ([c655a7e](https://github.com/gotgenes/pi-packages/commit/c655a7e737aeac9a8f10909804260c65d339c8b7))
14
+
15
+
16
+ ### Documentation
17
+
18
+ * **pi-permission-system:** document cd-aware bash path resolution ([a2e6541](https://github.com/gotgenes/pi-packages/commit/a2e65410e89eb1e62579078c16af05aead013603))
19
+
8
20
  ## [7.4.0](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v7.3.3...pi-permission-system-v7.4.0) (2026-05-29)
9
21
 
10
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gotgenes/pi-permission-system",
3
- "version": "7.4.0",
3
+ "version": "7.4.1",
4
4
  "description": "Permission enforcement extension for the Pi coding agent.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,8 +1,9 @@
1
1
  import { createRequire } from "node:module";
2
- import { basename } from "node:path";
2
+ import { basename, resolve } from "node:path";
3
3
 
4
4
  import {
5
- isPathOutsideWorkingDirectory,
5
+ isPathWithinDirectory,
6
+ isSafeSystemPath,
6
7
  normalizePathForComparison,
7
8
  } from "#src/path-utils";
8
9
 
@@ -501,11 +502,90 @@ function classifyTokenAsPathCandidate(token: string): string | null {
501
502
  return null;
502
503
  }
503
504
 
505
+ // ── Leading cd detection ───────────────────────────────────────────────────
506
+
507
+ /**
508
+ * Walk down from the root to find the first `command` node in the program.
509
+ *
510
+ * Only descends into `program` and `list` nodes — subshells, pipelines, and
511
+ * other compound statements are ignored because a `cd` inside them does not
512
+ * affect the outer shell's working directory.
513
+ */
514
+ function findFirstCommand(node: TSNode): TSNode | null {
515
+ if (node.type === "command") return node;
516
+ if (node.type === "program" || node.type === "list") {
517
+ const firstChild = node.child(0);
518
+ if (firstChild) return findFirstCommand(firstChild);
519
+ }
520
+ return null;
521
+ }
522
+
523
+ /**
524
+ * Extract the target directory of a leading `cd` command from the parsed AST.
525
+ *
526
+ * When a bash command begins with `cd <dir> && …`, the shell resolves
527
+ * subsequent relative paths against `<dir>`, not the original working
528
+ * directory. The external-directory guard must do the same, otherwise a
529
+ * path that the shell keeps inside the working directory can appear to
530
+ * escape it and trigger a spurious permission prompt.
531
+ *
532
+ * Returns `undefined` when the first command is not `cd`, or when the
533
+ * target cannot be meaningfully resolved (`cd -`, bare `cd`, or `cd ~…`).
534
+ */
535
+ function extractLeadingCdTarget(rootNode: TSNode): string | undefined {
536
+ const firstCmd = findFirstCommand(rootNode);
537
+ if (!firstCmd) return undefined;
538
+
539
+ const cmdName = extractCommandName(firstCmd);
540
+ if (cmdName !== "cd") return undefined;
541
+
542
+ for (let i = 0; i < firstCmd.childCount; i++) {
543
+ const child = firstCmd.child(i);
544
+ if (!child) continue;
545
+ if (child.type === "command_name" || child.type === "variable_assignment")
546
+ continue;
547
+ if (!ARG_NODE_TYPES.has(child.type)) continue;
548
+
549
+ const text = resolveNodeText(child);
550
+ // Skip `--` (end-of-flags marker)
551
+ if (text === "--") continue;
552
+ // `cd -` jumps to $OLDPWD; `cd ~…` is home-relative — neither can be
553
+ // resolved against the working directory.
554
+ if (text === "-" || text.startsWith("~")) return undefined;
555
+ return text;
556
+ }
557
+ return undefined;
558
+ }
559
+
560
+ /**
561
+ * Compute the effective base directory for resolving relative path candidates.
562
+ *
563
+ * When the leading `cd` target stays within the working directory, subsequent
564
+ * relative paths should be resolved against it. An escaping target is itself
565
+ * an external access (reported via its own candidate token) and must never
566
+ * silence checks on subsequent paths, so the function falls back to `cwd`.
567
+ */
568
+ function computeEffectiveResolveBase(
569
+ cdTarget: string | undefined,
570
+ cwd: string,
571
+ ): string {
572
+ if (cdTarget === undefined) return cwd;
573
+ const resolved = resolve(cwd, cdTarget);
574
+ const normalizedCwd = resolve(cwd);
575
+ return isPathWithinDirectory(resolved, normalizedCwd) ? resolved : cwd;
576
+ }
577
+
578
+ // ── Public extractors ──────────────────────────────────────────────────────
579
+
504
580
  /**
505
581
  * Extracts paths from a bash command string that resolve outside CWD.
506
582
  * Uses tree-sitter-bash to parse the command into a full AST, then walks
507
583
  * command argument and redirect-destination nodes. Heredoc bodies, comments,
508
584
  * and other non-argument content are skipped, eliminating false positives.
585
+ *
586
+ * When the command begins with `cd <dir> && …`, relative candidate paths are
587
+ * resolved against `<dir>` (if it stays within CWD) rather than CWD itself,
588
+ * mirroring how the shell would resolve them.
509
589
  */
510
590
  export async function extractExternalPathsFromBashCommand(
511
591
  command: string,
@@ -515,13 +595,18 @@ export async function extractExternalPathsFromBashCommand(
515
595
  const tree = parser.parse(command);
516
596
  if (!tree) return [];
517
597
 
598
+ let cdTarget: string | undefined;
518
599
  const tokens: string[] = [];
519
600
  try {
601
+ cdTarget = extractLeadingCdTarget(tree.rootNode);
520
602
  collectPathCandidateTokens(tree.rootNode, tokens);
521
603
  } finally {
522
604
  tree.delete();
523
605
  }
524
606
 
607
+ const resolveBase = computeEffectiveResolveBase(cdTarget, cwd);
608
+ const normalizedCwd = normalizePathForComparison(cwd, cwd);
609
+
525
610
  const seen = new Set<string>();
526
611
  const externalPaths: string[] = [];
527
612
 
@@ -529,11 +614,13 @@ export async function extractExternalPathsFromBashCommand(
529
614
  const candidate = classifyTokenAsPathCandidate(token);
530
615
  if (!candidate) continue;
531
616
 
532
- const normalized = normalizePathForComparison(candidate, cwd);
617
+ const normalized = normalizePathForComparison(candidate, resolveBase);
533
618
  if (!normalized) continue;
534
619
 
535
620
  if (
536
- isPathOutsideWorkingDirectory(candidate, cwd) &&
621
+ normalizedCwd !== "" &&
622
+ !isSafeSystemPath(normalized) &&
623
+ !isPathWithinDirectory(normalized, normalizedCwd) &&
537
624
  !seen.has(normalized)
538
625
  ) {
539
626
  seen.add(normalized);
@@ -822,6 +822,82 @@ describe("extractExternalPathsFromBashCommand", () => {
822
822
  expect(result).toContain("/etc/hosts");
823
823
  });
824
824
  });
825
+
826
+ describe("leading cd prefix", () => {
827
+ test("regression: cd to subdir with relative path traversing back into cwd is not flagged", async () => {
828
+ // Real-world command that triggered a false-positive external-directory
829
+ // prompt. The relative path .pi/../../../.pi/skills/... resolves inside
830
+ // cwd when resolved from the cd target, but outside cwd when resolved
831
+ // from cwd itself.
832
+ const result = await extractExternalPathsFromBashCommand(
833
+ 'cd /projects/my-app/packages/sub && grep -n "pattern" .pi/../../../.pi/skills/pkg/SKILL.md',
834
+ cwd,
835
+ );
836
+ expect(result).toHaveLength(0);
837
+ });
838
+
839
+ test("cd to subdir: still flags genuinely external paths after cd", async () => {
840
+ const result = await extractExternalPathsFromBashCommand(
841
+ "cd /projects/my-app/packages/sub && cat /etc/hosts",
842
+ cwd,
843
+ );
844
+ expect(result).toContain("/etc/hosts");
845
+ });
846
+
847
+ test("cd to subdir: relative path that stays inside cwd is not flagged", async () => {
848
+ const result = await extractExternalPathsFromBashCommand(
849
+ "cd /projects/my-app/src && cat ../README.md",
850
+ cwd,
851
+ );
852
+ expect(result).toHaveLength(0);
853
+ });
854
+
855
+ test("cd to external dir: paths after cd are still checked against cwd", async () => {
856
+ // When cd target is outside cwd, we fall back to cwd as the resolve base.
857
+ // The cd target itself should be flagged, and paths after cd are resolved
858
+ // against cwd.
859
+ const result = await extractExternalPathsFromBashCommand(
860
+ "cd /tmp && cat ../etc/hosts",
861
+ cwd,
862
+ );
863
+ expect(result.length).toBeGreaterThan(0);
864
+ });
865
+
866
+ test("cd with relative target: resolves inside cwd", async () => {
867
+ const result = await extractExternalPathsFromBashCommand(
868
+ 'cd packages/sub && grep -n "x" .pi/../../../.pi/skills/pkg/SKILL.md',
869
+ cwd,
870
+ );
871
+ expect(result).toHaveLength(0);
872
+ });
873
+
874
+ test("no cd prefix: ../ path that escapes cwd is flagged", async () => {
875
+ // Without the cd prefix, the path resolves against cwd and escapes.
876
+ const result = await extractExternalPathsFromBashCommand(
877
+ 'grep -n "pattern" .pi/../../../.pi/skills/pkg/SKILL.md',
878
+ cwd,
879
+ );
880
+ expect(result.length).toBeGreaterThan(0);
881
+ });
882
+
883
+ test("cd is not first command: cd is ignored", async () => {
884
+ // cd after another command should not affect path resolution.
885
+ const result = await extractExternalPathsFromBashCommand(
886
+ "echo hello && cd /projects/my-app/src && cat ../../outside.txt",
887
+ cwd,
888
+ );
889
+ // ../../outside.txt resolves against cwd, not the cd target
890
+ expect(result.length).toBeGreaterThan(0);
891
+ });
892
+
893
+ test("cd with semicolon separator", async () => {
894
+ const result = await extractExternalPathsFromBashCommand(
895
+ "cd /projects/my-app/src ; cat ../README.md",
896
+ cwd,
897
+ );
898
+ expect(result).toHaveLength(0);
899
+ });
900
+ });
825
901
  });
826
902
 
827
903
  describe("formatBashExternalDirectoryAskPrompt", () => {