Skip to content

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

Open
zerone0x wants to merge 1 commit intoalibaba:mainfrom
zerone0x:fix/metadata-label-key-length-validation
Open

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

Conversation

@zerone0x
Copy link

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.

"""Label prefix (DNS subdomain) exceeding 253 characters should be rejected."""
# Build a prefix that is longer than 253 chars: 5 labels of 62 chars = 314 chars
label_part = "a" * 62
long_prefix = ".".join([label_part] * 5) # 62*5 + 4 = 314 chars
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

Consider adding a direct assertion for the constructed prefix length (e.g., assert len(long_prefix) > 253 or == 314). It makes the intent of the test more self-documenting and guards against accidental edits to the construction logic.

Suggested change
long_prefix = ".".join([label_part] * 5) # 62*5 + 4 = 314 chars
long_prefix = ".".join([label_part] * 5) # 62*5 + 4 = 314 chars
assert len(long_prefix) == 314

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +57
with pytest.raises(HTTPException) as exc_info:
ensure_metadata_labels({long_name: "value"})
assert exc_info.value.status_code == 400
assert exc_info.value.detail["code"] == SandboxErrorCodes.INVALID_METADATA_LABEL
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

Several new tests repeat the same pytest.raises(HTTPException) + status_code + detail['code'] assertions. Consider consolidating the invalid cases into a single @pytest.mark.parametrize test to reduce duplication and make it easier to extend the matrix of invalid inputs over time.

Copilot uses AI. Check for mistakes.
@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
Author

@Pangjiping already signed!

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