dsh-extra-paths 0.2.0 → 0.3.0

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.
Files changed (52) hide show
  1. package/README.md +192 -28
  2. package/README.zh.md +166 -21
  3. package/lib/argv.js +96 -24
  4. package/lib/argv.js.map +1 -1
  5. package/lib/client.js +1310 -450
  6. package/lib/config.js +140 -44
  7. package/lib/config.js.map +1 -1
  8. package/lib/context.js +48 -12
  9. package/lib/context.js.map +1 -1
  10. package/lib/denial.js +132 -0
  11. package/lib/denial.js.map +1 -0
  12. package/lib/index.js +40 -5
  13. package/lib/index.js.map +1 -1
  14. package/lib/name-rule.js +217 -0
  15. package/lib/name-rule.js.map +1 -0
  16. package/lib/paths.js +155 -27
  17. package/lib/paths.js.map +1 -1
  18. package/lib/pattern.js +128 -62
  19. package/lib/pattern.js.map +1 -1
  20. package/lib/platform.js +325 -0
  21. package/lib/platform.js.map +1 -0
  22. package/lib/preview.js +47 -20
  23. package/lib/preview.js.map +1 -1
  24. package/lib/resolve.js +345 -64
  25. package/lib/resolve.js.map +1 -1
  26. package/lib/schema.js +19 -6
  27. package/lib/schema.js.map +1 -1
  28. package/lib/shell-dialect.js +92 -0
  29. package/lib/shell-dialect.js.map +1 -0
  30. package/lib/tool-guard.js +214 -0
  31. package/lib/tool-guard.js.map +1 -0
  32. package/lib/types/argv.d.ts +50 -12
  33. package/lib/types/config.d.ts +29 -13
  34. package/lib/types/context.d.ts +6 -1
  35. package/lib/types/denial.d.ts +88 -0
  36. package/lib/types/index.d.ts +8 -4
  37. package/lib/types/name-rule.d.ts +112 -0
  38. package/lib/types/paths.d.ts +55 -10
  39. package/lib/types/pattern.d.ts +43 -5
  40. package/lib/types/platform.d.ts +148 -0
  41. package/lib/types/preview.d.ts +39 -3
  42. package/lib/types/resolve.d.ts +129 -23
  43. package/lib/types/schema.d.ts +24 -8
  44. package/lib/types/shell-dialect.d.ts +60 -0
  45. package/lib/types/tool-guard.d.ts +103 -0
  46. package/lib/types/wrap-fs.d.ts +86 -4
  47. package/lib/types/wrap-sandbox.d.ts +26 -6
  48. package/lib/wrap-fs.js +323 -47
  49. package/lib/wrap-fs.js.map +1 -1
  50. package/lib/wrap-sandbox.js +149 -12
  51. package/lib/wrap-sandbox.js.map +1 -1
  52. package/package.json +3 -1
package/lib/argv.js CHANGED
@@ -1,45 +1,117 @@
1
1
  /**
2
- * Runner-dialect widening: append one write grant per configured root to an
3
- * argv the base provider already wrapped. Detection is structural (flags, not
4
- * program names) and every unrecognised shape fails closed by returning
5
- * `undefined` — the caller then leaves the argv untouched, so the write is
6
- * denied rather than silently mis-granted.
2
+ * Runner-dialect widening: append the configured write grants AND the configured
3
+ * restrictions to an argv the base provider already wrapped.
4
+ *
5
+ * The two outer runners can express a restriction by APPENDING to what the base
6
+ * provider built, but only because both dialects resolve conflicts in favour of
7
+ * the LATER entry:
8
+ * - seatbelt (macOS): SBPL is last-match-wins, so a deny rule appended after the
9
+ * host's `(allow default)` + `(deny file-write*)` + per-root allows wins;
10
+ * - bwrap (Linux): a later mount covers an earlier one, so `--ro-bind`/`--mask`
11
+ * appended after the writable binds win.
12
+ * The inner runners cannot: landlock is a pure allow-list (nothing can be
13
+ * subtracted) and windows-acl's runner rejects unknown flags. Those return the
14
+ * unchanged argv plus the dialect that could not express the rules, and the
15
+ * caller warns honestly instead of inventing a flag.
16
+ *
17
+ * Detection is structural (flags, never a program-name grep) and every
18
+ * unrecognised shape fails closed by returning `undefined` — the caller then
19
+ * leaves the argv untouched, so the write is denied rather than silently
20
+ * mis-granted.
7
21
  * @module dsh-extra-paths/argv
8
22
  */
