Skip to content

Commit 1439439

Browse files
authored
fix(code-mappings): handle malformed source path (#105683)
Fixes SENTRY-47GS Passing a malformed/empty `source_path` when creating a code mapping causes `source_path` to be `""`, which results in an error here (since `stack_path` always will end with `""`) ``` elif stack_path.endswith(source_path): stack_prefix = stack_path.rpartition(source_path)[0] ``` Since the user can input invalid `source_path`s, we should handle this and return 400 instead of 500
1 parent ee6a1e8 commit 1439439

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/sentry/api/endpoints/project_repo_path_parsing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
from sentry.integrations.services.integration import RpcIntegration, integration_service
1616
from sentry.integrations.source_code_management.repository import RepositoryIntegration
1717
from sentry.issues.auto_source_code_config.code_mapping import find_roots
18-
from sentry.issues.auto_source_code_config.errors import UnsupportedFrameInfo
18+
from sentry.issues.auto_source_code_config.errors import (
19+
UnexpectedPathException,
20+
UnsupportedFrameInfo,
21+
)
1922
from sentry.issues.auto_source_code_config.frame_info import FrameInfo, create_frame_info
2023
from sentry.models.project import Project
2124
from sentry.models.repository import Repository
@@ -142,7 +145,14 @@ def post(self, request: Request, project: Project) -> Response:
142145

143146
branch = installation.extract_branch_from_source_url(repo, source_url)
144147
source_path = installation.extract_source_path_from_source_url(repo, source_url)
145-
stack_root, source_root = find_roots(frame_info, source_path)
148+
149+
try:
150+
stack_root, source_root = find_roots(frame_info, source_path)
151+
except UnexpectedPathException:
152+
return self.respond(
153+
{"detail": "Could not determine code mapping from provided paths"},
154+
status=status.HTTP_400_BAD_REQUEST,
155+
)
146156

147157
return self.respond(
148158
{

src/sentry/issues/auto_source_code_config/code_mapping.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ def find_roots(frame_filename: FrameInfo, source_path: str) -> tuple[str, str]:
420420
Returns a tuple containing the stack_root, and the source_root.
421421
If there is no overlap, raise an exception since this should not happen
422422
"""
423+
if not source_path:
424+
raise UnexpectedPathException("Source path is empty")
425+
423426
stack_path = frame_filename.raw_path
424427
stack_root = ""
425428
if stack_path[0] == "/" or stack_path[0] == "\\":

tests/sentry/api/endpoints/test_project_repo_path_parsing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ def test_bad_source_url(self) -> None:
180180
assert resp.status_code == 400, resp.content
181181
assert resp.data == {"sourceUrl": ["Enter a valid URL."]}
182182

183+
def test_malformed_source_url_empty_source_path(self) -> None:
184+
source_url = "https://github.com/getsentry/sentry/tree/master/src/sentry/api/endpoints/project_stacktrace_link.py"
185+
stack_path = "sentry/api/endpoints/project_stacktrace_link.py"
186+
resp = self.make_post(source_url, stack_path)
187+
assert resp.status_code == 400, resp.content
188+
assert resp.data == {"detail": "Could not determine code mapping from provided paths"}
189+
183190
def test_wrong_file(self) -> None:
184191
source_url = "https://github.com/getsentry/sentry/blob/master/src/sentry/api/endpoints/project_releases.py"
185192
stack_path = "sentry/api/endpoints/project_stacktrace_link.py"

tests/sentry/issues/auto_source_code_config/test_code_mapping.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ def test_find_roots_bad_source_path(self) -> None:
308308
"nothing/something.js",
309309
)
310310

311+
def test_find_roots_empty_source_path(self) -> None:
312+
with pytest.raises(UnexpectedPathException):
313+
find_roots(
314+
create_frame_info({"filename": "sentry/foo.py"}),
315+
"",
316+
)
317+
311318
def test_find_roots_windows_path_with_spaces(self) -> None:
312319
stacktrace_root, source_path = find_roots(
313320
create_frame_info({"filename": "C:\\Program Files\\MyApp\\src\\file.py"}), "src/file.py"

0 commit comments

Comments
 (0)