@namzu/sdk 44.3.0 → 45.0.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 (116) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/authorization/command-line.d.ts +66 -19
  3. package/dist/authorization/command-line.d.ts.map +1 -1
  4. package/dist/authorization/command-line.js +130 -270
  5. package/dist/authorization/command-line.js.map +1 -1
  6. package/dist/authorization/gate.d.ts +7 -0
  7. package/dist/authorization/gate.d.ts.map +1 -1
  8. package/dist/authorization/gate.js +1 -1
  9. package/dist/authorization/gate.js.map +1 -1
  10. package/dist/authorization/rules.d.ts +10 -1
  11. package/dist/authorization/rules.d.ts.map +1 -1
  12. package/dist/authorization/rules.js +21 -5
  13. package/dist/authorization/rules.js.map +1 -1
  14. package/dist/authorization/shell-lexer.d.ts +138 -0
  15. package/dist/authorization/shell-lexer.d.ts.map +1 -0
  16. package/dist/authorization/shell-lexer.js +2143 -0
  17. package/dist/authorization/shell-lexer.js.map +1 -0
  18. package/dist/authorization/skill-grant.d.ts +182 -0
  19. package/dist/authorization/skill-grant.d.ts.map +1 -0
  20. package/dist/authorization/skill-grant.js +314 -0
  21. package/dist/authorization/skill-grant.js.map +1 -0
  22. package/dist/persona/assembler.d.ts.map +1 -1
  23. package/dist/persona/assembler.js +5 -2
  24. package/dist/persona/assembler.js.map +1 -1
  25. package/dist/public-runtime.d.ts +1 -0
  26. package/dist/public-runtime.d.ts.map +1 -1
  27. package/dist/public-runtime.js +4 -0
  28. package/dist/public-runtime.js.map +1 -1
  29. package/dist/public-tools.d.ts.map +1 -1
  30. package/dist/public-tools.js +2 -1
  31. package/dist/public-tools.js.map +1 -1
  32. package/dist/public-types.d.ts +3 -1
  33. package/dist/public-types.d.ts.map +1 -1
  34. package/dist/runtime/jobs/registry.d.ts +2 -2
  35. package/dist/runtime/jobs/registry.d.ts.map +1 -1
  36. package/dist/runtime/jobs/registry.js +6 -2
  37. package/dist/runtime/jobs/registry.js.map +1 -1
  38. package/dist/runtime/query/executor.d.ts +34 -40
  39. package/dist/runtime/query/executor.d.ts.map +1 -1
  40. package/dist/runtime/query/executor.js +81 -51
  41. package/dist/runtime/query/executor.js.map +1 -1
  42. package/dist/runtime/query/index.d.ts.map +1 -1
  43. package/dist/runtime/query/index.js +7 -0
  44. package/dist/runtime/query/index.js.map +1 -1
  45. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  46. package/dist/runtime/query/iteration/index.js +6 -0
  47. package/dist/runtime/query/iteration/index.js.map +1 -1
  48. package/dist/runtime/query/iteration/phases/context.d.ts +7 -0
  49. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  50. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  51. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  52. package/dist/runtime/query/iteration/phases/tool-review.js +48 -0
  53. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  54. package/dist/runtime/query/review-policy.d.ts +11 -0
  55. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  56. package/dist/runtime/query/review-policy.js +32 -0
  57. package/dist/runtime/query/review-policy.js.map +1 -1
  58. package/dist/runtime/query/tooling.d.ts +3 -0
  59. package/dist/runtime/query/tooling.d.ts.map +1 -1
  60. package/dist/runtime/query/tooling.js +1 -0
  61. package/dist/runtime/query/tooling.js.map +1 -1
  62. package/dist/skills/loader.d.ts +8 -0
  63. package/dist/skills/loader.d.ts.map +1 -1
  64. package/dist/skills/loader.js +7 -1
  65. package/dist/skills/loader.js.map +1 -1
  66. package/dist/tools/builtins/bash.d.ts.map +1 -1
  67. package/dist/tools/builtins/bash.js +18 -6
  68. package/dist/tools/builtins/bash.js.map +1 -1
  69. package/dist/tools/builtins/skill.d.ts +2 -9
  70. package/dist/tools/builtins/skill.d.ts.map +1 -1
  71. package/dist/tools/builtins/skill.js +59 -51
  72. package/dist/tools/builtins/skill.js.map +1 -1
  73. package/dist/tools/command-shell.d.ts +90 -0
  74. package/dist/tools/command-shell.d.ts.map +1 -0
  75. package/dist/tools/command-shell.js +129 -0
  76. package/dist/tools/command-shell.js.map +1 -0
  77. package/dist/tools/defineTool.d.ts +11 -0
  78. package/dist/tools/defineTool.d.ts.map +1 -1
  79. package/dist/tools/defineTool.js +29 -1
  80. package/dist/tools/defineTool.js.map +1 -1
  81. package/dist/types/hitl/index.d.ts +23 -0
  82. package/dist/types/hitl/index.d.ts.map +1 -1
  83. package/dist/types/hitl/index.js.map +1 -1
  84. package/dist/types/tool/index.d.ts +67 -5
  85. package/dist/types/tool/index.d.ts.map +1 -1
  86. package/dist/types/tool/index.js.map +1 -1
  87. package/dist/utils/frontmatter.d.ts +17 -1
  88. package/dist/utils/frontmatter.d.ts.map +1 -1
  89. package/dist/utils/frontmatter.js +32 -2
  90. package/dist/utils/frontmatter.js.map +1 -1
  91. package/package.json +1 -1
  92. package/src/authorization/command-line.ts +148 -293
  93. package/src/authorization/gate.ts +8 -0
  94. package/src/authorization/rules.ts +33 -4
  95. package/src/authorization/shell-lexer.ts +2319 -0
  96. package/src/authorization/skill-grant.ts +400 -0
  97. package/src/persona/assembler.ts +5 -2
  98. package/src/public-runtime.ts +9 -0
  99. package/src/public-tools.ts +2 -1
  100. package/src/public-types.ts +7 -0
  101. package/src/runtime/jobs/registry.ts +19 -11
  102. package/src/runtime/query/executor.ts +99 -55
  103. package/src/runtime/query/index.ts +7 -0
  104. package/src/runtime/query/iteration/index.ts +5 -0
  105. package/src/runtime/query/iteration/phases/context.ts +7 -0
  106. package/src/runtime/query/iteration/phases/tool-review.ts +45 -0
  107. package/src/runtime/query/review-policy.ts +53 -0
  108. package/src/runtime/query/tooling.ts +4 -0
  109. package/src/skills/loader.ts +8 -1
  110. package/src/tools/builtins/bash.ts +24 -6
  111. package/src/tools/builtins/skill.ts +74 -53
  112. package/src/tools/command-shell.ts +166 -0
  113. package/src/tools/defineTool.ts +33 -1
  114. package/src/types/hitl/index.ts +21 -0
  115. package/src/types/tool/index.ts +67 -5
  116. package/src/utils/frontmatter.ts +52 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # Changelog
