What if your code could only do what you say, nothing more?
Why Deno is secure by default - The Real Reasons
Imagine running a JavaScript program that can read all your files, access the internet, and even run other programs without asking you.
You might not realize what it's doing behind the scenes.
Traditional JavaScript runtimes let programs do anything by default.
This can lead to accidental data leaks, security risks, or harmful actions if the code is unsafe.
Deno blocks all file, network, and environment access unless you explicitly allow it.
This means your programs run in a safe box, protecting your computer and data.
node app.js # runs with full access to files and networkdeno run --allow-net app.ts # only network access allowedYou can run code confidently knowing it can only do what you permit.
Running a third-party script safely without worrying it will steal your files or send data without permission.
Deno restricts dangerous actions by default.
You grant permissions explicitly when needed.
This design helps keep your system and data safe.
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
