Check who owns a record
Let a viewer open only their own records, and a viewer with a stronger role anyone's. The access
rule admits the role, a Guard on the page checks the owner, and the app that holds the data
checks again. The example is the contacts tab of a user in the people example, at
/users/<login>/contacts, whose data the contacts app holds.
Steps
-
Ask for a
Guardin the page's access rule withguard="true". A page with an id in its URL says whose records it shows:guard="true"says that itsGuarddecides, andshared="true"that everyone the rule admits may see every record.aicoded generaterefuses such a page with neither (E-GEN-054). The framework calls a page'sGuardonly when its own<ssr:access>hasguard="true", soaicoded generatealso refuses aGuardmethod on a page without it (E-GEN-032):<ssr:access role="staff" guard="true"/> <ssr:var name="phones" type="[]string"/> <ssr:var name="emails" type="[]string"/> <h2>Phones</h2> <ul id="phones"><li ssr:for="phone in phones">{{ phone }}</li></ul> <h2>Emails</h2> <ul id="emails"><li ssr:for="email in emails">{{ email }}</li></ul> -
Run
aicoded generate, or save underaicoded dev. The page gets aGuardhook, which refuses every viewer (403) until you write it. TheGuardruns after the access rules of the whole path and before any form,Data, live value or page call of the page. On a layout or a gate, it covers every page below it. A page below needs aGuardof its own only for a check of its own, such as "only the author edits", and thenguard="true"in its own<ssr:access>too. Here the user card above,pages/users/s_login/, declares<ssr:access role="staff" shared="true"/>, since every member ofstaffsees every profile, and the contacts tab narrows it with its ownGuard. -
Write the rule: the viewer owns the record, or has the role that sees everyone's.
// Guard lets a viewer see their own contacts, and a viewer with the hr role anyone's. The // contacts app checks the same rule again when it serves the call. func (p *DP) Guard(ctx context.Context, r *web.Request) error { viewer := auth.Viewer(ctx) if viewer.Subject != r.URLParam("login") && !viewer.HasRole("hr") { return web.Forbidden() } return nil }auth.Viewer(ctx)is the viewer the company login verified, andaicoded devuses the persona you chose.Subjectis the viewer's id, here a login, andHasRolechecks a role. Returnweb.NotFound()instead ofweb.Forbidden()when a viewer must not learn that the record exists. -
Check the same rule again where the data is. contacts holds the contacts, so its function checks the viewer before it reads anything, and before it can tell whether the login exists:
// Contacts returns the contacts of a person to that person and to hr. The people app checks the // same rule in its Guard; this app checks it again, since it holds the data. // //ssr:access caller=people role=staff func Contacts(ctx context.Context, in ContactsIn) (ContactsOut, error) { if !allowed(auth.Viewer(ctx), in.Login) { return ContactsOut{}, frameworkrpc.Error(frameworkrpc.PermissionDenied, "only the person and hr may read these contacts") } phones, emails, err := deps.Contacts(ctx, in.Login) if errors.Is(err, deps.ErrNoPerson) { return ContactsOut{}, frameworkrpc.Error(frameworkrpc.NotFound, "no such person") } if err != nil { return ContactsOut{}, err } return ContactsOut{Phones: phones, Emails: emails}, nil }// allowed reports whether viewer may read the contacts of login: they are that person, or they // have the hr role. func allowed(viewer auth.Identity, login string) bool { return viewer.Authenticated() && (viewer.Subject == login || viewer.HasRole("hr")) }The page calls contacts on behalf of the viewer, so contacts sees the same viewer: Call another app shows the call.
Compare owners exactly. Both apps keep logins in columns with COLLATE utf8mb4_0900_bin, which
has no case folding and no trailing-space padding, so that the database, like the Guard, tells
Alice and alice from alice.
Check it
aicoded checkgenerates, builds, vets, lints and tests the apps. contacts testsallowedas a plain function, with no runner, and callsContactswith no viewer and no database open, so the test fails if the function ever reads before it checks.aicoded generaterecordsguard: truefor"/users/{login}/contacts"andshared: truefor"/users/{login}/info"in theaccesssection of people'saicoded.yaml, andaicoded describeshowsguardandsharednext to the pages.- With the personas alice (
staff) and bob (staffandhr): alice opens/users/alice/contactsand gets 403 on/users/bob/contacts, and bob opens both.
See also
- Access rules
- E-GEN-032: a
Guardthat noguard="true"asks for. - E-GEN-054: a page with an id in its URL that does not say whose records it shows.
- List own records, or all of them for a role: the list that goes with the check, with one constant statement for each case.
- The request pipeline
- Calls between apps