我们将把 dotenvy 分叉(fork)为 dotenv-ng。
We Are Forking dotenvy into dotenv-ng

原始链接: https://secretspec.dev/blog/we-are-forking-dotenvy-into-dotenv-ng/

SecretSpec 发布了 **dotenv-ng 1.0**,这是一个用于解析和渲染 `.env` 文件的现代化 Rust 库。 该项目最初是 `dotenvy` 的一个分支。起因是发现 `dotenvy` 的解析器会错误地将 bcrypt 哈希片段视为变量替换,从而导致静默的身份验证失败。由于 `dotenvy` 已停滞超过三年,SecretSpec 决定通过构建一个更稳健、更可预测的解决方案来解决这一问题以及其他解析不一致(包括 JSON 和 Unicode 处理)的问题。 **dotenv-ng 1.0 的主要特性包括:** * **可选的替换功能:** 默认处理字面量美元符号,防止密钥被错误修改。 * **扩展语法:** 全面支持连字符、Unicode 和复杂字符集。 * **高可靠性:** 核心解析器和渲染器具备 100% 的测试覆盖率,并经过了严格的属性测试。 * **安全性:** 改进了验证机制,并对环境变量的变动设定了明确的边界。 `dotenv-ng` 现已成为 SecretSpec 0.20+ 的默认引擎。用户可以通过依赖别名(`dotenv = { package = "dotenv-ng", version = "1" }`)轻松采用它,在保持与现有项目结构兼容的同时,从更安全、更准确的实现中获益。

社区正在讨论 `dotenvy` 库将被分叉(fork)为 `dotenv-ng` 的公告。 在评论区中,用户 `hinkley` 指出,弃用 `.env` 文件需要对环境变量进行严谨的处理。他主张使用自研库来中转配置数据的访问。这种方法能让开发者集中管理密钥、实现可重载配置,并强制执行更严格的安全模式——例如将密钥与标准数据区分对待,以防止意外泄露或日志记录。`hinkley` 指出,简单的基于功能(capability-based)的系统往往因缺乏精细控制而失效,导致系统复杂度增加时出现意外的数据泄露。 另一位用户 `eterm` 则表示,更希望产品公告是由作者亲自撰写,而非通过 AI 生成或总结。
相关文章

原文

We have released dotenv-ng 1.0, a modern Rust implementation for loading and rendering .env files. It began as a fork of dotenvy after its parser changed a secret while reading it.

That may sound contradictory. SecretSpec is still on a mission to eliminate environment variables as a secrets interface, and we have written about where .env went wrong. It should not be the final home of a secret.

But migrating away from .env starts with reading it correctly.

The immediate failure was SecretSpec issue #73. A dotenv file contained a value with bcrypt fragments:

TEST="foo:$2a$10$TWoviNHS27HJMw1PKe4tBeIMlms6tWdYS9hKoHANKCQhluDlEt/gu"

The file was intact. Reading it through the dotenv provider returned a different value because dotenvy treated the dollar-prefixed fragments as variable substitutions. The failure appeared later as an authentication error, not a parse error.

An upstream request to make substitution configurable had been open since 2024. A pull request arrived in 2026 but targeted an unreleased API. A migration tool cannot require users to recognize and escape parser syntax inside their secrets.

The original Rust dotenv crate stopped releasing in 2020 and was eventually marked unmaintained by RustSec, which listed dotenvy as an alternative.

Dotenvy’s description still calls it “a well-maintained fork.” Its latest published version, 0.15.7, was released on March 22, 2023. A Rust forum discussion noted the two-year release gap in 2025. By the time the bcrypt bug blocked SecretSpec, it was more than three years.

There is an uncomfortable irony in a maintained fork repeating its upstream’s release problem. Its maintainers do not owe us a release, but SecretSpec needed breaking fixes on a schedule we control.

We first considered a small patch. Auditing the parser uncovered more problems around JSON, Windows paths, Unicode names, precedence, and partial environment mutation.

dotenv-ng therefore starts from dotenvy 0.15.7 but deliberately breaks compatibility where correctness requires it. Version 1.0 adds:

  • a source-aware parser with structured errors;
  • literal dollar signs by default, with substitution available only when a caller explicitly enables it;
  • a broader key grammar that supports dashes, leading digits, leading dots, and Unicode;
  • a renderer that adds only the quoting and escaping needed to parse a value back unchanged;
  • validation before process-environment mutation; and
  • an explicit unsafe boundary around that mutation.

Property tests exercise arbitrary Unicode and syntax-heavy values, check that quoting is used only when necessary, and round-trip complete documents. The parser and renderer, the core of the rewrite, both have 100% line coverage.

The complete compatibility and API changes are recorded in the dotenv-ng 1.0 changelog.

The package is available on crates.io. Applications can keep the familiar dotenv crate name with a dependency alias:

dotenv = { package = "dotenv-ng", version = "1" }

Starting in SecretSpec 0.20, dotenv-ng powers dotenv parsing and rendering throughout SecretSpec.

联系我们 contact @ memedata.com