- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 225
Added email validation to CaptureFeedback methods #4284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 5 commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a325003
              
                Added email validation to CaptureFeedback methods
              
              
                jamescrosswell c2bd973
              
                Update CHANGELOG.md
              
              
                jamescrosswell b60c9e0
              
                Format code
              
              
                getsentry-bot e37422f
              
                Review feedback
              
              
                jamescrosswell dd924e5
              
                Scrub email instead of dropping feedback entirely
              
              
                jamescrosswell 292b392
              
                Update CHANGELOG.md
              
              
                jamescrosswell 2448c9e
              
                Made EmailValidator more generic / rigorous
              
              
                jamescrosswell b8c2018
              
                Merge branch 'feedback-email-validation' of github.com:getsentry/sent…
              
              
                jamescrosswell 605413a
              
                Format code
              
              
                getsentry-bot File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System.Text.RegularExpressions; | ||
|  | ||
| namespace Sentry.Internal; | ||
|  | ||
| /// <summary> | ||
| /// Helper class for email validation. | ||
| /// </summary> | ||
| internal static partial class EmailValidator | ||
| { | ||
| private const string EmailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$"; | ||
|  | ||
| #if NET9_0_OR_GREATER | ||
| [GeneratedRegex(EmailPattern)] | ||
| private static partial Regex Email { get; } | ||
| #elif NET8_0 | ||
| [GeneratedRegex(EmailPattern)] | ||
| private static partial Regex EmailRegex(); | ||
| private static readonly Regex Email = EmailRegex(); | ||
| #else | ||
| private static readonly Regex Email = new(EmailPattern, RegexOptions.Compiled); | ||
| #endif | ||
|  | ||
| /// <summary> | ||
| /// Validates an email address. | ||
| /// </summary> | ||
| /// <param name="email">The email address to validate.</param> | ||
| /// <returns>True if the email is valid, false otherwise.</returns> | ||
| public static bool IsValidEmail(string? email) | ||
| { | ||
| if (string.IsNullOrEmpty(email)) | ||
|         
                  Flash0ver marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| { | ||
| return true; | ||
| } | ||
|  | ||
| return Email.IsMatch(email); | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| using System.IO.Abstractions.TestingHelpers; | ||
| using Sentry.Internal.Http; | ||
| using Sentry.Protocol; | ||
| using Sentry.Tests.Internals; | ||
|  | ||
| namespace Sentry.Tests; | ||
|  | @@ -1742,7 +1743,7 @@ public void CaptureUserFeedback_HubEnabled(bool enabled) | |
| hub.Dispose(); | ||
| } | ||
|  | ||
| var feedback = new UserFeedback(SentryId.Create(), "foo", "bar", "baz"); | ||
| var feedback = new UserFeedback(SentryId.Create(), "foo", "bar@example.com", "baz"); | ||
|  | ||
| // Act | ||
| hub.CaptureUserFeedback(feedback); | ||
|  | @@ -1890,6 +1891,103 @@ await transport.Received(1) | |
| } | ||
|  | ||
| private static Scope GetCurrentScope(Hub hub) => hub.ScopeManager.GetCurrent().Key; | ||
|  | ||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| [InlineData("[email protected]")] | ||
| [InlineData("[email protected]")] | ||
| [InlineData("[email protected]")] | ||
| public void CaptureFeedback_ValidEmail_FeedbackRegistered(string email) | ||
| { | ||
| // Arrange | ||
| var hub = _fixture.GetSut(); | ||
| var feedback = new SentryFeedback("Test feedback", email); | ||
|  | ||
| // Act | ||
| hub.CaptureFeedback(feedback); | ||
|  | ||
| // Assert | ||
| _fixture.Client.Received(1).CaptureFeedback(Arg.Any<SentryFeedback>(), Arg.Any<Scope>(), Arg.Any<SentryHint>()); | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData("invalid-email")] | ||
| [InlineData("missing@domain")] | ||
| [InlineData("@missing-local.com")] | ||
| [InlineData("spaces [email protected]")] | ||
| public void CaptureFeedback_InvalidEmail_FeedbackDropped(string email) | ||
| { | ||
| // Arrange | ||
| _fixture.Options.Debug = true; | ||
| _fixture.Options.DiagnosticLogger = Substitute.For<IDiagnosticLogger>(); | ||
| _fixture.Options.DiagnosticLogger!.IsEnabled(Arg.Any<SentryLevel>()).Returns(true); | ||
| var hub = _fixture.GetSut(); | ||
| var feedback = new SentryFeedback("Test feedback", email); | ||
|  | ||
| // Act | ||
| hub.CaptureFeedback(feedback); | ||
|  | ||
| // Assert | ||
| _fixture.Options.DiagnosticLogger.Received(1).Log( | ||
| SentryLevel.Warning, | ||
| Arg.Is<string>(s => s.Contains("invalid email format")), | ||
| null, | ||
| Arg.Any<object[]>()); | ||
| _fixture.Client.Received(1).CaptureFeedback(Arg.Is<SentryFeedback>(f => f.ContactEmail.IsNull()), | ||
| Arg.Any<Scope>(), Arg.Any<SentryHint>()); | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| [InlineData("[email protected]")] | ||
| [InlineData("[email protected]")] | ||
| [InlineData("[email protected]")] | ||
| public void CaptureUserFeedback_ValidEmail_FeedbackRegistered(string email) | ||
| { | ||
| #pragma warning disable CS0618 // Type or member is obsolete | ||
| // Arrange | ||
| var hub = _fixture.GetSut(); | ||
| var feedback = new UserFeedback(SentryId.Create(), "Test name", email, "Test comment"); | ||
|  | ||
| // Act | ||
| hub.CaptureUserFeedback(feedback); | ||
|  | ||
| // Assert | ||
| _fixture.Client.Received(1).CaptureUserFeedback(Arg.Any<UserFeedback>()); | ||
| #pragma warning restore CS0618 // Type or member is obsolete | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData("invalid-email")] | ||
| [InlineData("missing@domain")] | ||
| [InlineData("@missing-local.com")] | ||
| [InlineData("spaces [email protected]")] | ||
| public void CaptureUserFeedback_InvalidEmail_FeedbackDropped(string email) | ||
| { | ||
| #pragma warning disable CS0618 // Type or member is obsolete | ||
| // Arrange | ||
| _fixture.Options.Debug = true; | ||
| _fixture.Options.DiagnosticLogger = Substitute.For<IDiagnosticLogger>(); | ||
| _fixture.Options.DiagnosticLogger!.IsEnabled(Arg.Any<SentryLevel>()).Returns(true); | ||
| var hub = _fixture.GetSut(); | ||
| var feedback = new UserFeedback(SentryId.Create(), "Test name", email, "Test comment"); | ||
|  | ||
| // Act | ||
| hub.CaptureUserFeedback(feedback); | ||
|  | ||
| // Assert | ||
| _fixture.Options.DiagnosticLogger.Received(1).Log( | ||
| SentryLevel.Warning, | ||
| Arg.Is<string>(s => s.Contains("invalid email format")), | ||
| null, | ||
| Arg.Any<object[]>()); | ||
| _fixture.Client.Received(1).CaptureUserFeedback(Arg.Is<UserFeedback>(f => f.Email.IsNull())); | ||
| #pragma warning restore CS0618 // Type or member is obsolete | ||
| } | ||
| } | ||
|  | ||
| #if NET6_0_OR_GREATER | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.