Bird
Raised Fist0
Denoframework~5 mins

Granular permission scoping in Deno

Choose your learning style10 modes available
Introduction

Granular permission scoping lets you control exactly what parts of your app can access sensitive features. It keeps your app safer by only giving the permissions it really needs.

When your app needs to read files but only from specific folders.
When your app accesses the network but only to certain websites.
When you want to limit access to environment variables for security.
When running scripts that should not have full system access.
When testing code that requires some permissions but not all.
Syntax
Deno
deno run --allow-read=/path/to/folder --allow-net=example.com script.ts
Use flags like --allow-read, --allow-net, --allow-env to specify permissions.
You can limit permissions to specific paths or hosts by adding = after the flag.
Examples
This allows the script to read files only inside the ./data folder.
Deno
deno run --allow-read=./data script.ts
This allows network access only to api.example.com.
Deno
deno run --allow-net=api.example.com script.ts
This allows access only to the API_KEY environment variable.
Deno
deno run --allow-env=API_KEY script.ts
Sample Program

This Deno script tries to read a file ./data/info.txt and print its content. You must run it with permission to read only the ./data folder for safety.

Deno
import { readTextFile } from "https://deno.land/std/fs/mod.ts";

async function readData() {
  try {
    const data = await readTextFile("./data/info.txt");
    console.log("File content:", data);
  } catch (error) {
    console.error("Error reading file:", error.message);
  }
}

readData();
OutputSuccess
Important Notes

If you try to access a resource without permission, Deno will stop the script and show an error.

Granting only needed permissions helps protect your system and data.

You can combine multiple permission flags to be very specific.

Summary

Granular permission scoping limits what your Deno app can access.

Use flags like --allow-read=path or --allow-net=host to be specific.

This improves security by reducing risks from unwanted access.

Practice

(1/5)
1. What does the --allow-read=./data flag do when running a Deno script?
easy
A. Allows the script to read any file on the system
B. Allows the script to read files only inside the ./data directory
C. Blocks all file reading operations
D. Allows the script to write files inside the ./data directory

Solution

  1. Step 1: Understand the --allow-read flag

    The --allow-read flag grants permission to read files. When given a path, it limits reading to that path.
  2. Step 2: Analyze the given path ./data

    Since the flag is --allow-read=./data, the script can only read files inside the ./data directory, not outside.
  3. Final Answer:

    Allows the script to read files only inside the ./data directory -> Option B
  4. Quick Check:

    --allow-read=path limits read to that path [OK]
Hint: Remember: --allow-read=path restricts reading to that path only [OK]
Common Mistakes:
  • Thinking it allows reading all files
  • Confusing read permission with write permission
  • Assuming it blocks all reading
2. Which of the following is the correct syntax to allow network access only to example.com in Deno?
easy
A. deno run --allow-net=example.com script.ts
B. deno run --allow-net example.com script.ts
C. deno run --allow-net:example.com script.ts
D. deno run --allow-net[example.com] script.ts

Solution

  1. Step 1: Recall the correct flag format for network permission

    The correct syntax to allow network access to a specific host is --allow-net=host.
  2. Step 2: Match the options with the correct syntax

    deno run --allow-net=example.com script.ts uses --allow-net=example.com, which is correct. Other options use incorrect separators or missing equals sign.
  3. Final Answer:

    deno run --allow-net=example.com script.ts -> Option A
  4. Quick Check:

    Use --allow-net=host for specific network access [OK]
Hint: Use equals sign (=) to specify hosts in --allow-net flag [OK]
Common Mistakes:
  • Using space instead of equals sign
  • Using colon or brackets instead of equals
  • Omitting the host after --allow-net
3. Consider this command: deno run --allow-read=./files --allow-net=api.example.com script.ts. What will happen if script.ts tries to read ./files/data.txt and fetch from api.example.com?
medium
A. Both reading the file and fetching will fail
B. Reading the file will fail, fetching will succeed
C. Reading the file will succeed, fetching will fail
D. Both reading the file and fetching from the API will succeed

Solution

  1. Step 1: Check read permission for ./files/data.txt

    The flag --allow-read=./files allows reading any file inside ./files, so reading data.txt is allowed.
  2. Step 2: Check network permission for api.example.com

    The flag --allow-net=api.example.com allows network requests only to api.example.com, so fetching from that API is allowed.
  3. Final Answer:

    Both reading the file and fetching from the API will succeed -> Option D
  4. Quick Check:

    Permissions match actions, so both succeed [OK]
Hint: Match file path and host exactly with permission flags [OK]
Common Mistakes:
  • Assuming read permission covers all files
  • Assuming --allow-net allows all hosts
  • Confusing read and write permissions
4. You run deno run --allow-read=./data script.ts but your script tries to read ./config/settings.json and fails. What is the most likely cause?
medium
A. The script has no network permission
B. The script has no permission to write to ./config
C. The script has no permission to read outside the ./data directory
D. The script has a syntax error in reading files

Solution

  1. Step 1: Understand the --allow-read=./data scope

    This flag only allows reading files inside the ./data directory, not outside.
  2. Step 2: Analyze the attempted file path ./config/settings.json

    The script tries to read a file outside the allowed ./data directory, so permission is denied.
  3. Final Answer:

    The script has no permission to read outside the ./data directory -> Option C
  4. Quick Check:

    Permission scope limits file access [OK]
Hint: Check if file path is inside allowed read directory [OK]
Common Mistakes:
  • Confusing read and write permissions
  • Assuming network permission affects file reading
  • Blaming syntax errors for permission issues
5. You want to run a Deno script that reads files only from ./logs and ./configs directories and fetches data only from api.service.com and cdn.service.com. Which command correctly grants these permissions?
hard
A. deno run --allow-read=./logs,./configs --allow-net=api.service.com,cdn.service.com script.ts
B. deno run --allow-read=./logs --allow-read ./configs --allow-net=api.service.com --allow-net=cdn.service.com script.ts
C. deno run --allow-read=./logs ./configs --allow-net=api.service.com cdn.service.com script.ts
D. deno run --allow-read=./logs --allow-net=api.service.com,cdn.service.com script.ts

Solution

  1. Step 1: Understand how to specify multiple paths or hosts

    Deno allows multiple paths or hosts in a single flag separated by commas, e.g., --allow-read=path1,path2.
  2. Step 2: Analyze each option for correct syntax

    deno run --allow-read=./logs,./configs --allow-net=api.service.com,cdn.service.com script.ts correctly uses commas to list multiple directories and hosts in one flag. deno run --allow-read=./logs --allow-read ./configs --allow-net=api.service.com --allow-net=cdn.service.com script.ts has incorrect syntax (missing = after second --allow-read). Options C and D have incorrect syntax or missing permissions.
  3. Final Answer:

    deno run --allow-read=./logs,./configs --allow-net=api.service.com,cdn.service.com script.ts -> Option A
  4. Quick Check:

    Use commas to list multiple paths or hosts in one flag [OK]
Hint: Use commas to separate multiple paths or hosts in permission flags [OK]
Common Mistakes:
  • Missing the equals sign (=) in permission flags
  • Using spaces instead of commas to separate paths
  • Omitting one of the required permissions
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