2
2
 
3
+ ## 45.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 2e2ea14: A skill's `allowed-tools` now pre-approves its tools. It no longer restricts the tool set.
8
+
9
+ The field comes from the Agent Skills format, and that format defines it this way: the listed tools skip the approval prompt for the rest of the turn that loaded the skill, and every other tool stays callable. Namzu read it the other way. After the `skill` tool loaded a skill, the next batch was narrowed to the listed tools and the model was told to "restrict yourself to" them. A skill with `allowed-tools: Read Grep` therefore took `bash` away, and the model stopped doing the work.
10
+
11
+ **What breaks in `@namzu/sdk`**
12
+
13
+ - A loaded skill no longer narrows `ToolContext.allowedTools`. If a host relied on `allowed-tools` to confine the model, it should narrow the turn itself with `QueryParams.allowedTools`, a step's `allowedTools`, or `deny` rules.
14
+ - `ToolContext.adoptSkillScope` is deprecated. The kernel never supplies it, so a tool that calls it through `?.` now does nothing. It will be removed in the next major. Use `ToolContext.grantSkillTools`.
15
+ - `createReviewHandler` / `createReviewPolicy` in `prompt` and `accept-edits` modes now approve a batch without asking when every call it would ask about is covered by a skill loaded earlier in the turn. To keep asking about every call, pass `skillGrants: 'ignore'`. `plan` and `strict` still refuse such calls, and an operator `deny` or `ask` rule, a destructive call, or a path outside the roots or the sandbox is never covered. Each approval made this way is written to the audit trail under the skill's name.
16
+ - The `bash` tool runs `bash -c` instead of `/bin/sh -c` wherever bash exists (the first `bash` on `PATH`, then `/bin/bash`, then `/usr/bin/bash`), including for background jobs. On a host whose `/bin/sh` is `dash` or `busybox sh` (Debian, Ubuntu, Alpine), commands now run in bash; a command that relied on `dash` behaviour, such as `echo` expanding `\n`, behaves as bash does. `BASH_ENV`, `ENV`, `SHELLOPTS`, `BASHOPTS` and `BASH_FUNC_*` are removed from its environment. To keep `/bin/sh`, set `NAMZU_BASH_SHELL=/bin/sh` in the environment of the process that runs the SDK. In a sandbox the tool now passes the guest `/bin/sh -c '<launcher>' sh '<command>'`, which runs bash when the guest has it; a custom `Sandbox.exec` or `spawnDetached` that inspected its arguments for `['-c', command]` sees the launcher instead. With no bash, the host still runs `/bin/sh -c`.
17
+ - A command line is read for the shell that runs it. `AuthorizationGate.evaluate`, `evaluateRule` and `SkillGrantSet.coveringSkill` called without a dialect read it for any POSIX shell (`sh`), so an allow rule or `Bash(<pattern>)` entry no longer approves a line using a bash-only construct (`$'…'`, `&>`, `|&`, `<<<`, `[[`, arrays, brace expansion, `time` and others) unless the caller says the shell is bash. The kernel says so for the `bash` tool on a host that has bash; inside a sandbox it reads in `sh`, because the guest may not have bash. A host calling the gate itself passes `commandDialect: 'bash'` in the `ToolCallContext` (or `{ commandDialect: 'bash' }` as the last argument of `evaluateRule` and `coveringSkill`) when the command will run in bash. Deny rules are unaffected: they still see every command.
18
+ - `parseAllowedTools` splits on whitespace as well as commas, and keeps `Tool(pattern)` entries whole. `"read write edit"` used to be one name and is now three.
19
+ - The skill manifest in the system prompt renders the field as `<pre_approved_tools>` instead of `<allowed_tools>`. The `skill` tool's notice lists what was pre-approved and what was ignored, and says every other tool remains available.
20
+
21
+ **Added:** `ToolDefinition.commandDialect` (and the `defineTool` option), `ToolCallContext.commandDialect`, `EvaluateRuleOptions`, the `ShellDialect` type, and `NAMZU_BASH_SHELL`; `ToolContext.grantSkillTools`, `ToolCallSummary.skillGrant`, `approve_tools.skillGranted`, `ReviewPolicyOptions.skillGrants`, `SkillGrantSet`, `compileSkillGrant`, `SKILL_TOOL_NAME_ALIASES`, `permissionPatternToRegExpSource`, and a `FrontmatterOptions` third argument to `parseFrontmatter` (`lists`), which the skill loader uses so that `allowed-tools` can be a YAML list. Names are matched case-insensitively and through the format's aliases (`Read` → `read`, `WebFetch` → `web_fetch`). `Bash(git status *)` uses the CLI permission-table glob, applied to each command in the line. `${CLAUDE_SKILL_DIR}` and `${NAMZU_SKILL_DIR}` expand to the skill's directory. An unknown name is ignored and reported, and never widens the grant. A tool that is destructive for every input (the shipped `write` and `run_code`) is ignored and reported too, because each of its calls is reviewed anyway. `BashOutput`, `KillShell`, `TaskOutput` and `TaskStop` map to `job`, and `TaskCreate`, `TaskUpdate` and `TaskList` to `task_*`. `ToolContext.grantSkillTools` returns a `commit()`, and the `skill` tool records the grant only once it has delivered the skill's instructions. The grant also ends when the operator sends a message while the turn is still running (`inboundMessages` or steering), through the new `SkillGrantSet.clear()`. A `Bash(<pattern>)` entry never covers a line that redirects output into a file (`git status > ~/.bashrc`); `/dev/null` and `2>&1` stay covered. A whole-tool `Bash` entry grants the tool as it is, writes outside the working directory included, because bash has no path argument for the escalation check. An entry naming a tool that `allowedTools` withholds from the turn or step is ignored as not available, and `SkillGrantToolResolver` gains an optional `unavailable` field for that.
22
+
23
+ **`@namzu/cli`:** a plugin skill's `allowed-tools` pre-approves for the turn and no longer takes tools away. `SKILL.md` files whose `allowed-tools` is a YAML list are now listed instead of refused. The `[permissions]` glob now comes from the SDK, and it matches the same commands as before. `permissionChecks` read a builtin tool's command line in the dialect that tool reports, as the runtime does.
24
+
25
+ ### Patch Changes
26
+
27
+ - 2e2ea14: Permission rules on a command line now read it the way bash does. A `deny` rule catches commands it used to miss, and a few lines that used to be approved or refused by accident are now decided on what they run.
28
+
29
+ An `argument_pattern` rule on a command argument, a skill's `Bash(<pattern>)` entry and the check that such an entry never covers a write through redirection used to rely on three separate readers of bash quoting, which disagreed in places. With `Bash(git status *)` granted, `git status $'\'' ; touch pwned #'` and `git status $'\'' > ~/.bashrc #'` were pre-approved, because `$'\''` was read as a closed quote and an open one. One lexer now serves all three (see `docs/sdk/command-lines.md`). It was checked against bash 5.2 and 5.3 on 2.9 million generated lines with no disagreement.
30
+
31
+ What changes for a host:
32
+
33
+ - A `deny` rule also matches each command's words after quote removal. `^git push` now denies `'git' push`, `g\it push`, `$'\x67it' push`, `GIT_DIR=. git push`, `bash "-c" "git push"` (the quoted `-c` is still the flag) and `bash -lc 'git push'`. None of these were denied before.
34
+ - A line whose only quoting is an ANSI-C quote is decoded rather than refused: an `allow` rule or a `Bash(<pattern>)` entry that matches `git status $'-s'` now approves it, since it runs `git status -s`. `> $'/dev/nul\x6c'` is `/dev/null` and is not a write.
35
+ - A here-document body is data, not commands, so `cat <<EOF … EOF` is matched as `cat <<EOF`.
36
+ - A line is refused by `allow` (opaque) in some cases it used to approve: a syntax error, `[[ … ]]`, arithmetic on a variable (`$((x))`), `${!x}`, a function definition, `shopt`/`set -o posix`, `time` followed by an option (bash as `/bin/sh` runs the command `time` there), and two forms bash itself reads two ways (`"$${…"` in double quotes, and a quoted or expanding `>&` target, whose substitution bash 5.2 runs). They now go to review.
37
+ - A segment no longer carries a trailing comment: `git push #'` is `git push`.
38
+ - An `argument_pattern` rule on an argument the tool declares as its `pathArgument` tests the whole value and does not read it as shell, so `src/app/(auth)/page.tsx` is not refused as a syntax error.
39
+
40
+ No configuration change is needed.
41
+
3
42
  ## 44.3.0
