What if your script could only talk to the websites you trust, nothing else?
Why Network permissions (--allow-net) in Deno? - Purpose & Use Cases
Imagine running a script that fetches data from the internet with full network access, allowing connections to any site. You might accidentally allow harmful connections or leak sensitive information.
With unrestricted --allow-net, scripts can access any website or server, which is risky and hard to control. This can lead to security breaches or unexpected data usage.
Network permissions like --allow-net let you explicitly allow only trusted network access, keeping your scripts safe and predictable.
deno run --allow-net script.ts
deno run --allow-net=example.com script.ts
This lets you run networked scripts securely by controlling exactly which sites they can reach.
When building a tool that fetches weather data, you only allow network access to the weather API, preventing any other internet connections.
Unrestricted network access is risky and uncontrolled.
--allow-net restricts scripts to trusted sites only.
This improves security and predictability of network operations.
Practice
--allow-net flag do when running a Deno program?Solution
Step 1: Understand the purpose of
This flag is used to grant network access to a Deno program, which is otherwise restricted for security.--allow-netStep 2: Compare options with the flag's function
Only "It allows the program to access the network." correctly states that--allow-netallows network access. Other options describe unrelated permissions or features.Final Answer:
It allows the program to access the network. -> Option AQuick Check:
Network permission = Allow network access [OK]
- Confusing --allow-net with file permissions
- Thinking it disables network access
- Mixing it up with debugging flags
app.ts?Solution
Step 1: Identify correct flag syntax for domain restriction
The correct syntax is--allow-net=domainplaced before the script name.Step 2: Check command order and placement
deno run --allow-net=example.com app.ts correctly places--allow-net=example.combeforeapp.ts. Other options have incorrect order or syntax.Final Answer:
deno run --allow-net=example.com app.ts -> Option CQuick Check:
Correct flag syntax = deno run --allow-net=example.com app.ts [OK]
- Placing domain after script name
- Using space instead of '=' between flag and domain
- Putting script name before flags
deno run --allow-net=deno.land app.ts
And
app.ts contains:const response = await fetch('https://deno.land/std/http/file_server.ts');
console.log(response.status);What will be the output?
Solution
Step 1: Check network permission for the domain
The command allows network access only to "deno.land", which matches the fetch URL domain.Step 2: Understand fetch behavior with permission
Since permission is granted, fetch succeeds and returns HTTP status 200 for a valid resource.Final Answer:
200 -> Option DQuick Check:
Allowed domain fetch = 200 status [OK]
- Assuming permission error despite allowed domain
- Confusing 404 with permission issues
- Expecting syntax errors from fetch
deno run --allow-net=example.com app.ts
Inside
app.ts you fetch https://google.com. What error will you get?Solution
Step 1: Analyze allowed domains
The command only allows network access to "example.com" but the script tries to fetch "google.com".Step 2: Understand Deno's permission enforcement
Deno blocks network requests to domains not allowed, causing a PermissionDenied error.Final Answer:
PermissionDenied: network access to "google.com" is not allowed -> Option AQuick Check:
Disallowed domain fetch = PermissionDenied error [OK]
- Thinking fetch will succeed without permission
- Confusing permission error with URL or reference errors
- Ignoring domain restrictions in --allow-net
api.example.com and data.service.org. Which command correctly grants network access to both domains using a single --allow-net flag?Solution
Step 1: Understand multiple domain syntax for --allow-net
Deno accepts multiple domains separated by commas in a single--allow-netflag.Step 2: Evaluate each option's correctness
deno run --allow-net=api.example.com,data.service.org app.ts uses comma-separated domains correctly. deno run --allow-net=api.example.com --allow-net=data.service.org app.ts repeats the flag (not a single flag). deno run --allow-net app.ts api.example.com data.service.org misplaces domains as arguments. deno run --allow-net=api.example.com;data.service.org app.ts uses semicolon which is incorrect.Final Answer:
deno run --allow-net=api.example.com,data.service.org app.ts -> Option BQuick Check:
Multiple domains = comma-separated in one flag [OK]
- Using multiple --allow-net flags
- Separating domains with semicolons
- Placing domains after script name
Ready to prove it?
Pass the test and this pattern moves unknown
