cli icon indicating copy to clipboard operation
cli copied to clipboard

edge-functions: Deno.env should contain the same environment variables as in production

Open Skn0tt opened this issue 3 years ago • 4 comments

Describe the bug

Running edge functions locally with netlify dev, Deno.env contains all environment variables inherited from the CLI's shell. This is different from production, where it will only contain env vars specified in the Netlify UI, and two Deno-specific ones. This should be replicated in local dev.

One possible solution is to work with downstream @netlify/edge-bundler, and pass the --allow-env=... flag to only allow access to the environment variables that'd also exist in production:

https://github.com/netlify/edge-bundler/blob/0d2c6af674d30f0322bf376ad66acb0d5b5b0124/src/server/server.ts#L113

Steps to reproduce

Write an edge function, log Deno.env.toObject(). It should contain just placeholders for DENO_REGION and DENO_DEPLOYMENT_ID (probably not even that), but instead it contains all env vars inherited from the CLI's shell.

Configuration

No response

Environment

not relevant

Skn0tt avatar May 16 '22 16:05 Skn0tt

cc @eduardoboucas

Skn0tt avatar May 16 '22 16:05 Skn0tt

It should contain just placeholders for DENO_REGION and DENO_DEPLOYMENT_ID (probably not even that), but instead it contains all env vars inherited from the CLI's shell.

It also needs to contain the environment variables loaded from the API, which are merged into process.env. This is also the case with serverless functions.

eduardoboucas avatar May 16 '22 16:05 eduardoboucas

One possible solution is to work with downstream @netlify/edge-bundler, and pass the --allow-env=... flag to only allow access to the environment variables that'd also exist in production:

This does not work, because first of all we would need to remove --allow-all, because it overrides --allow-env. But the real problem is that DENO will throw once you try to access a not allowed environment variable. So doing Deno.env.toObject() throws PermissionDenied: Requires env access to "DENO_DEPLOYMENT_ID", run again with the --allow-env flag

I guess we have to explicitly set the environment variables.

danez avatar Aug 01 '22 15:08 danez

wow that's weird behaviour 🤔

Skn0tt avatar Aug 01 '22 16:08 Skn0tt