4
43
 
5
44
  ### Minor Changes
@@ -32,8 +32,9 @@
32
32
  * caller must read the two decisions differently, and {@link evaluateRule}
33
33
  * does:
34
34
  *
35
- * - **deny** matches when ANY segment matches. One prohibited command poisons
36
- * the line it rides on.
35
+ * - **deny** matches when ANY segment matches, or when any command's decoded
36
+ * words ({@link decodedCommands}) do. One prohibited command poisons the
37
+ * line it rides on, however it is quoted.
37
38
  * - **allow** matches only when EVERY segment matches, and never when the line
38
39
  * is {@link CommandLineDecomposition.opaque}. Permission is a claim about the
39
40
  * whole line, and a claim that cannot be checked is not granted.
@@ -41,34 +42,45 @@
41
42
  * That asymmetry is the same one `refuse-do-not-degrade` describes: when the
42
43
  * analysis is uncertain, the uncertainty spends against the permissive answer.
43
44
  *
45
+ * ## Where the commands come from
46
+ *
47
+ * One lexer, {@link lexShellCommandLine}, reads the line the way bash does and
48
+ * is the only thing in the SDK that knows bash's quoting. This module and
49
+ * {@link writesThroughRedirection} are views of its result. There used to be
50
+ * three hand-written walkers here, each with its own idea of where a quote
51
+ * ends, and every disagreement between them was a way to run a command the
52
+ * rules never saw.
53
+ *
44
54
  * ## What `opaque` means
