Bird
Raised Fist0
Denoframework~30 mins

Granular permission scoping in Deno - Mini Project: Build & Apply

Choose your learning style10 modes available
Granular Permission Scoping in Deno
📖 Scenario: You are building a simple Deno script that needs to read a file and access the network. To keep your script secure, you want to use Deno's granular permission scoping to allow only the permissions your script needs.
🎯 Goal: Create a Deno script that requests only the read permission for a specific file and the net permission for a specific host. The script should check if these permissions are granted before proceeding.
📋 What You'll Learn
Create a variable with the file path to read
Create a variable with the network hostname to access
Use Deno.permissions.query() to check read permission for the file path
Use Deno.permissions.query() to check net permission for the hostname
Print messages based on permission status
💡 Why This Matters
🌍 Real World
In real projects, limiting permissions to only what your script needs helps protect user data and system security.
💼 Career
Understanding Deno's permission system is important for developers building secure server-side applications or scripts that interact with files and networks.
Progress0 / 4 steps
1
Set up file path and hostname variables
Create a variable called filePath and set it to "./data.txt". Create another variable called hostName and set it to "example.com".
Deno
Hint

Use const to declare variables with the exact names filePath and hostName.

2
Create permission descriptors for read and net
Create a variable called readPermissionDesc and set it to an object with name as "read" and path as filePath. Create another variable called netPermissionDesc and set it to an object with name as "net" and host as hostName.
Deno
Hint

Permission descriptors are objects with name and either path or host properties.

3
Check permissions using Deno.permissions.query()
Use await Deno.permissions.query(readPermissionDesc) and store the result in readStatus. Use await Deno.permissions.query(netPermissionDesc) and store the result in netStatus.
Deno
Hint

Use await with Deno.permissions.query() and assign to variables readStatus and netStatus.

4
Print permission status messages
Write console.log statements to print "Read permission is granted." if readStatus.state is "granted", otherwise print "Read permission is denied.". Similarly, print "Network permission is granted." if netStatus.state is "granted", otherwise print "Network permission is denied.".
Deno
Hint

Use if statements to check state property and print messages accordingly.

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