Repository navigation
Commit 04d4114
fix(cli): os datasource introspect --out jail judges the realpath, not the spelling (#33994)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbTGhxJSYAmQXU7efVdNqrpw)_
Fixes #33841
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:`os datasource introspect --out`
写文件前按解析符号链接后的真实位置判断是否在当前目录内;`--out` 本身是符号链接也拒绝。
- 为什么改:原检查只看路径字面,项目里一个指向外部目录的符号链接就能让服务端生成的草稿写到项目外,且退出码 0。
- 风险与回滚:只动这一个命令的 `--out` 分支;拒绝文案与退出码 2 不变;回滚即 revert 本 PR。
- 先看:`packages/cli/src/commands/datasource/introspect.ts` 的
`landsInside`(上溯到最近的已存在目录再 realpath),然后是同目录新测试。
- 两分钟验证:`pnpm --filter @objectstack/cli exec vitest run --project unit
src/commands/datasource/introspect-out-jail.test.ts`,预期 8 个用例全绿。
- 需要你做的:无,席位落地。
## What changes
`packages/cli/src/commands/datasource/introspect.ts`: after the existing
lexical check (absolute path, `../` escape; both unchanged), `--out` is
judged again on the location the write would really land on:
1. an `--out` that is an existing symlink is refused (`writeFile`
follows it, even when it dangles);
2. the realpath of the nearest existing ancestor directory of the target
must sit under the realpath of the current directory.
Both refusals reuse the existing message (`--out must be a relative path
within the current directory: OUT`) and its exit code 2. A plain
relative in-tree path writes as before, also from a current directory
reached through a symlink. No directory is created: a missing
intermediate directory still fails in `writeFile` with `ENOENT`, as
before.
New test beside it, `introspect-out-jail.test.ts` (unit tier: no spawn,
no driver), each case in its own temp tree removed in `afterEach`:
- in-tree directory symlink to an outside dir: refused, exit 2, outside
dir stays empty;
- target that is an existing (dangling) symlink: refused, exit 2,
nothing written outside;
- plain nested in-tree path: writes the server's source;
- cwd reached through a symlink: still writes;
- missing subdirectory: `ENOENT` as before, not the jail refusal,
nothing created;
- `../x.object.ts`, `objects/../../x.object.ts` and an absolute path:
refused exactly as before.
Changeset: `@objectstack/cli` patch.
## Tests
<details><summary>Ablation 1: realpath of the ancestor replaced by the
lexical path (on-disk proof, red quoted)</summary>
```
ablation-replace: anchor "const real = await realpath(dir);" x1 (before)
ablation-replace: ok mutation landed: anchor 1 -> 0, blob 1337969 -> 579c69b33151
FAIL ... > refuses an in-tree directory symlink that points outside, and writes nothing there
AssertionError: expected undefined to deeply equal { …(2) }
FAIL ... > still writes when the cwd itself is reached through a symlink
Tests 2 failed | 6 passed (8)
ablation-replace: ok restored: blob == HEAD (1337969) and `git diff HEAD` is empty
```
</details>
<details><summary>Ablation 2: the symlink-target leg removed</summary>
```
ablation-replace: ok mutation landed: anchor 1 -> 0, blob 1337969 -> 6665d1768957
FAIL ... > refuses a target that is itself an existing symlink, even a dangling one
Tests 1 failed | 7 passed (8)
ablation-replace: ok restored: blob == HEAD (1337969) and `git diff HEAD` is empty
```
</details>
<details><summary>Suites on head 7229d62</summary>
```
pnpm --filter @objectstack/cli exec vitest run --project unit --maxWorkers=2 src/commands/datasource/
Test Files 2 passed (2)
Tests 20 passed (20) (8 new + 12 in envelope-unwrap.test.ts; run on fff6074, before the main merge, which touched no datasource file)
NODE_OPTIONS=--max-old-space-size=8192 pnpm --filter @objectstack/cli typecheck -> TYPECHECK-EXIT=0
pnpm --filter @objectstack/cli exec vitest run --project unit --maxWorkers=2 (whole unit tier)
Test Files 2 failed | 421 passed (423)
Tests 5981 passed | 29 skipped (6010)
the two failures: test/published-subpath-console.pin.test.ts, test/published-subpath-hook-body.pin.test.ts,
both "packages/cli is not built (./dist/index.js is absent)" (a prerequisite, the closure build excluded cli itself);
after pnpm --filter @objectstack/cli build:
Test Files 2 passed (2)
Tests 29 passed (29)
```
The `integration` tier is left to CI: the diff touches no
integration-tier file, no spawn entry and no driver boot path.
</details>
## Acceptance notes
- The check and the write are two steps, so a symlink swapped in between
them is not caught (time-of-check to time-of-use). The card's threat is
a symlink already present in the tree; closing the race would need an
`O_NOFOLLOW` open on every path component, which is not what the card
asked for.
- The existing lexical prefix check still spells the separator as `/`,
so on Windows it reads as refusing every `--out` (read from the code,
not run). Unchanged here, noted only; the new check uses `path.sep`.
- The pre-existing `envelope-unwrap.test.ts` also drives this command;
these pins went into a new file as the claim's file surface names, not
into that one.
## Verification
<details><summary>Derived gate families (dispatch-gates --commands, head
7229d62)</summary>
```
Run reconciliation: 65 derived, 61 run, 1 NOT-MEASURED, 3 UNRUN.
61 run: exit 0 each (pnpm check:i18n-walk-parity first exited 3, CLI not built; exit 0 after the cli build).
NOT MEASURED: pnpm check:i18n-coverage, exit 3: examples/app-showcase needs @objectstack/connector-slack built (example closure, unrelated to this diff); CI runs it.
UNRUN, left to CI: pnpm check:type-check-debt (verify-lock gate, rebuilds spec dist), pnpm check:dual-build-cjs-loads, pnpm check:i18n.
dispatch-gates --tier: no path-derived mandate; Clause-② no (narrows a CLI flag's accepted paths to what its own refusal already declared; no spec, schema or published export moves).
eslint --no-inline-config on the two touched .ts files: exit 0.
```
</details>
Session: `session_01QsR2jTEivi2wogPpeo3xd5`
(https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>1 parent c3c0c30 commit 04d4114
3 files changed
Lines changed: 207 additions & 3 deletions
File tree
- .changeset
- packages/cli/src/commands/datasource
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
16 | 46 | | |
17 | 47 | | |
18 | 48 | | |
| |||
73 | 103 | | |
74 | 104 | | |
75 | 105 | | |
| 106 | + | |
76 | 107 | | |
77 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
78 | 115 | | |
79 | 116 | | |
80 | 117 | | |
| |||
0 commit comments