45
55
  *
46
56
  * Some lines contain text that is not the command that runs. Command
47
57
  * substitution (`$(…)`, backticks, `<(…)`) executes something whose text is
48
- * not in the line at all, and `eval` runs a string assembled at runtime. No
49
- * decomposition of the source can be a decomposition of what ran, so the line
50
- * is marked opaque and `allow` declines it. `deny` still tests what is visible,
51
- * because a deny that matches too much costs a prompt and a deny that matches
52
- * too little costs the thing it was written to prevent.
58
+ * not in the line at all, and `eval` or `source` runs a string assembled at
59
+ * runtime. The lexer also reports a line opaque when it does not parse, or
60
+ * contains a construct it does not model. No decomposition of the source can
61
+ * be a decomposition of what ran, so `allow` declines it. `deny` still tests
62
+ * what is visible, because a deny that matches too much costs a prompt and a
63
+ * deny that matches too little costs the thing it was written to prevent.
53
64
  *
54
65
  * ## What it deliberately does not do
55
66
  *
56
- * A value with no chain operator, no nested shell and nothing opaque comes back
57
- * as itself, byte for byte. That keeps every rule about a non-command argument
58
- * — a path, a number, a URL — behaving exactly as it did, and confines this
59
- * machinery to the case that motivated it.
67
+ * A value that is one plain command comes back as itself, byte for byte. That
68
+ * keeps every rule about a non-command argument — a path, a number, a URL —
69
+ * behaving exactly as it did, and confines this machinery to the case that
70
+ * motivated it.
60
71
  *
61
- * It is a decomposition, not a shell. `xargs sh -c`, a command read from a
62
- * file, and a shell invoked through an interpreter it does not recognise all
63
- * pass through as ordinary text. Each of those either denies as before or, for
64
- * an allow rule, fails to match every segment and so declines. The failure mode
65
- * is a prompt, never a silent grant.
72
+ * It is a decomposition, not a shell. `xargs sh -c`, `env git push`, a command
73
+ * read from a file, and a shell invoked through an interpreter it does not
74
+ * recognise all pass through as ordinary text. Each of those either denies as
75
+ * before or, for an allow rule, fails to match every segment and so declines.
76
+ * The failure mode is a prompt, never a silent grant.
66
77
  */
78
+ import { type ShellDialect } from './shell-lexer.js';
67
79
  /** The commands a line runs, and whether that list can be trusted as complete. */
68
80
  export interface CommandLineDecomposition {
69
81
  /**
70
- * The individual commands, in source order. Never empty: a line that
71
- * decomposes to nothing yields the original.
82
+ * The individual commands' source text, in the order they were read. Never
83
+ * empty: a line that decomposes to nothing yields the original.
72
84
  */
73
85
  readonly segments: readonly string[];
74
86
  /**
@@ -77,5 +89,40 @@ export interface CommandLineDecomposition {
77
89
  */
78
90
  readonly opaque: boolean;
79
91
  }
