Skip to content

fix(server): correct Kubernetes label key length validation#398

Merged
Pangjiping merged 1 commit intoalibaba:mainfrom
zerone0x:fix/metadata-label-key-length-validation
Mar 11, 2026
Merged

fix(server): correct Kubernetes label key length validation#398
Pangjiping merged 1 commit intoalibaba:mainfrom
zerone0x:fix/metadata-label-key-length-validation

Conversation

@zerone0x
Copy link
Contributor

Summary

The _is_valid_label_key function in server/src/services/validators.py incorrectly rejected valid Kubernetes label keys when the total key length (prefix + / + name) exceeded 253 characters — even if the prefix alone was within the allowed 253-character limit.

Root Cause

The old guard condition:

if len(key) > 253 or "/" in key and len(key.split("/", 1)[0]) > 253:

was evaluated (due to Python operator precedence) as:

if (len(key) > 253) or ("/" in key and len(prefix) > 253):

The first clause len(key) > 253 applied to all keys, including prefixed ones. Because of this, a key whose prefix was valid (≤ 253 chars) but whose combined length exceeded 253 would be incorrectly rejected.

Fix

Per the Kubernetes label key specification:

  • The prefix (DNS subdomain) may be at most 253 characters.
  • The name segment may be at most 63 characters.
  • The total combined key length has no specified upper bound (theoretical max: 253 + 1 + 63 = 317 chars).

The fix removes the total-length check and uses an explicit len(prefix) > 253 check instead.

Testing

All existing tests continue to pass. New regression tests are added for:

  • A valid key with a 251-char prefix and total length > 253 (previously rejected — now correctly accepted).
  • A prefix that exceeds 253 chars (correctly rejected).
  • Other invalid key/value forms: invalid prefix format, name > 63 chars, value > 63 chars, non-string key, empty prefix.
uv run pytest tests/test_validators.py -v
# 54 passed

Checklist

  • Bug fix (no breaking changes)
  • Added regression tests
  • All existing tests pass

The previous implementation of _is_valid_label_key checked whether the
total key length exceeded 253 characters. This incorrectly rejected
valid label keys where the prefix portion was within the allowed 253-char
limit but the combined prefix+"/"+name exceeded 253 characters.

According to Kubernetes label key constraints:
- The optional prefix must be a DNS subdomain with at most 253 characters
- The name segment must be at most 63 characters
- There is no constraint on the combined total length (max: 253+1+63=317)

This commit removes the total-length check and replaces it with a direct
prefix-length check, which matches the Kubernetes specification.

Regression coverage is added for:
- Valid key rejected by the old check (prefix=251 chars, total>253)
- Prefix that exceeds 253 chars (should be rejected)
- Invalid prefix format
- Name that exceeds 63 chars
- Value that exceeds 63 chars
- Non-string metadata keys
- Key with empty prefix (starts with '/')
@zerone0x zerone0x requested a review from Pangjiping as a code owner March 10, 2026 02:50
Copilot AI review requested due to automatic review settings March 10, 2026 02:50
@chatgpt-codex-connector
Copy link

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Kubernetes label key validation to correctly allow label keys whose combined prefix/name length exceeds 253 characters, while still enforcing the prefix (DNS subdomain) ≤ 253 and name ≤ 63.

Changes:

  • Remove incorrect total-length guard from _is_valid_label_key and validate prefix length explicitly.
  • Add regression tests covering long-but-valid prefixed keys and multiple invalid key/value cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
server/src/services/validators.py Corrects label key validation logic to align with Kubernetes spec (prefix length vs total key length).
server/tests/test_validators.py Adds regression tests for long prefix/name combinations and invalid metadata label inputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@CLAassistant
Copy link

CLAassistant commented Mar 10, 2026

CLA assistant check
All committers have signed the CLA.

@Pangjiping
Copy link
Collaborator

Thanks for fixes. Please sign CLA and I will review and merge those changes as soon as possible.

@zerone0x
Copy link
Contributor Author

@Pangjiping already signed!

Copy link
Collaborator

@Pangjiping Pangjiping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@Generalwin Generalwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Pangjiping Pangjiping merged commit a978b1d into alibaba:main Mar 11, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working component/server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants