Move permission checks out of the interface
Learn backend authorization with an organization wiki, scoped roles, direct endpoint tests and checks for alternate data paths.

When moving from frontend into backend work, learn authorization as a decision about a requested resource. The browser may hide controls to make the interface clear, but the server must decide whether the identified caller may perform the requested action. I find a permission matrix more useful than beginning with a collection of middleware names.
Use a hypothetical organization wiki. Members can read published pages in their organization. Editors can also change drafts. An administrator can manage membership. A page belonging to another organization remains inaccessible even when the caller has an editor role in their own organization.
Define the resource and action together
A role name is incomplete without a scope. “Editor” might mean editor of one document, one workspace or every workspace in a system. Write down the scope before implementing the check.
For the wiki exercise, use these cases:
| Caller | Requested action | Expected decision |
|---|---|---|
| Member of organization A | Read a published A page | Allow |
| Member of organization A | Edit an A draft | Deny |
| Editor of organization A | Edit an A draft | Allow |
| Editor of organization A | Read an organization B page | Deny |
| Former member of organization A | Read an A page after removal | Deny under the chosen revocation rule |
This is a simplified product policy. Public pages, guests and legal retention could introduce additional rules. Leaving those features outside the exercise keeps the initial matrix small enough to inspect.
The OWASP authorization cheat sheet distinguishes authorization from authentication and recommends validating permissions on every request. A valid identity tells you who the caller is. It does not automatically grant access to every object the caller can name.
The organization check stays in the path even for an editor. That prevents a local role from accidentally becoming a system-wide permission merely because the implementation checks the role and forgets the resource boundary.
Test the endpoint independently of the screen
Build the interface so a member sees the actions they can use. Then send requests directly to the endpoint with fictional exercise accounts. Change the page identifier to one from another organization. Attempt an edit with a read-only member. Remove a member and repeat the earlier request under the documented session and revocation behavior.
These tests should inspect both the response and the protected state. A denied write must not still update the page. A read failure should not return the protected body in an error field or another response property. The visible HTTP status is part of the evidence, but the content and side effects matter too.
The MDN HTTP overview gives the request-response model used by these checks. The application must decide how to represent denial and whether a response should reveal that a particular object exists. Those choices can depend on the product and threat model; do not copy a status code without understanding the information it exposes.
Locate every path to the same data
A wiki page may be available through a detail endpoint, search results, an export and a background notification. Protecting the detail page does not establish that the other paths enforce the same policy. List the routes that can return the page or act on it.
For the learning exercise, add one alternate path, such as search. An editor in organization A should not find the title or snippet of a restricted organization B page through a broad query. This introduces an important data-access question: can the query enforce the resource scope before returning candidates?
A later background export raises a timing question. If access is removed after a job is requested, may the user still download the result? There is no universal answer hidden in an authentication library. State the product policy and test the point at which permission is evaluated.
Keep the policy understandable
A complicated permission system can become difficult to review. Start with a small set of resources and actions, and keep the decision close enough to the data model that its scope is visible. Reuse policy code when doing so preserves a clear contract, but avoid a generic helper that turns every resource into an untyped string and loses context.
The tradeoff is that explicit policies can initially look repetitive. Some repetition may be useful while resource semantics differ. A wiki page, organization membership and billing action do not necessarily share the same authorization rule just because they all have an owner field.
For a final learning check, ask another developer to predict all five matrix results from the policy description before reading the implementation. Then run the requests and compare the observations. A mismatch means either the policy was unclear or the implementation did not enforce it. Both are useful findings to resolve before adding more roles.
Give the decision trusted inputs
The endpoint needs more than a page identifier and a role string. It needs an authenticated subject, the action being attempted, the stored resource and the subject’s applicable membership. A request may name an organization, but that name is not evidence that the caller belongs to it. Likewise, accepting role: "editor" from a request body would let the caller supply the conclusion the server is supposed to decide.
For this exercise, resolve the page and its organization from application storage. Look up the caller’s current membership for that organization. Then evaluate the action against that membership and the page state. You may combine these steps in a scoped query, but the explanation should still identify which facts came from trusted application state and which values merely selected the requested operation.
OWASP’s deny-by-default guidance matters when a new action appears. A helper that recognizes read and edit should not quietly allow delete because delete falls through its conditions. Make an unknown action produce a denial until the product defines its rule.
Exercise policy changes without a browser
A small pure function can make the chosen page policy inspectable. This model covers page actions only: members can read published pages; editors can also read and edit drafts. Membership administration is a different resource and is deliberately outside this function. The dictionaries stand in for trusted storage in the exercise; they are not request bodies.
def may_access(user_id, action, page, memberships):
role = memberships.get((user_id, page["org_id"]))
if role not in {"member", "editor"}:
return False
if action == "read":
return page["state"] == "published" or (
role == "editor" and page["state"] == "draft"
)
if action == "edit":
return role == "editor" and page["state"] == "draft"
return False
memberships = {("ana", "A"): "member", ("eli", "A"): "editor"}
a_draft = {"id": "a1", "org_id": "A", "state": "draft"}
a_live = {"id": "a2", "org_id": "A", "state": "published"}
b_live = {"id": "b1", "org_id": "B", "state": "published"}
assert may_access("ana", "read", a_live, memberships)
assert not may_access("ana", "edit", a_draft, memberships)
assert may_access("eli", "edit", a_draft, memberships)
assert not may_access("eli", "read", b_live, memberships)
assert not may_access("eli", "delete", a_draft, memberships)
del memberships[("eli", "A")]
assert not may_access("eli", "read", a_live, memberships)
The final assertion models a fresh membership lookup after removal. It does not prove a deployed application’s sessions or permission caches are invalidated. That needs a separate integration case with the real identity and data-access path. A passing unit model is useful because it makes the intended answers precise; it cannot establish that every endpoint actually calls the policy with the right inputs.
Keep the policy connected to the write
Even a correct decision can become stale before a mutation. Suppose an editor is removed after the handler checks membership but before it updates a draft. Decide whether the product permits an operation already authorized to finish or requires removal to win against in-flight edits. Those are different concurrency contracts. A fresh lookup at request start does not settle the later race.
The implementation may need a transactional relationship between the relevant state and the write, or another explicitly versioned policy mechanism. The correct choice depends on the database and revocation requirements. Do not attach the word “immediate” to revocation without identifying the point at which operations are ordered and how a competing request is handled.
For search, apply scope before returning titles, snippets and counts. Filtering a page of results afterward can also make pagination misleading, even if the protected text is eventually removed. For an export, follow the same resource policy through generation and delivery. Reusing a helper is only useful when those callers supply the actual resource and action it was designed to evaluate.
At the learning checkpoint, keep three different forms of evidence: the policy answers, direct endpoint behavior and protected-state observations. Change one membership and rerun the relevant cases. That exercise teaches the connection between product policy and enforcement more effectively than counting how many routes have middleware attached.