Bird
Raised Fist0
Denoframework~20 mins

Environment variable permissions in Deno - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Deno Env Permissions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Deno's environment variable access
What happens when a Deno script tries to access an environment variable without the proper permission flag?
Deno
console.log(Deno.env.get('MY_SECRET'));
AThe script prints the environment variable value if it exists.
BThe script throws a PermissionDenied error at runtime.
CThe script returns undefined without any error.
DThe script crashes with a syntax error.
Attempts:
2 left
💡 Hint
Deno requires explicit permission flags to access environment variables.
❓ state_output
intermediate
2:00remaining
Output when accessing environment variables with partial permissions
Given the following Deno script and running it with deno run --allow-env=MY_VAR script.ts, what is the output?
Deno
console.log(Deno.env.get('MY_VAR'));
console.log(Deno.env.get('OTHER_VAR'));
A"value1" followed by a PermissionDenied error
B"value1" followed by undefined
Cundefined followed by undefined
DPermissionDenied error on the first line
Attempts:
2 left
💡 Hint
The --allow-env flag can specify which variables are accessible.
🔧 Debug
advanced
2:00remaining
Identify the cause of a PermissionDenied error
Why does the following Deno script throw a PermissionDenied error when run with deno run script.ts?
Deno
const token = Deno.env.get('API_TOKEN');
console.log(`Token: ${token}`);
AThe environment variable API_TOKEN is not set in the system.
BDeno does not support environment variables.
CThe script has a syntax error in the template literal.
DThe script lacks the <code>--allow-env</code> permission flag.
Attempts:
2 left
💡 Hint
Check the permissions required to access environment variables in Deno.
📝 Syntax
advanced
2:00remaining
Correct usage of environment variable access with permission flags
Which command correctly runs a Deno script that accesses environment variables named USER and PATH?
Adeno run --allow-env=USER PATH script.ts
Bdeno run --allow-env script.ts
Cdeno run --allow-env=USER,PATH script.ts
Ddeno run --allow-env=USER;PATH script.ts
Attempts:
2 left
💡 Hint
Check how to specify multiple environment variables in the permission flag.
❓ component_behavior
expert
3:00remaining
Behavior of environment variable access in a Deno script with dynamic permission requests
Consider a Deno script that tries to read an environment variable and requests permission dynamically using Deno.permissions.request({ name: 'env', variable: 'SECRET' }). What is the expected behavior if the user denies the permission request?
Deno
async function getSecret() {
  const status = await Deno.permissions.request({ name: 'env', variable: 'SECRET' });
  if (status.state === 'granted') {
    return Deno.env.get('SECRET');
  } else {
    return 'Permission denied';
  }
}

getSecret().then(console.log);
AThe script prints the value of SECRET if permission is granted; otherwise, it prints 'Permission denied'.
BThe script throws a runtime error if permission is denied.
CThe script ignores the permission and always prints the SECRET value.
DThe script crashes immediately without any output.
Attempts:
2 left
💡 Hint
Deno's permissions API allows dynamic permission requests and returns status objects.

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