written-files-registry.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Session-scoped registry of files that the agent has successfully written
  3. * during the current session.
  4. *
  5. * Purpose
  6. * -------
  7. *
  8. * When an agent writes a script (Write/Edit tool returns `allow`) and then
  9. * tries to execute it (e.g. `bash /tmp/foo.sh`, `bash $WORKSPACE/run.sh`),
  10. * we want to skip the SOFT remote-execution ASK that would otherwise
  11. * misclassify the local script as a piped-shell payload.
  12. *
  13. * Scope
  14. * -----
  15. *
  16. * - Keyed by session id; cleared via {@link clearSession} when the session
  17. * ends (the bridge calls this in its `SessionEnd` path).
  18. * - In-memory only — agents that write before a daemon restart will not
  19. * benefit, but daemon restart implies new session ids anyway, so the
  20. * registry is rebuilt as the new session writes.
  21. * - Cross-platform absolute path normalization (`path.resolve`) before
  22. * storage and lookup so `/tmp/a/../b/x.sh` and `/tmp/b/x.sh` match.
  23. *
  24. * Safety
  25. * ------
  26. *
  27. * The registry is consulted ONLY by the bash SOFT remote-execution pre-scan
  28. * inside `bash-permission.ts`. It does not override:
  29. * - HARD-blocked operations (catastrophic-standalone, sensitive-read,
  30. * irrecoverable-delete, etc.)
  31. * - explicit user `deny` rules
  32. * - per-subcommand safety checks downstream of the pre-scan
  33. *
  34. * Concretely, even if a session previously wrote `/tmp/x.sh`, a command like
  35. * `rm -rf / && bash /tmp/x.sh` is still DENIED by the catastrophic-standalone
  36. * gate at the top of `checkBashPermission`, and a `curl ... | bash` still
  37. * trips the pipe-to-shell HARD pattern. The registry only saves the user
  38. * from one specific false positive: `<sep> bash <local-script>`.
  39. */
  40. import path from 'node:path';
  41. import { logger, backgroundCtx } from './host-utils.js';
  42. const registry = new Map<string, Set<string>>();
  43. /**
  44. * Record that the given absolute path was written by this session.
  45. *
  46. * Called by the fs permission checker after a Write/Edit tool returns
  47. * `allow` (regardless of which allow branch — workingDirectory,
  48. * tempDirectory, sandbox, or rule).
  49. */
  50. export function recordWrite(sessionId: string, absPath: string): void {
  51. if (!sessionId || !absPath) return;
  52. const resolved = path.resolve(absPath);
  53. let set = registry.get(sessionId);
  54. if (!set) {
  55. set = new Set<string>();
  56. registry.set(sessionId, set);
  57. }
  58. if (!set.has(resolved)) {
  59. set.add(resolved);
  60. logger.debug(
  61. backgroundCtx(),
  62. `[written-files] recorded sessionId=${sessionId} path=${resolved} total=${set.size}`,
  63. );
  64. }
  65. }
  66. /**
  67. * Returns true if the session previously wrote this exact absolute path.
  68. */
  69. export function hasWritten(sessionId: string | undefined, absPath: string): boolean {
  70. if (!sessionId || !absPath) return false;
  71. const set = registry.get(sessionId);
  72. if (!set) return false;
  73. return set.has(path.resolve(absPath));
  74. }
  75. /**
  76. * Drop the registry for a session. Called from session lifecycle on
  77. * SessionEnd so the registry doesn't leak forever.
  78. */
  79. export function clearSession(sessionId: string): void {
  80. if (!sessionId) return;
  81. const had = registry.delete(sessionId);
  82. if (had) {
  83. logger.debug(backgroundCtx(), `[written-files] cleared sessionId=${sessionId}`);
  84. }
  85. }
  86. /**
  87. * Test-only: wipe all entries.
  88. */
  89. export function _resetForTests(): void {
  90. registry.clear();
  91. }
  92. /**
  93. * Test-only: snapshot of a session's set for assertions.
  94. */
  95. export function _snapshotForTests(sessionId: string): string[] {
  96. const set = registry.get(sessionId);
  97. return set ? [...set].sort() : [];
  98. }