Branch Deploy Preview Environment Variable Exposure
0 reputation · 17 Nov 2022, 22:28 UTC
0 reputation · 17 Nov 2022, 22:28 UTC
Netlify branch deploys generate public preview URLs for each commit, and environment variables assigned to a branch deploy may be available to the preview build runtime. The goal is to keep sensitive credentials out of preview contexts while preserving non-secret variables required for builds.
It is unclear whether netlify.toml can scope variables to production deploys only, or whether branch deploy previews inherit all account-level variables by default. Free tier users may also have limited UI controls for restricting variable visibility.
Can environment variables be restricted to production contexts so branch deploy previews do not receive them? Do preview builds respect the same variable filtering rules as production deploys? How can a team confirm that a specific variable is absent from a branch deploy preview runtime without exposing its value?
Yes – Netlify lets you scope variables to the Production context only. Branch deploy previews will not receive those variables. The same scoping rules apply to all preview builds (deploy‑preview, branch‑deploy). You can verify a variable’s absence by checking the build log for its name (not its value) or by using the Netlify CLI to list context‑specific variables.
Production, Deploy preview, Branch deploy, or All (default).netlify env:set CLI command. The netlify.toml file does not support variable scoping.API_SECRET). Edit its scopes and uncheck Deploy preview and Branch deploy so it is only selected for Production.netlify env:set API_SECRET "s3cr3t" --scope production
Yes. Netlify’s preview build pipeline reads the same environment‑variable configuration. Anything scoped to Production alone is invisible to both Deploy preview and Branch deploy runtimes.
console.log(process.env.API_SECRET !== undefined ? "API_SECRET present" : "API_SECRET absent");
API_SECRET present. If it is not present, the variable was not injected.netlify env:list --site --context branch
API_SECRET does not appear in the output.PUBLIC_ are exposed to the client.Do you have Netlify CLI access, or will you rely solely on the web UI to adjust scopes?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.