fleet icon indicating copy to clipboard operation
fleet copied to clipboard

Error handling improvements for /api/v1/fleet/os_versions endpoint

Open thisisjoegrant opened this issue 4 months ago β€’ 5 comments

Fleet versions

  • Discovered: 4.90.0
  • Reproduced: 4.90.0

Web browser and operating system: N/A

πŸ’₯ Β Actual behavior

As discovered on #35075, there are a couple incorrect or incomplete error responses from the /os_verisons endpoint, as described below:

  • [ ] Providing an invalid platform for /os_versions?platform=notrealplatform responds with a count of zero, rather than an error about the platform being invalid.
    • Response:
      {
        "meta": {
          "has_next_results": false,
          "has_previous_results": false
        },
        "count": 0,
        "counts_updated_at": "2026-07-17T15:10:56Z",
        "os_versions": []
      }
      
  • [ ] Similarly, when providing an invalid id like /os_versions/99999, the response does not give an error message, just the standard json response full of nulls, blanks, and zeroes.
    • Response:
      {
        "counts_updated_at": null,
        "os_version": {
          "os_version_id": 0,
          "hosts_count": 0,
          "name": "",
          "name_only": "",
          "version": "",
          "platform": "",
          "vulnerabilities": null,
          "vulnerabilities_count": 0
        }
      }
      
  • [ ] When providing an invalid max_vunerabilities such as /os_versions?max_vulnerabilities=-5, there is an unencoded character in the reason: "reason": "max_vulnerabilities must be \u003e= 0"

πŸ› οΈ Expected behavior

As with the other error message provided by these endpoints, expected an error state and a reason for each, with characters encoded correctly.

πŸ§‘β€πŸ’» Β Steps to reproduce

These steps:

  • [x] Have been confirmed to consistently lead to reproduction in multiple Fleet instances.
  • [ ] Describe the workflow that led to the error, but have not yet been reproduced in multiple Fleet instances.
  1. See examples in Actual Behavior section

thisisjoegrant avatar Jul 17 '26 16:07 thisisjoegrant

I have an open PR for this: #49899 β€” all checks are passing. Could I be assigned so it doesn't get picked up twice while it sits in Ready?

One decision is needed before it can merge. The single-version handler previously swallowed the datastore's not-found error and returned an empty result on purpose, with the comment "It is possible the os version exists, but the aggregation job has not run yet." My PR removes that so an unknown id returns 404.

@noahtalerman @sharon-fdm could you confirm 404 is the behaviour you want here, or should the empty result be preserved for the not-yet-aggregated case? Happy to adjust either way.

Also worth flagging: two of the three fixes change responses that were previously 200 into 422 and 404, so this is a breaking change for any client relying on the old shape.

Dhvanit41 avatar Jul 30 '26 04:07 Dhvanit41

could you confirm 404 is the behaviour you want here, or should the empty result be preserved for the not-yet-aggregated case? Happy to adjust either way.

Thanks @Dhvanit41!

Up to @rachaelshaw

cc @sharon-fdm

noahtalerman avatar Jul 30 '26 17:07 noahtalerman

@rachaelshaw PR looks good. Leaving this in Waiting until we are g2g with the decision on returning 404 on /os_versions/<NON_EXISTENT_ID> (which makes sense to me).

lucasmrod avatar Aug 04 '26 14:08 lucasmrod

Branch: rc-minor-fleet-v4.91.0 Β· Server: 4.91.0-rc.2608181727 Β· Validated: 2026-08-18 Fix: PR #49899 (commit a6b541d029), merged to main 2026-08-06; commit confirmed on rc-minor-fleet-v4.91.0. Method: code review (server/service/hosts.go) + live API reproduction of all 3 cases + positive regression checks + unit test.

Fix summary (verified in source):

  • OSVersions / OSVersion in server/service/hosts.go: added platform allow-list validation; reworded negative max_vulnerabilities message to avoid the HTML-escaped >; removed the not-found swallow in the single-version handler so a missing id now surfaces the datastore not-found error.

Live reproduction (running RC server)

# Case Before (reported) Expected Actual Result
1 GET /os_versions?platform=notrealplatform 200, count: 0 validation error listing platforms 422 β€” platform: Invalid platform: must be one of "darwin", "windows", "linux", "chrome", "ios", "ipados", or "android". βœ…
2 GET /os_versions/99999 200, null/zero object not-found error 404 β€” "Resource Not Found" / "OSVersion was not found in the datastore" βœ…
3 GET /os_versions?max_vulnerabilities=-5 reason contained escaped \u003e= readable, correctly-encoded reason 422 β€” max_vulnerabilities cannot be negative (no escaped char) βœ…
4 Positive: ?platform=darwin β€” still 200 200 βœ…
5 Positive: ?max_vulnerabilities=5 β€” still 200 200 βœ…
6 Unit test TestOSVersionsErrorHandling (server/service) β€” pass ok (all 3 cases covered) βœ…
7 Fix in RC branch β€” commit on rc-minor-fleet-v4.91.0 a6b541d029 contained βœ…

Verdict: Fixed and shipping in 4.91.0. All three invalid inputs now return proper error states with correct status codes (422/404/422) and clean messages; valid requests unaffected.

xpkoala avatar Aug 19 '26 17:08 xpkoala

Requests crash on rocks clear error messages guide them safe harbor awaits

fleet-release avatar Sep 02 '26 22:09 fleet-release