Skip to content

fix(client): return Detail on non-JSON 2xx for api_tokens and honeytoken-with-context#184

Merged
benjaminrigaud-gg merged 1 commit into
masterfrom
benjaminrigaud/end-590-harden-api-tokens-non-json
Jun 23, 2026
Merged

fix(client): return Detail on non-JSON 2xx for api_tokens and honeytoken-with-context#184
benjaminrigaud-gg merged 1 commit into
masterfrom
benjaminrigaud/end-590-harden-api-tokens-non-json

Conversation

@benjaminrigaud-gg

Copy link
Copy Markdown
Contributor

Summary

api_tokens() and create_honeytoken_with_context() called resp.json() behind a bare resp.ok, so a 2xx response with a non-JSON body — e.g. the dashboard SPA's HTML, returned when the instance URL is wrong — crashed callers with a raw requests.exceptions.JSONDecodeError instead of returning a Detail.

This is the root cause behind the ggshield api-status crash fixed defensively in GitGuardian/ggshield#1312.

Fix

Gate JSON parsing on both the response status and content type via the existing is_ok() / is_create_ok() helpers — exactly what every other endpoint already does. A non-JSON body now falls through to load_detail() and a Detail is returned.

  • api_tokens()is_ok(resp) (200 + JSON)
  • create_honeytoken_with_context()is_create_ok(resp) (201 + JSON), matching create_honeytoken()

The existing test_api_tokens success case mocked a 201; corrected to the 200 the endpoint actually returns (required by is_ok).

Test plan

  • New tests: a non-JSON 2xx body returns a Detail (not a raised JSONDecodeError) for both api_tokens() and create_honeytoken_with_context()
  • Existing success/error tests for both endpoints still pass
  • Full suite: 125 passed; black clean

@linear

linear Bot commented Jun 22, 2026

Copy link
Copy Markdown

END-590

@codecov-commenter

codecov-commenter commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.81%. Comparing base (2c89d05) to head (d1486fc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #184      +/-   ##
==========================================
+ Coverage   95.75%   95.81%   +0.06%     
==========================================
  Files           5        5              
  Lines        1483     1483              
==========================================
+ Hits         1420     1421       +1     
+ Misses         63       62       -1     
Flag Coverage Δ
unittests 95.81% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ken-with-context

api_tokens() and create_honeytoken_with_context() guarded JSON parsing
with a bare resp.ok, so a 2xx response with a non-JSON body (e.g. an HTML
page served when the instance URL is wrong) crashed callers with a raw
JSONDecodeError instead of returning a Detail.

Gate JSON parsing on the response status and content type via the
existing is_ok()/is_create_ok() helpers, matching every other endpoint,
so a non-JSON body falls through to load_detail() and a Detail is
returned. Fix the api_tokens success test to use the 200 the endpoint
actually returns.
@benjaminrigaud-gg benjaminrigaud-gg force-pushed the benjaminrigaud/end-590-harden-api-tokens-non-json branch from 2695fd9 to d1486fc Compare June 23, 2026 11:28
@benjaminrigaud-gg benjaminrigaud-gg merged commit 75e8147 into master Jun 23, 2026
19 checks passed
@benjaminrigaud-gg benjaminrigaud-gg deleted the benjaminrigaud/end-590-harden-api-tokens-non-json branch June 23, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants