Skip to content

env: a NUL byte in a --file entry reaches std::env::set_var unvalidated and aborts the process (exit 134) #14490

Description

@leeewee

env --file FILE parses an .env/INI file and passes each key and value straight to std::env::set_var, which panics when either contains a NUL byte. A single NUL anywhere in the file therefore aborts env with a Rust panic instead of a diagnostic.

The argv path (env NAME=VALUE ...) is guarded — apply_specified_env_vars checks for an empty name before calling set_var, but the --file path has no guard at all.

Steps to reproduce

A NUL in the value:

$ printf 'KEY=a\x00b\n' > conf.env
$ env --file conf.env
thread 'main' panicked at library/std/src/env.rs:361:9:
failed to set environment variable `"KEY"` to `"a\0b"`: file name contained an unexpected NUL byte
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ echo $?
134

A NUL in the key does the same:

$ printf 'a\x00b=c\n' > k.env
$ env --file k.env       # exit 134, identical panic

Root cause

for (_, prop) in &conf {
// ignore all INI section lines (treat them as comments)
for (key, value) in prop {
unsafe {
env::set_var(key, value);
}
}
}

std::env::set_var panics if either argument contains a NUL. Nothing between the parser and this call validates the bytes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions