Deno is secure by default. Scripts cannot access files, network, or environment variables without explicit permission. This design prevents malicious code from accessing sensitive resources on your system.
Without permissions, Deno code runs in a sandbox with no access to:
# Allow reading from any locationdeno run --allow-read script.ts# Allow reading from specific directorydeno run --allow-read=/etc script.ts# Allow multiple pathsdeno run --allow-read=/etc,/var/log script.ts
—allow-write: Allow file system write access
# Allow writing to any locationdeno run --allow-write script.ts# Allow writing to specific directorydeno run --allow-write=./data script.ts
# Allow all network accessdeno run --allow-net server.ts# Allow specific domainsdeno run --allow-net=deno.land,api.github.com script.ts# Allow specific portsdeno run --allow-net=localhost:8000 server.ts
You can explicitly deny permissions to prevent accidental access:
# Deny network access to specific hostsdeno run --deny-net=malicious.com script.ts# Deny write access to sensitive directoriesdeno run --allow-write --deny-write=/etc script.ts
When running in interactive mode (TTY), Deno prompts for permissions:
⚠️ ┌ Deno requests net access to "deno.land". ├ Requested by `fetch()` API. ├ Run again with --allow-net to bypass this prompt. └ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all net permissions) >
Permission prompts include the specific resource being accessed and the API that triggered the request.
# Good: Specific permissionsdeno run --allow-read=./config --allow-net=api.example.com script.ts# Bad: Overly broad permissionsdeno run --allow-all script.ts
Use Permission API in Libraries
Check permissions before attempting operations:
export async function saveData(data: string) { const status = await Deno.permissions.query({ name: "write", path: "./data" }); if (status.state !== "granted") { throw new Error("Write permission required"); } await Deno.writeTextFile("./data/file.txt", data);}
Document Required Permissions
Always document what permissions your script needs:
/** * Fetches user data from the API * * Required permissions: * - --allow-net=api.example.com * - --allow-env=API_KEY */export async function getUserData() { // ...}