A feed with hidden names
Security · Difficulty 3/5 · Python · 26 linesOrigin: Real project (anonymized)
Task
What the AI was asked to do
Write the code behind the community site's public GET /feed endpoint that returns published posts, newest first. Posts can mention members by name in the title and the body. While the site setting hide_names is on, no member's name may appear in the feed: the author is shown as "A member" and the post's anonymous version (a title and body written without names) is used. anonymous is an optional field: older posts have it, newer ones may not.
Handoff note
What the AI wrote when it handed over the code:
“Privacy mode is in place: while names are hidden, the author shows as "A member" and the anonymous version of the post is used. Records without an anonymous version still work without errors.”
The code the AI wrote
Python26 linesMark the lines you think are wrong; give each mark a severity and a one-sentence reason.
Click or tap a line. With a keyboard, move to the line and press Enter.
How to pick a severity
- CriticalData loss, a security hole or lost money
- MajorThe user gets a wrong result in a case the task asks for
- MinorA rare edge case outside the task, or readability
In the Testing topic, severity is set by the effect of the broken implementation the test doesn't catch.
Your reason isn't graded; you'll compare it with a sample answer.
HIDDEN_AUTHOR = "A member"def public_post(post: dict, hide_names: bool) -> dict: """Shape one post for the public feed.""" if not hide_names: return { "id": post["id"], "author": post["author"], "title": post["title"], "body": post["body"], } anonymous = post.get("anonymous") or {} return { "id": post["id"], "author": HIDDEN_AUTHOR, "title": anonymous.get("title", post["title"]), "body": anonymous.get("body", post["body"]), }def public_feed(posts: list, hide_names: bool, limit: int = 20) -> list: """GET /feed: the newest published posts, newest first.""" published = [post for post in posts if post["published"]] published.sort(key=lambda post: post["published_at"], reverse=True) return [public_post(post, hide_names) for post in published[:limit]]The test that proves it
The right option is the test that fails on this code, which means it exposes the bug.
Submitting sends seven pieces of information to a counter; your reason text stays on your device. Data