Security and limits
.env files often hold secrets, and tools that read them often read files
they did not write. This page collects what adotenv does and does not do on
both fronts.
What the core never does
Section titled “What the core never does”Parsing, lookup, edits, formatting, sorting, comparison and example generation:
- never read files, environment variables or the working directory;
- never expand
$VARreferences or run$(command)substitutions; - never write to the process environment;
- never use unsafe code. The core is
#![forbid(unsafe_code)]with every feature combination.
Each of these happens only through an explicit API: the load feature reads
files, Document::resolve substitutes references from a lookup you pass,
Document::resolve_with_commands and the exec feature run commands, and
adotenv-process writes the process environment.
Untrusted input
Section titled “Untrusted input”- Input must be UTF-8. Decode bytes yourself; the loader rejects invalid UTF-8 instead of replacing it.
- A retained
Documentkeeps item and token metadata for the whole file, so memory grows with the input. UseParseOptionswithparse_with_options,parse_strict_with_options,parse_values_with_options,parse_values_compact_with_options,parse_entries_compact_with_optionsorparse_owned_with_optionsto bound input bytes, physical lines, items and diagnostics. Limits are opt-in; the 1.x default keeps accepting valid input. A future major release may choose default limits.parse_values,parse_values_compactandparse_entries_compactskip the document model and use much less memory. - For untrusted edits, formatting or sorting, pass
EditOptionstoDocument::edit_with_options,set_with_options,remove_with_options,insert_with_options,rename_with_options,rename_with_references_with_options,comment_with_options,uncomment_with_options,set_export_with_options,format_with_optionsorsort_with_optionsto bound output bytes (LimitKind::OutputBytes), output physical lines (LimitKind::Lines), total scanned reference candidates (EditOptions::max_references,LimitKind::References) and verification re-parsing (parse_limits). Planned byte and line budgets are checked before allocating the output string; exceeding a limit fails withEditError::Limit. - The loader accepts the same limits with
LoadOptions::with_limits.max_input_bytesbounds the file size and the read itself, so a file that changes after its metadata check cannot grow without bound. Exceeding a limit returnsLoadError::Limitwith the kind, configured maximum and observed size or count. Document::lookup_indexgrows with the number of participating keys, not the total number of declarations. A dialect that ignores bare keys still spends memory only for keys that take part in lookup.parsecollects at most 10,000 ordinary diagnostics, plus the first error found after the budget, and ends a truncated list with adotenv.too-many-diagnosticswarning. The retained error and summary make the total at most 10,002 entries.ParseOptions::with_max_diagnosticslowers the budget; a truncation then ends withdotenv.limit-exceeded.parse_strictandparse_valuesstop at the first error and do not collect the list.- Parsing and reference scanning run in linear time, including inputs with
many statements on one line, long
$runs and deeply nested forms. Resolution time is linear in the number of references in a value, and edit batches that set and insert entries stay linear. - Public
Entry::referencesbuilds one candidate record per possible reference, so its memory grows much faster than the value. Bound or reject untrusted values before inspecting them. - Resolution enforces limits on nesting depth (64 in every preset), reference
candidates scanned per value (65,536), the expanded size of each value
(1 MiB) and the total output size (64 MiB). The reference limit bounds the
candidate list, and delimiter matching is bounded by the depth limit, so a
deeply nested value fails rather than retaining a form record per level.
Public
Entry::referencesis intentionally unbounded. Exceeding them fails withResolveError::DepthExceeded,TooManyReferences,ValueTooLargeorOutputTooLarge. See References and resolution. - Fuzz targets cover parsing, edits, encoding, resolution, formatting, parse limits and differential parsing. A bounded fuzz run is evidence, not a proof; see Quality.
Secret values
Section titled “Secret values”Values may be secrets. These outputs include them:
Debugoutput of documents, items, entries, edit and format results, comparisons, loaded environments and entries, resolved values, lookup indexes, effective entries, compact values, compact entries, line indexes, groups, portability reports and the adapter types. The core document, edit, comparison, load, resolution, compact-values, compact-entries, line-index, group and portability types support a value-redactingDebugform throughadotenv::Maskedandadotenv::mask; useDeserializeError::masked()for serde errors. Adapter types still include values. Do not log normalDebugoutput when values are secret.- Serde conversion errors name the key and line, and their messages include
the value. Figment and
configerrors may also include the value they failed to convert. - Command call records (
CommandCall) keep each command’s output, andexec::Shellerror messages include the command’s standard error. adotenv-cli get,loadanddiffprint values by default.diff --maskreplaces compared values with***in human, JSON and matrix output.fmtprints or writes the whole file.checkprints source lines only with--source.Document::exampleandadotenv-cli exampleremove assigned values but keep comments verbatim, including any secret written in a comment.
Commands and child processes
Section titled “Commands and child processes”- Command substitution runs only when you pass a callback to
Document::resolve_with_commandsand the reference rules allow commands.exec::Shellruns commands through the platform shell in a directory you name, caps standard output at 1 MiB, caps standard error at 64 KiB and stops the direct shell after 300 seconds by default. The builders change or disable those limits. A command that backgrounds its own children may leave them running; use a sandbox or process supervisor when that matters. UseShellonly with trusted files. adotenv-cli runstarts a program with the parent environment plus the loaded values. Loaded files are trusted like code: a loadedPATHchanges which program runs, and variables such asLD_PRELOADchange how it runs.runnever expands references or runs command substitutions.adotenv_process::apply,apply_compactandapply_compact_entrieschange the environment of the current process. All three are unsafe because the environment is shared by every thread.validate,validate_compactandvalidate_compact_entriesrun the same checks without changing anything. On Windows, keys that differ only by case and values longer than 32,767 UTF-16 code units are rejected.adotenv-cli runapplies the same Windows key-case and value-length checks before starting a child.
File writes
Section titled “File writes”adotenv-cli fmt --write and set resolve the write target once before
reading, so a symbolic link swapped after that point cannot redirect the write.
They do not resolve the target again while replacing it. They also check the
target’s device, inode, size and modification time before the temporary file
and immediately before the replace, and fail if that identity changed after the
read. This is best effort against concurrent changes; a writer that preserves
all four values can still race the replacement. They leave an unchanged
file untouched, so its inode, hard links, modification time, extended
attributes and ACLs stay. They replace a changed file through a temporary file
in the same directory. On Unix the temporary file is created with mode 0600
before anything is written, then
receives the original mode, owner and group where the user may set them. Hard
links, extended attributes and ACLs are not kept across a replace. On Windows
the fallback replace is not crash-atomic. See
input and output for the details.
Reporting a vulnerability
Section titled “Reporting a vulnerability”Report security problems privately through GitHub security advisories rather than a public issue.