Module 10 · Complete, test and operate the application
10.4 Recap, and your capstone feature
Retell the whole course as one request, then build a feature on your own: work orders for maintenance issues, with their own data, pages, Cedar policies, a forbid, and tests.
Starting point
Caretaker is complete: CRUD, authentication, Cedar authorization with a schema, nine policies and four forbids, scoped lists, grant and revoke, isolation tests, hardening, and a clean-checkout run. 131 tests pass.
Outcome
You can explain every step from a browser request to a Cedar decision without guessing, and you have a specification, a plan and hints for a feature you will build yourself. Lesson 10.5 holds a complete, verified solution; try first.
The problem
Following steps is not the same as owning them. The test of this course is whether you can add a feature that needs new data, new pages, new policies, a context and a forbid, and get the authorization right without a lesson telling you each line.
Build it
1. The whole course in one request
When Alice opens /issues/101, this is what happens, with the module that built each step:
| Step | What happens | Trusted because | Module |
|---|---|---|---|
| 1 | The browser sends GET /issues/101 with the caretaker.sid cookie |
It is not; it is input | 1 |
| 2 | express-session checks the cookie's signature and loads the session row: { "userId": 1 } |
Signed with our secret; written by us after a correct password | 4 |
| 3 | loadUser loads user 1, then loadAccess(1): memberships and active assignments |
Our database; revoked rows filtered | 4, 5 |
| 4 | The CSRF check is skipped (GET); requireLogin passes |
Deny by default for everything after it | 4 |
| 5 | The router parses "101" with parseId, and findIssue(101) loads the row with its building and organisation |
The ID is only a key; the row decides | 2, 3 |
| 6 | can(user, 'ViewIssue', issue) → buildAuthorizationCall → the mapper builds User::"1" (role sets), Issue::"101" (building, status, reporter, parent building), Building::"10" (parent organisation), Organisation::"1" |
Built only from steps 2, 3 and 5 | 6, 7 |
| 7 | Cedar evaluates the pre-parsed policies: building-read matches (Issue::"101" in Building::"10", which is in adminBuildings); no forbid matches |
Policies are reviewed files, validated at startup | 6, 7, 8 |
| 8 | evaluate returns allowed: true (decision allow, no errors); with AUTHZ_LOG=1 a line is logged |
Fails closed on any error or failure | 6 |
| 9 | The template asks UpdateIssue, DeleteIssue for the buttons; the response gets security headers and a request ID |
Buttons are hints; routes enforce | 5, 10 |
Step through it once more with recorded values:
2. The capstone: work orders
Maintenance issues lead to work orders: "replace the lift door sensor on floor 2, assigned to Dave, due 15 December". Build this feature.
Data. A work order belongs to one issue. It has a title (3 to 200 characters), an assignee (a user), a creator, a due date, and a status, open or done. Deleting an issue deletes its work orders.
Pages. The issue page lists its work orders, with a form to add one, a "Mark done" button on open work orders, and a delete button.
Rules (write them as Cedar policies):
| Action | Who | Notes |
|---|---|---|
| View a work order | Anyone who may view the issue | |
| Create a work order on an issue | Building admins of the issue's building; the issue's reporter, while the issue is not closed | Context: the proposed assignee |
| Mark a work order done | Its assignee, while it is open; building admins | |
| Delete a work order | Building admins | |
| Never | Nobody assigns a work order to themselves | A forbid, using the context |
Data rule (not a policy): the assignee must be someone who may view the issue. Hint: you already have a function that answers that.
Expected decisions with the seeded data, after you seed one work order, number 301, on issue 101, assigned to Dave and created by Alice:
| Request | alice | bob | carol | dave |
|---|---|---|---|---|
| Add a work order to issue 101, assigned to Dave | ✓ | 404 | ✓ | ✗ 403 |
| Add a work order to issue 101, assigned to yourself | ✗ 403 | 404 | ✗ 403 | ✗ 403 |
| Mark work order 301 done | ✓ admin | 404 | ✓ inherited | ✓ assignee |
| Delete work order 301 | ✓ | 404 | ✓ | ✗ 403 |
Bob gets 404 everywhere because he cannot view issue 101.
3. A plan
Work in this order, and run the tests after each step:
- Migration
007_create_work_orders.sql; seed work order 301. - Model functions: list for an issue, find by ID and issue ID, create, complete (conditional on
open), delete. - Schema: an entity type, four actions, and where they sit in the action groups.
- Mapper: an entity for a work order, and the entities a request about it needs.
- Policies: two new permits and one forbid. Check them with
npm run authz:validate. - Routes under
/issues/:id/actions, following load → authorize → act. - The issue page.
- Tests: the table above, plus "a work order ID from another issue is 404".
Hint Hint 1: the entity and its parents
Make WorkOrder in [Issue]. Then WorkOrder → Issue → Building → Organisation is one parent chain, and building-read and building-admin cover work orders, whose actions are in ReadBuildingData and AdministerBuilding, without any change: resource in principal.adminBuildings follows the chain.
Hint Hint 2: which resource for "create"?
The work order does not exist yet, so create is an action on its parent, the issue, exactly like CreateIssue is an action on a building (lesson 8.2). The proposed assignee goes into the context: { assignee: User }.
Hint Hint 3: the entities a request needs
For a request about a work order, Cedar needs the work order entity, its issue entity (the work order's parent), the issue's building and the building's organisation. Give the route a row that contains the work order and its issue, and let resourceEntities build all four.
Hint Hint 4: the template
The form needs a permission check, but the context (the assignee) is unknown until the form is submitted. Which existing action is allowed for exactly the same people who may create work orders? Compare the create rule with UpdateIssue.
Hint Hint 5: the data rule
"The assignee may view the issue" is can(assignee, 'ViewIssue', issue), with the assignee loaded like loadUser does. For the dropdown, an SQL query can reuse visibleBuildingCondition('s', 'u.id'): the second argument is inserted into the SQL as-is, so it can be a column instead of a placeholder.
Run and inspect
Before you start, check that the baseline is clean:
npm test
> caretaker@1.0.0 test
> node --env-file=.env.test --test --test-concurrency=1 --test-reporter=spec "test/**/*.test.js"
✔ view issue 101 (building 10) (478.066791ms)
✔ edit issue 101 (58.802333ms)
✔ view issue 103 (building 20) (29.927667ms)
✔ edit issue 103 (reported by dave) (30.781708ms)
✔ report an issue at building 20 (31.201833ms)
✔ view issue 105 (building 30) (24.617875ms)
✔ view issue 106 (building 40, reported by bob) (24.1155ms)
✔ approve inspection 201 (created by dave) (24.134ms)
✔ approve inspection 202 (created by alice) (23.74825ms)
✔ update organisation 1 (21.860291ms)
✔ change own role at building 10 (7.864542ms)
✔ grant building_admin at building 10 to dave (36.784042ms)
✔ grant viewer at building 10 to dave (24.470958ms)
✔ issues in each user's list come only from the expected buildings (158.409583ms)
✔ the scenario loads exactly as specified (200.290542ms)
✔ only one active assignment per user, building and role (0.8275ms)
✔ a revoked role can be granted again (1.100708ms)
✔ the My access page shows what the server loaded (70.492209ms)
✔ a correct login creates a session and shows the user (264.80025ms)
✔ a wrong password or unknown email gets the same 401 (52.959208ms)
✔ registration validates, creates a user, and logs them in (43.139917ms)
✔ registering an existing email is a field error (32.394542ms)
✔ logout destroys the session (53.461917ms)
✔ allow: a permit matched and nothing forbade it (218.928125ms)
✔ explicit deny: a forbid overrides a matching permit (2.777666ms)
✔ implicit deny: no permit matched (2.457958ms)
authz problem {
user: 1,
action: 'ApproveInspection',
resource: 'Inspection::201',
decision: 'failure',
errors: [
'entity does not conform to the schema: in attribute `status` on `Caretaker::Inspection::"201"`, type mismatch: value was expected to have type string, but it actually has type long: `7`'
]
}
✔ failure: a row that does not fit the schema is never allowed (3.157ms)
Show 112 more lines
✔ unknown actions throw instead of deciding (1.267375ms)
✔ unknown callers are refused (209.256458ms)
✔ decisions match the app (24.920916ms)
✔ attributes sent by the caller are ignored (3.349625ms)
✔ context is validated and used (10.730666ms)
✔ revoking dave's inspector role at building 10 takes effect on his next request (400.38225ms)
✔ a revoked assignment cannot be revoked again (4.728083ms)
✔ an assignment id from another building is not found here (4.286084ms)
✔ alice cannot revoke her own building_admin role; carol can (17.881084ms)
✔ grants are limited to members of the building's organisation (6.219875ms)
✔ a building can be created inside an organisation (281.069292ms)
✔ a duplicate building name in the same organisation is a field error (5.542375ms)
✔ the same building name is allowed in another organisation (8.445916ms)
✔ a building with issues cannot be deleted (15.72075ms)
✔ an empty building can be deleted (10.855834ms)
✔ an organisation with buildings cannot be deleted, an empty one can (25.078041ms)
✔ a logged-in POST without a token is refused with 403 (273.694834ms)
✔ a token from another session is refused (35.933041ms)
✔ the right token in the form field works (18.3925ms)
✔ every POST form on every page carries the token (52.829042ms)
✔ the dashboard counts match the database (281.313208ms)
✔ only the buildings carol can see appear (5.429042ms)
✔ headers that claim another identity are ignored (269.049458ms)
✔ query strings and form fields that claim roles are ignored (11.739459ms)
✔ a forged reporter or status in an edit form does not reach the database (5.789042ms)
✔ a forged grant context cannot make dave a building admin (5.13875ms)
✔ responses carry security headers and a request id, and no framework banner (205.981166ms)
✔ error pages show the request id as a reference (67.096667ms)
✔ unsafe production settings are reported (0.341292ms)
✔ create a draft, edit it, submit it, approve it (345.363708ms)
✔ invalid transitions are refused with 409 and change nothing (12.098ms)
✔ only drafts can be edited (5.473125ms)
✔ returning a submitted inspection makes it editable again (7.861458ms)
✔ concurrent approvals: exactly one wins (24.765125ms)
✔ unknown actions are 404 (3.312584ms)
✔ the issues list contains only issues from visible buildings (567.704208ms)
✔ a filter for another tenant's building returns nothing (3.973042ms)
✔ the inspections list is scoped too (10.032917ms)
✔ organisations are listed for their members only (7.838417ms)
✔ the dashboard counts only visible data (7.102708ms)
✔ view issue 101 (building 10) (489.31175ms)
✔ open the edit form for issue 101 (25.951875ms)
✔ open the edit form for issue 103 (reported by dave, building 20) (28.144125ms)
✔ view issue 105 (building 30) and 106 (building 40) (42.409709ms)
✔ report an issue at building 20 (28.774042ms)
✔ a reporter cannot edit their own issue once it is closed (8.895583ms)
✔ only building admins may delete; refusals change nothing (16.960541ms)
✔ the edit form cannot move an issue to another building (4.047416ms)
✔ the list pages 10 at a time and reports the total (298.869375ms)
✔ pages do not overlap (22.726792ms)
✔ filters combine with AND (7.093417ms)
✔ sorting by priority puts critical first when descending (7.366291ms)
✔ unknown sort keys and SQL in the query string are ignored (39.027458ms)
✔ alice may update 101 at building 10 but may not create at building 20, so she cannot move it there (350.961375ms)
✔ dave may update his own issue 103 but is not a reporter at building 10 (7.511583ms)
✔ dave cannot move issue 101: he may not update it where it is (5.095167ms)
✔ carol moves 101 to building 20, but never to another organisation (10.119292ms)
✔ a forged building id on create is refused and creates nothing (22.208917ms)
✔ GET /issues lists issues from the database (290.052875ms)
✔ GET /issues/101 shows one issue with its building (7.033708ms)
✔ GET /issues/:id answers 404 for unknown or malformed ids (14.124958ms)
✔ POST /issues re-renders the form with errors (4.908625ms)
✔ POST /issues stores the issue and redirects to it (8.162583ms)
✔ POST /issues with a building that does not exist is a form error, not a 500 (4.179583ms)
✔ the stored row has the cleaned values and ignores a client-sent status (4.296041ms)
✔ an issue time in the future is rejected (4.424625ms)
✔ GET /issues/101/edit shows the current values (4.882416ms)
✔ POST /issues/:id/edit changes the status and updated_at (4.974375ms)
✔ POST /issues/:id/delete removes the issue (11.427417ms)
✔ the issues list contains exactly the issues Cedar lets each user view (326.665584ms)
✔ the inspections list contains exactly the inspections Cedar lets each user view (20.979ms)
✔ only organisation admins can open the members page (419.681833ms)
✔ controlled onboarding: carol adds nora; nora sees the organisation but no building (14.958041ms)
✔ promoting dave to org_admin gives him the organisation's admin rights on his next request (12.309167ms)
✔ nobody changes their own membership (3.984833ms)
✔ unknown emails and roles are form errors (9.055958ms)
✔ a member cannot add members, even with a well-formed request (3.185541ms)
✔ GET /organisations lists organisations with building counts (266.801209ms)
✔ GET /organisations/1 shows its buildings, not other organisations' buildings (13.794042ms)
✔ POST /organisations validates and creates (13.315625ms)
✔ POST /organisations/:id/edit renames the organisation (10.829334ms)
✔ unknown organisations are 404 (6.618041ms)
✔ every policy is valid against the schema (32.405292ms)
✔ every action used by the app is mentioned by a policy, directly or through its group (9.382ms)
✔ failed logins are limited per client; the limit then blocks even correct passwords (349.686125ms)
✔ anonymous requests are redirected to the login page (215.369083ms)
✔ anonymous writes are refused and change nothing (10.698666ms)
✔ login and register pages stay public (5.412459ms)
✔ login issues a new session id (no session fixation) (31.913833ms)
✔ returnTo only follows local paths (28.995667ms)
✔ new issues and transitions record the logged-in user (87.537166ms)
✔ the session cookie has the attributes we chose (26.229042ms)
✔ organisation 1: view and update (476.350541ms)
✔ ravi is still a member of organisation 2 but can see none of its buildings (8.952958ms)
✔ building 10: view, update, delete (56.121834ms)
✔ creating buildings needs org_admin (14.658792ms)
✔ creating an organisation makes the creator its admin (10.786542ms)
✔ inspections: view 201, open the edit form for draft 203 (41.774791ms)
✔ approve inspection 201 (dave's): only building admins (18.872708ms)
✔ creating an inspection needs inspector or building_admin at the chosen building (13.965125ms)
✔ bob gets 404 for every Liffey route, and nothing changes (452.33775ms)
✔ ravi gets 404 for every Liffey route, and nothing changes (92.261125ms)
✔ nora gets 404 for every Liffey route, and nothing changes (87.997708ms)
✔ inside one tenant: alice, a viewer at building 20, cannot change anything there (69.868208ms)
ℹ tests 131
ℹ suites 0
ℹ pass 131
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 14821.498792
git status
On branch main
nothing to commit, working tree clean
Create a branch for your work, so that you can compare with the solution later:
git switch -c capstone
Experiment
Before writing code, predict the answers to two design questions, then check them against the hints and the solution.
building-read policy need any change to let viewers see work orders?Reveal Answer
No, if the work order's parent is its issue and ViewWorkOrder is in ReadBuildingData. Parents and action groups were built for exactly this kind of extension.
Reveal Answer
From the context, which the route builds after loading the assignee by the ID in the form and checking that they may view the issue. The context describes the request; it is not a claim about the requester's rights.
Checkpoint
You have finished the course when your branch:
- passes
npm run check, including your new tests for every cell of the table above; - answers
404to Bob for every new route, with no database change (add your routes to the isolation suite); - shows correct buttons for Alice, Carol and Dave on issue 101.
Recap and next step
You can now trace a request through every layer, say which data is trusted and why, and extend the authorization model with parents, groups, a context and a forbid. Build the capstone on your own branch. When you are done, or stuck, open lesson 10.5: a complete solution, verified by the same replay as every other lesson.
Open the checkpoint files for lesson 10.4 (separate page; only files from this lesson and earlier)