Authorization and ownership
Sign in to the Journal with a second account and open somebody else’s article. You can read it. What stops you from sending the editor’s request yourself?
Check the policy where the write happens
Look at the update the Journal runs when an edit is saved. It matches two things together, the article’s slug and the account that wrote it. Keeping them in the same SQL statement makes the ownership check part of the write.
Take the account from the signed-in context
There are two IDs to keep track of here. The address supplies the slug of the article the visitor wants to edit. The context supplies the account actually making the request. Read that second value with Controller.signed_in_user and use it for the ownership check.
This call goes in a controller that already takes Controller.SignedIn.
actor = Controller.signed_in_user(context)Match the article and the author in SQL
Put the author check in the update itself. If you read the article, check its owner and only then issue a separate update by slug, another request can change the row between those two statements. Matching the author in the update makes the condition apply to the row you actually change.
UPDATE articles SET title = ${title}
WHERE slug = ${slug} AND author_id = ${actor_id}
Read the affected row count or the returned row to tell whether the update took place.
Choose the response for a refusal
A public page can be readable by everyone and have different rules for editing or moderation. Decide whether the response should distinguish a missing resource from a forbidden edit. Use the variant declared in the operation.
The Journal can represent those two cases with one write response.
Api.ArticleWrite404{"Article not found or not owned by you"}Try the same edit with two accounts
Use the author’s session in one request and another account’s session in the second. Use each session’s own CSRF token so both requests reach the ownership check.
Compare the statuses and read the row after the denied request. The hidden edit button is only part of the interface. Check that the write itself was refused.
The title and author should be the same before and after the refused request.
SELECT title, author_id
FROM articles
WHERE slug = 'example-article';