80
- export declare function decomposeCommandLine(command: string): CommandLineDecomposition;
92
+ /**
93
+ * `dialect` is the shell that will run the line (see `ShellDialect`); a
94
+ * caller that does not know passes `sh`, whose reading holds for any POSIX
95
+ * shell.
96
+ */
97
+ export declare function decomposeCommandLine(command: string, dialect?: ShellDialect): CommandLineDecomposition;
98
+ /**
99
+ * Each command's words as bash passes them (quotes removed, `$'…'` decoded),
100
+ * joined by single spaces — and, for a command led by assignments, the same
101
+ * without them. For `deny` only.
102
+ *
103
+ * A deny rule written as `^git push` must not be evaded by `'git' push`,
104
+ * `g\it push`, `$'git' push` or `GIT_DIR=x git push`: the source text of each
105
+ * differs from the pattern and the command that runs does not. `allow` does
106
+ * not use these. Its subject stays the source text, so a pattern that names
107
+ * quotes keeps meaning what its author wrote, and a decoded form can only ever
108
+ * add a match — which for `deny` is the safe direction and for `allow` is not.
109
+ */
110
+ export declare function decodedCommands(command: string, dialect?: ShellDialect): readonly string[];
111
+ /**
112
+ * Whether a command line sends output into a file through a shell redirection.
113
+ *
114
+ * A permission pattern names commands. `>`, `>>`, `>|`, `&>`, `&>>`, `<>` and
115
+ * `>&word` open a file for writing whose path is not the command's argument,
116
+ * so a pattern that covers `git status *` would otherwise also cover
117
+ * `git status > ~/.bashrc`. Callers that grant on a pattern's say-so decline
118
+ * such a line.
119
+ *
120
+ * Not writes: a target of `/dev/null`, descriptor duplication and closing
121
+ * (`2>&1`, `>&2`, `>&-`), and anything quoted or escaped so that it is not an
122
+ * operator. Anything whose target is not known before the line runs — a
123
+ * target built from a variable, a glob or a tilde — counts as a write, and so
124
+ * does a line that does not parse or holds a process substitution: the
125
+ * uncertainty spends against the grant.
126
+ */
127
+ export declare function writesThroughRedirection(command: string, dialect?: ShellDialect): boolean;
81
128
  //# sourceMappingURL=command-line.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"command-line.d.ts","sourceRoot":"","sources":["../../src/authorization/command-line.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAEH,kFAAkF;AAClF,MAAM,WAAW,wBAAwB;IACxC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;IACpC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;CACxB;AAwBD,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,CAe9E"}
