@capybara-hq/plugin-sdk 0.1.0-rc.7 → 0.1.0-rc.70
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/README.md +1 -1
- package/agent-docs/agent-templates.md +14 -2
- package/agent-docs/concepts.md +14 -5
- package/agent-docs/connector-templates.md +11 -3
- package/agent-docs/extension-points/channel.md +1 -1
- package/agent-docs/extension-points/context.md +15 -0
- package/agent-docs/extension-points/device-provider.md +15 -0
- package/agent-docs/extension-points/file-preview.md +2 -0
- package/agent-docs/extension-points/model-provider.md +3 -1
- package/agent-docs/extension-points/object-store.md +2 -0
- package/agent-docs/extension-points/push-provider.md +17 -0
- package/agent-docs/extensions.md +76 -3
- package/agent-docs/plugin-format.md +51 -12
- package/agent-docs/setup.md +54 -0
- package/dist/agent-template.d.ts +15 -6
- package/dist/agent-template.js +32 -13
- package/dist/avatar-presets.d.ts +8 -0
- package/dist/avatar-presets.js +17 -0
- package/dist/bootstrap.d.ts +109 -1
- package/dist/bootstrap.js +185 -2
- package/dist/cards.d.ts +60 -13
- package/dist/cards.js +88 -18
- package/dist/channel.d.ts +128 -47
- package/dist/channel.js +10 -5
- package/dist/client-css.d.ts +36 -0
- package/dist/client-css.js +326 -0
- package/dist/client-section.d.ts +35 -0
- package/dist/client-section.js +133 -0
- package/dist/config-ui.d.ts +28 -6
- package/dist/config-ui.js +88 -25
- package/dist/config-url-fetch.d.ts +19 -0
- package/dist/config-url-fetch.js +43 -0
- package/dist/connector-auth.d.ts +13 -2
- package/dist/connector-auth.js +82 -8
- package/dist/connector.d.ts +74 -32
- package/dist/context.d.ts +40 -0
- package/dist/context.js +2 -0
- package/dist/ctx.d.ts +210 -30
- package/dist/ctx.js +9 -1
- package/dist/define-extension.d.ts +23 -13
- package/dist/define-extension.js +19 -5
- package/dist/device-denied-paths.d.ts +20 -0
- package/dist/device-denied-paths.js +118 -0
- package/dist/device-operation.d.ts +339 -0
- package/dist/device-operation.js +312 -0
- package/dist/device-protocol.d.ts +285 -50
- package/dist/device-protocol.js +755 -90
- package/dist/device-provider.d.ts +708 -0
- package/dist/device-provider.js +325 -0
- package/dist/device-service.d.ts +52 -0
- package/dist/device-service.js +105 -0
- package/dist/dial-in.d.ts +239 -0
- package/dist/dial-in.js +155 -0
- package/dist/directory.d.ts +15 -12
- package/dist/extension-point.d.ts +2 -0
- package/dist/extension-point.js +6 -2
- package/dist/field-meta.d.ts +19 -0
- package/dist/field-meta.js +54 -0
- package/dist/file-preview.d.ts +26 -10
- package/dist/git-hosts.d.ts +53 -0
- package/dist/git-hosts.js +98 -0
- package/dist/hooks.d.ts +125 -20
- package/dist/hooks.js +5 -0
- package/dist/http-access.d.ts +62 -0
- package/dist/http-access.js +12 -0
- package/dist/identity.d.ts +38 -9
- package/dist/identity.js +21 -1
- package/dist/index.d.ts +31 -4
- package/dist/index.js +31 -4
- package/dist/model-params.d.ts +41 -22
- package/dist/model-params.js +28 -15
- package/dist/model.d.ts +210 -75
- package/dist/model.js +152 -19
- package/dist/net.d.ts +52 -0
- package/dist/net.js +34 -0
- package/dist/network-classes.d.ts +98 -0
- package/dist/network-classes.js +291 -0
- package/dist/permissions.d.ts +65 -22
- package/dist/permissions.js +139 -43
- package/dist/platform-only.d.ts +27 -0
- package/dist/platform-only.js +35 -0
- package/dist/plugin-manifest.d.ts +40 -9
- package/dist/plugin-manifest.js +171 -60
- package/dist/plugin-package.d.ts +82 -39
- package/dist/plugin-package.js +68 -41
- package/dist/push.d.ts +75 -0
- package/dist/push.js +87 -0
- package/dist/sandbox-linux-runtime-source.d.ts +1 -0
- package/dist/sandbox-linux-runtime-source.js +2343 -0
- package/dist/sandbox-native-runtime-source.d.ts +10 -0
- package/dist/sandbox-native-runtime-source.js +476 -0
- package/dist/sandbox-runtime-source.d.ts +47 -0
- package/dist/sandbox-runtime-source.js +73 -0
- package/dist/sandbox-write-denies.d.ts +139 -0
- package/dist/sandbox-write-denies.js +869 -0
- package/dist/secret.d.ts +8 -2
- package/dist/secret.js +11 -4
- package/dist/setup-check.d.ts +128 -0
- package/dist/setup-check.js +142 -0
- package/dist/setup-guide-parse.d.ts +121 -0
- package/dist/setup-guide-parse.js +356 -0
- package/dist/setup-guide-render.d.ts +45 -0
- package/dist/setup-guide-render.js +351 -0
- package/dist/setup-guide.d.ts +86 -72
- package/dist/setup-guide.js +340 -108
- package/dist/shell-capabilities.d.ts +211 -0
- package/dist/shell-capabilities.js +44 -0
- package/dist/shell-environment.d.ts +57 -0
- package/dist/shell-environment.js +21 -0
- package/dist/skill.d.ts +27 -17
- package/dist/skill.js +40 -26
- package/dist/storage.d.ts +52 -25
- package/dist/svg-viewbox.d.ts +10 -0
- package/dist/svg-viewbox.js +24 -0
- package/dist/theme-assets.d.ts +55 -0
- package/dist/theme-assets.js +489 -0
- package/dist/theme-readability.d.ts +37 -0
- package/dist/theme-readability.js +145 -0
- package/dist/theme-tokens.generated.d.ts +299 -0
- package/dist/theme-tokens.generated.js +184 -0
- package/dist/theme.d.ts +190 -0
- package/dist/theme.js +471 -0
- package/dist/tool.d.ts +104 -30
- package/dist/tool.js +29 -9
- package/dist/ui-parts.d.ts +23 -0
- package/dist/ui-parts.js +385 -0
- package/dist/ui-resolve.d.ts +35 -0
- package/dist/ui-resolve.js +42 -0
- package/dist/ui-slots.d.ts +65 -0
- package/dist/ui-slots.js +261 -0
- package/dist/ui.d.ts +221 -77
- package/dist/ui.js +104 -27
- package/dist/url.d.ts +4 -1
- package/dist/url.js +4 -1
- package/dist/web.d.ts +26 -20
- package/package.json +13 -1
- package/schemas/bootstrap-extra.v1.schema.json +186 -0
- package/schemas/bootstrap.v1.schema.json +20 -0
- package/schemas/connector-template.v1.schema.json +62 -4
- package/schemas/plugin.v1.schema.json +93 -143
- package/schemas/theme.v1.schema.json +302 -0
- package/agent-docs/extension-points/sandbox.md +0 -15
- package/dist/org-access.d.ts +0 -27
- package/dist/org-access.js +0 -35
- package/dist/sandbox.d.ts +0 -254
- package/dist/sandbox.js +0 -153
|
@@ -0,0 +1,869 @@
|
|
|
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 and logout files, git's global files, direnv
|
|
9
|
+
* and npm configuration, editor and agent configuration, and the OS persistence locations
|
|
10
|
+
* (launchd, systemd, environment.d, XDG and KDE autostart, X session files, fish). Entries are
|
|
11
|
+
* written already folded. {@link SANDBOX_SYSTEM_WRITE_DENIES}: absolute system paths denied
|
|
12
|
+
* with everything beneath them (the crontab and at spools), anchored at `/`.
|
|
13
|
+
* 2. Inside a `.git` directory (the first `.git` component of a path) only git's ordinary
|
|
14
|
+
* internals are writable, an allowlist ({@link SANDBOX_GIT_ALLOWED_DIRECTORIES},
|
|
15
|
+
* {@link SANDBOX_GIT_ALLOWED_FILES}, `info/exclude`). Everything else there is denied:
|
|
16
|
+
* `config`, `config.worktree`, `commondir`, `gitdir`, `hooks/**`, `info/attributes`, unknown
|
|
17
|
+
* names. The gitdirs of submodules (`modules/<name>/`, where a name may contain `/`) and
|
|
18
|
+
* worktrees (`worktrees/<name>/`) follow the same allowlist; a submodule path is refused when
|
|
19
|
+
* any reading of its name would make it a denied internal.
|
|
20
|
+
* 3. Outside `.git`, a file named `HEAD` is denied: with `objects/` and `refs/` it would make a
|
|
21
|
+
* bare-repository lookalike whose configuration git honours.
|
|
22
|
+
* 4. Moves ({@link isSandboxMoveDenied}): neither end may be a denied write; nothing leaves a
|
|
23
|
+
* protected name ({@link SANDBOX_PROTECTED_PATH_NAMES}: `.git` and the single-component
|
|
24
|
+
* entries) except within the same protected directory; a directory is not moved under a
|
|
25
|
+
* protected name from outside it; no non-directory becomes `.git` (a `gitdir:` pointer). For a
|
|
26
|
+
* multi-component entry `[a, b, …]` (or an absolute system path) a destination ending in a
|
|
27
|
+
* proper prefix of it (`…/a`) takes no non-directory (a link there would redirect the entry)
|
|
28
|
+
* and no directory holding the entry's next component (`b`): a prepared directory never moves
|
|
29
|
+
* into place, while ordinary names (`library`, `.config`) stay movable. An ancestor of an
|
|
30
|
+
* anchored path (a system path, or a consumer's resolved target) never moves away.
|
|
31
|
+
*
|
|
32
|
+
* 5. Inside an allowlisted subtree nothing can make a repository: no `.git`, no `HEAD` except
|
|
33
|
+
* where git keeps one (`logs/HEAD`, `refs/remotes/<r>/HEAD`, …), and no `objects`, `commondir`
|
|
34
|
+
* or `gitdir` beside such a `HEAD`.
|
|
35
|
+
*
|
|
36
|
+
* 6. A component starting with {@link SANDBOX_RESERVED_NAME_PREFIX} (the broker's staging and
|
|
37
|
+
* quarantine names) is denied with everything beneath it.
|
|
38
|
+
*
|
|
39
|
+
* Names are compared after Unicode NFKC normalization and full case folding
|
|
40
|
+
* ({@link sandboxFoldName}, identical to Python's `casefold`). Known limitations: a
|
|
41
|
+
* restricted command cannot `git init`, add a submodule or worktree, or create a bare repository;
|
|
42
|
+
* continuing an operation it started (`git rebase --continue`) runs the todo list it wrote, like
|
|
43
|
+
* any script it wrote; files that tools execute by convention without configuration (for example
|
|
44
|
+
* `package.json` scripts) are not covered; a forged rebase or cherry-pick in progress is possible
|
|
45
|
+
* (its state directories are writable for git's own use) and continuing it runs its `exec` lines.
|
|
46
|
+
* The macOS worker expresses this policy as ordered Seatbelt regex rules
|
|
47
|
+
* (`packages/device/src/exec/mac-write-policy.ts`).
|
|
48
|
+
*
|
|
49
|
+
* Constraints on consumers:
|
|
50
|
+
*
|
|
51
|
+
* - The policy judges the path it is given. A protected home location reached through a symbolic
|
|
52
|
+
* link (`~/.config` or `~/Library` linked elsewhere, as dotfile managers do) is denied only at
|
|
53
|
+
* its resolved target: a device worker resolves the protected home locations at operation start
|
|
54
|
+
* and adds their resolved targets as absolute denies.
|
|
55
|
+
* - The move check needs the entries of a moved directory. A consumer passes them from the source
|
|
56
|
+
* descriptor; the Python `move_denied` otherwise lists the source path, refusing when it cannot.
|
|
57
|
+
* A listing is a snapshot: a concurrent writer could add the next component between the check
|
|
58
|
+
* and the rename, so a consumer that can re-checks the destination after the rename.
|
|
59
|
+
*/
|
|
60
|
+
export const SANDBOX_MANDATORY_WRITE_DENIES = Object.freeze([
|
|
61
|
+
[".gitconfig"],
|
|
62
|
+
[".gitmodules"],
|
|
63
|
+
[".bashrc"],
|
|
64
|
+
[".bash_profile"],
|
|
65
|
+
[".zshrc"],
|
|
66
|
+
[".zprofile"],
|
|
67
|
+
[".profile"],
|
|
68
|
+
[".ripgreprc"],
|
|
69
|
+
[".mcp.json"],
|
|
70
|
+
[".envrc"],
|
|
71
|
+
[".npmrc"],
|
|
72
|
+
[".vscode"],
|
|
73
|
+
[".idea"],
|
|
74
|
+
[".claude", "commands"],
|
|
75
|
+
[".claude", "agents"],
|
|
76
|
+
// hook managers: hooks git runs (through a manager) from files in the working tree
|
|
77
|
+
[".husky"],
|
|
78
|
+
[".huskyrc"],
|
|
79
|
+
[".huskyrc.json"],
|
|
80
|
+
[".huskyrc.js"],
|
|
81
|
+
["husky.config.js"],
|
|
82
|
+
[".pre-commit-config.yaml"],
|
|
83
|
+
[".pre-commit-config.yml"],
|
|
84
|
+
["lefthook.yml"],
|
|
85
|
+
["lefthook.yaml"],
|
|
86
|
+
["lefthook.json"],
|
|
87
|
+
["lefthook.toml"],
|
|
88
|
+
[".lefthook.yml"],
|
|
89
|
+
[".lefthook.yaml"],
|
|
90
|
+
[".lefthook.json"],
|
|
91
|
+
[".lefthook.toml"],
|
|
92
|
+
["lefthook-local.yml"],
|
|
93
|
+
["lefthook-local.yaml"],
|
|
94
|
+
["lefthook-local.json"],
|
|
95
|
+
["lefthook-local.toml"],
|
|
96
|
+
[".lefthook-local.yml"],
|
|
97
|
+
[".lefthook-local.yaml"],
|
|
98
|
+
[".lefthook-local.json"],
|
|
99
|
+
[".lefthook-local.toml"],
|
|
100
|
+
[".lefthook"],
|
|
101
|
+
[".lefthook-local"],
|
|
102
|
+
[".config", "lefthook.yml"],
|
|
103
|
+
[".config", "lefthook.yaml"],
|
|
104
|
+
[".config", "lefthook.json"],
|
|
105
|
+
[".config", "lefthook.toml"],
|
|
106
|
+
[".overcommit.yml"],
|
|
107
|
+
[".simple-git-hooks.json"],
|
|
108
|
+
[".simple-git-hooks.js"],
|
|
109
|
+
[".simple-git-hooks.cjs"],
|
|
110
|
+
["simple-git-hooks.json"],
|
|
111
|
+
["simple-git-hooks.js"],
|
|
112
|
+
["simple-git-hooks.cjs"],
|
|
113
|
+
// the conventional core.hooksPath directory a repository's existing config may name
|
|
114
|
+
[".githooks"],
|
|
115
|
+
// every other shell startup and logout file (the kernel review's persistence rules name them)
|
|
116
|
+
[".zshenv"],
|
|
117
|
+
[".zlogin"],
|
|
118
|
+
[".zlogout"],
|
|
119
|
+
[".bash_login"],
|
|
120
|
+
[".bash_logout"],
|
|
121
|
+
[".logout"],
|
|
122
|
+
[".tcshrc"],
|
|
123
|
+
[".cshrc"],
|
|
124
|
+
[".kshrc"],
|
|
125
|
+
[".config", "git", "config"],
|
|
126
|
+
[".config", "fish", "config.fish"],
|
|
127
|
+
[".config", "fish", "conf.d"],
|
|
128
|
+
// persistence locations the OS runs later (approval-only profiles design, "Fixed denies"):
|
|
129
|
+
// launchd agents and daemons, systemd units, drop-ins and session environment, XDG and KDE
|
|
130
|
+
// autostart, the X session files. The crontab spools are SANDBOX_SYSTEM_WRITE_DENIES.
|
|
131
|
+
["library", "launchagents"],
|
|
132
|
+
["library", "launchdaemons"],
|
|
133
|
+
[".config", "systemd"],
|
|
134
|
+
[".local", "share", "systemd"],
|
|
135
|
+
[".config", "environment.d"],
|
|
136
|
+
[".config", "autostart"],
|
|
137
|
+
[".config", "autostart-scripts"],
|
|
138
|
+
[".config", "plasma-workspace", "env"],
|
|
139
|
+
[".xprofile"],
|
|
140
|
+
[".xsessionrc"],
|
|
141
|
+
// PATH-shim persistence: programs placed here shadow system ones in the owner's later shells
|
|
142
|
+
// (`~/bin` is home-anchored: SANDBOX_HOME_WRITE_DENIES)
|
|
143
|
+
[".local", "bin"],
|
|
144
|
+
[".cargo", "bin"],
|
|
145
|
+
].map((entry) => Object.freeze(entry)));
|
|
146
|
+
/**
|
|
147
|
+
* Locations denied only directly under the device owner's home, with everything beneath them, never
|
|
148
|
+
* elsewhere: `~/bin`, a directory many shells put on `PATH`, where a planted program would shadow a
|
|
149
|
+
* system one in the owner's later unsandboxed shell (a project's own `bin/` stays writable).
|
|
150
|
+
* Consumers anchor each entry at the home they protect: the device workers add it, and the target
|
|
151
|
+
* of a link it is, as absolute denies; the kernel never proposes it as a write directory
|
|
152
|
+
* ({@link isSandboxHomeWriteDenied}). `~/.local/bin` and `~/.cargo/bin` are
|
|
153
|
+
* {@link SANDBOX_MANDATORY_WRITE_DENIES} entries.
|
|
154
|
+
*/
|
|
155
|
+
export const SANDBOX_HOME_WRITE_DENIES = Object.freeze([
|
|
156
|
+
Object.freeze(["bin"]),
|
|
157
|
+
]);
|
|
158
|
+
/**
|
|
159
|
+
* System runtime and socket directories (compared folded, anchored at `/`): other programs keep
|
|
160
|
+
* their UNIX sockets and device nodes there (`/run/user/<uid>/bus`, `/var/run/docker.sock`,
|
|
161
|
+
* `/dev`, `/proc`), so a restricted write root never is one, lies inside one or contains one
|
|
162
|
+
* ({@link sandboxRuntimeDirectoryOf}).
|
|
163
|
+
*/
|
|
164
|
+
export const SANDBOX_RUNTIME_DIRECTORIES = Object.freeze([
|
|
165
|
+
"/run",
|
|
166
|
+
"/var/run",
|
|
167
|
+
"/private/var/run",
|
|
168
|
+
"/dev",
|
|
169
|
+
"/proc",
|
|
170
|
+
"/sys",
|
|
171
|
+
]);
|
|
172
|
+
/** The shared temporary directories: never a write root themselves (sockets sit directly in them). */
|
|
173
|
+
const TEMP_DIRECTORIES = ["/tmp", "/private/tmp"];
|
|
174
|
+
/** Socket directories other programs create directly in a temporary directory (folded names). */
|
|
175
|
+
const TEMP_SOCKET_DIRECTORY = /^(?:\.x11-unix|\.ice-unix|\.xim-unix|\.font-unix|\.test-unix|tmux-.*|ssh-.*|com\.apple\.launchd\..*|dbus-.*|systemd-private-.*|zellij-.*|emacs[0-9]+|screen.*|vscode-.*|nvim.*)$/;
|
|
176
|
+
/**
|
|
177
|
+
* Absolute system paths denied with everything beneath them, compared folded and anchored at `/`:
|
|
178
|
+
* the crontab and at spools of Linux (`/var/spool/cron` in its `crontabs` and `tabs` layouts,
|
|
179
|
+
* `/var/spool/at`) and macOS (`/private/var/at`, also spelled `/var/at`; `/usr/lib/cron/tabs` is a
|
|
180
|
+
* link resolving there). Never protected move names: a project's own `at/` or `spool/` stays
|
|
181
|
+
* ordinary.
|
|
182
|
+
*/
|
|
183
|
+
export const SANDBOX_SYSTEM_WRITE_DENIES = Object.freeze([
|
|
184
|
+
"/var/spool/cron",
|
|
185
|
+
"/var/spool/at",
|
|
186
|
+
"/private/var/at/tabs",
|
|
187
|
+
"/private/var/at/jobs",
|
|
188
|
+
"/var/at",
|
|
189
|
+
]);
|
|
190
|
+
/**
|
|
191
|
+
* The name prefix the Linux broker reserves for its own staging (a directory checked before it
|
|
192
|
+
* moves into a guarded place) and quarantine names. Any path with a component starting with it
|
|
193
|
+
* (compared folded) is denied to every sandboxed write, so nothing sandboxed can create, write
|
|
194
|
+
* into, or move into or out of such a directory.
|
|
195
|
+
*/
|
|
196
|
+
export const SANDBOX_RESERVED_NAME_PREFIX = ".capybara-staging-";
|
|
197
|
+
/** Directories inside a gitdir whose whole subtree is writable (objects, refs, operation state). */
|
|
198
|
+
export const SANDBOX_GIT_ALLOWED_DIRECTORIES = Object.freeze([
|
|
199
|
+
"objects",
|
|
200
|
+
"refs",
|
|
201
|
+
"logs",
|
|
202
|
+
"rebase-merge",
|
|
203
|
+
"rebase-apply",
|
|
204
|
+
"sequencer",
|
|
205
|
+
]);
|
|
206
|
+
/** Entries directly in a gitdir that are writable, each also with a `.lock` suffix. */
|
|
207
|
+
export const SANDBOX_GIT_ALLOWED_FILES = Object.freeze([
|
|
208
|
+
"index",
|
|
209
|
+
"head",
|
|
210
|
+
"orig_head",
|
|
211
|
+
"fetch_head",
|
|
212
|
+
"merge_head",
|
|
213
|
+
"merge_msg",
|
|
214
|
+
"merge_mode",
|
|
215
|
+
"merge_rr",
|
|
216
|
+
"auto_merge",
|
|
217
|
+
"cherry_pick_head",
|
|
218
|
+
"revert_head",
|
|
219
|
+
"rebase_head",
|
|
220
|
+
"bisect_log",
|
|
221
|
+
"bisect_start",
|
|
222
|
+
"bisect_expected_rev",
|
|
223
|
+
"bisect_ancestors_ok",
|
|
224
|
+
"bisect_names",
|
|
225
|
+
"bisect_terms",
|
|
226
|
+
"packed-refs",
|
|
227
|
+
"packed-refs.new",
|
|
228
|
+
"merge_autostash",
|
|
229
|
+
"commit_editmsg",
|
|
230
|
+
"tag_editmsg",
|
|
231
|
+
"squash_msg",
|
|
232
|
+
"shallow",
|
|
233
|
+
"description",
|
|
234
|
+
"gc.pid",
|
|
235
|
+
"gc.log",
|
|
236
|
+
"info",
|
|
237
|
+
"modules",
|
|
238
|
+
"worktrees",
|
|
239
|
+
]);
|
|
240
|
+
/** Gitdir entries a submodule path must never be readable as. */
|
|
241
|
+
const GIT_DANGER = new Set(["config", "config.worktree", "commondir", "gitdir", "hooks", "info"]);
|
|
242
|
+
/**
|
|
243
|
+
* For consumers that can only express denies as path patterns (the macOS runtime): the gitdir
|
|
244
|
+
* internals that run or redirect code, denied at any depth below `.git`. Such a consumer cannot
|
|
245
|
+
* express the allowlist or the `HEAD` rule exactly; see the rules above.
|
|
246
|
+
*/
|
|
247
|
+
export const SANDBOX_GIT_DENIED_INTERNALS = Object.freeze([
|
|
248
|
+
"config",
|
|
249
|
+
"config.worktree",
|
|
250
|
+
"commondir",
|
|
251
|
+
"gitdir",
|
|
252
|
+
"hooks",
|
|
253
|
+
"info/attributes",
|
|
254
|
+
]);
|
|
255
|
+
/**
|
|
256
|
+
* Names nothing leaves, and no directory is moved under, from outside: `.git` and the
|
|
257
|
+
* single-component entries. A multi-component entry is guarded by its prefix rule instead (rule 4),
|
|
258
|
+
* so its first component (`library`, `.config`) stays an ordinary name.
|
|
259
|
+
*/
|
|
260
|
+
export const SANDBOX_PROTECTED_PATH_NAMES = Object.freeze([
|
|
261
|
+
".git",
|
|
262
|
+
...new Set(SANDBOX_MANDATORY_WRITE_DENIES.filter((entry) => entry.length === 1).map((entry) => entry[0])),
|
|
263
|
+
]);
|
|
264
|
+
/** The multi-component entries and the anchored system paths, as folded name sequences. */
|
|
265
|
+
const PREFIX_GUARDED = [
|
|
266
|
+
...SANDBOX_MANDATORY_WRITE_DENIES.filter((entry) => entry.length > 1).map((names) => ({
|
|
267
|
+
names,
|
|
268
|
+
anchored: false,
|
|
269
|
+
})),
|
|
270
|
+
...SANDBOX_SYSTEM_WRITE_DENIES.map((path) => ({
|
|
271
|
+
names: path.split("/").filter((p) => p !== ""),
|
|
272
|
+
anchored: true,
|
|
273
|
+
})),
|
|
274
|
+
];
|
|
275
|
+
/**
|
|
276
|
+
* Python's `str.casefold` where it differs from `toLowerCase` (full case folding, Unicode 15.1),
|
|
277
|
+
* so both implementations fold identically: `ß`→`ss`, `ſ`→`s`, final sigma, Cherokee, ….
|
|
278
|
+
*/
|
|
279
|
+
const CASEFOLD_DIFF = Object.freeze({
|
|
280
|
+
181: "\u03bc",
|
|
281
|
+
223: "ss",
|
|
282
|
+
329: "\u02bcn",
|
|
283
|
+
383: "s",
|
|
284
|
+
496: "j\u030c",
|
|
285
|
+
837: "\u03b9",
|
|
286
|
+
912: "\u03b9\u0308\u0301",
|
|
287
|
+
944: "\u03c5\u0308\u0301",
|
|
288
|
+
962: "\u03c3",
|
|
289
|
+
976: "\u03b2",
|
|
290
|
+
977: "\u03b8",
|
|
291
|
+
981: "\u03c6",
|
|
292
|
+
982: "\u03c0",
|
|
293
|
+
1008: "\u03ba",
|
|
294
|
+
1009: "\u03c1",
|
|
295
|
+
1013: "\u03b5",
|
|
296
|
+
1415: "\u0565\u0582",
|
|
297
|
+
5024: "\u13a0",
|
|
298
|
+
5025: "\u13a1",
|
|
299
|
+
5026: "\u13a2",
|
|
300
|
+
5027: "\u13a3",
|
|
301
|
+
5028: "\u13a4",
|
|
302
|
+
5029: "\u13a5",
|
|
303
|
+
5030: "\u13a6",
|
|
304
|
+
5031: "\u13a7",
|
|
305
|
+
5032: "\u13a8",
|
|
306
|
+
5033: "\u13a9",
|
|
307
|
+
5034: "\u13aa",
|
|
308
|
+
5035: "\u13ab",
|
|
309
|
+
5036: "\u13ac",
|
|
310
|
+
5037: "\u13ad",
|
|
311
|
+
5038: "\u13ae",
|
|
312
|
+
5039: "\u13af",
|
|
313
|
+
5040: "\u13b0",
|
|
314
|
+
5041: "\u13b1",
|
|
315
|
+
5042: "\u13b2",
|
|
316
|
+
5043: "\u13b3",
|
|
317
|
+
5044: "\u13b4",
|
|
318
|
+
5045: "\u13b5",
|
|
319
|
+
5046: "\u13b6",
|
|
320
|
+
5047: "\u13b7",
|
|
321
|
+
5048: "\u13b8",
|
|
322
|
+
5049: "\u13b9",
|
|
323
|
+
5050: "\u13ba",
|
|
324
|
+
5051: "\u13bb",
|
|
325
|
+
5052: "\u13bc",
|
|
326
|
+
5053: "\u13bd",
|
|
327
|
+
5054: "\u13be",
|
|
328
|
+
5055: "\u13bf",
|
|
329
|
+
5056: "\u13c0",
|
|
330
|
+
5057: "\u13c1",
|
|
331
|
+
5058: "\u13c2",
|
|
332
|
+
5059: "\u13c3",
|
|
333
|
+
5060: "\u13c4",
|
|
334
|
+
5061: "\u13c5",
|
|
335
|
+
5062: "\u13c6",
|
|
336
|
+
5063: "\u13c7",
|
|
337
|
+
5064: "\u13c8",
|
|
338
|
+
5065: "\u13c9",
|
|
339
|
+
5066: "\u13ca",
|
|
340
|
+
5067: "\u13cb",
|
|
341
|
+
5068: "\u13cc",
|
|
342
|
+
5069: "\u13cd",
|
|
343
|
+
5070: "\u13ce",
|
|
344
|
+
5071: "\u13cf",
|
|
345
|
+
5072: "\u13d0",
|
|
346
|
+
5073: "\u13d1",
|
|
347
|
+
5074: "\u13d2",
|
|
348
|
+
5075: "\u13d3",
|
|
349
|
+
5076: "\u13d4",
|
|
350
|
+
5077: "\u13d5",
|
|
351
|
+
5078: "\u13d6",
|
|
352
|
+
5079: "\u13d7",
|
|
353
|
+
5080: "\u13d8",
|
|
354
|
+
5081: "\u13d9",
|
|
355
|
+
5082: "\u13da",
|
|
356
|
+
5083: "\u13db",
|
|
357
|
+
5084: "\u13dc",
|
|
358
|
+
5085: "\u13dd",
|
|
359
|
+
5086: "\u13de",
|
|
360
|
+
5087: "\u13df",
|
|
361
|
+
5088: "\u13e0",
|
|
362
|
+
5089: "\u13e1",
|
|
363
|
+
5090: "\u13e2",
|
|
364
|
+
5091: "\u13e3",
|
|
365
|
+
5092: "\u13e4",
|
|
366
|
+
5093: "\u13e5",
|
|
367
|
+
5094: "\u13e6",
|
|
368
|
+
5095: "\u13e7",
|
|
369
|
+
5096: "\u13e8",
|
|
370
|
+
5097: "\u13e9",
|
|
371
|
+
5098: "\u13ea",
|
|
372
|
+
5099: "\u13eb",
|
|
373
|
+
5100: "\u13ec",
|
|
374
|
+
5101: "\u13ed",
|
|
375
|
+
5102: "\u13ee",
|
|
376
|
+
5103: "\u13ef",
|
|
377
|
+
5104: "\u13f0",
|
|
378
|
+
5105: "\u13f1",
|
|
379
|
+
5106: "\u13f2",
|
|
380
|
+
5107: "\u13f3",
|
|
381
|
+
5108: "\u13f4",
|
|
382
|
+
5109: "\u13f5",
|
|
383
|
+
5112: "\u13f0",
|
|
384
|
+
5113: "\u13f1",
|
|
385
|
+
5114: "\u13f2",
|
|
386
|
+
5115: "\u13f3",
|
|
387
|
+
5116: "\u13f4",
|
|
388
|
+
5117: "\u13f5",
|
|
389
|
+
7296: "\u0432",
|
|
390
|
+
7297: "\u0434",
|
|
391
|
+
7298: "\u043e",
|
|
392
|
+
7299: "\u0441",
|
|
393
|
+
7300: "\u0442",
|
|
394
|
+
7301: "\u0442",
|
|
395
|
+
7302: "\u044a",
|
|
396
|
+
7303: "\u0463",
|
|
397
|
+
7304: "\ua64b",
|
|
398
|
+
7830: "h\u0331",
|
|
399
|
+
7831: "t\u0308",
|
|
400
|
+
7832: "w\u030a",
|
|
401
|
+
7833: "y\u030a",
|
|
402
|
+
7834: "a\u02be",
|
|
403
|
+
7835: "\u1e61",
|
|
404
|
+
7838: "ss",
|
|
405
|
+
8016: "\u03c5\u0313",
|
|
406
|
+
8018: "\u03c5\u0313\u0300",
|
|
407
|
+
8020: "\u03c5\u0313\u0301",
|
|
408
|
+
8022: "\u03c5\u0313\u0342",
|
|
409
|
+
8064: "\u1f00\u03b9",
|
|
410
|
+
8065: "\u1f01\u03b9",
|
|
411
|
+
8066: "\u1f02\u03b9",
|
|
412
|
+
8067: "\u1f03\u03b9",
|
|
413
|
+
8068: "\u1f04\u03b9",
|
|
414
|
+
8069: "\u1f05\u03b9",
|
|
415
|
+
8070: "\u1f06\u03b9",
|
|
416
|
+
8071: "\u1f07\u03b9",
|
|
417
|
+
8072: "\u1f00\u03b9",
|
|
418
|
+
8073: "\u1f01\u03b9",
|
|
419
|
+
8074: "\u1f02\u03b9",
|
|
420
|
+
8075: "\u1f03\u03b9",
|
|
421
|
+
8076: "\u1f04\u03b9",
|
|
422
|
+
8077: "\u1f05\u03b9",
|
|
423
|
+
8078: "\u1f06\u03b9",
|
|
424
|
+
8079: "\u1f07\u03b9",
|
|
425
|
+
8080: "\u1f20\u03b9",
|
|
426
|
+
8081: "\u1f21\u03b9",
|
|
427
|
+
8082: "\u1f22\u03b9",
|
|
428
|
+
8083: "\u1f23\u03b9",
|
|
429
|
+
8084: "\u1f24\u03b9",
|
|
430
|
+
8085: "\u1f25\u03b9",
|
|
431
|
+
8086: "\u1f26\u03b9",
|
|
432
|
+
8087: "\u1f27\u03b9",
|
|
433
|
+
8088: "\u1f20\u03b9",
|
|
434
|
+
8089: "\u1f21\u03b9",
|
|
435
|
+
8090: "\u1f22\u03b9",
|
|
436
|
+
8091: "\u1f23\u03b9",
|
|
437
|
+
8092: "\u1f24\u03b9",
|
|
438
|
+
8093: "\u1f25\u03b9",
|
|
439
|
+
8094: "\u1f26\u03b9",
|
|
440
|
+
8095: "\u1f27\u03b9",
|
|
441
|
+
8096: "\u1f60\u03b9",
|
|
442
|
+
8097: "\u1f61\u03b9",
|
|
443
|
+
8098: "\u1f62\u03b9",
|
|
444
|
+
8099: "\u1f63\u03b9",
|
|
445
|
+
8100: "\u1f64\u03b9",
|
|
446
|
+
8101: "\u1f65\u03b9",
|
|
447
|
+
8102: "\u1f66\u03b9",
|
|
448
|
+
8103: "\u1f67\u03b9",
|
|
449
|
+
8104: "\u1f60\u03b9",
|
|
450
|
+
8105: "\u1f61\u03b9",
|
|
451
|
+
8106: "\u1f62\u03b9",
|
|
452
|
+
8107: "\u1f63\u03b9",
|
|
453
|
+
8108: "\u1f64\u03b9",
|
|
454
|
+
8109: "\u1f65\u03b9",
|
|
455
|
+
8110: "\u1f66\u03b9",
|
|
456
|
+
8111: "\u1f67\u03b9",
|
|
457
|
+
8114: "\u1f70\u03b9",
|
|
458
|
+
8115: "\u03b1\u03b9",
|
|
459
|
+
8116: "\u03ac\u03b9",
|
|
460
|
+
8118: "\u03b1\u0342",
|
|
461
|
+
8119: "\u03b1\u0342\u03b9",
|
|
462
|
+
8124: "\u03b1\u03b9",
|
|
463
|
+
8126: "\u03b9",
|
|
464
|
+
8130: "\u1f74\u03b9",
|
|
465
|
+
8131: "\u03b7\u03b9",
|
|
466
|
+
8132: "\u03ae\u03b9",
|
|
467
|
+
8134: "\u03b7\u0342",
|
|
468
|
+
8135: "\u03b7\u0342\u03b9",
|
|
469
|
+
8140: "\u03b7\u03b9",
|
|
470
|
+
8146: "\u03b9\u0308\u0300",
|
|
471
|
+
8147: "\u03b9\u0308\u0301",
|
|
472
|
+
8150: "\u03b9\u0342",
|
|
473
|
+
8151: "\u03b9\u0308\u0342",
|
|
474
|
+
8162: "\u03c5\u0308\u0300",
|
|
475
|
+
8163: "\u03c5\u0308\u0301",
|
|
476
|
+
8164: "\u03c1\u0313",
|
|
477
|
+
8166: "\u03c5\u0342",
|
|
478
|
+
8167: "\u03c5\u0308\u0342",
|
|
479
|
+
8178: "\u1f7c\u03b9",
|
|
480
|
+
8179: "\u03c9\u03b9",
|
|
481
|
+
8180: "\u03ce\u03b9",
|
|
482
|
+
8182: "\u03c9\u0342",
|
|
483
|
+
8183: "\u03c9\u0342\u03b9",
|
|
484
|
+
8188: "\u03c9\u03b9",
|
|
485
|
+
43888: "\u13a0",
|
|
486
|
+
43889: "\u13a1",
|
|
487
|
+
43890: "\u13a2",
|
|
488
|
+
43891: "\u13a3",
|
|
489
|
+
43892: "\u13a4",
|
|
490
|
+
43893: "\u13a5",
|
|
491
|
+
43894: "\u13a6",
|
|
492
|
+
43895: "\u13a7",
|
|
493
|
+
43896: "\u13a8",
|
|
494
|
+
43897: "\u13a9",
|
|
495
|
+
43898: "\u13aa",
|
|
496
|
+
43899: "\u13ab",
|
|
497
|
+
43900: "\u13ac",
|
|
498
|
+
43901: "\u13ad",
|
|
499
|
+
43902: "\u13ae",
|
|
500
|
+
43903: "\u13af",
|
|
501
|
+
43904: "\u13b0",
|
|
502
|
+
43905: "\u13b1",
|
|
503
|
+
43906: "\u13b2",
|
|
504
|
+
43907: "\u13b3",
|
|
505
|
+
43908: "\u13b4",
|
|
506
|
+
43909: "\u13b5",
|
|
507
|
+
43910: "\u13b6",
|
|
508
|
+
43911: "\u13b7",
|
|
509
|
+
43912: "\u13b8",
|
|
510
|
+
43913: "\u13b9",
|
|
511
|
+
43914: "\u13ba",
|
|
512
|
+
43915: "\u13bb",
|
|
513
|
+
43916: "\u13bc",
|
|
514
|
+
43917: "\u13bd",
|
|
515
|
+
43918: "\u13be",
|
|
516
|
+
43919: "\u13bf",
|
|
517
|
+
43920: "\u13c0",
|
|
518
|
+
43921: "\u13c1",
|
|
519
|
+
43922: "\u13c2",
|
|
520
|
+
43923: "\u13c3",
|
|
521
|
+
43924: "\u13c4",
|
|
522
|
+
43925: "\u13c5",
|
|
523
|
+
43926: "\u13c6",
|
|
524
|
+
43927: "\u13c7",
|
|
525
|
+
43928: "\u13c8",
|
|
526
|
+
43929: "\u13c9",
|
|
527
|
+
43930: "\u13ca",
|
|
528
|
+
43931: "\u13cb",
|
|
529
|
+
43932: "\u13cc",
|
|
530
|
+
43933: "\u13cd",
|
|
531
|
+
43934: "\u13ce",
|
|
532
|
+
43935: "\u13cf",
|
|
533
|
+
43936: "\u13d0",
|
|
534
|
+
43937: "\u13d1",
|
|
535
|
+
43938: "\u13d2",
|
|
536
|
+
43939: "\u13d3",
|
|
537
|
+
43940: "\u13d4",
|
|
538
|
+
43941: "\u13d5",
|
|
539
|
+
43942: "\u13d6",
|
|
540
|
+
43943: "\u13d7",
|
|
541
|
+
43944: "\u13d8",
|
|
542
|
+
43945: "\u13d9",
|
|
543
|
+
43946: "\u13da",
|
|
544
|
+
43947: "\u13db",
|
|
545
|
+
43948: "\u13dc",
|
|
546
|
+
43949: "\u13dd",
|
|
547
|
+
43950: "\u13de",
|
|
548
|
+
43951: "\u13df",
|
|
549
|
+
43952: "\u13e0",
|
|
550
|
+
43953: "\u13e1",
|
|
551
|
+
43954: "\u13e2",
|
|
552
|
+
43955: "\u13e3",
|
|
553
|
+
43956: "\u13e4",
|
|
554
|
+
43957: "\u13e5",
|
|
555
|
+
43958: "\u13e6",
|
|
556
|
+
43959: "\u13e7",
|
|
557
|
+
43960: "\u13e8",
|
|
558
|
+
43961: "\u13e9",
|
|
559
|
+
43962: "\u13ea",
|
|
560
|
+
43963: "\u13eb",
|
|
561
|
+
43964: "\u13ec",
|
|
562
|
+
43965: "\u13ed",
|
|
563
|
+
43966: "\u13ee",
|
|
564
|
+
43967: "\u13ef",
|
|
565
|
+
64256: "ff",
|
|
566
|
+
64257: "fi",
|
|
567
|
+
64258: "fl",
|
|
568
|
+
64259: "ffi",
|
|
569
|
+
64260: "ffl",
|
|
570
|
+
64261: "st",
|
|
571
|
+
64262: "st",
|
|
572
|
+
64275: "\u0574\u0576",
|
|
573
|
+
64276: "\u0574\u0565",
|
|
574
|
+
64277: "\u0574\u056b",
|
|
575
|
+
64278: "\u057e\u0576",
|
|
576
|
+
64279: "\u0574\u056d",
|
|
577
|
+
});
|
|
578
|
+
/** NFKC, then full case folding: the same as Python's `normalize("NFKC", s).casefold()`. */
|
|
579
|
+
export function sandboxFoldName(name) {
|
|
580
|
+
let out = "";
|
|
581
|
+
for (const ch of name.normalize("NFKC"))
|
|
582
|
+
out += CASEFOLD_DIFF[ch.codePointAt(0)] ?? ch.toLowerCase();
|
|
583
|
+
return out;
|
|
584
|
+
}
|
|
585
|
+
const fold = sandboxFoldName;
|
|
586
|
+
const parts = (path) => path
|
|
587
|
+
.split("/")
|
|
588
|
+
.filter((part) => part !== "")
|
|
589
|
+
.map(fold);
|
|
590
|
+
/**
|
|
591
|
+
* Whether `prefix` (below the allowlisted directory `head`) is a directory where git itself keeps
|
|
592
|
+
* a `HEAD`: `logs/HEAD`, `logs/refs/remotes/<r>/HEAD`, `refs/remotes/<r>/HEAD`, `sequencer/head`.
|
|
593
|
+
*/
|
|
594
|
+
function holdsHead(head, prefix) {
|
|
595
|
+
if (head === "logs")
|
|
596
|
+
return (prefix.length === 0 ||
|
|
597
|
+
(prefix.length === 3 && prefix[0] === "refs" && prefix[1] === "remotes"));
|
|
598
|
+
if (head === "refs")
|
|
599
|
+
return prefix.length === 2 && prefix[0] === "remotes";
|
|
600
|
+
return head === "sequencer" && prefix.length === 0;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Inside an allowlisted subtree no repository can be forged: no `.git` component, no `HEAD`
|
|
604
|
+
* except where git keeps one, and next to such a `HEAD` no `objects`, `commondir` or `gitdir`
|
|
605
|
+
* (what git needs beside a `HEAD` to take a directory for a gitdir).
|
|
606
|
+
*/
|
|
607
|
+
function subtreeAllowed(head, rest) {
|
|
608
|
+
return rest.every((name, k) => {
|
|
609
|
+
if (name === ".git")
|
|
610
|
+
return false;
|
|
611
|
+
if (name === "head")
|
|
612
|
+
return k === rest.length - 1 && holdsHead(head, rest.slice(0, k));
|
|
613
|
+
if (["objects", "commondir", "gitdir"].includes(name))
|
|
614
|
+
return !holdsHead(head, rest.slice(0, k));
|
|
615
|
+
return true;
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
function gitInternalAllowed(rel) {
|
|
619
|
+
if (rel.length === 0)
|
|
620
|
+
return true;
|
|
621
|
+
const head = rel[0];
|
|
622
|
+
if (SANDBOX_GIT_ALLOWED_DIRECTORIES.includes(head))
|
|
623
|
+
return subtreeAllowed(head, rel.slice(1));
|
|
624
|
+
if ((head === "modules" || head === "worktrees") && rel.length > 1) {
|
|
625
|
+
const rest = rel.slice(1);
|
|
626
|
+
if (head === "worktrees")
|
|
627
|
+
return gitInternalAllowed(rest.slice(1));
|
|
628
|
+
const readings = rest.map((_, k) => rest.slice(k + 1));
|
|
629
|
+
if (readings.some((r) => r.length > 0 && GIT_DANGER.has(r[0]) && !gitInternalAllowed(r)))
|
|
630
|
+
return false;
|
|
631
|
+
return readings.some((r) => r.length > 0 && gitInternalAllowed(r));
|
|
632
|
+
}
|
|
633
|
+
if (rel.length === 1) {
|
|
634
|
+
const base = head.endsWith(".lock") ? head.slice(0, -5) : head;
|
|
635
|
+
// git stash's temporary index: index.stash.<pid>
|
|
636
|
+
return SANDBOX_GIT_ALLOWED_FILES.includes(base) || /^index\.stash\.[0-9]+$/.test(base);
|
|
637
|
+
}
|
|
638
|
+
return (head === "info" && rel.length === 2 && ["exclude", "exclude.lock"].includes(rel[1]));
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* The system runtime or socket directory a restricted write root `root` (absolute, normalized) is,
|
|
642
|
+
* lies inside or contains, or null: {@link SANDBOX_RUNTIME_DIRECTORIES} with everything beneath
|
|
643
|
+
* them; `/tmp` and `/private/tmp` themselves (a subdirectory is fine unless it is a known socket
|
|
644
|
+
* directory such as `/tmp/.X11-unix`, `/tmp/tmux-<uid>` or `/tmp/ssh-*`, or lies inside one); and
|
|
645
|
+
* every ancestor of one (`/var`, `/private`). The kernel's candidate builder and admission and the
|
|
646
|
+
* device's root check refuse such a root: a socket there may belong to a process outside the
|
|
647
|
+
* sandbox (the Linux broker also refuses every pathname UNIX connect to a socket the operation did
|
|
648
|
+
* not bind itself).
|
|
649
|
+
*/
|
|
650
|
+
export function sandboxRuntimeDirectoryOf(root) {
|
|
651
|
+
const names = parts(root);
|
|
652
|
+
const within = (anchor) => anchor.length <= names.length && anchor.every((name, j) => names[j] === name);
|
|
653
|
+
const above = (anchor) => names.length < anchor.length && names.every((name, j) => anchor[j] === name);
|
|
654
|
+
for (const directory of SANDBOX_RUNTIME_DIRECTORIES) {
|
|
655
|
+
const anchor = parts(directory);
|
|
656
|
+
if (within(anchor) || above(anchor))
|
|
657
|
+
return directory;
|
|
658
|
+
}
|
|
659
|
+
for (const directory of TEMP_DIRECTORIES) {
|
|
660
|
+
const anchor = parts(directory);
|
|
661
|
+
if (above(anchor) || (within(anchor) && names.length === anchor.length))
|
|
662
|
+
return directory;
|
|
663
|
+
const next = names[anchor.length];
|
|
664
|
+
if (within(anchor) && next !== undefined && TEMP_SOCKET_DIRECTORY.test(next))
|
|
665
|
+
return `${directory}/${next}`;
|
|
666
|
+
}
|
|
667
|
+
return null;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Whether `path` (absolute, normalized) is a {@link SANDBOX_HOME_WRITE_DENIES} location under
|
|
671
|
+
* `home`, or beneath one (names compared folded).
|
|
672
|
+
*/
|
|
673
|
+
export function isSandboxHomeWriteDenied(path, home) {
|
|
674
|
+
const names = parts(path);
|
|
675
|
+
const base = parts(home);
|
|
676
|
+
return SANDBOX_HOME_WRITE_DENIES.some((entry) => {
|
|
677
|
+
const anchor = [...base, ...entry.map(fold)];
|
|
678
|
+
return anchor.length <= names.length && anchor.every((name, j) => names[j] === name);
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
/** Whether restricted execution may never write `path` (an absolute, normalized path). */
|
|
682
|
+
export function isSandboxWriteDenied(path) {
|
|
683
|
+
const names = parts(path);
|
|
684
|
+
if (names.some((name) => name.startsWith(SANDBOX_RESERVED_NAME_PREFIX)))
|
|
685
|
+
return true;
|
|
686
|
+
for (const system of SANDBOX_SYSTEM_WRITE_DENIES) {
|
|
687
|
+
const anchored = parts(system);
|
|
688
|
+
if (anchored.every((name, j) => names[j] === name))
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
for (const entry of SANDBOX_MANDATORY_WRITE_DENIES)
|
|
692
|
+
for (let i = 0; i + entry.length <= names.length; i++)
|
|
693
|
+
if (entry.every((name, j) => names[i + j] === name))
|
|
694
|
+
return true;
|
|
695
|
+
const git = names.indexOf(".git");
|
|
696
|
+
if (git >= 0)
|
|
697
|
+
return !gitInternalAllowed(names.slice(git + 1));
|
|
698
|
+
return names[names.length - 1] === "head";
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* The next components a moved entry must not bring to `to`: for each guarded sequence whose proper
|
|
702
|
+
* prefix `to` ends in (anchored: equals), the component that follows. Empty when `to` is no prefix.
|
|
703
|
+
*/
|
|
704
|
+
function prefixNext(to) {
|
|
705
|
+
const next = [];
|
|
706
|
+
for (const { names, anchored } of PREFIX_GUARDED)
|
|
707
|
+
for (let k = 1; k < names.length; k++) {
|
|
708
|
+
if (anchored && to.length !== k)
|
|
709
|
+
continue;
|
|
710
|
+
if (to.length < k)
|
|
711
|
+
continue;
|
|
712
|
+
if (names.slice(0, k).every((name, j) => to[to.length - k + j] === name))
|
|
713
|
+
next.push(names[k]);
|
|
714
|
+
}
|
|
715
|
+
return next;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Whether a restricted rename or link of `source` to `destination` is refused (rule 4).
|
|
719
|
+
* `sourceEntries` are the names directly in a moved directory; the check needs them only when the
|
|
720
|
+
* destination ends in a proper prefix of a guarded entry, and without them it refuses.
|
|
721
|
+
*/
|
|
722
|
+
export function isSandboxMoveDenied(source, destination, sourceIsDirectory, sourceEntries) {
|
|
723
|
+
if (isSandboxWriteDenied(source) || isSandboxWriteDenied(destination))
|
|
724
|
+
return true;
|
|
725
|
+
const from = parts(source);
|
|
726
|
+
const to = parts(destination);
|
|
727
|
+
// an anchored deny's ancestor never moves away: a link resolved into it would dangle
|
|
728
|
+
for (const { names, anchored } of PREFIX_GUARDED)
|
|
729
|
+
if (anchored && from.length < names.length && from.every((name, k) => names[k] === name))
|
|
730
|
+
return true;
|
|
731
|
+
const next = prefixNext(to);
|
|
732
|
+
if (next.length > 0) {
|
|
733
|
+
if (!sourceIsDirectory || sourceEntries === undefined)
|
|
734
|
+
return true;
|
|
735
|
+
if (sourceEntries.some((name) => next.includes(fold(name))))
|
|
736
|
+
return true;
|
|
737
|
+
}
|
|
738
|
+
const protectedAt = (names) => names.findIndex((n) => SANDBOX_PROTECTED_PATH_NAMES.includes(n));
|
|
739
|
+
const i = protectedAt(from);
|
|
740
|
+
const samePrefix = (n) => n >= 0 && to.length > n + 1 && from.slice(0, n + 1).every((name, k) => to[k] === name);
|
|
741
|
+
if (i >= 0 && !samePrefix(i))
|
|
742
|
+
return true;
|
|
743
|
+
const j = protectedAt(to);
|
|
744
|
+
if (sourceIsDirectory && j >= 0 && !(i === j && samePrefix(j)))
|
|
745
|
+
return true;
|
|
746
|
+
return !sourceIsDirectory && to[to.length - 1] === ".git";
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* The same policy for the Python consumers (`linux-sandbox.py`, `native-files.py`): defines
|
|
750
|
+
* `write_denied(path)` and `move_denied(source, destination, is_dir)` with identical rules.
|
|
751
|
+
*/
|
|
752
|
+
export const SANDBOX_WRITE_POLICY_PYTHON = `
|
|
753
|
+
import unicodedata as _unicodedata
|
|
754
|
+
WRITE_DENIES = [[_unicodedata.normalize("NFKC", n).casefold() for n in e] for e in ${JSON.stringify(SANDBOX_MANDATORY_WRITE_DENIES)}]
|
|
755
|
+
SYSTEM_WRITE_DENIES = [[_unicodedata.normalize("NFKC", n).casefold() for n in p.split("/") if n] for p in ${JSON.stringify(SANDBOX_SYSTEM_WRITE_DENIES)}]
|
|
756
|
+
PREFIX_GUARDED = [(e, False) for e in WRITE_DENIES if len(e) > 1] + [(e, True) for e in SYSTEM_WRITE_DENIES]
|
|
757
|
+
GIT_ALLOWED_DIRECTORIES = set(${JSON.stringify(SANDBOX_GIT_ALLOWED_DIRECTORIES)})
|
|
758
|
+
GIT_ALLOWED_FILES = set(${JSON.stringify(SANDBOX_GIT_ALLOWED_FILES)})
|
|
759
|
+
GIT_DANGER = set(${JSON.stringify([...GIT_DANGER])})
|
|
760
|
+
PROTECTED_PATH_NAMES = set(${JSON.stringify(SANDBOX_PROTECTED_PATH_NAMES)})
|
|
761
|
+
RESERVED_NAME_PREFIX = ${JSON.stringify(SANDBOX_RESERVED_NAME_PREFIX)}
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
def _fold_parts(path):
|
|
765
|
+
return [_unicodedata.normalize("NFKC", p).casefold() for p in path.split("/") if p]
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
def _holds_head(head, prefix):
|
|
769
|
+
if head == "logs":
|
|
770
|
+
return not prefix or (len(prefix) == 3 and prefix[0] == "refs" and prefix[1] == "remotes")
|
|
771
|
+
if head == "refs":
|
|
772
|
+
return len(prefix) == 2 and prefix[0] == "remotes"
|
|
773
|
+
return head == "sequencer" and not prefix
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
def _subtree_allowed(head, rest):
|
|
777
|
+
for k, name in enumerate(rest):
|
|
778
|
+
if name == ".git":
|
|
779
|
+
return False
|
|
780
|
+
if name == "head" and not (k == len(rest) - 1 and _holds_head(head, rest[:k])):
|
|
781
|
+
return False
|
|
782
|
+
if name in ("objects", "commondir", "gitdir") and _holds_head(head, rest[:k]):
|
|
783
|
+
return False
|
|
784
|
+
return True
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
def _git_internal_allowed(rel):
|
|
788
|
+
if not rel:
|
|
789
|
+
return True
|
|
790
|
+
head = rel[0]
|
|
791
|
+
if head in GIT_ALLOWED_DIRECTORIES:
|
|
792
|
+
return _subtree_allowed(head, rel[1:])
|
|
793
|
+
if head in ("modules", "worktrees") and len(rel) > 1:
|
|
794
|
+
rest = rel[1:]
|
|
795
|
+
if head == "worktrees":
|
|
796
|
+
return _git_internal_allowed(rest[1:])
|
|
797
|
+
readings = [rest[k + 1:] for k in range(len(rest))]
|
|
798
|
+
if any(r and r[0] in GIT_DANGER and not _git_internal_allowed(r) for r in readings):
|
|
799
|
+
return False
|
|
800
|
+
return any(r and _git_internal_allowed(r) for r in readings)
|
|
801
|
+
if len(rel) == 1:
|
|
802
|
+
base = head[:-5] if head.endswith(".lock") else head
|
|
803
|
+
stash = base.startswith("index.stash.") and base[12:].isascii() and base[12:].isdigit()
|
|
804
|
+
return base in GIT_ALLOWED_FILES or stash
|
|
805
|
+
return head == "info" and len(rel) == 2 and rel[1] in ("exclude", "exclude.lock")
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
def write_denied(path):
|
|
809
|
+
names = _fold_parts(path)
|
|
810
|
+
if any(n.startswith(RESERVED_NAME_PREFIX) for n in names):
|
|
811
|
+
return True
|
|
812
|
+
for system in SYSTEM_WRITE_DENIES:
|
|
813
|
+
if names[:len(system)] == system:
|
|
814
|
+
return True
|
|
815
|
+
for entry in WRITE_DENIES:
|
|
816
|
+
for i in range(len(names) - len(entry) + 1):
|
|
817
|
+
if names[i:i + len(entry)] == entry:
|
|
818
|
+
return True
|
|
819
|
+
if ".git" in names:
|
|
820
|
+
return not _git_internal_allowed(names[names.index(".git") + 1:])
|
|
821
|
+
return bool(names) and names[-1] == "head"
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
def _prefix_next(dst):
|
|
825
|
+
nxt = []
|
|
826
|
+
for names, anchored in PREFIX_GUARDED:
|
|
827
|
+
for k in range(1, len(names)):
|
|
828
|
+
if (anchored and len(dst) != k) or len(dst) < k:
|
|
829
|
+
continue
|
|
830
|
+
if dst[len(dst) - k:] == names[:k]:
|
|
831
|
+
nxt.append(names[k])
|
|
832
|
+
return nxt
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
def move_denied(source, destination, is_dir, entries=None):
|
|
836
|
+
if write_denied(source) or write_denied(destination):
|
|
837
|
+
return True
|
|
838
|
+
src, dst = _fold_parts(source), _fold_parts(destination)
|
|
839
|
+
# an anchored deny's ancestor never moves away: a link resolved into it would dangle
|
|
840
|
+
for names, anchored in PREFIX_GUARDED:
|
|
841
|
+
if anchored and len(src) < len(names) and names[:len(src)] == src:
|
|
842
|
+
return True
|
|
843
|
+
nxt = _prefix_next(dst)
|
|
844
|
+
if nxt:
|
|
845
|
+
if not is_dir:
|
|
846
|
+
return True
|
|
847
|
+
if entries is None:
|
|
848
|
+
# no entries given: list the source path itself, refusing what cannot be listed
|
|
849
|
+
try:
|
|
850
|
+
import os as _os
|
|
851
|
+
entries = _os.listdir(source)
|
|
852
|
+
except OSError:
|
|
853
|
+
return True
|
|
854
|
+
if any(_unicodedata.normalize("NFKC", n).casefold() in nxt for n in entries):
|
|
855
|
+
return True
|
|
856
|
+
|
|
857
|
+
def protected_at(names):
|
|
858
|
+
return next((k for k, n in enumerate(names) if n in PROTECTED_PATH_NAMES), -1)
|
|
859
|
+
|
|
860
|
+
def same_prefix(n):
|
|
861
|
+
return n >= 0 and len(dst) > n + 1 and dst[:n + 1] == src[:n + 1]
|
|
862
|
+
i = protected_at(src)
|
|
863
|
+
if i >= 0 and not same_prefix(i):
|
|
864
|
+
return True
|
|
865
|
+
j = protected_at(dst)
|
|
866
|
+
if is_dir and j >= 0 and not (i == j and same_prefix(j)):
|
|
867
|
+
return True
|
|
868
|
+
return (not is_dir) and bool(dst) and dst[-1] == ".git"
|
|
869
|
+
`;
|