Skip to content

feat(dstack-util): validate env values against specs in allowed_envs - #1649

Merged
kvinwang merged 25 commits into
nextfrom
feat/value-spec
Nov 27, 2026
Merged

kvinwang merged 25 commits into
nextfrom
feat/value-spec

Conversation

@kvinwang

@kvinwang kvinwang commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Examples

"allowed_envs": [
  "PORT: int(1..=65535)",
  "RPC_PORT: int(0|1024..<65536)",
  "REPLICAS: int(1..)",
  "OFFSET: int",
  "RATIO: float(0<..=1)",
  "TEMPERATURE: float(..<0|0.5..)",
  "NETWORK: /mainnet|testnet/",
  "APP_KEY: /[A-Za-z0-9_-]{1,128}/",
  "TIMEOUT: int(1..=3600)?",
  "API_TOKEN"
]
Spec Accepts
int(1..=65535) integers from 1 to 65535
int(0|1024..<65536) 0, or integers from 1024 up to but excluding 65536
int(1..) integers from 1
int any integer within i128
float(0<..=1) decimals above 0 and up to 1
float(..<0|0.5..) decimals below 0, or from 0.5
/mainnet|testnet/ exactly mainnet or testnet
/[A-Za-z0-9_-]{1,128}/ 1 to 128 of those characters
int(1..=3600)? unset or empty, or integers from 1 to 3600
no spec any value, as before
# in an init_script, for values taken from .user-config
PORT=$PORT dstack-util validate-env --allow-unlisted 'PORT: int(1024..=65535)' || exit 1
dstack-util validate-env --compose /dstack/app-compose.json --env-file env.json || exit 1

Problem

allowed_envs only filters environment variables by name, and vmm-cli and the web UI rebuild it from the names of the variables being encrypted. The values come from the host-supplied encrypted env, which anyone holding the app's public key can produce, so an app that needs PORT to be a port or NETWORK to be one of two names has to validate it itself.

Fix

  • New value-spec crate: a spec is int, int(items), float, float(items) or /regex/, optionally followed by ? to also accept the empty string. Items are numbers or ranges joined by |, e.g. int(0|1024..=65535), float(0<..=1). Parsing uses winnow; decimals are compared exactly with bigdecimal.

  • An allowed_envs entry may be NAME: spec (the space is optional; a name with a spec must be [A-Za-z_][A-Za-z0-9_]*). The guest fails the boot when a decrypted value does not match every spec given for its name. Before the app starts, after the init_scripts, a variable that is still not set counts as empty, so a spec makes its variable required unless it accepts the empty string (int(1..=3600)?); entries without a spec behave as before. The compose schema is unchanged, and older guests see NAME: spec as a name no variable can have, so they drop the variable instead of delivering it unchecked.

  • dstack-util validate-env applies specs from arguments and --compose, with the same rule, to the environment or --env-file, for values an init_script takes from unmeasured sources such as .user-config. The arguments and allowed_envs are also the list of permitted names: any other variable is an error unless --allow-unlisted is given.

  • dstack-util import-env NAME… copies variables from the env object of .user-config into .decrypted-env.json and .decrypted-env; each must be in allowed_envs and match its specs, and may not replace a different value from the encrypted env.

  • dstack-util exec-with-env --compose rejects variables outside allowed_envs and checks the specs again before exec, and app-compose.service passes it on start, so an init_script that rewrites .decrypted-env.json is covered too.

  • VMM: CreateVm, UpgradeApp and GetComposeHash reject a malformed int or float spec, or a malformed name before a spec, in allowed_envs. Regex specs are not compiled there and are left to the guest: one short entry such as /\w{200}/ takes about 85 ms to compile, and a compose file can hold any number of them.

  • vmm-cli: compose --env-spec 'PORT: int(1..=65535)' (repeatable; a bare NAME is allowed too). update --env-file keeps allowed_envs as is.

  • Web UI: each encrypted variable has an optional value spec; resetting secrets lists the variables that have specs in the current compose, to keep, edit or remove. Specs are stripped before encryption.

Values are only checked in the guest: they are encrypted client-side, and the tools do not reimplement the spec language. Syntax and semantics are documented in docs/security/cvm-boundaries.md.

