Bird
Raised Fist0
Denoframework~30 mins

Environment variable permissions in Deno - Mini Project: Build & Apply

Choose your learning style10 modes available
Environment Variable Permissions in Deno
📖 Scenario: You are building a small Deno script that reads environment variables. Deno requires explicit permission to access environment variables for security reasons.In this project, you will create a script that safely reads environment variables with permission checks.
🎯 Goal: Build a Deno script that reads environment variables API_KEY and DB_HOST only if the script has permission to access environment variables.If permission is denied, the script should handle it gracefully.
📋 What You'll Learn
Create a list of environment variable names to read
Add a variable to store the permission status
Use Deno's permission API to check environment variable access
Read and store environment variables only if permission is granted
💡 Why This Matters
🌍 Real World
Many Deno scripts and applications need to read environment variables for configuration, such as API keys or database hosts. Deno's permission system helps keep these scripts secure by requiring explicit permission.
💼 Career
Understanding how to manage environment variable permissions in Deno is important for secure backend development and deployment, especially when working with sensitive data or cloud environments.
Progress0 / 4 steps
1
Set up environment variable names
Create a constant array called envVars with the exact strings "API_KEY" and "DB_HOST".
Deno
Hint

Use const to declare an array with the two environment variable names.

2
Add permission status variable
Add a variable called permissionStatus and initialize it with await Deno.permissions.query({ name: "env" }) to check environment variable permission.
Deno
Hint

Use await Deno.permissions.query({ name: "env" }) to get permission status.

3
Read environment variables if permission granted
Create an empty object called envValues. Use an if statement to check if permissionStatus.state === "granted". Inside the if, use a for loop with variable name over envVars to assign envValues[name] = Deno.env.get(name).
Deno
Hint

Use an if to check permission and a for loop to read variables into envValues.

4
Handle permission denied case
Add an else block after the if that sets envValues.error to the string "Permission to access environment variables denied.".
Deno
Hint

Use an else block to handle denied permission by setting an error message.

Practice

(1/5)
1. What does the --allow-env flag do in Deno?
easy
A. It encrypts environment variables.
B. It grants permission to access environment variables.
C. It sets environment variables automatically.
D. It disables environment variable access.

Solution

  1. Step 1: Understand the purpose of --allow-env

    This flag is used to allow a Deno program to read environment variables, which are otherwise blocked by default for security.
  2. Step 2: Identify the correct description

    Among the options, only granting permission to access environment variables matches the flag's purpose.
  3. Final Answer:

    It grants permission to access environment variables. -> Option B
  4. Quick Check:

    Permission flag for env vars = D [OK]
Hint: Remember: --allow-env means allow environment access [OK]
Common Mistakes:
  • Thinking it disables environment access
  • Confusing it with setting variables
  • Assuming it encrypts variables
2. Which of the following is the correct way to allow access only to the environment variable API_KEY in Deno?
easy
A. deno run --allow-env=API_KEY script.ts
B. deno run --allow-env API_KEY script.ts
C. deno run --allow-env API_KEY=script.ts
D. deno run --allow-env:API_KEY script.ts

Solution

  1. Step 1: Recall the syntax for environment variable permission

    Deno uses --allow-env=VAR_NAME to allow access only to a specific variable.
  2. Step 2: Match the correct syntax

    deno run --allow-env=API_KEY script.ts correctly uses --allow-env=API_KEY. Other options misuse spaces, equal signs, or colons.
  3. Final Answer:

    deno run --allow-env=API_KEY script.ts -> Option A
  4. Quick Check:

    Specific env var permission uses = [OK]
Hint: Use = to specify variable after --allow-env [OK]
Common Mistakes:
  • Using space instead of = after --allow-env
  • Adding extra characters like : or = in wrong places
  • Confusing argument order
3. Given this command: deno run --allow-env=HOME script.ts and this code inside script.ts:
console.log(Deno.env.get("HOME"));
console.log(Deno.env.get("PATH"));
What will be the output?
medium
A. Prints undefined for both variables.
B. Prints HOME variable value, then prints PATH variable value.
C. Throws a permission error on accessing PATH.
D. Prints HOME variable value, then prints undefined.

Solution

  1. Step 1: Understand permission scope

    The command allows access only to the HOME environment variable, not PATH.
  2. Step 2: Predict output for each variable

    Accessing HOME prints its value. Accessing PATH returns undefined because permission is not granted.
  3. Final Answer:

    Prints HOME variable value, then prints undefined. -> Option D
  4. Quick Check:

    Allowed var prints value, others undefined [OK]
Hint: Only allowed env vars return values; others give undefined [OK]
Common Mistakes:
  • Assuming all env vars print values
  • Expecting a permission error instead of undefined
  • Confusing variable names
4. You run deno run --allow-env=USER script.ts but inside script.ts you try to access Deno.env.get("HOME"). What will happen?
medium
A. It returns undefined without error.
B. It prints the HOME variable value.
C. It throws a runtime permission error.
D. It crashes the program.

Solution

  1. Step 1: Check permission granted

    Only USER variable access is allowed, not HOME.
  2. Step 2: Behavior when accessing disallowed env var

    Deno returns undefined for disallowed environment variables instead of throwing errors.
  3. Final Answer:

    It returns undefined without error. -> Option A
  4. Quick Check:

    Disallowed env vars return undefined, no error [OK]
Hint: Disallowed env vars return undefined, no crash [OK]
Common Mistakes:
  • Expecting a permission error
  • Thinking it prints the value anyway
  • Assuming program crashes
5. You want to run a Deno script that needs access to API_KEY and DB_PASS environment variables only. Which command correctly grants minimal permissions?
hard
A. deno run --allow-env=API_KEY script.ts && deno run --allow-env=DB_PASS script.ts
B. deno run --allow-env script.ts
C. deno run --allow-env=API_KEY,DB_PASS script.ts
D. deno run --allow-env=API_KEY DB_PASS script.ts

Solution

  1. Step 1: Understand how to specify multiple env vars

    Deno allows multiple variables by comma-separating them after --allow-env=.
  2. Step 2: Evaluate each option

    deno run --allow-env=API_KEY,DB_PASS script.ts correctly uses comma separation. deno run --allow-env script.ts grants all env vars (too broad). deno run --allow-env=API_KEY script.ts && deno run --allow-env=DB_PASS script.ts runs script twice separately (inefficient). deno run --allow-env=API_KEY DB_PASS script.ts syntax is invalid.
  3. Final Answer:

    deno run --allow-env=API_KEY,DB_PASS script.ts -> Option C
  4. Quick Check:

    Multiple vars comma-separated after --allow-env= [OK]
Hint: Use commas to list multiple env vars after --allow-env= [OK]
Common Mistakes:
  • Granting all env vars instead of specific ones
  • Running script multiple times instead of once
  • Using spaces instead of commas
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard