Deno keeps your computer safe by not allowing programs to do risky things unless you say it's okay.
Why Deno is secure by default
deno run [--allow-read] [--allow-net] script.ts
You add flags like --allow-read to let your program read files.
Without flags, Deno blocks access to files, network, environment, and more.
deno run script.ts
deno run --allow-read script.ts
deno run --allow-net script.ts
deno run --allow-read --allow-net script.ts
This program prints a greeting, then tries to read a file called example.txt. Without permission, it will show an error.
console.log('Hello from Deno!'); // Try reading a file without permission try { const data = await Deno.readTextFile('example.txt'); console.log('File content:', data); } catch (error) { console.log('Error:', error.message); }
Deno asks for permissions only when needed, making it safer than some other runtimes.
You can give permissions permanently or just for one run using flags.
This security model helps prevent accidental or harmful actions by code.
Deno blocks risky actions by default to keep your system safe.
You must explicitly allow permissions like file or network access.
This makes running code safer, especially from unknown sources.
Practice
Solution
Step 1: Understand Deno's security design
Deno is designed to be secure by default to protect users from running unsafe code.Step 2: Reason about default blocking
Blocking file and network access prevents malicious code from harming your system without permission.Final Answer:
To protect your computer from harmful or unknown code -> Option AQuick Check:
Security by default = Protect computer [OK]
- Thinking Deno lacks file/network features
- Assuming blocking improves speed
- Believing Deno runs only offline
Solution
Step 1: Recall Deno permission flags
Deno uses flags like --allow-read to grant file read access explicitly.Step 2: Identify correct syntax
Only --allow-read is the valid flag to allow reading files in Deno.Final Answer:
deno run --allow-read script.ts -> Option BQuick Check:
File read permission flag = --allow-read [OK]
- Using incorrect flags like --read or --allow-file
- Forgetting to add permission flags
- Confusing permission flag names
console.log(Deno.readTextFileSync("data.txt"));Solution
Step 1: Understand default permissions
Deno blocks file reading unless --allow-read is given.Step 2: Predict script behavior without permissions
Trying to read a file without permission causes a runtime permission error.Final Answer:
It throws a permission error -> Option DQuick Check:
No permission = permission error [OK]
- Assuming file reads succeed without permission
- Thinking file is created automatically
- Expecting silent failure without error
deno run script.tsHow do you fix it to allow network access?
Solution
Step 1: Identify correct network permission flag
Deno requires --allow-net to permit network access explicitly.Step 2: Correct the command
Adding --allow-net fixes the permission error for network operations.Final Answer:
Add --allow-net flag to the command -> Option CQuick Check:
Network permission flag = --allow-net [OK]
- Using wrong flag like --allow-network or --net-access
- Assuming network is allowed by default
- Not adding any permission flag
Solution
Step 1: Identify required permissions
Reading a file needs --allow-read; network requests need --allow-net.Step 2: Choose minimal permissions
--allow-read and --allow-net together grant only needed access, no extras.Step 3: Eliminate incorrect options
--allow-all grants too much; --allow-write is unnecessary; --allow-read alone misses network.Final Answer:
deno run --allow-read --allow-net script.ts -> Option AQuick Check:
Minimal needed permissions = --allow-read + --allow-net [OK]
- Using --allow-all when minimal is better
- Confusing --allow-write with --allow-read
- Forgetting network permission
Ready to prove it?
Pass the test and this pattern moves unknown
