Fix set_alias 404 handling: simplify failure check to prevent silent regression#1599
Fix set_alias 404 handling: simplify failure check to prevent silent regression#1599
Conversation
…nlocking endpoints and expand allowed column names for updates #1598
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughAdded three device endpoints and clarified POST semantics; expanded allowed device-column whitelist and renamed/changed defaults for lock/unlock request fields; set-alias endpoint now returns an explicit failure JSON when the underlying update fails. Changes
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/API_DEVICE.md`:
- Around line 209-213: The docs state the `lock` default is true but runtime
defaults to false; update the API docs to reflect the actual behavior used by
api_device_field_lock (which calls data.get("lock", False)) by changing the
`lock` field default description to indicate it defaults to false (unlock) when
omitted, and optionally add a brief note referencing api_device_field_lock to
avoid future drift.
- Around line 247-252: The docs use the camelCase key `clearAll` but the API
schema defines snake_case `clear_all` in the UnlockDeviceFieldsRequest, causing
a contract mismatch; update the API_DEVICE.md table to use `clear_all` (and
describe that it is a boolean with same semantics: true clears all sources,
false/omitted clears only LOCKED/USER), or alternatively change the schema field
in UnlockDeviceFieldsRequest from `clear_all` to `clearAll` so names match—pick
one consistent naming approach, update the docs or schema accordingly, and
ensure examples and any references to `clearAll` are replaced with `clear_all`
(or vice versa) so the documentation matches the actual OpenAPI schema.
- Around line 289-293: The docs claim set-alias returns HTTP 404 for "Device not
found" but the handler api_device_set_alias currently returns jsonify(result)
with no status change; update the handler (api_device_set_alias) so that when
the backend response has success:false and error equals "Device not found" it
returns a JSON response with HTTP status 404 (e.g., jsonify(result), status=404)
to match the docs, or alternatively update docs/API_DEVICE.md to state the
handler returns 200 with a JSON error payload—choose and implement one of these
two consistent fixes and ensure the condition checks result['success'] and
result['error'] exactly as used in the existing code.
In `@server/api_server/openapi/schemas.py`:
- Around line 35-49: ALLOWED_DEVICE_COLUMNS currently includes "devCanSleep" and
"devReqNicsOnline" which are not present in the Devices DB schema and will cause
UPDATE failures; to fix, remove those two literals from the
ALLOWED_DEVICE_COLUMNS Literal so the whitelist matches the actual schema used
by server/models/device_instance.py (the code that builds "UPDATE Devices SET
{column_name}=?"), or alternatively add corresponding columns to the Devices
schema if they are intended fields — ensure ALLOWED_DEVICE_COLUMNS and the
Devices schema remain in sync.
- Around line 40-47: ALLOWED_DEVICE_COLUMNS currently permits sensitive fields
like devForceStatus, devIsArchived, and devIsNew without per-field checks;
update the device column update handler (the /device/<mac>/update-column
endpoint implementation—e.g., the function that consumes ALLOWED_DEVICE_COLUMNS)
to enforce field-level authorization: if the requested column is one of
{"devForceStatus","devIsArchived","devIsNew"} verify the caller has an elevated
permission/role (e.g., admin or specific device-state-update scope) using the
existing token auth/permission APIs, return 403 on failure, and log unauthorized
attempts; keep existing behavior for non-sensitive columns.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 39482fe6-7f44-48b4-a65e-38c557f6eed7
📒 Files selected for processing (2)
docs/API_DEVICE.mdserver/api_server/openapi/schemas.py
…ate semantics for device fields #1597
…r device fields and enhance error handling in device alias update
… pattern Agent-Logs-Url: https://github.com/netalertx/NetAlertX/sessions/661c66ce-45e8-4f96-b51d-1bb0b918c669 Co-authored-by: jokob-sk <96159884+jokob-sk@users.noreply.github.com>
…failure Agent-Logs-Url: https://github.com/netalertx/NetAlertX/sessions/05ab18a3-4ac2-492d-bb80-67a1cc089bd9 Co-authored-by: jokob-sk <96159884+jokob-sk@users.noreply.github.com>
Fix set-alias endpoint: return HTTP 200 with normalized `error` key on failure
📌 Description
The
set_aliasendpoint had an overly-specific error check that would silently return HTTP 200 for anyupdateDeviceColumnfailure that wasn't exactly"Device not found". Simplified to match the pattern used by the genericupdateDeviceColumnendpoint.Before:
After:
🔍 Related Issues
📋 Type of Change
📷 Screenshots or Logs (if applicable)
N/A
🧪 Testing Steps
set_aliaswith a non-existent MAC → expect HTTP 404set_aliaswith a valid MAC → expect HTTP 200 with{"success": true}✅ Checklist
🙋 Additional Notes
The generic
updateDeviceColumnendpoint at line 465 already usedif not result.get("success")— this change makesset_aliasconsistent with that pattern.Summary by CodeRabbit
Documentation
Bug Fixes