@capybara-hq/plugin-sdk 0.1.0-rc.30 → 0.1.0-rc.32

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,4 +1,5 @@
1
1
  import { DEVICE_SCAN_SKIPPED_BASE_DIRS, DEVICE_SCAN_SKIPPED_DIRS, MAX_DEVICE_PROJECT_NAME_LENGTH, MAX_DEVICE_SCAN_DEPTH, MAX_DEVICE_SCAN_DIRS, MAX_DEVICE_SCAN_RESULTS, } from "./sandbox.js";
2
+ import { SANDBOX_WRITE_POLICY_PYTHON } from "./sandbox-write-denies.js";
2
3
  /**
3
4
  * Private Python 3 transport: one JSON request on stdin, one result/error on stdout.
4
5
  * The trusted caller supplies already-authorized canonical root paths and inherited directory
@@ -24,6 +25,9 @@ SKIP_BASE = ${JSON.stringify(DEVICE_SCAN_SKIPPED_BASE_DIRS)}
24
25
  # ECMAScript whitespace (Python's str.strip also removes several extra control characters).
25
26
  SPACE = "\u0009\u000a\u000b\u000c\u000d\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
26
27
 
28
+ # Restricted roots ({"denyWrites": true}) follow the shared write policy (sandbox-write-denies.ts).
29
+ ${SANDBOX_WRITE_POLICY_PYTHON}
30
+
27
31
  class ScopeDenied(Exception):
28
32
  pass
29
33
 
@@ -58,6 +62,7 @@ def valid_name(name):
58
62
  class NativeFiles:
59
63
  def __init__(self, roots):
60
64
  self.roots = roots
65
+ self.deny_writes = roots.get("denyWrites") is True
61
66
  # Descriptors are supplied by the trusted consumer, never reopened by root pathname.
62
67
  for root in roots["read"] + roots["write"]:
63
68
  if not valid_path(root["path"]) or os.path.normpath(root["path"]) != root["path"]:
@@ -73,6 +78,8 @@ class NativeFiles:
73
78
  if not valid_path(path):
74
79
  raise ValueError("Supply a valid absolute native path")
75
80
  path = os.path.normpath(path)
81
+ if write and self.deny_writes and write_denied(path):
82
+ raise ScopeDenied("Native path is a protected write target")
76
83
  roots = self.roots["write"] if write else self.roots["read"] + self.roots["write"]
77
84
  matches = [root for root in roots if within(root["path"], path)]
78
85
  if not matches:
@@ -81,7 +88,11 @@ class NativeFiles:
81
88
  fd = os.dup(root["fd"])
82
89
  current = root["path"]
83
90
  parts = os.path.relpath(path, current).split("/")
84
- return self.walk(fd, current, parts, write, create, links, must_exist)
91
+ result = self.walk(fd, current, parts, write, create, links, must_exist)
92
+ if write and self.deny_writes and write_denied(result[2]):
93
+ os.close(result[0])
94
+ raise ScopeDenied("Native path is a protected write target")
95
+ return result
85
96
 
86
97
  def walk(self, fd, current, parts, write, create, links, must_exist):
87
98
  try:
@@ -91,6 +102,8 @@ class NativeFiles:
91
102
  info = os.stat(name, dir_fd=fd, follow_symlinks=False)
92
103
  except FileNotFoundError:
93
104
  if not final and create:
105
+ if self.deny_writes and write_denied(os.path.join(current, name)):
106
+ raise ScopeDenied("Native path is a protected write target")
94
107
  try:
95
108
  os.mkdir(name, 0o755, dir_fd=fd)
96
109
  except FileExistsError:
@@ -144,13 +157,18 @@ class NativeFiles:
144
157
  def open(self, path, write=False, directory=False):
145
158
  parent, name = self.parent(path, write, write)
146
159
  try:
160
+ if write and self.deny_writes and _fold_parts(name) == [".git"]:
161
+ os.close(parent)
162
+ parent = None
163
+ raise ScopeDenied("A file named .git would point git elsewhere")
147
164
  flags = os.O_NOFOLLOW | os.O_NONBLOCK
148
165
  flags |= os.O_WRONLY | os.O_CREAT if write else os.O_RDONLY
149
166
  if directory:
150
167
  flags |= os.O_DIRECTORY
151
168
  return os.open(name, flags, 0o600, dir_fd=parent)
152
169
  finally:
153
- os.close(parent)
170
+ if parent is not None:
171
+ os.close(parent)
154
172
 
155
173
  def metadata(self, path):
156
174
  parent, name = self.parent(path)
@@ -17,12 +17,12 @@ export const SANDBOX_RUNTIME_SOURCE = Object.freeze({
17
17
  Object.freeze({
18
18
  name: "native-files.py",
19
19
  source: NATIVE_FILES_SOURCE,
20
- sha256: "601f51c1c46bedac5ab56f7ca38d7c15fdea8481efc6476d5a5b3243318ef79b",
20
+ sha256: "09a78c9bb7264ad6a1fe3b1aadd4c0f489192dc47c903c7fcc42fdf405590b42",
21
21
  }),
22
22
  Object.freeze({
23
23
  name: "linux-sandbox.py",
24
24
  source: LINUX_SANDBOX_SOURCE,
25
- sha256: "f27ab699dbb5f7fc0ea9eb02cfa2257a405e5ccf3b8c3e011df29ab54bc3d599",
25
+ sha256: "b17b714a445912508d5082598af42960df550387c4a73b379561af19b0b45c87",
26
26
  }),
27
27
  ]),
28
28
  });
@@ -0,0 +1,60 @@
1
+ /**
2
+ * What restricted execution may never write inside a granted root: one policy for every consumer
3
+ * (the Linux launcher's broker, `native-files.py`, the device agent and, as far as its glob
4
+ * patterns can express it, the macOS runtime). It keeps a later unsandboxed tool run by the owner
5
+ * from executing what a restricted command left behind.
6
+ *
7
+ * 1. {@link SANDBOX_MANDATORY_WRITE_DENIES}: component sequences denied wherever they appear (the
8
+ * entry and everything beneath it): shell startup files, git's global files, direnv and npm
9
+ * configuration, editor and agent configuration.
10
+ * 2. Inside a `.git` directory (the first `.git` component of a path) only git's ordinary
11
+ * internals are writable, an allowlist ({@link SANDBOX_GIT_ALLOWED_DIRECTORIES},
12
+ * {@link SANDBOX_GIT_ALLOWED_FILES}, `info/exclude`). Everything else there is denied:
13
+ * `config`, `config.worktree`, `commondir`, `gitdir`, `hooks/**`, `info/attributes`, unknown
14
+ * names. The gitdirs of submodules (`modules/<name>/`, where a name may contain `/`) and
15
+ * worktrees (`worktrees/<name>/`) follow the same allowlist; a submodule path is refused when
16
+ * any reading of its name would make it a denied internal.
17
+ * 3. Outside `.git`, a file named `HEAD` is denied: with `objects/` and `refs/` it would make a
18
+ * bare-repository lookalike whose configuration git honours.
19
+ * 4. Moves ({@link isSandboxMoveDenied}): nothing leaves a protected name (`.git`, `.claude`, …)
20
+ * except within the same protected directory; a directory is not moved under a protected name
21
+ * from outside it; no non-directory becomes `.git` (a `gitdir:` pointer).
22
+ *
23
+ * 5. Inside an allowlisted subtree nothing can make a repository: no `.git`, no `HEAD` except
24
+ * where git keeps one (`logs/HEAD`, `refs/remotes/<r>/HEAD`, …), and no `objects`, `commondir`
25
+ * or `gitdir` beside such a `HEAD`.
26
+ *
27
+ * Names are compared after Unicode NFKC normalization and full case folding
28
+ * ({@link sandboxFoldName}, identical to Python's `casefold`). Known limitations: a
29
+ * restricted command cannot `git init`, add a submodule or worktree, or create a bare repository;
30
+ * continuing an operation it started (`git rebase --continue`) runs the todo list it wrote, like
31
+ * any script it wrote; files that tools execute by convention without configuration (for example
32
+ * `package.json` scripts) are not covered; a forged rebase or cherry-pick in progress is possible
33
+ * (its state directories are writable for git's own use) and continuing it runs its `exec` lines.
34
+ * The macOS worker expresses this policy as ordered Seatbelt regex rules
35
+ * (`packages/device/src/exec/mac-write-policy.ts`).
36
+ */
37
+ export declare const SANDBOX_MANDATORY_WRITE_DENIES: readonly (readonly string[])[];
38
+ /** Directories inside a gitdir whose whole subtree is writable (objects, refs, operation state). */
39
+ export declare const SANDBOX_GIT_ALLOWED_DIRECTORIES: readonly string[];
40
+ /** Entries directly in a gitdir that are writable, each also with a `.lock` suffix. */
41
+ export declare const SANDBOX_GIT_ALLOWED_FILES: readonly string[];
42
+ /**
43
+ * For consumers that can only express denies as path patterns (the macOS runtime): the gitdir
44
+ * internals that run or redirect code, denied at any depth below `.git`. Such a consumer cannot
45
+ * express the allowlist or the `HEAD` rule exactly; see the rules above.
46
+ */
47
+ export declare const SANDBOX_GIT_DENIED_INTERNALS: readonly string[];
48
+ /** Names nothing leaves, and no directory is moved under, from outside. */
49
+ export declare const SANDBOX_PROTECTED_PATH_NAMES: readonly string[];
50
+ /** NFKC, then full case folding: the same as Python's `normalize("NFKC", s).casefold()`. */
51
+ export declare function sandboxFoldName(name: string): string;
52
+ /** Whether restricted execution may never write `path` (an absolute, normalized path). */
53
+ export declare function isSandboxWriteDenied(path: string): boolean;
54
+ /** Whether a restricted rename or link of `source` to `destination` is refused (rule 4). */
55
+ export declare function isSandboxMoveDenied(source: string, destination: string, sourceIsDirectory: boolean): boolean;
56
+ /**
57
+ * The same policy for the Python consumers (`linux-sandbox.py`, `native-files.py`): defines
58
+ * `write_denied(path)` and `move_denied(source, destination, is_dir)` with identical rules.
59
+ */
60
+ export declare const SANDBOX_WRITE_POLICY_PYTHON: string;