9
23
  /** Quote one path as an SBPL string literal. */
10
24
  function sbplString(path) {
11
25
  return `"${path.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`;
12
26
  }
27
+ /** One SBPL subpath operation, with the leading space that joins it to a profile. */
28
+ function sbplSubpath(verb, operations, path) {
29
+ return ` (${verb} ${operations} (subpath ${sbplString(path)}))`;
30
+ }
31
+ /**
32
+ * The SBPL forms to append to the base profile, in decision order: grants first,
33
+ * then the write downgrade, then the full denial (SBPL: the LAST matching rule
34
+ * wins, so the denial must come last).
35
+ * @param rules - canonical roots to append.
36
+ * @returns the concatenated forms (each with its own leading space).
37
+ */
38
+ function seatbeltRules(rules) {
39
+ const grants = rules.grants.map(root => sbplSubpath('allow', 'file-write*', root)).join('');
40
+ const readOnly = rules.readOnly.map(root => sbplSubpath('deny', 'file-write*', root)).join('');
41
+ const denied = rules.denied
42
+ .map(root => sbplSubpath('deny', 'file-read*', root) + sbplSubpath('deny', 'file-write*', root))
43
+ .join('');
44
+ return `${grants}${readOnly}${denied}`;
45
+ }
46
+ /** Whether a head carries one of the flags in `list`. */
47
+ function hasFlag(head, ...flags) {
48
+ return flags.some(flag => head.includes(flag));
49
+ }
50
+ /** The runner basename: `--`-safe, prefix-directory-safe, Windows-separator-safe. */
51
+ function runnerName(argv) {
52
+ return (argv[0] ?? '').replaceAll('\\', '/').split('/').pop() ?? '';
53
+ }
54
+ /**
55
+ * Whether the base provider's head is the windows-acl runner. Its argv carries a
56
+ * workspace root and a mode flag and REJECTS unknown arguments, so the plugin
57
+ * must never append anything to it.
58
+ * @param head - the confined argv before the caller's separator.
59
+ * @returns true when the runner is windows-acl.
60
+ */
61
+ function isWindowsAcl(head) {
62
+ return runnerName(head).includes('windows-acl') || (hasFlag(head, '--workspace') && hasFlag(head, '--mode'));
63
+ }
13
64
  /**
14
- * Insert write grants for `roots` into a confined argv.
15
- * @param confined - the argv the base provider produced (runner, profile, `--`, command).
16
- * @param wrappedLength - length of the caller's original argv, which locates the separator.
17
- * @param roots - canonical roots to grant.
18
- * @returns the widened argv, the unchanged argv when there is nothing to grant,
19
- * or `undefined` when the dialect is unrecognised (the caller must not grant).
65
+ * Append the shell-surface rules to a confined argv.
66
+ *
67
+ * The caller's separator (`--`) is located from `wrappedLength`, exactly like the
68
+ * grant path always has: it is the anchor that guarantees the appended flags land
69
+ * in the WRAPPER's argv and never in the user's command.
70
+ * @param confined - the argv the base provider produced.
71
+ * @param wrappedLength - length of the caller's original argv.
72
+ * @param rules - canonical roots to append (grants/readOnly/denied).
73
+ * @returns the widened argv; the unchanged argv plus `unsupported` when the
74
+ * dialect cannot express the restrictions; `undefined` when the dialect is
75
+ * unrecognised (the caller must not claim enforcement and must warn).
20
76
  */