Verification

  • cargo test -p value-spec -p dstack-util -p dstack-vmm, cargo clippy -- -D warnings, cargo fmt --check; python3 -m unittest dstack/vmm/src/tests/test_vmm_cli.py and npm test in dstack/vmm/ui.
  • formal/DstackFormal/Guest/ValueSpec.lean models integer parsing, range ordering and the integer emptiness check; Guest/EnvFile.lean now proves accepted values match their specs. lake build passes. The spec syntax parser, float grammar, ? and regexes are covered by unit tests only.
  • Hostile input: a spec is capped at 4096 bytes and the regex size limit is pinned at 10 MiB. One-off runs on a release build (not in the test suite): 3M random specs with random values produced no panic; 4M random regex patterns were compared against an independently anchored regex-automata build, 32M match results all equal, and no pattern was accepted that the reference rejects; the slowest spec compile found was about 0.2 s; rejecting a 128 KB non-number takes about 0.1 ms.
  • Ran validate-env and exec-with-env --compose by hand on a debug build for matching, mismatching, unset, empty and unlisted values, and import-env for both env files. The VMM check takes 30–360 ms for a 3–4 MB compose on a release build.
  • Not done: the app-compose.service change has not been booted in a CVM, the UI change was not exercised in a browser, and nothing was run against a live VMM.

@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.

The guest image job is skipped on this PR, so I built the dev image from 845520d with os/mkosi/repro-build/repro-build.sh (builds fine, os_image_hash 18c94f9f...8470ead) and booted it on a TDX host. No KMS, so the encrypted-env check itself is not covered; everything else on the boot path is. The host VMM was 0.6.6 without this PR's VMM-side check, which is why the malformed spec in the last row reached the guest.

allowed_envs Supplied by Result
[] - boots, app starts
["FOO", "BAR"] nothing boots, app starts
["PORT: int(1..=65535)"] import-env PORT in init_script, .user-config has PORT=8080 boots, container sees PORT=8080, .decrypted-env.json is {"PORT":"8080"}
["PORT: int(1..=65535)"] same, .user-config has PORT=99999 import-env fails with value of env PORT does not match its spec, dstack-prepare fails, guest reboots, app never starts
["PORT: int(1..=65535)"] nothing prepare succeeds, app-compose.service fails with env PORT is not set and its spec does not accept an empty value, failure is reported to the host, VM stays up without the app
["PORT: int(1..=65535)?"] nothing boots, app starts
["PORT: int(1..65535)"] (malformed) nothing dstack-prepare fails with invalid allowed_envs entry "PORT: int(1..65535)", guest reboots

All of that matches the description, and app-compose.service with --compose does not break apps that have no allowed_envs or only plain names.

One thing I would change, inline: validate-env --allow-unlisted passes silently when the separator in a positional spec is mistyped.

Nit, take it or leave it: int(1..65535) (Rust-style, no =) is an easy slip, and invalid value spec at offset 7: invalid int does not point at the missing = / <. vmm-cli compose --env-spec accepts it too, which is fine given the VMM rejects it at deploy, but a hint in the parser message would save a round trip.

Comment thread dstack/dstack-util/src/main.rs Resolved
Leechael
Leechael previously approved these changes Nov 26, 2026
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
… specs

Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Copies variables from the env object of the user config into the env
file, by name or with --all. Each must be in allowed_envs and match its
specs, so an init_script needs one line to take values such as API keys
from the unmeasured user config.

Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
…pp starts

The boot-time check ran before any init_script, so a variable with a spec could never be supplied by import-env. import-env now refuses to replace a different value and applies the encrypted env limits, and validate-env rejects non-UTF-8 variables instead of checking a lossy copy.

Signed-off-by: Kevin Wang <wy721@qq.com>
The web UI keeps variables with a spec when a file or text replaces the list.

Signed-off-by: Kevin Wang <wy721@qq.com>
The web UI no longer encrypts an empty value for them or hashes it as the launch token, and vmm-cli update reports --drop-env-spec without --env-file.

Signed-off-by: Kevin Wang <wy721@qq.com>
…range syntax

Signed-off-by: Kevin Wang <wy721@qq.com>
@kvinwang
kvinwang merged commit e5d3ee2 into next Nov 27, 2026
27 checks passed
@kvinwang
kvinwang deleted the feat/value-spec branch November 27, 2026 05:32
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