fallow_types/extract_env.rs
1//! Env var-name predicates that decide whether an env read is public by
2//! convention. `crate::extract` re-exports every public item.
3
4/// Env var-name prefixes that frameworks inline into the client bundle by
5/// convention. A read of one of these is normal and safe, so it does NOT count
6/// as a secret source (issue #890). Shared by the extract layer (so public env
7/// vars never become source signals) and the bespoke `client-server-leak` rule.
8pub const PUBLIC_ENV_PREFIXES: &[&str] = &[
9 "NEXT_PUBLIC_",
10 "VITE_",
11 "NUXT_PUBLIC_",
12 "REACT_APP_",
13 "PUBLIC_",
14 "GATSBY_",
15 "EXPO_PUBLIC_",
16 "STORYBOOK_",
17];
18
19/// Exact env var names that are public by convention (no prefix).
20pub const PUBLIC_ENV_EXACT: &[&str] = &["NODE_ENV"];
21
22/// Env var-name tokens that usually describe public build or deployment
23/// metadata rather than secrets. Secret-shaped names win over these tokens.
24pub const PUBLIC_ENV_METADATA_TOKENS: &[&str] =
25 &["BRANCH", "ENVIRONMENT", "MODE", "REF", "SHA", "TAG"];
26
27/// Env var-name tokens that should keep a variable source-backed even when the
28/// name also contains public metadata tokens such as `REF` or `SHA`.
29pub const SECRET_ENV_TOKENS: &[&str] = &[
30 "AUTH",
31 "CREDENTIAL",
32 "CREDENTIALS",
33 "KEY",
34 "PASS",
35 "PASSWORD",
36 "PRIVATE",
37 "SECRET",
38 "TOKEN",
39];
40
41fn env_name_has_token(name: &str, tokens: &[&str]) -> bool {
42 name.split(|ch: char| !ch.is_ascii_alphanumeric())
43 .filter(|part| !part.is_empty())
44 .any(|part| tokens.contains(&part))
45}
46
47/// Whether an env var name is public-by-convention (build-inlined into the
48/// client bundle), and therefore not a secret.
49#[must_use]
50pub fn is_public_env_var(name: &str) -> bool {
51 if PUBLIC_ENV_EXACT.contains(&name) || PUBLIC_ENV_PREFIXES.iter().any(|p| name.starts_with(p)) {
52 return true;
53 }
54 env_name_has_token(name, PUBLIC_ENV_METADATA_TOKENS)
55 && !env_name_has_token(name, SECRET_ENV_TOKENS)
56}
57
58/// Whether a flattened member path is a PUBLIC env-secret read
59/// (`process.env.NEXT_PUBLIC_X`, `import.meta.env.VITE_Y`), which must not be
60/// recorded as a secret source. Non-env paths (`req.query.id`) are never public.
61#[must_use]
62pub fn is_public_env_path(path: &str) -> bool {
63 for object in ["process.env.", "import.meta.env."] {
64 if let Some(var) = path.strip_prefix(object) {
65 return is_public_env_var(var);
66 }
67 }
68 false
69}
70
71/// The syntax that reads an env var.
72#[derive(
73 Debug,
74 Clone,
75 Copy,
76 PartialEq,
77 Eq,
78 serde::Serialize,
79 serde::Deserialize,
80 bitcode::Encode,
81 bitcode::Decode,
82)]
83pub enum EnvReadForm {
84 /// `process.env.X` or `import.meta.env.X`.
85 Dotted,
86 /// `process.env['X']` with a static key.
87 Computed,
88 /// `const { X } = process.env`, also with a rename or a default value.
89 Destructured,
90}
91
92/// One read of a non-public env var (`process.env.X`, `import.meta.env.X`).
93///
94/// Extraction records the read where it happens, so the `client-server-leak`
95/// rule can report the line of the read and can tell whether the read is in
96/// client code of a React Router route module. A public var
97/// ([`is_public_env_var`]) is not recorded.
98#[derive(
99 Debug,
100 Clone,
101 PartialEq,
102 Eq,
103 serde::Serialize,
104 serde::Deserialize,
105 bitcode::Encode,
106 bitcode::Decode,
107)]
108pub struct EnvRead {
109 /// The full read path, for example `process.env.SESSION_SECRET`.
110 pub name: String,
111 /// The byte offset of the var name in the source: the property of a
112 /// member read, or the key of a destructured property.
113 pub span_start: u32,
114 /// The syntax of the read.
115 pub form: EnvReadForm,
116}