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

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

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/6237
Author: @AhmdFahad
Created: 5/21/2026
Status: Closed

Base: mainHead: fix/setup-key-negative-usage-limit


📝 Commits (1)

  • ec239ae [management] reject negative usage_limit on setup key creation

📊 Changes

3 files changed (+15 additions, -0 deletions)

View changed files

📝 management/server/http/handlers/setup_keys/setupkeys_handler.go (+5 -0)
📝 management/server/http/handlers/setup_keys/setupkeys_handler_test.go (+9 -0)
📝 shared/management/http/api/openapi.yml (+1 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/6237 **Author:** [@AhmdFahad](https://github.com/AhmdFahad) **Created:** 5/21/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/setup-key-negative-usage-limit` --- ### 📝 Commits (1) - [`ec239ae`](https://github.com/netbirdio/netbird/commit/ec239aebb9ffce92f400278032d0f76de14da97f) [management] reject negative usage_limit on setup key creation ### 📊 Changes **3 files changed** (+15 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `management/server/http/handlers/setup_keys/setupkeys_handler.go` (+5 -0) 📝 `management/server/http/handlers/setup_keys/setupkeys_handler_test.go` (+9 -0) 📝 `shared/management/http/api/openapi.yml` (+1 -0) </details> ### 📄 Description ## 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 --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 07:09:18 -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#27838