Skip to content

fix(api): normalize non-object JSON bodies to empty dict in token PATCH#3976

Merged
alteixeira20 merged 2 commits into
odysseus-dev:devfrom
michaelxer:fix-api-token-patch-non-object-3966
Jun 15, 2026
Merged

fix(api): normalize non-object JSON bodies to empty dict in token PATCH#3976
alteixeira20 merged 2 commits into
odysseus-dev:devfrom
michaelxer:fix-api-token-patch-non-object-3966

Conversation

@michaelxer

Copy link
Copy Markdown
Contributor

Summary

The /api/tokens/{token_id} PATCH route catches JSON parse failures and falls back to {}, but after a successful parse it assumes the payload is a mapping. Valid non-dict JSON (e.g. [] or null) reaches payload.get(...) and raises AttributeError, returning an unhandled 500 to the client.

Add a type check after parsing: if the payload is not a dict, normalize it to {} so the route treats it as an empty update body.

Target branch

  • This PR targets dev, not main. All PRs land in dev; main is curated by the maintainer at each release. If your PR is on main by accident, click "Edit" on this PR and change the base.

Linked Issue

Fixes #3966

Type of Change

  • Bug fix (non-breaking — fixes a confirmed issue)

Checklist

  • I searched open issues and open PRs — this is not a duplicate.
  • This PR targets dev
  • My changes are limited to the scope described above — no unrelated refactors or whitespace changes mixed in.

How to Test

  1. Run the app on current dev and authenticate as admin.
  2. Create or select an existing API token.
  3. curl -X PATCH http://localhost:PORT/api/tokens/TOKEN_ID -H "Content-Type: application/json" -d "[]" — should return the token unchanged (currently returns 500).
  4. Repeat with -d "null" and -d "\"hello\"" — same result.
  5. A normal dict body like -d '{"name":"test"}' should still work as before.

@github-actions github-actions Bot added the ready for review Description complete — ready for maintainer review label Jun 11, 2026
@michaelxer
michaelxer marked this pull request as draft June 11, 2026 18:04
@michaelxer
michaelxer marked this pull request as ready for review June 11, 2026 18:08
@michaelxer
michaelxer force-pushed the fix-api-token-patch-non-object-3966 branch from 0139df5 to 0fd29a7 Compare June 11, 2026 20:08

@alteixeira20 alteixeira20 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the small targeted fix. The implementation direction looks correct, but I would like this to include focused regression coverage before approval.

The linked issue asks specifically for coverage of valid non-object JSON bodies reaching the PATCH route, especially [] and null. Right now the PR only changes routes/api_token_routes.py, so the behavior is fixed in code but not protected against regression.

Please add focused tests, preferably in tests/test_api_token_routes.py, covering:

  • PATCH /api/tokens/{token_id} with body [] does not raise a 500;
  • PATCH /api/tokens/{token_id} with body null does not raise a 500;
  • the normal object body path still behaves as before.

After that I can re-run the focused audit and merge if everything stays green.

Valid non-dict JSON (e.g. [] or null) reaches payload.get(...) and
raises AttributeError. Normalize to {} so the route returns a controlled
response instead of an unhandled 500.

Fixes odysseus-dev#3966
Covers array body ([]), null body, and normal object body as requested
in alteixeira20's review of odysseus-dev#3976.
@michaelxer
michaelxer force-pushed the fix-api-token-patch-non-object-3966 branch from 9b0f22a to 15f5b66 Compare June 11, 2026 22:25
@alteixeira20 alteixeira20 added the bug Something isn't working label Jun 12, 2026
@michaelxer

Copy link
Copy Markdown
Contributor Author

All three regression tests are pushed and CI is green. Ready for re-review when you get a chance.

@alteixeira20 alteixeira20 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Re-audited the current head after the requested regression coverage was added. The fix is narrowly scoped to normalizing valid non-object JSON payloads on the API token PATCH route, and the added tests cover [], null, and the normal object update path. The merge probe is clean, GitHub checks are green, changed-module compile passed, and git diff --check passed. The local test_security_regressions.py failures are unrelated to this PR and caused by a missing local nh3 dependency.

@alteixeira20
alteixeira20 merged commit 5bafc30 into odysseus-dev:dev Jun 15, 2026
14 checks passed
entitycs pushed a commit to entitycs/odysseus that referenced this pull request Jun 15, 2026
…CH (odysseus-dev#3976)

* fix(api): normalize non-object JSON bodies to empty dict in token PATCH

Valid non-dict JSON (e.g. [] or null) reaches payload.get(...) and
raises AttributeError. Normalize to {} so the route returns a controlled
response instead of an unhandled 500.

Fixes odysseus-dev#3966

* test(api): add regression tests for PATCH with non-object JSON bodies

Covers array body ([]), null body, and normal object body as requested
in alteixeira20's review of odysseus-dev#3976.

---------

Co-authored-by: michaelxer <michaelxer@users.noreply.github.com>
entitycs pushed a commit to entitycs/odysseus that referenced this pull request Jun 15, 2026
…CH (odysseus-dev#3976)

* fix(api): normalize non-object JSON bodies to empty dict in token PATCH

Valid non-dict JSON (e.g. [] or null) reaches payload.get(...) and
raises AttributeError. Normalize to {} so the route returns a controlled
response instead of an unhandled 500.

Fixes odysseus-dev#3966

* test(api): add regression tests for PATCH with non-object JSON bodies

Covers array body ([]), null body, and normal object body as requested
in alteixeira20's review of odysseus-dev#3976.

---------

Co-authored-by: michaelxer <michaelxer@users.noreply.github.com>
kootenayalex pushed a commit to kootenayalex/odysseus-mlx that referenced this pull request Jun 16, 2026
…CH (odysseus-dev#3976)

* fix(api): normalize non-object JSON bodies to empty dict in token PATCH

Valid non-dict JSON (e.g. [] or null) reaches payload.get(...) and
raises AttributeError. Normalize to {} so the route returns a controlled
response instead of an unhandled 500.

Fixes odysseus-dev#3966

* test(api): add regression tests for PATCH with non-object JSON bodies

Covers array body ([]), null body, and normal object body as requested
in alteixeira20's review of odysseus-dev#3976.

---------

Co-authored-by: michaelxer <michaelxer@users.noreply.github.com>
ToyVo pushed a commit to ToyVo/odysseus that referenced this pull request Jul 7, 2026
…CH (odysseus-dev#3976)

* fix(api): normalize non-object JSON bodies to empty dict in token PATCH

Valid non-dict JSON (e.g. [] or null) reaches payload.get(...) and
raises AttributeError. Normalize to {} so the route returns a controlled
response instead of an unhandled 500.

Fixes odysseus-dev#3966

* test(api): add regression tests for PATCH with non-object JSON bodies

Covers array body ([]), null body, and normal object body as requested
in alteixeira20's review of odysseus-dev#3976.

---------

Co-authored-by: michaelxer <michaelxer@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready for review Description complete — ready for maintainer review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API token PATCH should reject non-object JSON bodies instead of 500ing

2 participants