[TT-17585] MinTokenLength: apply the default floor of 3 to the create-time guard#8346
Conversation
…-time guard The create-time custom-key guard added in TT-17065 only fired when min_token_length was explicitly configured (`minLen > 0`), while the auth-time gate (CheckSessionAndIdentityForValidKey) falls back to a minimum of 3 when min_token_length is unset (issue #1681). This left a gap at default config: a 1-2 char custom key ID was accepted at create (200) but unreachable at auth (403/AKI) — the same silent-accept trap TT-17065 closed, just at the default-config boundary reported in TT-17585. Mirror the auth gate's fallback in handleAddOrUpdate so the create guard uses the same effective floor of 3 when min_token_length is unset. The Dashboard /api/keys path inherits this automatically: it proxies to the gateway's /tyk/keys and bubbles up the 4xx (verified on 5.8.15-rc1). - gateway/api.go: fall back to minLen=3 when MinTokenLength is 0. - gateway/api_test.go: add TestKeyHandler_DefaultMinTokenLengthFloorOnCreate (custom ID len 2 -> 400, len 3 -> 200 at default/unset min_token_length). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KdwVB9D3hGDjeTR58pWZMn
🎯 Recommended Merge TargetsBased on JIRA ticket TT-17585: [Innersource] Create custom key API doesn't respect default minimum token length Fix Version: Tyk 5.8.15Required:
Fix Version: Tyk 5.13.1Required:
Fix Version: Tyk 5.14.0Required:
Recommended:
📋 Workflow
|
|
This PR addresses a bug where custom API keys with IDs shorter than 3 characters could be created but would subsequently fail authentication. The fix aligns the validation logic at key creation with the existing authentication logic by enforcing a default minimum token length of 3, even when not explicitly configured. Files Changed AnalysisThe changes are focused and well-contained across three files:
Architecture & Impact Assessment
Flow Diagram: Before vs. AftersequenceDiagram
participant Client
participant Gateway API
rect rgb(255, 230, 230)
note over Client, Gateway API: Old Behavior (min_token_length unset)
Client->>Gateway API: POST /tyk/keys/ab (2-char ID)
Gateway API-->>Client: 200 OK (Key created)
Client->>Gateway API: Use key "ab" for auth
Gateway API-->>Client: 403 Forbidden (Key unusable)
end
rect rgb(230, 255, 230)
note over Client, Gateway API: New Behavior (min_token_length unset)
Client->>Gateway API: POST /tyk/keys/ab (2-char ID)
Gateway API-->>Client: 400 Bad Request (Rejected upfront)
end
Scope Discovery & Context ExpansionThe issue stemmed from a discrepancy between two key functions within the gateway:
This PR resolves this inconsistency by creating a shared source of truth for the validation logic ( Metadata
Powered by Visor from Probelabs Last updated: 2026-06-25T10:43:26.148Z | Triggered by: pr_updated | Commit: f7e312f 💡 TIP: You can chat with Visor using |
✅ Security Check PassedNo security issues found – changes LGTM. ✅ Performance Check PassedNo performance issues found – changes LGTM. ✅ Security Check PassedNo security issues found – changes LGTM. \n\n \n\n✅ Performance Check PassedNo performance issues found – changes LGTM. \n\n✅ Quality Check PassedNo quality issues found – changes LGTM. Powered by Visor from Probelabs Last updated: 2026-06-25T10:43:23.446Z | Triggered by: pr_updated | Commit: f7e312f 💡 TIP: You can chat with Visor using |
The default floor of 3 (issue #1681) was duplicated in the auth-time gate (middleware.go) and the create-time guard (api.go) — the exact divergence that caused TT-17585. Hoist it into a single defaultMinTokenLength const plus an effectiveMinTokenLength helper used by both sites, so the read-config-then-fallback logic can no longer drift between the two. Pure refactor, no behaviour change; both min-token tests remain green. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KdwVB9D3hGDjeTR58pWZMn
|
SentinelOne CNS Hardcoded Secret Detector SentinelOne CNS is a cloud-agnostic, agentless CSPM & CWPP solution that continuously detects and prevents vulnerabilities that have the highest probability of being exploited in Azure, AWS, Google Cloud, and Kubernetes. |
🚨 Jira Linter FailedCommit: The Jira linter failed to validate your PR. Please check the error details below: 🔍 Click to view error detailsNext Steps
This comment will be automatically deleted once the linter passes. |
|
|
/release to release-5.8 |
|
✅ Cherry-pick successful. A PR was created: #8371 |
|
/release to release-5.8.15 |
|
✅ Cherry-pick successful. A PR was created: #8372 |
|
/release to release-5.13 |
|
✅ Cherry-pick successful. A PR was created: #8373 |
|
/release to release-5.13.1 |
|
✅ Cherry-pick successful. A PR was created: #8374 |
|
/release to release-5.14 |
|
/release to release-5.14.0 |
|
✅ Cherry-pick successful. A PR was created: #8375 |
|
✅ Cherry-pick successful. A PR was created: #8376 |



What
Follow-up to TT-17065. The create-time custom-key guard added there only engages when
min_token_lengthis explicitly configured (minLen > 0). The auth-time gate,CheckSessionAndIdentityForValidKey, falls back to a minimum of 3 whenmin_token_lengthis unset (issue #1681). The two used different effective floors at default config, leaving a gap:min_token_lengthA 1–2 char custom key ID at default config was accepted at create but unreachable at auth — the same silent-accept trap TT-17065 set out to close, just at the default-config boundary. Reported in TT-17585.
Change
gateway/api.go— inhandleAddOrUpdate's POST (create) branch, apply the samemin_token_length == 0 → 3fallback the auth gate uses, so the create guard rejects custom IDs shorter than the effective floor:The Dashboard
/api/keyspath inherits this automatically — it proxies to the gateway's/tyk/keysand bubbles up the 4xx, so no Dashboard change is needed (TT-17585 was originally filed as a Dashboard gap; it is not).Tests
TestKeyHandler_DefaultMinTokenLengthFloorOnCreate: at default/unsetmin_token_length, custom ID len 2 →400, len 3 →200.TestKeyHandler_RejectCustomKeyBelowMinTokenLength(explicitMinTokenLength=32) unchanged and still passes.TestKeyHandler*group passes. A repo-wide search confirmed no existing test creates a 1–2 char custom key ID at default config (shortest is"user", 4 chars), so nothing else is affected.Backwards compatibility
POST /tyk/keys/<id>creates with a 1–2 char custom ID at default config change behaviour (now400instead of200). Such keys were already unusable — they403at auth — so no previously-working key creation is affected.min_token_length, autogenerated creates (POST /tyk/keys/create), PUT/update, and all existing keys are unchanged.Verification on a real stack
Reproduced and validated on a matched 5.8.15-rc1 tyk-demo (gateway-ee + dashboard):
min_token_length=32: gatewayPOST /tyk/keys/ee→400; dashboardPOST /api/keys/ee→403(gateway message bubbled up).200, then403at traffic. This PR closes that window.Refs: TT-17585, TT-17065 (#8154)
🤖 Generated with Claude Code
https://claude.ai/code/session_01KdwVB9D3hGDjeTR58pWZMn