PeaBox Lab › PeaLang › Learn › Chapter 8 › Lesson 5

Part II — Intermediate Development · Chapter 8 · Lesson 5 of 6 · 5 min read

Lesson 5: Capability Permissions and Safe Usage Patterns

Level: Intermediate

Purpose. The previous lessons used files, HTTP, cryptography, logging, and concurrency as std facilities. This lesson shows how PeaLang's capability model governs which of those facilities a program may actually use, and how to design programs that request little, fail predictably when denied, and remain auditable.

Learning objectives

5.1 The capability model

A capability is a granted right to perform a class of sensitive operation, such as reading files, writing files, making HTTP requests, or running processes. Code that holds no capability can still compute, transform, and validate data, but any attempt to touch the outside world is refused by the runtime. The host or the project configuration decides what is granted; a script cannot grant itself more.

ConceptMeaningDesign consequence
RequestWhat the program or package declares it needsKeep it minimal and documented
GrantWhat the host or policy actually allowsMay be narrower than the request
DenialThe runtime refuses an operationHandle as an expected outcome
AuditA record of decisions and useLog reason codes, never secrets

Release note: capability names and denial messages shown here are illustrative. Verify them against the installed release's documentation and std manifest.

5.2 Least privilege in practice

Start with the behavior, not the permission list. For each operation the program performs, write the resource, the direction, and the reason. Then request only what that table justifies.

OperationResourceNeeded capabilityNarrowing option
Read input CSVOne input directoryFile readRestrict to a single directory
Write reportOne output directoryFile writeSeparate from the input location
Fetch ratesOne known hostHTTPAllow-list the host
Parse and aggregateIn memoryNoneKeep in the pure core

Parsing, aggregation, validation, and formatting need nothing. The more logic lives in the pure core, the smaller the permission surface.

5.3 Denial as a stable outcome

A denied operation should produce a clear, classified failure that callers can recognize. Handle it where the adapter lives, translate it to a stable reason code such as permission_denied, and decide whether the program degrades, uses another source, or stops.

5.4 Safe adapter pattern

Place each privileged operation in one small adapter that takes a narrow input, performs exactly one kind of access, and returns a result value instead of raising across the whole program. The pure core receives data and returns decisions; it never imports a privileged facility.

function read_orders(path) -> Result // adapter: file read only function summarize(rows) -> Report // pure core: no capability function write_report(report, path) // adapter: file write only

Because the adapters are tiny, the permission list can be read directly off them, and tests can replace them with in-memory fakes.

5.5 Auditability and redaction

Record each privileged decision as a structured event: operation, resource class, outcome, and reason code. Never log credentials, tokens, full file contents, or personal data. Use synthetic data in examples and tests.

5.6 Permission drift and review

Permissions tend to grow. Review the requested set whenever code changes: remove any capability no adapter uses, re-check narrowing options, and treat a new request as a change needing justification in review.

5.7 Common mistakes

MistakeEffectCorrection
Requesting every capability up frontLarge attack surfaceDerive the set from the adapters
Treating denial as a transient errorPointless retriesClassify and fail closed
Catching all errors and continuingHidden policy failuresHandle denial explicitly
Logging secrets while auditingData leakLog reason codes only
Testing only the allowed pathUntested denial behaviorAdd deny tests for every adapter
Mixing I/O into core logicHard to test and to restrictMove I/O into adapters

5.8 Diagnostic workflow

5.9 Guided practice

5.10 Hands-on lab: permission-aware report tool

Requirements. Build a tool that reads a synthetic CSV, summarizes it, and writes a report, using one read adapter, one write adapter, and a pure core.

Tests. Cover the allowed path, a denied read, a denied write, a missing file, and a malformed row. Verify each failure yields a distinct stable reason code.

Deliverables. Source files, a documented permission list, a test file, and a short audit-log sample with no sensitive content.

Acceptance criteria.

5.11 Review questions

5.12 Applied challenge

Given a tool that currently requests file read, file write, HTTP, and process capabilities, audit its code, remove everything unjustified, narrow the rest, and write deny-path tests proving the reduced set still behaves predictably.

5.13 Lesson summary

5.14 Transition to the Chapter 8 checkpoint

The next item is the Chapter 8 checkpoint, a permission-aware data utility that combines JSON and CSV handling, collections, dates, files and paths, logging, and the capability practices from this lesson.

Lesson status: Ready for review.

← Previous Next →