The rule
A trip has an owner, editors and viewers. Editors can change the plan. Only a site administrator can publish a trip so anyone can read it, or take it back down.
The “yes” that wasn’t true
The server refused to publish for anyone else if the request said is_published was True, "true", "1" or 1.
The framework that reads the request is more generous. It also accepts "True", "yes", "on", "y" and "t", and treats them all as true. So a request saying "yes" walked past the check, and was then saved as published.
Three things followed, all confirmed by tests against the old code:
- An editor could publish my draft, and anyone could then read it without signing in.
- Nothing checked the other direction at all, so a collaborator could unpublish a public trip.
- The same check wrongly refused an editor’s ordinary save if it echoed back
truefor a trip that was already published.
The lines that decide
Before, the check read the raw request, word for word:
if request.data.get('is_published') in (
True, 'true', '1', 1) \
and not request.user.is_superuser:
raise PermissionDenied(
"Only superusers may publish itineraries.")
Now it reads the value after the framework has parsed it, so every spelling arrives as the same True or False. And it only cares about a change:
def _guard_publication_change(self, serializer):
if 'is_published' not in serializer.validated_data:
return
requested = serializer.validated_data['is_published']
current = serializer.instance.is_published
if requested != current and not self.request.user.is_superuser:
raise PermissionDenied(
"Only superusers may publish or unpublish itineraries.")
Comparing with the saved value does two things. Sending the current value back is not a change, so normal edits work. And a change in either direction, publish or unpublish, is refused. The row being compared is the one the save already locked, so another save can’t slip in between.
The same review found more
- An editor could delete someone else’s trip. Deleting used the same permission as editing. Now only the owner can delete.
- Access outlived removal. After someone was removed from a trip, their saved and liked lists could still show it, and their notifications could still show its private details. Now access is checked again every time those are read.
- Old invitations still worked. An invitation could be accepted after its person was removed, and re-inviting an editor as a viewer could leave them as an editor. Now removal and role changes update the invitation too.
The common thread: every check happened once, at the door. These fixes check again where the data is read or changed, and lock the row first so a permission removed mid-request is still respected.
Decisions and tradeoffs
- Check the meaning, not the words
- Adding
"yes"to the list would have fixed the one case and left the next spelling open. Reading the parsed value covers every spelling and every request format at once. - Old invitations left alone
- Old records don’t say whether a removal was meant to cancel an invitation. I chose not to rewrite them in bulk without knowing, and made the rules safe going forward instead.
- Hide, don’t explain
- Someone without access is told a trip doesn’t exist, rather than that it’s private, so its existence isn’t confirmed.
How it was tested
The publish tests try every role, from anonymous to administrator, against creating, replacing and editing a trip, in both request formats, with every spelling of true and false the framework accepts. Each case also checks that a refused request saved nothing else.
I reran each new suite against the code just before its fix, and again after. A suite that passes before the fix proves nothing, so the “before” column matters.
| Suite | Covers | Result |
|---|---|---|
| Publishing | Every role, request type and spelling | Before: 226 failing cases. After: 14 of 14 passed |
| Ownership and roles | Deleting, re-inviting, removal, locks under concurrency | Before: 30 failing cases. After: 31 of 31 passed |
| Saved and liked lists | Access removed, then restored | Before: 30 failing cases. After: 18 of 18 passed |
Outcome and limits
All of these are in production. I found no sign that any of them was used by anyone, and I can’t prove they weren’t. The site hasn’t had an outside security review.
What I took from it: test the input the way the framework reads it, not the way you expect people to type it.