feat: Check moca's grants on both paths, bind credential lookups to them, and serve MOCA_TENANCY=multi

未关闭
#2,090 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
8/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
go

调研方向

Start with core/listener/vmcontrol/registry.go around Register (lines 110-112), where a non-empty grant is currently refused, and core/listener/vmcontrol/wire.go for the ControlRequest layout. The work is blocked on moca step 6 and on #2087, #2088 and #2089, so confirm those have landed first. Done means both grant paths verify, the lookup is bound to the grant, and the multi-tenant conformance cases pass.

由索引模型根据 Issue 内容生成。

描述

Parent: #2042, step 7 of the build order (#2085 → #2087 → #2086 → #2088 → #2091 → #2089 → (moca step 6) → #2090). Needs #2087 (Binding), #2088 (credresolve) and #2089 (Record).

Blocked by moca (#2042 step 6):

  • The grant on Exec, in Register and in Rebind (D4, D5).
  • The grant-bound credential lookup (D10): the sandbox service's phase 2, and MI1 §6.3's token endpoint for harness sessions.
  • multi-tenant cases in moca's conformance suite, and a split of grant_refused_under_single.

The token formats are fixed in moca's code already, so the verifiers can be built and tested now.

Problem

MOCA_TENANCY=multi is the reason Cortex replaces moca-egress (#2042). Two things must hold in multi-tenant mode:

  1. A VM's binding is proven by a grant that moca's control plane signed. A compromised VM worker can then bind only sessions it holds live grants for.
  2. A credential is released only against that grant. A stolen host token then yields nothing on its own.

Today Cortex refuses any grant, refuses to start under multi, and looks up credentials with the host's token alone.

Shared contract

Nothing new. The grant lives in the moca driver's vmBinding and reaches the rest of Cortex only through Binding.Admit, as Caller.Subject, Caller.Claims["tenant"], Caller.Credentials, Caller.Expires and the opaque Caller.Grant (#2042, "Shared contract").

moca's design (read in full on 2026-11-23)

Two paths, each with its own token:

Path The grant How the credential lookup uses it Source
Sandbox service (mocactl sbx) The user's sandbox token: typ moca-sandbox+jwt, aud moca-sandbox, with the sandbox, the worker (wrk) and the workspace (wsk) Phase 2: the egress proxy adds the token to POST /v1/egress/credential. The control plane verifies it in addition to the host token, and caps ttlSeconds at the token's remaining life. moca docs/specs/2026-10-26-sbx-egress-credentials-design.md §9
Harness sessions moca's grant: typ moca-grant+jwt, aud moca-sandbox, claims iss, sub, tenant, sid, iat, exp, jti, turn_exp, egress[], cfg? MI1 §6.3: POST /internal/token, RFC 8693: grant_type=urn:ietf:params:oauth:grant-type:token-exchange, subject_token=<grant>, subject_token_type=urn:moca:token-type:session-grant, audience=<destination host>. Reply {access_token, issued_token_type, token_type, expires_in, moca_binding: {header, format}}, expires_in at most 60 s. Errors: grant_invalid, session_not_found, credential_ambiguous, no_credential. Only the egress proxy may call it, with MOCA_EGRESS_CP_TOKEN. Cache per (sid, credential) for min(expires_in, grant exp). MI1 (docs/specs/2026-09-28-moca-multi-user-isolation-design.md) §6.1, §6.3, §10.1 · packages/control-plane/src/grant.ts:24-86

Both paths deliver the grant the same way: the relay or the worker puts it on Exec, and the VM worker passes the newest grant for each session in Register{vm_id, sid, grant} and in Rebind{sid, grant} (MI1 §8.3, the sandbox spec §9).

Today

Read at Cortex main 7f3eec7 and moca main e464dd6 (2026-11-23).

What Where
Cortex refuses grants vmcontrol.Registry.Register: a non-empty grant is bad_request (core/listener/vmcontrol/registry.go:110-112)
Cortex refuses multi MOCA_TENANCY=multi is a boot failure (cmd/cortex/vmcontrol.go)
Rebind ControlRequest field 3 is reserved (core/listener/vmcontrol/wire.go:24, moca proto/egress/v1/control.proto)
The grant on Exec Not built. Both specs name field 7, which is now mode (moca proto/sandbox/v1/sandbox.proto). Fields 1–12 are used, so 13 is the next free one (D4).
moca's grant check verifyGrant (moca packages/control-plane/src/grant.ts:184-240) and golden vectors (packages/control-plane/test/fixtures/grant-vectors.json, MI1 §12.2). The vectors include aud moca-inference grants that must be accepted for that audience. Keyset rule: each kid equals the first 16 hex characters of sha256(SPKI DER) (moca token.ts:103-106, 157-162).
moca's sandbox-token check moca packages/control-plane/src/sandbox-token.ts:11-41, 100-158, and the relay's packages/sandbox-relay/src/sandbox-auth.ts:38, 91-99. The relay requires SH_SANDBOX_TOKEN_ISSUER whenever the keyset is set, with no default (sbx-control-plane is only the minting default). The verifier does not check sbx == wsk. The control plane checks sbx = sandbox and wrk ∈ the host's workers (sbx spec §9).
Keys Grants: MOCA_GRANT_PUBLIC_KEYS, <kid>:<base64 SPKI>, comma-separated, Ed25519 only (moca token.ts, docs/reference/configuration.md:251). Sandbox tokens: SH_SANDBOX_TOKEN_PUBLIC_KEYS, same format.
Grant lifetime MOCA_GRANT_TTL_S, default 300 s. exp = min(iat + TTL, turn_exp). Refreshed at half-life, except for async turns, where exp == turn_exp for the whole turn and there is no refresh (moca docs/reference/configuration.md:252). No revocation list.
A JWT library Cortex already uses github.com/lestrrat-go/jwx/v2 v2.1.7 (core/go.mod), with EdDSA. Its jwt.Parse validation refuses now >= exp (jwt/validate.go:380), which fails moca's valid-through-exp vector, and refuses a future iat, which moca allows.
The RFC 8693 client tokenexchange/exchange.Client (core/plugins/tokenexchange/exchange/client.go). Exchange hardcodes subject_token_type and requested_token_type to urn:ietf:params:oauth:token-type:access_token (:83-85). The reply parser reads only access_token, token_type and expires_in (:164-178). It uses a default http.Client with a 30 s timeout (:51-53). ClientAuth.Apply can add form fields only (auth.go:10-12). Option exists, with no options defined (:41-44).
Rebind in moca's proto reserved 3 is at message level, not inside the oneof (moca proto/egress/v1/control.proto:15-20)
The conflict in moca's suite grant_refused_under_single also requires that MOCA_TENANCY=multi fails to start, naming MOCA_TENANCY (moca remote-worker/egress/conformance/cases.go:908-921). A Cortex that serves multi fails it as written.
The SSRF guard listener.egress_guard is off by default and set only in YAML (cmd/cortex/main.go:905, core/config/config.go:1206)
Lease VMs A lease VM lives up to 8 h (4 h at level 2) across many Execs, and level-2 processes use egress between Execs (moca docs/specs/2026-11-08-sbx-lease-mode-design.md §4, §7). Register happens once per VM restore. The sandbox token lives 15 min by default (SH_SANDBOX_TOKEN_TTL_SECONDS). The relay cancels a running Exec 30 s after its token expires (moca packages/sandbox-relay/src/main.ts:91).

Change

  1. Keep the newest grant for each session in the moca driver. Under multi, Register must carry a grant. Rebind{sid, grant} (field 3, shape per D5) replaces it for every VM of that session. Reads and writes are safe across goroutines, because Admit runs on every request.
  2. Check the grant on Register and Rebind, by its path. Select the path from the token's typ: one proxy serves both kinds of VM on a host, and both workers run as root, so the peer's UID cannot tell them apart. Each path accepts only its own token type. Use lestrrat-go/jwx/v2's jws.Verify for the signature, check the typ header by hand, parse the claims with encoding/json, and run moca's checks yourself. Do not use jwt.Parse's validation (see the table).
    • Harness grant, the checks of moca's verifyGrant:
      • A valid Ed25519 signature from a known kid. alg is EdDSA. typ is moca-grant+jwt. aud is moca-sandbox. iss is moca-control-plane-grant.
      • sub, tenant and sid are not empty strings. iat, exp and turn_exp are numbers, and jti is a string. For aud moca-sandbox, egress is an array of strings. cfg, when present, is a string.
      • Each kid in MOCA_GRANT_PUBLIC_KEYS equals the first 16 hex characters of sha256 of its SPKI DER. A key that breaks this is a start-up error.
      • now > exp and now > turn_exp are refused, so a grant is valid through its exp second. There is no clock leeway. (MI1's 2026-11-05 amendment says turn_exp is not built. The code checks it, so follow the code.)
      • sid equals the Register's sid.
      • The verifier takes the audience as a parameter. Every vector in moca's golden file passes or fails for the right reason, run with its own aud.
    • Sandbox token, the checks of moca's sandbox-token verifier:
      • typ is moca-sandbox+jwt. aud is moca-sandbox. iss equals SH_SANDBOX_TOKEN_ISSUER, which is required with the keyset (no default). scope contains exec.
      • sub, tenant, sbx, wrk and wsk are not empty strings. iat is a number and jti a string. The mode claims (mode, mode_epoch, idle_timeout_s) pass moca's checks (sandbox-token.ts:100-158).
      • sbx equals the Register's sid (the sandbox ID is the session on this path). sbx == wsk is not checked, because moca's verifier does not check it.
      • exp with a 30 s leeway. The keys are SH_SANDBOX_TOKEN_PUBLIC_KEYS, not MOCA_GRANT_PUBLIC_KEYS.
      • Refuse typ moca-sandbox-mode+jwt, which has the same aud and keys.
    • A failure answers the control request with a named refusal (grant_invalid proposed).
  3. Fill the caller in vmBinding.Admit: Subject from sub, Claims["tenant"] from tenant, Credentials from egress (nil on the sandbox path, whose token has no egress claim), Expires from exp, Grant from the raw token. After Expires, Admit answers 403 grant_expired (MI1 §8.5 item 1) through vmBinding.Refusal, and nothing is dialed. This one check covers CONNECTs and every decrypted request, because each one passes Admit.
  4. Bind the lookup to the grant, in the moca resolver (#2088):
    • Sandbox service: add the sandbox token to POST /v1/egress/credential (phase 2). No cache entry outlives the token.
    • Harness: call POST /internal/token through exchange.Client, with small, general additions to that package: a SubjectTokenType field on ExchangeRequest (empty keeps today's value), the reply's unknown fields kept as raw JSON on ExchangeResponse, and an option WithHTTPClient(*http.Client). The moca driver passes a client with #2088's hardening (no proxy, no redirects, its CA, a 3 s timeout) and a RoundTripper that adds its own authentication. requested_token_type keeps its default, which RFC 8693 allows. The moca driver reads moca_binding from them and turns the reply into one credresolve.Credential (Headers from moca_binding.header and format, PathPrefix: "/", the destination's port). A missing moca_binding means Authorization: Bearer. The client authenticates with MOCA_EGRESS_CP_TOKEN, read from a file. The token-exchange plugin and its config do not change.
    • Map session_not_found to an empty list cached for 5 s, and grant_invalid and credential_ambiguous to &credresolve.Refusal{Status: 401, Code: <code>}.
    • no_credential is a departure from MI1. MI1 §6.3 item 4 answers the guest 401, because MI1 intercepts by matching the host against the grant's egress names first (§8.5 item 4). This design decides interception by the lookup at CONNECT, for both paths, so no_credential becomes an empty list: an opaque tunnel. Confirm with moca (below).
    • Set each Credential.Expires to min(now + expires_in, Caller.Expires), so #2088's 5 s rule removes it before the grant ends. Start a time.AfterFunc at Caller.Expires that calls Forget(sid), and reset it on each Rebind.
  5. Never use the guest's own Authorization as a subject token. The subject comes only from Caller.Grant.
  6. Start-up under multi. The moca driver adds its rules to shared mode's checks (#2087). Cortex refuses to start, naming the missing setting, unless all of these are present:
    • The interception CA key.
    • A non-empty allowlist, and not * (D14: allow-all is refused under multi).
    • The SSRF guard on.
    • The credentials URL and token.
    • MOCA_EGRESS_CP_TOKEN as a file, when the harness path is on.
    • the keys for each enabled path.
  7. Under single (the default), grants stay refused, exactly as today.

Not in scope

  • The container tier's identity source (MI1 S5, later).
  • Minting or refreshing grants and sandbox tokens. That is moca's control plane.

Tests

  • moca's golden grant vectors: every valid one passes, and every invalid one is refused for the right reason.
  • Sandbox tokens: each check in item 2, moca-sandbox-mode+jwt refused, and the 30 s leeway at both edges.
  • Each path refuses the other path's token type.
  • Under multi: a Register without a grant is refused. After exp, a CONNECT and a decrypted request on an open tunnel each get 403 grant_expired. A Rebind extends every VM of the session. Concurrent Rebind and requests pass -race.
  • Sandbox service: the lookup carries the token, and no cache entry outlives it.
  • Harness: the exchange request has RFC 8693's form fields and subject_token_type=urn:moca:token-type:session-grant. Each named error maps as item 4 says. moca_binding sets the header. The token-exchange plugin's own tests pass unchanged.
  • Under single: a Register with a grant is still bad_request.
  • The start-up checks fail once for each missing setting.

Done when

  • A hostegress build serves MOCA_TENANCY=multi. Each binding is proven by a grant, and each credential lookup carries that grant.
  • moca's multi-tenant conformance cases pass against it (#1489), once moca has written them.

For a human after review

  • Ask moca: D5's field numbers for Rebind. The proposal is sid = 1, grant = 2.

  • Ask moca: the name of the field that carries the sandbox token on /v1/egress/credential (phase 2), and the grammar of moca_binding.format. The proposal is {"header": "Authorization", "format": "Bearer {token}"}.

  • Ask moca: the no_credential departure from MI1 §6.3 above.

  • Ask moca: how does the egress proxy authenticate to /internal/token? MI1 §6.3 says only "moca-egress's OWN credential" (MOCA_EGRESS_CP_TOKEN). The proposal is Authorization: Bearer, as on /v1/egress/credential, which needs one more Option on exchange.Client.

  • Ask moca: D15 (lease mode). Who sends Rebind with a fresh token while a lease VM runs between Execs?

  • Ask moca: split grant_refused_under_single, so that multi can pass the suite.

主要语言
Go
星标
31
派生
49
平均合并
9 小时 43 分钟
30 天内合并 PR
367

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rossoctl/cortex 的其他 Issue

查看 rossoctl/cortex 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。