Skip to content

feat(kms/auth-eth): require n of m RPC providers to agree - #1650

Merged
kvinwang merged 5 commits into
nextfrom
feat/auth-eth-rpc-quorum
Nov 27, 2026
Merged

kvinwang merged 5 commits into
nextfrom
feat/auth-eth-rpc-quorum

Conversation

@kvinwang

@kvinwang kvinwang commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #1649, with #1654 merged in for vmm-cli compose --init-script; review the last two commits.

Problem

auth-eth reads policy through one RPC endpoint, so whoever runs that endpoint decides what the KMS authorizes.

Fix

environment:
  ETH_RPC_URLS: |
    https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}
    https://mainnet.infura.io/v3/${INFURA_KEY}
    https://ethereum-rpc.publicnode.com
  ETH_RPC_QUORUM: 2/3
  ETH_MAX_BLOCK_AGE: 1800
  • Quorum transport. A viem transport sends each request to all m endpoints and answers only with what n return alike; failures, timeouts and reverts cast no vote, and without n votes the request fails closed. It handles eth_chainId, eth_getBlockByNumber and eth_call, and refuses any other method.
  • Block alignment. A block tag resolves to the highest block n providers have reached. The backend then agrees on that block's hash, number and timestamp and pins every eth_call to the hash (EIP-1898, requireCanonical). ETH_RPC_URL alone takes the same path as 1/1, the server no longer falls back to http://localhost:8545 when neither variable is set, and GET /app/:appId resolves latest the same way.
  • ETH_MAX_BLOCK_AGE. Rejects a block older than the given number of seconds. It is the only bound on m-n+1 providers that lag or report an old block, which make the server read an earlier state, so the server refuses to start with several endpoints and no such limit; deploy-to-vmm.sh checks the same before deploying.
  • Lean model. formal/DstackFormal/Kms/RpcQuorum.lean proves, for fewer than n faulty providers, that an agreed answer is the honest one and that a tag resolves to a block no higher than an honest provider has reached; no lower takes n honest answers, and m/m lets one provider hold it back arbitrarily.
  • API keys. Only the origin of an endpoint is logged or served by GET /. m is stated apart from the list, so a value interpolated into one URL cannot add an endpoint. Redirects are refused.
  • Deployment. deploy-to-vmm.sh reads the public part from rpc.json, so the validation regex of each key is the deployer's to set:
    {
      "quorum": "2/3",
      "endpoints": [
        "https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}",
        "https://mainnet.infura.io/v3/${INFURA_KEY}",
        "https://ethereum-rpc.publicnode.com"
      ],
      "keys": {
        "ALCHEMY_KEY": "/[A-Za-z0-9_-]{32}/",
        "INFURA_KEY": "/[0-9a-f]{32}/"
      }
    }
    endpoints and quorum fill the compose file and keys becomes allowed_envs through vmm-cli compose --env-spec; a ${NAME} without a spec is an error. The key values come from .env and stay out of the measured compose file: they go under env in the user config, apart from the fields dstack defines, and vmm-cli compose --init-script (feat(vmm-cli)!: add compose --init-script, make update take a rendered compose #1654) sets the init_script to dstack-util import-env ALCHEMY_KEY INFURA_KEY (feat(dstack-util): validate env values against specs in allowed_envs #1649), which copies them into the env file and refuses a missing key or one that breaks its spec. A guest older than 0.6.7 has no import-env and stops at the init_script. An existing .env with ETH_RPC_URL deploys as before: the first run writes it to rpc.json as 1/1.

A failed request now logs the reason with the provider's origin (no 2 of 3 RPCs agree on eth_call (https://…: HTTP request failed.)), and the startup probe reports cannot resolve ETH_BLOCK_TAG=… with that reason instead of calling every failure an unsupported tag.

GET / reports ethRpcUrl as the origins joined by commas and adds ethRpcUrls (the origins as an array), rpcQuorum and maxBlockAge to authApiInfo.

Rendered example

deploy-to-vmm.sh with the rpc.json above and ETH_MAX_BLOCK_AGE=1800 in .env renders this docker-compose.yaml (the auth-api service; ${ALCHEMY_KEY} and ${INFURA_KEY} stay literal until the guest substitutes them):

services:
  auth-api:
    build:
      context: .
      dockerfile_inline: |
        FROM oven/bun:1.4.4-alpine@sha256:4cf82f5686b196504fe119db3fcd9185b191f82ece82c40680e597228ec6070a
        WORKDIR /app

        RUN apk add --no-cache git ca-certificates
        RUN git clone https://github.com/Dstack-TEE/dstack.git && \
            cd dstack && \
            git checkout <GIT_REV>
        WORKDIR /app/dstack/dstack/kms/auth-eth
        RUN bun install --frozen-lockfile --production --ignore-scripts
        ENV NODE_ENV=production
        CMD ["bun", "index.ts"]
    environment:
      PORT: 8000
      # One endpoint per line. Every read must be answered alike by n of the m
      # endpoints, with ETH_RPC_QUORUM as n/m. A ${NAME} is an API key from
      # env in the unmeasured .user-config; its allowed_envs spec keeps it
      # to one path segment, so it cannot change an endpoint or add one.
      ETH_RPC_URLS: |
        https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}
        https://mainnet.infura.io/v3/${INFURA_KEY}
        https://ethereum-rpc.publicnode.com
      ETH_RPC_QUORUM: 2/3
      # finalized on L1; an OP-stack chain read through Helios requires latest.
      ETH_BLOCK_TAG: finalized
      ETH_FINALITY_CONFIRMATIONS: 0
      # Empty disables the block age check.
      ETH_MAX_BLOCK_AGE: "1800"
      # Empty disables the chain ID check.
      ETH_CHAIN_ID: ""
      KMS_CONTRACT_ADDR: "0x59E4a36B01a87fD9D1A4C12377253FE9a7b018Ba"
    restart: unless-stopped
    ports:
      - 8001:8000

  kms:
    # unchanged

