-
Notifications
You must be signed in to change notification settings - Fork 0
fix(#28): reject unknown fields in POST and PATCH /items endpoints #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,11 @@ def create_item(): | |
| if not data or "name" not in data: | ||
| return jsonify(error="name is required"), 400 | ||
|
|
||
| allowed = {"name"} | ||
| unknown = set(data.keys()) - allowed | ||
| if unknown: | ||
| return jsonify(error=f"Unknown fields: {', '.join(sorted(unknown))}"), 400 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] error-message-format Error messages at lines 32 and 54 begin with uppercase 'Unknown fields' but the established codebase pattern uses lowercase (e.g., 'name is required', 'item not found'). Suggested fix: Change both to lowercase: error=f"unknown fields: {', '.join(sorted(unknown))}" |
||
|
|
||
| item = { | ||
| "id": len(_get_items()) + 1, | ||
| "name": data["name"], | ||
|
|
@@ -43,6 +48,11 @@ def update_item(item_id): | |
| return jsonify(error="item not found"), 404 | ||
|
|
||
| data = request.get_json() | ||
| allowed = {"name", "done"} | ||
| unknown = set(data.keys()) - allowed | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [medium] nil/null handling In update_item, request.get_json() can return None (empty body, wrong content type). The new validation at line 52 calls set(data.keys()) without a null guard, causing an unhandled AttributeError and HTTP 500. Unlike create_item, which guards with 'if not data or "name" not in data:' before the field validation block, update_item has no equivalent check. Suggested fix: Add 'if not data: return jsonify(error="request body is required"), 400' after 'data = request.get_json()' and before the 'allowed' set definition. |
||
| if unknown: | ||
| return jsonify(error=f"Unknown fields: {', '.join(sorted(unknown))}"), 400 | ||
|
|
||
| if "done" in data: | ||
| item["done"] = data["done"] | ||
| if "name" in data: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[medium] scope-creep
POST /items validation logic was added but is not authorized by issue #28. The issue exclusively describes the PATCH /items/:id bug. While POST exhibits the same silent-drop behavior, the fix extends beyond the linked issue's scope.
Suggested fix: Update issue #28 to include POST /items, or split the POST changes into a separate issue and PR.