Skip to content

Commit 97bbb2b

Browse files
authored
fix(highlights): Stricter context field regex (#89925)
this regex was too permissive and didn't really validate anything.
1 parent 22827d2 commit 97bbb2b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/sentry/issues/highlights.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
from sentry.models.project import Project
99
from sentry.utils.platform_categories import MOBILE
1010

11+
VALID_KEY_PATTERN = re.compile(r"^[a-zA-Z0-9_.:-]+$")
12+
1113

1214
@extend_schema_field(field=OpenApiTypes.OBJECT)
1315
class HighlightContextField(serializers.Field):
14-
def to_internal_value(self, data):
16+
def to_internal_value(self, data: object) -> dict[str, list[str]]:
1517
if not isinstance(data, dict):
1618
raise serializers.ValidationError("Expected a dictionary.")
17-
1819
for key, value in data.items():
19-
if not re.match(r"^.+$", key):
20+
if not VALID_KEY_PATTERN.match(key):
2021
raise serializers.ValidationError(f"Key '{key}' is invalid.")
2122
if not isinstance(value, list) or not all(isinstance(item, str) for item in value):
2223
raise serializers.ValidationError(f"Value for '{key}' must be a list of strings.")

tests/sentry/api/endpoints/test_project_details.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ def test_highlight_context(self):
956956
highlightContext={"": ["empty", "context", "type"]},
957957
)
958958
assert "Key '' is invalid" in resp.data["highlightContext"][0]
959+
resp = self.get_error_response(
960+
self.org_slug,
961+
self.proj_slug,
962+
highlightContext={"! {} #$%$?": ["empty", "context", "type"]},
963+
)
964+
assert "Key '! {} #$%$?' is invalid" in resp.data["highlightContext"][0]
959965
resp = self.get_error_response(
960966
self.org_slug,
961967
self.proj_slug,

0 commit comments

Comments
 (0)