and this .app-compose.json, which holds no key value:

{
  "manifest_version": 2,
  "name": "kms",
  "runner": "docker-compose",
  "docker_compose_file": "<the docker-compose.yaml above>",
  "kms_enabled": false,
  "gateway_enabled": false,
  "local_key_provider_enabled": true,
  "key_provider_id": "",
  "public_logs": true,
  "public_sysinfo": true,
  "allowed_envs": [
    "ALCHEMY_KEY: /[A-Za-z0-9_-]{32}/",
    "INFURA_KEY: /[0-9a-f]{32}/"
  ],
  "no_instance_id": true,
  "secure_time": true,
  "init_script": "dstack-util import-env ALCHEMY_KEY INFURA_KEY\n"
}

The key values travel in the unmeasured .user-config:

{"env": {"ALCHEMY_KEY": "<key>", "INFURA_KEY": "<key>"}}

Behavior changes for existing deployments

  • The RPC endpoint must accept a block hash in eth_call. Not checked against Helios.
  • Each request makes one more eth_getBlockByNumber call.

Not included

  • No log line when a minority of providers fails while the quorum still holds.
  • A provider that hangs delays each request by the 10-second RPC timeout, because resolving a tag waits for every provider.

Verification

  • bun run check (67 tests); lake build in formal.
  • Before the last rewrite of agree and reached, three local Anvil nodes (one chain, two forks of it), ETH_BLOCK_TAG=latest, 2/3: all agree; one node two blocks ahead on its own fork; one node down; two live nodes on different forks at the same height (fails closed); ETH_MAX_BLOCK_AGE exceeded (fails closed); no key in any log.
  • Ran deploy-to-vmm.sh with fake keys, the real vmm-cli compose and a stubbed vmm-cli deploy (default rpc.json, 2/3 with two keys, a key without a value, a ${NAME} without a spec, invalid JSON). Ran the generated init_script under set -e and then exec-with-env --compose with a debug dstack-util from feat(dstack-util): validate env values against specs in allowed_envs #1649: valid keys reach the app; a malformed key, a missing key and a missing user config stop at import-env; a dstack-util without import-env stops there too. Not booted in a CVM.

@kvinwang
kvinwang force-pushed the feat/auth-eth-rpc-quorum branch 4 times, most recently from d415922 to ff4df4e Compare November 25, 2026 16:28
@kvinwang
kvinwang added this pull request to stack #1653 November 26, 2026 01:52
@kvinwang
kvinwang force-pushed the feat/auth-eth-rpc-quorum branch from ff4df4e to 82cf1d1 Compare November 26, 2026 01:56
@kvinwang
kvinwang force-pushed the feat/auth-eth-rpc-quorum branch 11 times, most recently from 6810e58 to 9f9cc22 Compare November 26, 2026 11:16

