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
- Explain the capability model: sensitive operations are denied unless explicitly granted.
- Derive a least-privilege permission set from a program's real behavior.
- Handle permission denial as a stable, testable outcome rather than a crash.
- Isolate privileged operations behind narrow adapters around a pure core.
- Test both allow and deny paths and review permissions for drift.
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.
| Concept | Meaning | Design consequence |
|---|---|---|
| Request | What the program or package declares it needs | Keep it minimal and documented |
| Grant | What the host or policy actually allows | May be narrower than the request |
| Denial | The runtime refuses an operation | Handle as an expected outcome |
| Audit | A record of decisions and use | Log 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.
| Operation | Resource | Needed capability | Narrowing option |
|---|---|---|---|
| Read input CSV | One input directory | File read | Restrict to a single directory |
| Write report | One output directory | File write | Separate from the input location |
| Fetch rates | One known host | HTTP | Allow-list the host |
| Parse and aggregate | In memory | None | Keep 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.
- Fail closed: never fall back to a broader operation after a denial.
- Explain precisely: name the operation and resource class, not internal paths or secrets.
- Do not retry blindly: a denial is a policy decision, not a transient fault.
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
| Mistake | Effect | Correction |
|---|---|---|
| Requesting every capability up front | Large attack surface | Derive the set from the adapters |
| Treating denial as a transient error | Pointless retries | Classify and fail closed |
| Catching all errors and continuing | Hidden policy failures | Handle denial explicitly |
| Logging secrets while auditing | Data leak | Log reason codes only |
| Testing only the allowed path | Untested denial behavior | Add deny tests for every adapter |
| Mixing I/O into core logic | Hard to test and to restrict | Move I/O into adapters |
5.8 Diagnostic workflow
- Reproduce the failure and note the operation attempted.
- Check whether the failure is a denial, a missing resource, or a limit.
- Compare the attempted operation with the granted set.
- Decide whether the code or the policy is wrong; never widen policy only to silence an error.
- Add a regression test for the decision.
5.9 Guided practice
- List the operations of a small report tool and fill in the resource, direction, and reason for each.
- Mark which operations belong in the pure core and need no capability.
- Write the minimal permission set and justify each entry in one sentence.
- Describe the stable reason code and caller behavior for a denied file read.
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.
- The core imports no privileged facility.
- Every requested capability maps to a named adapter.
- Denial never triggers a broader fallback.
- Logs contain reason codes only.
5.11 Review questions
- Why is denial treated as a policy decision rather than a transient fault?
- How does a pure core reduce the permission set?
- What should an audit event contain, and what must it exclude?
- Why should policy never be widened just to silence an error?
- How would you detect permission drift over time?
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
- Capabilities are explicit, least-privilege grants; unprivileged code can still compute.
- Derive requests from adapter behavior and keep logic in a pure core.
- Handle denial as a stable, fail-closed outcome with a reason code.
- Audit with redaction, test allow and deny paths, and review for drift.
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.