Repository navigation
Commit a2be78d
fix(plugin-auth): a SCIM bearer write carrying a Cookie and no Origin is no longer refused 403 MISSING_OR_NULL_ORIGIN (#33243)
Fixes #33237
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:`AuthManager.handleRequest` 在把请求交给 better-auth 之前,对带
`Authorization: Bearer` 的 `/scim/v2/*` 请求去掉 `Cookie` 头;其余路由一律不动。
- 为什么改:better-auth 的来源检查只要请求带任何 Cookie 就要求 `Origin`,于是带 Cookie 罐(如
Cloudflare `__cf_bm`、粘性会话 Cookie)的身份提供方(Okta 已实测)做 SCIM 开通与停用全部被拒
403,离职员工的访问无法由目录撤销。SCIM 只认 bearer,Cookie 在该路径上不带任何权限。
- 风险与回滚:风险面只有「`/scim/v2/*` 且带 bearer」这一种请求;无 bearer 的 SCIM
请求与所有浏览器路由仍走原来的来源检查。回滚即 revert 本 PR 的那一个 `if` 块。
-
先看:`packages/plugins/plugin-auth/src/auth-manager-request-routing.ts`(新增的
`if`
块,判定条件三项是风险点);`packages/plugins/plugin-auth/src/scim-bearer-cookie-origin.test.ts`(四个用例与
`boot()` 里把来源检查打开的那几行)。
- 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run
src/scim-bearer-cookie-origin.test.ts`,预期 4 个用例全绿。
- 需要你做的:无,席位落地。
## What changes
`packages/plugins/plugin-auth/src/auth-manager-request-routing.ts`,
`handleRequest`: right after the vendor client-address rewrite, a
request whose better-auth endpoint path is under `/scim/v2`
(`isScimProtocolPath`, the predicate the SCIM request scope already keys
on) AND that carries `Authorization: Bearer` followed by a credential
AND that carries a `Cookie` is rebuilt without the `Cookie` header
before better-auth sees it. This is the route that relaxes no check: no
global or per-path origin-check exemption is added, and
`skipOriginCheck` / `disableCSRFCheck` are not touched.
Why it is safe, measured on better-auth 1.7.12 and `@better-auth/scim`
1.7.12 as installed:
- `validateOrigin` (`dist/api/middlewares/origin-check.mjs`) reads
`useCookies = headers.has("cookie")` off the request `handleRequest`
hands it; with a cookie and no Origin or Referer it throws
`MISSING_OR_NULL_ORIGIN`. That is the whole defect.
- Every `/scim/v2/*` mutation endpoint is `use: [authMiddleware]`, and
that middleware (`createSCIMConnectionMiddleware`) authenticates from
`Authorization: Bearer` alone: no bearer is `401 SCIM bearer token is
required`. The platform's `verifyBearerToken`
(`auth-manager-plugin-list.ts`) reads only `input.token`.
- SCIM writes are attributed to the verified connection
(`authSystemWriteContext` returns `actor: svc:scim:CONNECTION_ID` first
and never consults the session resolver when a SCIM connection is in
scope), so stripping the cookie changes no attribution either.
- A SCIM request with NO bearer keeps its cookie and still meets the
origin check (pinned below).
The landing is the expected file; no producer elsewhere was needed.
`auth-manager.ts`, `auth-plugin.ts` and the better-auth pin are
untouched.
`.changeset/33237-scim-bearer-cookie-origin.md`:
`@objectstack/plugin-auth` patch.
## Tests
New file
`packages/plugins/plugin-auth/src/scim-bearer-cookie-origin.test.ts`
(real `AuthManager`, real `ObjectQL` on better-sqlite3, SCIM on, a
minted connection credential: the `scim-transaction-scope.test.ts`
harness shape). Four cases:
1. SCIM bearer `POST /Users` with `Cookie: __cf_bm=...;
better-auth.session_token=` and no Origin: 201 and the `sys_user` row
exists.
2. SCIM bearer `PATCH /Users/{id}` `active:false` with the same Cookie
and no Origin: 200, resource `active: false`.
3. SCIM `POST /Users` with the Cookie and NO bearer: still 403, `code`
`MISSING_OR_NULL_ORIGIN`, no `sys_user` row written.
4. CONTROL: a real session cookie (from sign-up) on `POST /sign-out`
with no Origin: still 403, `code` `MISSING_OR_NULL_ORIGIN`.
How production semantics are ensured: two things would mask the bug
under vitest, and the harness turns off both. `handleRequest`
synthesizes a same-origin Origin outside production: every case runs
with `vi.stubEnv('NODE_ENV', 'production')` and asserts it reads back.
And better-auth reads NODE_ENV once at module load
(`@better-auth/core/env`), so under vitest it defaults `skipOriginCheck`
to `true` (`create-context.mjs`: `disableOriginCheck` unset means
`isTest()`): `boot()` sets the production value `false` on the live
`$context` and asserts `skipCSRFCheck` is `false`. Before that line was
added, cases 3 and 4 were red on the unfixed tree (the control POST
answered 200), which is how the masking was found.
Red on the unfixed tree (commit e9076fa, test only): cases 1 and 2
fail with `{"message":"Missing or null
Origin","code":"MISSING_OR_NULL_ORIGIN"}: expected 403 to be 201` (and
`to be 200`); cases 3 and 4 pass.
Ablation (one-time, on the fix commit c8abb27, through
`scripts/ablation-replace.mjs`): the anchor `headers.delete('cookie');`
replaced by a comment, anchor 1 to 0, blob 944e5a3 to efaf0d39e2b3;
cases 1 and 2 red again with 403, cases 3 and 4 green; restored, blob
after restore 944e5a3 equals HEAD and `git diff HEAD` empty. The
test imports `./auth-manager.js` relatively, so the subject resolves to
`src/`, no `dist/` step.
## Acceptance notes
- Behaviour change beyond the card's three cases, stated so it is not a
surprise: a `/scim/v2/*` bearer request that carries a Cookie AND an
untrusted Origin used to be refused 403 `INVALID_ORIGIN`; it now reaches
the SCIM bearer check like any bearer client. Same reasoning: the origin
check guards cookie authority, and there is none on this path.
- The vendor's origin check is skipped wholesale under a test NODE_ENV,
so any plugin-auth test that means to observe it must put
`skipOriginCheck = false` on the context as `boot()` here does.
Observation only; no card.
- The cloud hosted proxy strip (cloud#4498) can retire once a release
carrying this reaches cloud's pin; that is cloud's and the maintainer's
call on cloud#4491.
## Verification
All readings at head c8abb27 (worktree on `origin/main` 6c5d31f).
<details>
<summary>plugin-auth tests and typecheck: 270/270 files, 5118 passed,
typecheck exit 0</summary>
- `pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2` (whole package): `Test Files 270 passed (270)`, `Tests
5118 passed | 10 skipped (5128)`, lock `VERDICT command-exit 0`.
- `pnpm --filter @objectstack/plugin-auth typecheck`: exit 0;
`check:test-typecheck: OK ... 10 file(s) / 72 error(s) / 23 pinned
signature(s) held` (the new file adds none).
</details>
<details>
<summary>lint: exit 0</summary>
- `pnpm lint` (repo-wide, run on the dispatch's request): lock `VERDICT
command-exit 0`.
- `eslint --no-inline-config` on the two changed TS files: exit 0.
</details>
<details>
<summary>derived gate families: 65 exit 0, 1 NOT MEASURED</summary>
- `node scripts/pm/dispatch-gates.mjs --commands`: 66 derived families,
each run locally; `--ran` reconciliation: `66 derived famil(ies)
accounted for — 65 run, 1 NOT-MEASURED (1 DERIVED from a recorded exit
3)`.
- `pnpm check:dual-build-cjs-loads` exit 3 `PREREQUISITE NOT MET` (it
needs every package's `dist/`; this worktree built only plugin-auth's
closure): NOT MEASURED locally, CI runs it.
- `node scripts/check-nul-bytes.mjs`: exit 0.
- Not run locally, owned by CI: `test:repo`, the dogfood battery (no `!`
in the title, no narrowing arm), the UI lanes.
</details>
Overlap check before push: open drafts #33227 and #33221 do not touch
`auth-manager-request-routing.ts`; no open PR from #32115 was found.
---
_Generated by [Claude
Code](https://claude.ai/code/session_017xRBcSZ51q2s3nMfakhdJ3)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 199e362 commit a2be78d
3 files changed
Lines changed: 289 additions & 0 deletions
File tree
- .changeset
- packages/plugins/plugin-auth/src
| 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 | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 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 | + | |
120 | 144 | | |
121 | 145 | | |
122 | 146 | | |
| |||
| 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 | + | |
0 commit comments