Skip to content

Commit 2d8491e

Browse files
committed
Unify pulp_labels key validation to allow hyphens, spaces, and dots
The three places validating label keys (pulp_labels_validator, SetLabelSerializer/UnsetLabelSerializer, and LabelFilter) each allowed different characters, making it possible to create labels that couldn't be modified or filtered. Unify them all to accept alphanumerics, underscores, spaces, hyphens, and dots. closes #6456 Assisted-by: Claude (Cursor) Made-with: Cursor
1 parent 3426d35 commit 2d8491e

5 files changed

Lines changed: 17 additions & 5 deletions

File tree

CHANGES/6456.bugfix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Unified `pulp_labels` key validation across create/update, `set_label`/`unset_label`, and label
2+
filters to consistently allow alphanumerics, underscores, spaces, hyphens, and dots.

pulpcore/app/serializers/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ class SetLabelSerializer(serializers.Serializer):
564564
Serializer for synchronously setting a label.
565565
"""
566566

567-
key = serializers.SlugField(required=True)
567+
key = serializers.RegexField(regex=r"^[\w .\-]+$", required=True)
568568
value = serializers.CharField(required=True, allow_null=True, allow_blank=True)
569569

570570

@@ -573,7 +573,7 @@ class UnsetLabelSerializer(serializers.Serializer):
573573
Serializer for synchronously UNsetting a label.
574574
"""
575575

576-
key = serializers.SlugField(required=True)
576+
key = serializers.RegexField(regex=r"^[\w .\-]+$", required=True)
577577
value = serializers.CharField(read_only=True)
578578

579579
def validate_key(self, value):

pulpcore/app/serializers/fields.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,11 @@ def pulp_labels_validator(value):
427427
value = json.loads(value)
428428

429429
for k, v in value.items():
430-
if not re.match(r"^[\w ]+$", k):
431-
raise serializers.ValidationError(_("Key '{}' contains non-alphanumerics.").format(k))
430+
if not re.match(r"^[\w .\-]+$", k):
431+
raise serializers.ValidationError(
432+
_("Key '{}' contains invalid characters. Only alphanumerics, underscores,"
433+
" spaces, hyphens, and dots are allowed.").format(k)
434+
)
432435
if v is not None and re.search(r"[,()]", v):
433436
raise serializers.ValidationError(
434437
_("Key '{}' contains value with comma or parenthesis.").format(k)

pulpcore/app/viewsets/custom_filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def filter(self, qs, value):
313313
return qs
314314

315315
for term in value.split(","):
316-
match = re.match(r"(!?[\w\s]+)(=|!=|~)?(.*)?", term)
316+
match = re.match(r"(!?[\w\s.\-]+)(=|!=|~)?(.*)?", term)
317317
if not match:
318318
raise DRFValidationError(_("Invalid search term: '{}'.").format(term))
319319
key, op, val = match.groups()

pulpcore/tests/unit/serializers/test_fields.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
{"key": ""},
1313
{"key": None},
1414
{"key1": "value", "key2": None, "key3": ""},
15+
{"my-key": "value"},
16+
{"my.key": "value"},
17+
{"my key": "value"},
18+
{"my-dotted.key": "value"},
19+
{"spaced key-with.mixed_chars": "value"},
1520
],
1621
)
1722
def test_pulp_labels_validator_valid(labels):
@@ -27,6 +32,8 @@ def test_pulp_labels_validator_valid(labels):
2732
{"key": "val(ue"},
2833
{"key": "val)ue"},
2934
{"bad!key": "value"},
35+
{"bad:key": "value"},
36+
{"bad@key": "value"},
3037
],
3138
)
3239
def test_pulp_labels_validator_invalid(labels):

0 commit comments

Comments
 (0)