[PR #6237] [management] reject negative usage_limit on setup key creation #29226

Open
opened 2026-08-05 08:07:42 -04:00 by saavagebueno · 0 comments
Owner

Original Pull Request: https://github.com/netbirdio/netbird/pull/6237

State: closed
Merged: No


Summary

  • The POST /api/setup-keys endpoint accepted negative values for usage_limit. Semantically only 0 (unlimited) or a positive integer (a cap) is meaningful — a negative cap would either produce a key flagged as immediately overused by IsOverUsed(), or silently behave like unlimited depending on downstream code paths.
  • Added a handler-side guard in management/server/http/handlers/setup_keys/setupkeys_handler.go that returns 422 Unprocessable Entity with usage_limit can not be negative, mirroring the existing expires_in check just above it.
  • Documented the constraint in the OpenAPI schema (minimum: 0 on CreateSetupKeyRequest.usage_limit).
  • Added a handler test case (Create Setup Key With Negative Usage Limit) covering the rejection path.

Test plan

  • go build ./...
  • go test ./management/server/http/handlers/setup_keys/... -count=1
  • Manual: POST /api/setup-keys with "usage_limit": -1 returns 422 and a non-negative value still succeeds.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Setup key creation now validates the usage limit parameter to prevent negative values, returning a validation error when invalid.
    • Updated API documentation to clarify that usage limits must be zero or greater.

Review Change Stack

**Original Pull Request:** https://github.com/netbirdio/netbird/pull/6237 **State:** closed **Merged:** No --- ## Summary - The `POST /api/setup-keys` endpoint accepted negative values for `usage_limit`. Semantically only `0` (unlimited) or a positive integer (a cap) is meaningful — a negative cap would either produce a key flagged as immediately overused by `IsOverUsed()`, or silently behave like unlimited depending on downstream code paths. - Added a handler-side guard in `management/server/http/handlers/setup_keys/setupkeys_handler.go` that returns `422 Unprocessable Entity` with `usage_limit can not be negative`, mirroring the existing `expires_in` check just above it. - Documented the constraint in the OpenAPI schema (`minimum: 0` on `CreateSetupKeyRequest.usage_limit`). - Added a handler test case (`Create Setup Key With Negative Usage Limit`) covering the rejection path. ## Test plan - [x] `go build ./...` - [x] `go test ./management/server/http/handlers/setup_keys/... -count=1` - [ ] Manual: `POST /api/setup-keys` with `"usage_limit": -1` returns 422 and a non-negative value still succeeds. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Setup key creation now validates the usage limit parameter to prevent negative values, returning a validation error when invalid. * Updated API documentation to clarify that usage limits must be zero or greater. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/netbirdio/netbird/pull/6237?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
saavagebueno added the pull-request label 2026-08-05 08:07:42 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#29226