fix(gitlab): sanitize ANSI escape sequences from MR webhook author email/name#120024
Closed
billyvg wants to merge 1 commit into
Closed
fix(gitlab): sanitize ANSI escape sequences from MR webhook author email/name#120024billyvg wants to merge 1 commit into
billyvg wants to merge 1 commit into
Conversation
…ail/name A GitLab MR webhook arrived with ANSI terminal escape sequences appended to the commit author email (e.g. cursor-left/right sequences totalling ~170 extra bytes). The raw value was passed straight to CommitAuthor.objects.get_or_create, where PostgreSQL's varchar(200) constraint on the email column raised StringDataRightTruncation, failing the entire webhook request. Fix by sanitizing email and name strings at the point of extraction from the webhook payload — before they reach any model or database layer — using a two-pass regex: first strip complete ANSI/VT100 CSI sequences (which contain printable ASCII characters that a single non-printable pass would miss), then strip any remaining non-printable ASCII characters. Finally, truncate to the column's max_length as a belt-and-suspenders guard. Apply the same sanitization to push-event author name (push events already had a length guard on email, but no control-character stripping on name). Fixes SENTRY-5RKP. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ma5RaV2qEq4QrWH3gakKs5
Member
Author
|
this just affects a single user it looks like, not high priority |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a production
DataError: value too long for type character varying(200)crash in the GitLab webhook handler.Root Cause
A GitLab MR webhook arrived with ANSI terminal escape sequences appended to the commit author email field in the
last_commit.author.emailpayload key — e.g.user@example.comfollowed by 34 cursor-left (\x1b[d) and 33 cursor-right (\x1b[c) sequences, totalling ~170 extra bytes and pushing the value well past 200 characters.MergeEventWebhook.__call__extracted this value verbatim and passed it directly toCommitAuthor.objects.get_or_create(email=<malformed_value>, ...). PostgreSQL'svarchar(200)constraint on thesentry_commitauthor.emailcolumn rejected the insert withStringDataRightTruncation, which Django surfaced asDataError. The entire webhook request failed with a 500.What was ruled out
DataErrorsilently: hides the bug, allows future similar injections to fail undetected, and does not store the author at all so the PR record ends up without an author.\x1b[dconsist of a non-printable ESC byte (\x1b) followed by printable ASCII characters ([d). Stripping only non-printable bytes would leave[d[d[d…in the stored email, which is still wrong data.Fix
Add
_sanitize_author_field()insrc/sentry/integrations/gitlab/webhooks.pyand apply it when extracting author email and name from MR and push webhook payloads, before they reach any model layer.The sanitizer uses a two-pass regex approach:
\x1b[<params><letter>) and two-character escape sequences — these contain printable ASCII characters that the second pass would not catch.max_lengthas a belt-and-suspenders guard.Test plan
test_merge_event_author_email_with_ansi_sequences_is_sanitizedintests/sentry/integrations/gitlab/test_webhook.py— injects a production-style malformed email (valid address + 67 ANSI escape sequences, length > 200) into the webhook payload, posts it through the full webhook handler, and asserts that theCommitAuthoris created with the clean email and name.Generated by Claude Code