1
+ {"version":3,"file":"command-line.d.ts","sourceRoot":"","sources":["../../src/authorization/command-line.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4EG;AAEH,OAAO,EACN,KAAK,YAAY,EAKjB,MAAM,kBAAkB,CAAA;AAEzB,kFAAkF;AAClF,MAAM,WAAW,wBAAwB;IACxC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;IACpC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;CACxB;AAYD;;;;GAIG;AACH,wBAAgB,oBAAoB,CACnC,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,YAAqB,GAC5B,wBAAwB,CAmC1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,YAAqB,GAC5B,SAAS,MAAM,EAAE,CAenB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,YAAqB,GAAG,OAAO,CAKjG"}
@@ -32,8 +32,9 @@
32
32
  * caller must read the two decisions differently, and {@link evaluateRule}
33
33
  * does:
34
34
  *
35
- * - **deny** matches when ANY segment matches. One prohibited command poisons
36
- * the line it rides on.
35
+ * - **deny** matches when ANY segment matches, or when any command's decoded
36
+ * words ({@link decodedCommands}) do. One prohibited command poisons the
37
+ * line it rides on, however it is quoted.
37
38
  * - **allow** matches only when EVERY segment matches, and never when the line
38
39
  * is {@link CommandLineDecomposition.opaque}. Permission is a claim about the
39
40
  * whole line, and a claim that cannot be checked is not granted.
@@ -41,307 +42,166 @@
41
42
  * That asymmetry is the same one `refuse-do-not-degrade` describes: when the
42
43
  * analysis is uncertain, the uncertainty spends against the permissive answer.
43
44
  *
45
+ * ## Where the commands come from
46
+ *
47
+ * One lexer, {@link lexShellCommandLine}, reads the line the way bash does and
48
+ * is the only thing in the SDK that knows bash's quoting. This module and
49
+ * {@link writesThroughRedirection} are views of its result. There used to be
50
+ * three hand-written walkers here, each with its own idea of where a quote
51
+ * ends, and every disagreement between them was a way to run a command the
52
+ * rules never saw.
53
+ *
44
54
  * ## What `opaque` means
45
55
  *
46
56
  * Some lines contain text that is not the command that runs. Command
47
57
  * substitution (`$(…)`, backticks, `<(…)`) executes something whose text is
48
- * not in the line at all, and `eval` runs a string assembled at runtime. No
49
- * decomposition of the source can be a decomposition of what ran, so the line
50
- * is marked opaque and `allow` declines it. `deny` still tests what is visible,
51
- * because a deny that matches too much costs a prompt and a deny that matches
52
- * too little costs the thing it was written to prevent.
58
+ * not in the line at all, and `eval` or `source` runs a string assembled at
59
+ * runtime. The lexer also reports a line opaque when it does not parse, or
60
+ * contains a construct it does not model. No decomposition of the source can
61
+ * be a decomposition of what ran, so `allow` declines it. `deny` still tests
62
+ * what is visible, because a deny that matches too much costs a prompt and a
63
+ * deny that matches too little costs the thing it was written to prevent.
53
64
  *
54
65
  * ## What it deliberately does not do
55
66
  *
56
- * A value with no chain operator, no nested shell and nothing opaque comes back
57
- * as itself, byte for byte. That keeps every rule about a non-command argument
58
- * — a path, a number, a URL — behaving exactly as it did, and confines this
59
- * machinery to the case that motivated it.
67
+ * A value that is one plain command comes back as itself, byte for byte. That
68
+ * keeps every rule about a non-command argument — a path, a number, a URL —
69
+ * behaving exactly as it did, and confines this machinery to the case that
70
+ * motivated it.
60
71
  *
61
- * It is a decomposition, not a shell. `xargs sh -c`, a command read from a
62
- * file, and a shell invoked through an interpreter it does not recognise all
63
- * pass through as ordinary text. Each of those either denies as before or, for
64
- * an allow rule, fails to match every segment and so declines. The failure mode
65
- * is a prompt, never a silent grant.
72
+ * It is a decomposition, not a shell. `xargs sh -c`, `env git push`, a command
73
+ * read from a file, and a shell invoked through an interpreter it does not
74
+ * recognise all pass through as ordinary text. Each of those either denies as
75
+ * before or, for an allow rule, fails to match every segment and so declines.
76
+ * The failure mode is a prompt, never a silent grant.
66
77
  */
67
- /**
68
- * Shells whose `-c` argument is another command line.
69
- *
70
- * Matched on the basename, so `/bin/bash` and `bash` are the same entry. An
71
- * interpreter absent from this list is not a hole that grants anything: its
72
- * payload stays inside one segment, where an allow rule fails to match it.
73
- */
74
- const NESTED_SHELLS = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh', 'ash', 'busybox']);
78
+ import { basename, lexShellCommandLine, } from './shell-lexer.js';
75
79
  /** Commands whose argument is code assembled at runtime. */
76
80
  const RUNTIME_EVALUATORS = new Set(['eval', 'source', '.']);
77
81
  /**
78
- * Depth and width limits.
79
- *
80
- * A line that exceeds either is reported opaque rather than truncated: a
82
+ * Width limit. A line past it is reported opaque rather than truncated: a
81
83
  * shortened list of segments would read as complete to `allow`, which is the
82
84
  * one reading that must never be wrong.
83
85
  */
84
- const MAX_DEPTH = 4;
85
86
  const MAX_SEGMENTS = 64;
86
- export function decomposeCommandLine(command) {
87
- const state = { opaque: false, structured: false };
88
- const segments = split(command, state, 0);
89
- // The untouched-value case, kept exact. Nothing was cut and nothing was
90
- // unpacked, so there is no decomposition to report and the value goes back
91
- // as it arrived — which is what keeps a rule about a path or a URL seeing
92
- // the string it always saw, punctuation and surrounding space included.
93
- if (!state.structured)
94
- return { segments: [command], opaque: state.opaque };
95
- if (segments.length === 0)
96
- return { segments: [command], opaque: state.opaque };
97
- if (segments.length > MAX_SEGMENTS) {
98
- return { segments: segments.slice(0, MAX_SEGMENTS), opaque: true };
87
+ /**
88
+ * `dialect` is the shell that will run the line (see `ShellDialect`); a
89
+ * caller that does not know passes `sh`, whose reading holds for any POSIX
90
+ * shell.
91
+ */
92
+ export function decomposeCommandLine(command, dialect = 'bash') {
93
+ const lexed = lex(command, dialect);
94
+ let opaque = lexed.opaque;
95
+ for (const each of lexed.commands) {
96
+ const head = each.words[each.assignments];
97
+ if (head !== undefined && !head.expands && RUNTIME_EVALUATORS.has(basename(head.value))) {
98
+ // The argument is source text assembled elsewhere. Even when it is a
99
+ // visible literal, what runs is decided at runtime.
100
+ opaque = true;
101
+ }
99
102
  }
100
- return { segments, opaque: state.opaque };
103
+ // A line that does not parse runs none of the text from its error on, and
104
+ // what it ran before that is in `decodedCommands` for deny. The value goes
105
+ // back untouched, the way a path or a URL that is not shell at all does.
106
+ if (!lexed.complete || lexed.commands.length === 0)
107
+ return { segments: [command], opaque };
108
+ // The untouched-value case, kept exact: one command whose text is the
109
+ // whole line. Nothing was cut and nothing was unpacked, so the value goes
110
+ // back as it arrived — which is what keeps a rule about a path or a URL
111
+ // seeing the string it always saw, surrounding space included.
112
+ const only = lexed.commands[0];
113
+ if (lexed.commands.length === 1 &&
114
+ only !== undefined &&
115
+ only.origin === 'line' &&
116
+ only.text === command.trim()) {
117
+ return { segments: [command], opaque };
118
+ }
119
+ const segments = lexed.commands.map((each) => each.text);
120
+ if (segments.length > MAX_SEGMENTS)
121
+ return { segments: segments.slice(0, MAX_SEGMENTS), opaque: true };
122
+ return { segments, opaque };
101
123
  }
102
124
  /**
103
- * Walk the line once, quote-aware, cutting at every top-level separator.
104
- *
105
- * Quote tracking is the whole reason this is not a `String.split`: `echo "a &&
106
- * b"` is one command that prints a literal, and a splitter that cannot tell
107
- * would report a second command named `b"` — inventing a segment is as wrong as
108
- * missing one, because `allow` requires every segment to match.
125
+ * Each command's words as bash passes them (quotes removed, `$'…'` decoded),
126
+ * joined by single spaces — and, for a command led by assignments, the same
127
+ * without them. For `deny` only.
128
+ *
129
+ * A deny rule written as `^git push` must not be evaded by `'git' push`,
130
+ * `g\it push`, `$'git' push` or `GIT_DIR=x git push`: the source text of each
131
+ * differs from the pattern and the command that runs does not. `allow` does
132
+ * not use these. Its subject stays the source text, so a pattern that names
133
+ * quotes keeps meaning what its author wrote, and a decoded form can only ever
134
+ * add a match — which for `deny` is the safe direction and for `allow` is not.
109
135
  */
110
- function split(command, state, depth) {
111
- const segments = [];
112
- let current = '';
113
- let quote = null;
114
- const cut = () => {
115
- const trimmed = trimSegment(current);
116
- current = '';
117
- if (trimmed === '')
118
- return;
119
- for (const piece of expand(trimmed, state, depth))
120
- segments.push(piece);
121
- };
122
- for (let i = 0; i < command.length; i += 1) {
123
- const char = command[i];
124
- if (quote === "'") {
125
- // Single quotes suspend everything, including the backslash. This is
126
- // the branch that keeps `echo 'a && b'` one command.
127
- if (char === "'")
128
- quote = null;
129
- current += char;
136
+ export function decodedCommands(command, dialect = 'bash') {
137
+ const out = [];
138
+ for (const each of lex(command, dialect).commands) {
139
+ if (each.words.length === 0)
130
140
  continue;
141
+ out.push(each.words.map((word) => word.value).join(' '));
142
+ if (each.assignments > 0 && each.words.length > each.assignments) {
143
+ out.push(each.words
144
+ .slice(each.assignments)
145
+ .map((word) => word.value)
146
+ .join(' '));
131
147
  }
132
- if (char === '\\') {
133
- // An escaped separator is a literal, so both characters go through
134
- // untouched and the next loop never sees the separator as one.
135
- current += char + (command[i + 1] ?? '');
136
- i += 1;
137
- continue;
138
- }
139
- if (quote === '"') {
140
- if (char === '"')
141
- quote = null;
142
- // Substitution is live inside double quotes, which is exactly where
143
- // it hides best.
144
- else if (isSubstitutionStart(command, i))
145
- state.opaque = true;
146
- current += char;
147
- continue;
148
- }
149
- if (char === "'" || char === '"') {
150
- quote = char;
151
- current += char;
152
- continue;
153
- }
154
- if (isSubstitutionStart(command, i)) {
155
- state.opaque = true;
156
- current += char;
157
- continue;
158
- }
159
- const separator = separatorAt(command, i);
160
- if (separator > 0) {
161
- state.structured = true;
162
- cut();
163
- i += separator - 1;
164
- continue;
165
- }
166
- current += char;
167
148
  }
168
- // An unterminated quote means the line does not parse. Whatever it runs is
169
- // not what this walk saw, so the caller must not treat the result as a
170
- // complete account.
171
- if (quote !== null)
172
- state.opaque = true;
173
- cut();
174
- return segments;
149
+ return out;
175
150
  }
176
151
  /**
177
- * Length of the separator starting at `index`, or 0.
178
- *
179
- * The redirection cases are why this is a function. `2>&1` and `&>log` contain
180
- * `&` and are not separators; splitting there would manufacture a segment named
181
- * `1`, which no allow rule matches, and a command that redirects its output
182
- * would stop being approvable for a reason nobody could see.
152
+ * Whether a command line sends output into a file through a shell redirection.
153
+ *
154
+ * A permission pattern names commands. `>`, `>>`, `>|`, `&>`, `&>>`, `<>` and
155
+ * `>&word` open a file for writing whose path is not the command's argument,
156
+ * so a pattern that covers `git status *` would otherwise also cover
157
+ * `git status > ~/.bashrc`. Callers that grant on a pattern's say-so decline
158
+ * such a line.
159
+ *
160
+ * Not writes: a target of `/dev/null`, descriptor duplication and closing
161
+ * (`2>&1`, `>&2`, `>&-`), and anything quoted or escaped so that it is not an
162
+ * operator. Anything whose target is not known before the line runs — a
163
+ * target built from a variable, a glob or a tilde — counts as a write, and so
164
+ * does a line that does not parse or holds a process substitution: the
165
+ * uncertainty spends against the grant.
183
166
  */
184
- function separatorAt(command, index) {
185
- const char = command[index];
186
- const next = command[index + 1];
187
- if (char === '\n')
188
- return 1;
189
- if (char === ';')
190
- return next === ';' ? 2 : 1;
191
- if (char === '&') {
192
- if (next === '&')
193
- return 2;
194
- if (next === '>')
195
- return 0;
196
- if (command[index - 1] === '>')
197
- return 0;
198
- return 1;
199
- }
200
- if (char === '|') {
201
- if (next === '|')
202
- return 2;
203
- // `|&` pipes stderr as well; still a pipe, and both sides still run.
204
- if (next === '&')
205
- return 2;
206
- return 1;
207
- }
208
- return 0;
209
- }
210
- /** Whether a command substitution opens here. */
211
- function isSubstitutionStart(command, index) {
212
- const char = command[index];
213
- if (char === '`')
167
+ export function writesThroughRedirection(command, dialect = 'bash') {
168
+ const lexed = lex(command, dialect);
169
+ if (!lexed.complete)
214
170
  return true;
215
- if (char === '$' && command[index + 1] === '(')
171
+ if (lexed.reasons.includes('process substitution'))
216
172
  return true;
217
- // Process substitution: `diff <(a) <(b)` runs `a` and `b`.
218
- if ((char === '<' || char === '>') && command[index + 1] === '(')
219
- return true;
220
- return false;
221
- }
222
- /**
223
- * Strip the grouping punctuation a split leaves behind.
224
- *
225
- * `(cd build && make)` cuts into `(cd build` and `make)`. Leaving the bracket on
226
- * would stop an allow rule matching a command it names, and — worse — stop a
227
- * deny rule matching one, since `^make` does not match `make)`.
228
- */
229
- function trimSegment(segment) {
230
- return segment
231
- .trim()
232
- .replace(/^[({\s]+/, '')
233
- .replace(/[)}\s]+$/, '');
173
+ return lexed.redirections.some(writes);
234
174
  }
235
- /**
236
- * Turn one segment into the commands it stands for.
237
- *
238
- * A shell invoked with `-c` carries a whole second command line in an argument,
239
- * and that argument is where the smuggling this module exists for is easiest:
240
- * `bash -c "git push"` contains no separator at all, so nothing above this
241
- * function would have looked inside it.
242
- *
243
- * The outer segment is kept alongside the inner ones. A rule that denies the
244
- * interpreter itself must still fire, and for `allow` the extra segment only
245
- * makes the requirement stricter — which is the safe direction.
246
- */
247
- function expand(segment, state, depth) {
248
- const words = tokenize(segment);
249
- const head = words[0];
250
- if (head === undefined)
251
- return [segment];
252
- if (RUNTIME_EVALUATORS.has(basename(head.text))) {
253
- // The argument is source text assembled elsewhere. Even when it is a
254
- // visible literal, what runs is decided at runtime.
255
- state.opaque = true;
256
- return [segment];
257
- }
258
- if (!NESTED_SHELLS.has(basename(head.text)))
259
- return [segment];
260
- const flag = words.findIndex((word, index) => index > 0 && word.quoted === null && word.text === '-c');
261
- if (flag < 0)
262
- return [segment];
263
- const payload = words[flag + 1];
264
- if (payload === undefined) {
265
- // `bash -c` with nothing after it is either a syntax error or an
266
- // argument this tokenizer failed to read. Neither may be reported as
267
- // "there is no nested command".
268
- state.opaque = true;
269
- return [segment];
270
- }
271
- if (depth + 1 >= MAX_DEPTH) {
272
- state.opaque = true;
273
- return [segment];
175
+ function writes(redirection) {
176
+ const { operator, target } = redirection;
177
+ switch (operator) {
178
+ case '<':
179
+ case '<&':
180
+ case '<<':
181
+ case '<<-':
182
+ case '<<<':
183
+ return false;
184
+ case '>&':
185
+ // `>&N`, `>&N-`, `>&-` duplicate or close a descriptor. `>&word`
186
+ // with any other word redirects both streams into that file.
187
+ if (!target.expands && /^(?:\d+-?|-)$/.test(target.value))
188
+ return false;
189
+ return target.expands || target.value !== '/dev/null';
190
+ default:
191
+ return target.expands || target.value !== '/dev/null';
274
192
  }
275
- const nested = split(payload.text, state, depth + 1);
276
- if (nested.length === 0)
277
- return [segment];
278
- state.structured = true;
279
- return [segment, ...nested];
280
193
  }
281
194
  /**
282
- * Split a segment into words, removing one layer of quoting.
283
- *
284
- * The quote is reported rather than discarded because `-c` must be the flag and
285
- * not a literal: `echo "-c"` names no nested shell, and treating its next word
286
- * as a command line would decompose a string that never runs.
195
+ * One gate evaluation tests the same line against every rule, and each rule
196
+ * asks for it again. The last line lexed is kept so that is one lexing.
287
197
  */
288
- function tokenize(segment) {
289
- const words = [];
290
- let current = '';
291
- let quote = null;
292
- let sawQuote = null;
293
- let open = false;
294
- const push = () => {
295
- if (open)
296
- words.push({ text: current, quoted: sawQuote });
297
- current = '';
298
- sawQuote = null;
299
- open = false;
300
- };
301
- for (let i = 0; i < segment.length; i += 1) {
302
- const char = segment[i];
303
- if (quote === "'") {
304
- // Single quotes suspend the backslash too, so this branch precedes
305
- // the escape below rather than sharing it.
306
- if (char === "'")
307
- quote = null;
308
- else
309
- current += char;
310
- open = true;
311
- continue;
312
- }
313
- if (char === '\\' && i + 1 < segment.length) {
314
- current += segment[i + 1];
315
- i += 1;
316
- open = true;
317
- continue;
318
- }
319
- if (quote === '"') {
320
- if (char === '"')
321
- quote = null;
322
- else
323
- current += char;
324
- open = true;
325
- continue;
326
- }
327
- if (char === "'" || char === '"') {
328
- quote = char;
329
- sawQuote = char;
330
- open = true;
331
- continue;
332
- }
333
- if (char === ' ' || char === '\t') {
334
- push();
335
- continue;
336
- }
337
- current += char;
338
- open = true;
198
+ let cached;
199
+ function lex(command, dialect) {
200
+ if (cached !== undefined && cached.command === command && cached.dialect === dialect) {
201
+ return cached.result;
339
202
  }
340
- push();
341
- return words;
342
- }
343
- function basename(word) {
344
- const cut = word.lastIndexOf('/');
345
- return cut < 0 ? word : word.slice(cut + 1);
203
+ const result = lexShellCommandLine(command, { dialect });
204
+ cached = { command, dialect, result };
205
+ return result;
346
206
  }
347
207
  //# sourceMappingURL=command-line.js.map