@Leechael Leechael left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 9f9cc22 (the two commits of this PR; the branch also carries b67c22d, the pre-rewrite #1654, so it needs a re-sync once that lands).

I ran quorumTransport / EthereumBackend with real viem against local stub JSON-RPC servers (honest, lagging, lying, erroring, hanging), 2/3 unless noted. The safety side holds in everything I tried:

Scenario Result
one provider answers allowed=true on eth_call, two say revoked denied
one provider reports a much higher finalized block on its own fork the honest block is used, denied
three providers on three forks fails closed
one provider hangs allowed, about 10 s later (the tag step waits for every provider; KMS's webhook timeout is 60 s, so this is slow, not a denial)
one provider without EIP-1898 allowed, it casts no vote
1/1, endpoint answers with a 302 refused, the redirect target gets no request

Three things on the availability / configuration side, inline:

  1. getAppEpoch always resolves latest with 0 confirmations, the one combination the README says to avoid, so one faulty provider can fail it whatever the operator configured.
  2. The startup probe does not make a hash-pinned eth_call, so an endpoint that cannot serve EIP-1898 starts cleanly and then fails every request.
  3. ETH_RPC_QUORUM accepts n <= m/2, where a single provider can decide the answer.

Nit on deploy-to-vmm.sh: init_rpc_json only writes rpc.json when it is missing, so after the first run a changed ETH_RPC_URL in .env is ignored without a message. A warning when both exist and differ would do.

Comment thread dstack/kms/auth-eth/index.ts Resolved
Comment thread dstack/kms/auth-eth/index.ts Resolved
Comment thread dstack/kms/auth-eth/index.ts Resolved
@kvinwang
kvinwang force-pushed the feat/auth-eth-rpc-quorum branch from e73bba2 to 3301a94 Compare November 27, 2026 02:32
@kvinwang

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. All three inline points are fixed in 83a2575, and the rpc.json nit in a73f5ba (a warning when ETH_RPC_URL in .env differs from an existing rpc.json). The branch is rebased onto the current feat/value-spec, which already contains the merged #1654.

Two more fixes from testing in a CVM: deploy-to-vmm.sh now stops when a step fails (a73f5ba), and a failing init_script is reported to the VMM as a boot error (3301a94).

Base automatically changed from feat/value-spec to next November 27, 2026 05:32
ETH_RPC_URLS with ETH_RPC_QUORUM=n/m answers a read only with what n of
the m endpoints return alike. Each request resolves the block tag to the
highest block n providers have reached, agrees on that block's hash, and
pins every eth_call to it (EIP-1898), so ETH_RPC_URL alone now takes the
same path as 1/1. ETH_MAX_BLOCK_AGE rejects a block older than the given
number of seconds.

RPC URLs can carry API keys, so only their origins are logged or served,
and m is stated apart from the list so that a value interpolated into
one URL cannot add an endpoint.

Signed-off-by: Kevin Wang <wy721@qq.com>
deploy-to-vmm.sh takes ETH_RPC_URLS and ETH_RPC_QUORUM. An API key
written as ${NAME} in a URL stays out of the measured compose file: the
key goes under env in the user config, an init_script copies the named
keys into the env file, and an allowed_envs spec keeps each to one path
segment.

Signed-off-by: Kevin Wang <wy721@qq.com>
ETH_RPC_QUORUM now needs n > m/2: with less, one provider (1/3) or one of two
groups could decide a read, which the uniqueness proof rules out.

The startup probe also makes an eth_call at the resolved block hash, so an
endpoint without EIP-1898 fails the start instead of every request.

getAppEpoch read at latest with no confirmations, where one provider reporting
a head the others lack leaves no quorum. It now falls back to the confirmation
depth, or one block.

Signed-off-by: Kevin Wang <wy721@qq.com>
The script went on and exited 0 after vmm-cli failed. It also warns when
ETH_RPC_URL in .env differs from an existing rpc.json, which takes precedence.

Signed-off-by: Kevin Wang <wy721@qq.com>
The VMM showed only the init-script boot progress; the reason was in the
serial log alone.

Signed-off-by: Kevin Wang <wy721@qq.com>
@kvinwang
kvinwang force-pushed the feat/auth-eth-rpc-quorum branch from 3301a94 to 72bbb54 Compare November 27, 2026 05:32
@kvinwang
kvinwang merged commit bb8dc37 into next Nov 27, 2026
35 checks passed
@kvinwang
kvinwang deleted the feat/auth-eth-rpc-quorum branch November 27, 2026 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants