Repository navigation
feat(dstack-util): validate env values against specs in allowed_envs - #1649
Conversation
d83729a to
457c1a0
Compare
Leechael
left a comment
There was a problem hiding this comment.
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.
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>
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>
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>
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>
Signed-off-by: Kevin Wang <wy721@qq.com>
…range syntax Signed-off-by: Kevin Wang <wy721@qq.com>
Signed-off-by: Kevin Wang <wy721@qq.com>
d08aa77 to
7606192
Compare
Examples
int(1..=65535)int(0|1024..<65536)int(1..)inti128float(0<..=1)float(..<0|0.5..)/mainnet|testnet/mainnetortestnet/[A-Za-z0-9_-]{1,128}/int(1..=3600)?Problem
allowed_envsonly filters environment variables by name, andvmm-cliand 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 needsPORTto be a port orNETWORKto be one of two names has to validate it itself.Fix
New
value-speccrate: a spec isint,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 useswinnow; decimals are compared exactly withbigdecimal.An
allowed_envsentry may beNAME: 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 theinit_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 seeNAME: specas a name no variable can have, so they drop the variable instead of delivering it unchecked.dstack-util validate-envapplies specs from arguments and--compose, with the same rule, to the environment or--env-file, for values aninit_scripttakes from unmeasured sources such as.user-config. The arguments andallowed_envsare also the list of permitted names: any other variable is an error unless--allow-unlistedis given.dstack-util import-env NAME…copies variables from theenvobject of.user-configinto.decrypted-env.jsonand.decrypted-env; each must be inallowed_envsand match its specs, and may not replace a different value from the encrypted env.dstack-util exec-with-env --composerejects variables outsideallowed_envsand checks the specs again before exec, andapp-compose.servicepasses it on start, so aninit_scriptthat rewrites.decrypted-env.jsonis covered too.VMM:
CreateVm,UpgradeAppandGetComposeHashreject a malformedintorfloatspec, or a malformed name before a spec, inallowed_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 bareNAMEis allowed too).update --env-filekeepsallowed_envsas 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.pyandnpm testindstack/vmm/ui.formal/DstackFormal/Guest/ValueSpec.leanmodels integer parsing, range ordering and the integer emptiness check;Guest/EnvFile.leannow proves accepted values match their specs.lake buildpasses. The spec syntax parser,floatgrammar,?and regexes are covered by unit tests only.regex-automatabuild, 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.validate-envandexec-with-env --composeby hand on a debug build for matching, mismatching, unset, empty and unlisted values, andimport-envfor both env files. The VMM check takes 30–360 ms for a 3–4 MB compose on a release build.app-compose.servicechange has not been booted in a CVM, the UI change was not exercised in a browser, and nothing was run against a live VMM.