Repository navigation
Commit 416d9dd
fix(service-storage)!: a file field refuses a file whose upload was never completed (#26531)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"}
-->
_Requested by **Leehom** · [project
thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1X93jJ4BvZWR6vdK9XuH8QCS)_
Fixes #26479
Clause-②: yes (narrowing)
Metadata-protocol: none
Skills-docs: updated here — `content/docs/api/plugin-endpoints.mdx` (the
upload protocol section) now says a file field takes the `fileId` only
after `/upload/complete`. No `skills/**` file teaches the upload flow:
`git grep -n -i "presign\|upload/complete" -- skills/` finds nothing.
## 维护者速读
- **改了什么**:记录写入时,文件字段若引用一个上传还没 complete 的文件(`sys_file.status =
pending`),现在在字段处拒绝,返回 400 `VALIDATION_FAILED`,条目点名字段和文件
id;记录不落库,`sys_file` 行不盖 `ref_*`。调用过 `/upload/complete` 之后同一写入照常通过。
- **为什么改**:之前这种写入答 201,记录显示「已附件」,但文件下载口答 404,字节也从没被核对过(实测:声明 100 字节、实际
PUT 2 MiB,能过 1 MiB 的 `maxSize`,之后 complete 又被 #24548 拒,文件永远停在 pending
却被记录占着)。
- **风险与回滚**:会拒掉「先存 fileId、后调 complete」的客户端写法(201 变 400);SDK 的
`storage.upload()` 和 Console 的 UploadProvider 都先 complete 再返回
id,不受影响。系统写入(种子加载器)不判。回滚 = revert 本 PR。
-
**先看**:`packages/services/service-storage/src/file-reference-lifecycle.ts`
的 `applyCopyOnClaim` 判定循环(`incomplete` 分支与 `held`
豁免);`file-field-pending-upload.integration.test.ts`。
- **两分钟验证**:`pnpm --filter @objectstack/service-storage exec vitest run
src/file-field-pending-upload.integration.test.ts`,预期 8/8 通过。
- **需要你做的**:无,席位落地。
**BREAKING** accept-set narrowing at the file-field write gate
(`installFileReferenceHooks`, `@objectstack/service-storage`). A
signed-in record write that names a `sys_file` whose upload never
completed used to answer `201`. It now answers `400 VALIDATION_FAILED`.
Remedy: call `POST /api/v1/storage/upload/complete`, then save again.
ADR-0087: `not-required (no-migration-prescription)`, in the changeset.
## What changes
- `file-reference-lifecycle.ts`: copy-on-claim's judging pass
(`applyCopyOnClaim`, run by `beforeInsert` and `beforeUpdate` before the
driver) now refuses a field naming a file whose status is not
`committed` or `deleted`, when the write has a caller. The refusal is
`refuseIncompleteUpload`: the #26043 envelope (one ADR-0114
`invalid_value` entry per field, through `assertNoFileFieldRefused`),
naming the field and the file id. It runs after the read check, so a
file the caller may not read keeps #26043's answer, which names nothing.
- What "completed" means here: the ruling says "not `completed`".
`sys_file.status` is `pending | committed | deleted`. `committed` is
what `/upload/complete` writes. `deleted` is a committed file a record
let go, which `claimFile` already revives within its grace window, so it
stays accepted. This is the same split `field-file-service.ts`'s
`describeRow` uses.
- Not judged, as before: a system write and a context-less programmatic
write (`referencingCallerOf` answers null). The seed loader depends on
this: `IFieldFileService.storeFile` leaves the row `pending`, the seed
writes the record, and `claimFiles` commits it after.
- Also not refused: an update that resends a pending file the slot
already holds (`isSameSlot`). That claims nothing, and a record left in
that state by a write before this change can still be edited. An
ablation pins it (below).
- Doc: one paragraph in `content/docs/api/plugin-endpoints.mdx`.
### Doors checked
Every door that resolves a file field's `sys_file` is this one hook
pass.
- Single insert, update: covered.
- Batch insert: the engine fires `beforeInsert` per row (`engine.ts`
"Batch inserts trigger beforeInsert/afterInsert PER ROW"). Pinned: a
two-row batch is refused whole.
- Predicate (`multi: true`) update: a file id token is already refused
before this pass (`FILE_FIELD_BULK_WRITE_REFUSED`, #7102).
- REST CRUD, batch and the import routes (`data-action-routes.ts` →
`p.createData` / `p.updateData`) all reach the engine, so they reach
this pass.
- `IFieldFileService.claimFiles`: a server verb (seed loader only) that
commits the file itself. It is not a record write door.
- Anonymous doors (public forms, guest endpoints): refused earlier for
any file value (#25694).
## Tests
New
`packages/services/service-storage/src/file-field-pending-upload.integration.test.ts`.
It uses a real `ObjectQL` on sqlite `:memory:` and
`StorageServicePlugin` on the local adapter, calls the upload doors as
routes, and writes as the uploader:
1. The card's steps 1–4: the write is refused at the field (pinned
whole), no record is written, and the row stays `pending` with no
`ref_*`.
2. After `/upload/complete`, the same write is taken and the file is
claimed and `committed`.
3. Declared 100 bytes, PUT 2 MiB: the write is refused,
`/upload/complete` answers 409 (#24548), and the row stays pending and
unclaimed.
4. Update door: refused, and the record keeps its value.
5. Batch insert: refused whole, and neither row is written.
6. `multiple: true`: only the pending ids are named, and the committed
one is not claimed either.
7. Control: a system write is not judged (the seed path).
8. Control: a slot resending the pending file it holds is not refused.
## Acceptance notes
Measured on `origin/main` `45de8c6c` (triage step 1), with a scratch
copy of this harness before the fix:
- M1, the card's steps: `201`, the id is stored, and the row reads
`status=pending` with `ref_object=media`, `ref_field=poster` and
`ref_id` stamped. Reproduced.
- `accept` / `maxSize` on a pending file are **not skipped**. They run
on the row's declared metadata: declared `application/zip` is refused
`400 ERR_FILE_CONSTRAINT`, and declared 2 MiB against `maxSize` 1 MiB is
refused the same way.
- The #24548 byte check **is skipped**. It lives only at
`/upload/complete`. Declared 100 / PUT 2 MiB was claimed with `201`.
`/upload/complete` then answered `409 RESOURCE_CONFLICT`, so the row
stayed `pending` and claimed.
- `/upload/complete` on a file a record already claimed runs no field
constraint. It answered `200`, set `committed` and kept the `ref_*`.
Only uploader, organization and held-bytes are judged there.
- The download door for a claimed pending file answered `404
FILE_NOT_FOUND`.
- Read, not measured: the `sys_file` retention (`onlyWhen: { status:
'pending' }`, 7d) and the reap guard (`attachment-lifecycle.ts`,
`row.status === 'pending'` branch) delete a pending row's bytes without
looking at `ref_*`. So on main, a claimed pending file would be reaped
from under its record. After this change, a user write can no longer
produce that state.
## Verification
Run at head `c3aca48d` (the only commit on the branch).
<details><summary>Red first, and ablations</summary>
- Red: `file-reference-lifecycle.ts` restored to base `45de8c6c` (`git
checkout 45de8c6 -- PATH`, `grep -c isCompletedUpload` = 0), then the
new suite: **6 failed, 2 passed (8)**. The two that pass are the
controls (system write, held slot). Restored with `git checkout HEAD --
PATH`: `git diff HEAD` is empty, and the blob is `f111b144` = the HEAD
blob.
- Ablation of the held-slot exemption (`node
scripts/ablation-replace.mjs`, anchor `if (caller && !held &&
!isCompletedUpload(found))` x1 → x0, blob `f111b144` → `91f32d81`):
exactly `a record already holding a pending file can still be edited…`
fails. Restored, blob matches HEAD.
- `service-storage`'s vitest resolves its own `src/`, so no rebuild is
needed for these legs.
</details>
<details><summary>Tests</summary>
- `pnpm --filter @objectstack/service-storage exec vitest run
src/file-field-pending-upload.integration.test.ts`: 8 passed (8).
- `pnpm --filter @objectstack/service-storage test`: Test Files 69
passed (69), Tests 1253 passed (1253).
- `pnpm --filter @objectstack/service-storage typecheck` under the lock:
`VERDICT command-exit 0`.
- `eslint --no-inline-config packages/services/service-storage`: exit 0.
</details>
<details><summary>Full dogfood battery (bang title / narrowing
arm)</summary>
- `pnpm --filter '@objectstack/dogfood^...' build && pnpm --filter
@objectstack/dogfood test` under the lock, at `c3aca48d`: `VERDICT
command-exit 0`, Test Files 300 passed | 3 skipped (303), Tests 2532
passed | 18 skipped (2550). No failures.
</details>
<details><summary>Derived gates (`dispatch-gates.mjs --commands`,
reconciled with `--ran`)</summary>
- 93 derived families: 91 run with exit 0, 2 NOT MEASURED. `--ran`
reconciliation: `✓ dispatch-gates --ran: 93 derived famil(ies) accounted
for — 91 run, 2 NOT-MEASURED (2 DERIVED from a recorded exit 3)`.
- NOT MEASURED: `pnpm --filter @objectstack/spec run
check:skill-examples` and `pnpm check:dual-build-cjs-loads`. Both exited
3 (PREREQUISITE NOT MET: they need a full workspace build, and only the
dogfood closure was built). Neither reads a file this diff touches; CI
runs both.
- `node scripts/check-plugin-teardown-shape.mjs --self-test` first
exited 3 on the shallow clone. After `git fetch origin 621a487` (its
positive control) it exits 0: `48 cases pass`.
- Exit codes were captured before any pipe (`cmd > log 2>&1; ex=$?`).
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FEkQ5oz3SuMTEoG66BkXVV)_
Co-authored-by: Claude <noreply@anthropic.com>1 parent 185512c commit 416d9dd
4 files changed
Lines changed: 401 additions & 2 deletions
File tree
- .changeset
- content/docs/api
- packages/services/service-storage/src
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
168 | 173 | | |
169 | 174 | | |
170 | 175 | | |
| |||
| 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 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
0 commit comments