21
- export function widenArgv(confined, wrappedLength, roots) {
22
- if (roots.length === 0)
23
- return confined;
77
+ export function widenArgv(confined, wrappedLength, rules) {
78
+ const hasRules = rules.grants.length > 0 || rules.readOnly.length > 0 || rules.denied.length > 0;
79
+ if (!hasRules)
80
+ return { argv: confined };
24
81
  const separatorIndex = confined.length - wrappedLength - 1;
25
82
  if (separatorIndex <= 0 || confined[separatorIndex] !== '--')
26
83
  return undefined;
27
84
  const head = confined.slice(0, separatorIndex);
28
85
  const tail = confined.slice(separatorIndex);
29
- // Seatbelt: sandbox-exec -p <profile> -- <argv>
86
+ // Seatbelt: sandbox-exec -p <profile> -- <argv>; the profile is the only
87
+ // element edited so the base provider's own rules survive verbatim.
30
88
  if (head[1] === '-p' && head[2]?.startsWith('(version 1)') === true) {
31
- const extra = roots.map(root => ` (allow file-write* (subpath ${sbplString(root)}))`).join('');
32
- return [...head.slice(0, 2), `${head[2]}${extra}`, ...head.slice(3), ...tail];
89
+ return { argv: [...head.slice(0, 2), `${head[2]}${seatbeltRules(rules)}`, ...head.slice(3), ...tail] };
33
90
  }
34
- // bwrap: --ro-bind / / ... (a writable bind per root)
35
- if (head.includes('--ro-bind')) {
36
- return [...head, ...roots.flatMap(root => ['--bind', root, root]), ...tail];
91
+ // bwrap: later mounts cover earlier ones, so grants bind writable, read-only
92
+ // rules rebind read-only, and deny rules mask the path — in that order.
93
+ if (hasFlag(head, '--ro-bind')) {
94
+ const extra = [
95
+ ...rules.grants.flatMap(root => ['--bind', root, root]),
96
+ ...rules.readOnly.flatMap(root => ['--ro-bind', root, root]),
97
+ ...rules.denied.flatMap(root => ['--mask', root]),
98
+ ];
99
+ return { argv: [...head, ...extra, ...tail] };
37
100
  }
38
- // Landlock launcher: --ro <path> / --rw <path> grants
39
- if (head.includes('--ro') || head.includes('--rw')) {
40
- return [...head, ...roots.flatMap(root => ['--rw', root]), ...tail];
101
+ // Landlock: an allow-list. New writable roots are expressible (`--rw`), a
102
+ // subtraction is NOT: report the dialect so the caller warns honestly.
103
+ if (hasFlag(head, '--ro', '--rw')) {
104
+ const widened = rules.grants.length === 0
105
+ ? confined
106
+ : [...head, ...rules.grants.flatMap(root => ['--rw', root]), ...tail];
107
+ const restrictive = rules.readOnly.length > 0 || rules.denied.length > 0;
108
+ return restrictive ? { argv: widened, unsupported: 'landlock' } : { argv: widened };
41
109
  }
42
- // windows-acl and any future runner: not expressible here.
110
+ // windows-acl: the runner rejects arguments it does not know, so nothing can
111
+ // be appended — not even grants (the current fail-closed behaviour).
112
+ if (isWindowsAcl(head))
113
+ return { argv: confined, unsupported: 'windows-acl' };
114
+ // `sandbox-ssh`, a future runner, or a malformed shape: fail closed.
43
115
  return undefined;
44
116
  }
45
117
  //# sourceMappingURL=argv.js.map
package/lib/argv.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"argv.js","sourceRoot":"","sources":["../src/argv.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,gDAAgD;AAChD,SAAS,UAAU,CAAC,IAAY;IAC9B,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAA;AACpE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CACvB,QAA2B,EAC3B,aAAqB,EACrB,KAAwB;IAExB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,QAAQ,CAAA;IACvC,MAAM,cAAc,GAAG,QAAQ,CAAC,MAAM,GAAG,aAAa,GAAG,CAAC,CAAA;IAC1D,IAAI,cAAc,IAAI,CAAC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