Fix error handling on the os_versions API endpoint - #49899
Conversation
Previously /os_versions returned misleading successful responses for invalid input. Now: - an invalid `platform` filter returns a 422 validation error - an unknown OS version id returns a 404 not-found error - the max_vulnerabilities validation message no longer contains ">", which the JSON encoder was escaping to "\u003e" Adds TestOSVersionsErrorHandling covering all three cases. Resolves fleetdm#49483
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
WalkthroughUpdated the OS versions service to reject unsupported platform filters and negative 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ast-grep (0.44.1)server/service/integration_enterprise_test.goast-grep timed out on this file 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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #49899 +/- ##
==========================================
- Coverage 67.92% 67.92% -0.01%
==========================================
Files 3920 3906 -14
Lines 249774 249745 -29
Branches 13300 13334 +34
==========================================
- Hits 169663 169629 -34
+ Misses 64837 64834 -3
- Partials 15274 15282 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Adjusts existing integration tests that asserted the previous behavior:
- GET /os_versions/{id} with a missing id now expects 404 (was 200 with an empty object)
- the max_vulnerabilities validation message is now "cannot be negative"
Two team-scoped single-version lookups (an OS version id requested for a team that has no hosts, and by a user without access to the host's team) now return 404 instead of a 200 with an empty object. One of these cases already had a code comment saying it "should get 404".
lucasmrod
left a comment
There was a problem hiding this comment.
Looks good. Thanks for the contribution!
|
Change approved by @rachaelshaw (during our standup call) |
Related issue: Resolves #49483
What & why
The
/os_versionsAPI endpoint returned misleading success responses for three invalid inputs. This PR makes each return a proper error:Invalid
platformfilter (e.g.?platform=notrealplatform) previously returnedcount: 0with200 OK, indistinguishable from "no matching OS versions." It now returns a422validation error listing the supported platforms (darwin,windows,linux,chrome,ios,ipados,android— matching the documented filter values).Unknown OS version id (e.g.
/os_versions/99999) previously returned200 OKwith a null/zero-filledos_versionobject. It now returns a not-found (404) error.Negative
max_vulnerabilities(e.g.?max_vulnerabilities=-5) returned a message readingmust be >= 0— Go's JSON encoder HTML-escapes>. The message is reworded tomax_vulnerabilities cannot be negative, which is clearer and avoids the escaped character.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." This PR removes that swallow so a missing id returns
404. If you'd prefer to preserve the empty-result behavior for the "not yet aggregated" case, I'm happy to adjust — flagging so the change is intentional and visible.Checklist for submitter
changes/.SELECT *is avoided, SQL injection is prevented.Testing
TestOSVersionsErrorHandlinginserver/service/hosts_test.go, covering all three cases).Summary by CodeRabbit
max_vulnerabilitiesvalidation now rejects negative values with an accurate, readable message and consistent HTTP 422 responses.