--- name: capability-based-design description: >- Capability-based design (Tribble): every distinction in authority is a different object, and narrowing never widens. Use when designing an API, SDK, service, device, or object model that others will hold pieces of; when writing attenuation or builder methods (readOnly, subdir, narrow, restrict, scoped, forUser); when splitting read from write, or use from administration; when a scoped handle checks paths, URLs, or names. Not for what one callback receives (that's facet). --- # Capability-based design Give each distinct authority its own object; holding the object is the permission. 1. **One object per authority distinction.** Reading and writing are different objects; so are using a resource and administering it. ```js // bad: one object, a mode flag, and a check file.write(data, { as: user }); // good const { reader, writer } = openFile(path); ``` 2. **Narrowing returns a new object and only intersects.** A narrowed handle's narrowing methods can't escape its own limits: "widening is a security hole". ```js // bad: the second call replaces the first onlyTypes: types => make({ ...opts, types }), // good: keep every constraint; all must hold onlyTypes: types => make({ ...opts, filters: [...opts.filters, t => types.includes(t)] }), ``` 3. **Keep limits and secrets in closures,** never on properties the holder can read or overwrite (`this.root`, `this.token`). Freeze every object you return. 4. **Check the parsed form,** the one the far side will act on, not string prefixes of the raw input. ```js // bad: 'docs/../../etc/passwd' starts with 'docs/' if (!rel.startsWith(dir)) throw Error('outside'); // good const abs = path.resolve(root, rel); if (!abs.startsWith(root + path.sep)) throw Error('outside'); ``` 5. **Separate access control from use.** The code that decides who gets which handle is not the code that uses it; the handle's methods take no identity. 6. **Identify the authoritative source.** A file's name belongs to the directory, not the file; give each fact to the object that owns it. Before finishing, write a test that plays the holder of the narrowest handle: call every method with hostile arguments and narrow it again, and check that nothing reaches outside its limits. ## Sources - Dean Tribble, *Capability-based Design: Applying Security Techniques to Object-Oriented Design* (slides, Agoric): "All distinctions in authority are represented by different objects"; "'Widening' is a security hole"; "Capability-based design also drives clear abstraction boundaries".