📚 Encyclopedia

Security & Cryptography · updated 2026-11-02

API authorization from the verified identity (and the holes it closes)

authorizationjwtidorprivacynotificationstokensaudit

The single most common security bug found in the TLDR API was the same pattern everywhere: a route decided who you are from a username field in the request body or query string instead of from the verified token. Fixing it route by route closed admin takeover, edit-anyone, and a dozen IDORs. This article is the checklist that came out of a five-agent audit (v1.31.0, July 2026) and later bug hunts.

The rule

Authentication middleware verifies the JWT and puts the identity on the request (authorized_username, authorized_role). Every authorization decision reads those attributes:

$me   = $request->getAttribute('authorized_username') ?? '';
$role = $request->getAttribute('authorized_role', 'user');

A username in the body is only legitimate where it is the credential (login, register) or the target of an action that is separately authorized (an admin acting on another user).

Holes this pattern produced (all real, all fixed)

Privacy belongs in the query, not in a response filter

When private posts leaked through a detail endpoint, a "response middleware that strips private posts from all JSON" was proposed and rejected: it pattern-matches JSON shapes, sits at the wrong layer, hides behavior, and cannot tell contexts apart. Instead each query carries the rule, e.g. AND (p.privacy = 'public' OR p.username = :me), and detail endpoints 404 (not 403) on private posts the caller cannot see. 404 avoids confirming that the id exists.

Every side channel needs the same check

Notifications and pushes that link to a post leaked private titles: a scheduled-post cron notified all followers regardless of privacy, fork notifications told the source author about a private fork, and reply @mentions never checked visibility. One inbox had 65 entries of which 61 could not be opened. The fix pattern: - A single helper canView($postId, $user, $role), the code twin of the SQL visibility clause, plus the admin bypass; deleted or missing posts return false. - Call it before creating any notification or push that references a post. - Also filter on read (GET /notifications joins the linked post and drops what the caller cannot view), which hides historical bad rows without a risky delete.

Admin does not mean "sees everything" (posts #1882, #1883)

Tokens and secrets

Where the token lives (no cookies)

TLDR keeps users signed in with a JWT rather than a cookie (post #1829):

Gotchas

See also

Linked from

Sources: tldr memory (jwt-auth, release-v1.31.0-audit, bug-notification-privacy-leak, feature-change-password, audit-ai-permission-surface), bug posts #1628, #1636, tldr memory (feature-su-privacy, fix-post-versions-authz), posts #1882, #1883