PeaBox Lab › PeaLang › Learn › Chapter 14 › Lesson 1

Part III — Advanced Systems · Chapter 14 · Lesson 1 of 5 · 4 min read

Lesson 1: Capability-Safe Execution and Least Privilege

Lesson level: Advanced

Purpose: Understand the runtime security model in depth: what a capability is, how the host grants and scopes it, how denial works, and how to design scripts and hosts around least privilege.

Release note: capability names, policy formats, and host APIs in this lesson are illustrative. Verify them against your installed v0.63 release.

Learning objectives

By the end of this lesson, you should be able to:

1.1 Ambient authority versus capabilities

In many languages, any code can open any file or connect to any host the process can reach. That is ambient authority: power comes from simply running. PeaLang instead treats sensitive operations as capabilities: a script can perform them only if the host has explicitly granted them, and only within the granted scope.

1.2 The capability families

FamilyExamples of scope
File readSpecific directories or files
File writeSpecific output directories
HTTPAllowed hosts, methods, and paths
ProcessUsually denied; specific executables if ever granted
LoggingStructured logging allowed or denied
ConcurrencyWhether tasks may be started, and how many
Host functionsEach registered function names the capability it needs

Chapter 8 introduced these from the script's side. This lesson takes the host's view.

1.3 Deny by default

A runtime with no explicit grants should be able to compute, branch, loop, and return values, but not touch the file system, network, or processes. Every grant is an addition that must be justified.

1.4 Granting capabilities

# host policy (illustrative)
[capabilities]
file_read  = ["data/policies/"]
file_write = []
http       = [{ host = "rates.example.com", methods = ["GET"] }]
process    = false
logging    = true
tasks      = { max = 10 }

Each entry is narrow: one directory, one host, one method. Wildcards such as "any host" or the root directory defeat the purpose.

1.5 Scoping by path and host

Path scopes are checked after resolving relative segments and links, so data/policies/../../secrets is outside data/policies/. HTTP scopes check the final host after redirects. Hosts should normalize before checking, not after.

1.6 Narrow host functions instead of broad capabilities

Often the safest grant is no raw capability at all. Instead of granting HTTP access to an exchange-rate service, the host exposes host.rate_for(currency), which validates the currency code, calls the service, and returns a number. The script gets the answer without the ability to call arbitrary URLs.

1.7 How the layers combine

LayerDecides
Package permission manifest (Chapter 11)What a dependency asks for
Project policyWhat the project allows any dependency
Host runtime grantsWhat this execution may actually do
Host function designHow narrow each operation is

An operation succeeds only if every layer allows it. A package that declares HTTP access still fails at runtime if the host did not grant it.

1.8 Denial and permission errors

A denied operation raises a permission error naming the capability, not the secret details of the resource. Scripts should not catch and ignore permission errors; they indicate a design or policy mismatch the host must see.

1.9 Designing a minimal set

1.10 Worked example

The order_rules service needs customer tiers, stock checks, and a policy file. The final design grants no raw HTTP or database access. It exposes host.lookup_tier and host.reserve_stock under named capabilities, grants file read only to data/policies/, allows structured logging, and denies process execution. A test confirms that an attempt to read data/secrets.json raises a permission error.

1.11 Common mistakes

MistakeConsequenceFix
Granting broad access during developmentBroad access ships to productionStart from deny and add narrowly
Checking paths before normalizingTraversal escapes the scopeNormalize, then check
Catching permission errors in scriptsHidden policy problemsLet them reach the host
Raw HTTP where a host function would doLarger attack surfaceExpose narrow host functions

1.12 Guided practice

1.13 Hands-on lab

Run the order_rules service under a deny-by-default policy. Add grants one at a time until tests pass, recording each. Then add negative tests for file traversal, an unlisted HTTP host, and process execution.

Deliverable: the final policy with reasons, and test output for each negative case.

1.14 Review questions

1.15 Applied challenge

Audit a script from an earlier chapter. List every capability it uses, propose a narrower design for each, and estimate how the attack surface changes.

Lesson summary

Transition to Lesson 2

Capabilities decide what a script may do. Lesson 2 decides how much: time, instruction, memory, recursion, stack, collection, output, HTTP, and file-read limits.

Lesson status: Ready for review.

← Previous Next →