-
Notifications
You must be signed in to change notification settings - Fork 166
Log email sending and check for PII when sending asynchronous email #7155
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
mitchellhenke
merged 6 commits into
main
from
mitchellhenke/email-logging-and-pii-checking
Oct 19, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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
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
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,9 @@ | ||
| class EmailDeliveryObserver | ||
| def self.delivered_email(mail) | ||
| metadata = mail.instance_variable_get(:@_metadata) || {} | ||
| user = metadata[:user] || AnonymousUser.new | ||
| action = metadata[:action] | ||
| Analytics.new(user: user, request: nil, sp: nil, session: {}). | ||
| email_sent(action: action, ses_message_id: mail.header[:ses_message_id]&.value) | ||
| end | ||
| end |
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,54 @@ | ||
| class MailerSensitiveInformationChecker | ||
| class SensitiveKeyError < StandardError; end | ||
|
|
||
| class SensitiveValueError < StandardError; end | ||
|
|
||
| def self.check_for_sensitive_pii!(params, args_array, action) | ||
| args = ActiveJob::Arguments.serialize(args_array) | ||
| serialized_params = ActiveJob::Arguments.serialize(params) | ||
| serialized_args_string = args.to_s | ||
| serialized_params_string = serialized_params.to_s | ||
|
|
||
| if params[:email_address].is_a?(EmailAddress) | ||
| check_hash(params.except(:email_address), action) | ||
| else | ||
| check_hash(params, action) | ||
| end | ||
| args.each do |arg| | ||
| next unless arg.is_a?(Hash) | ||
| check_hash(arg, action) | ||
| end | ||
|
|
||
| if SessionEncryptor::SENSITIVE_REGEX.match?(serialized_args_string) | ||
| self.alert(SensitiveValueError.new) | ||
| end | ||
|
|
||
| if SessionEncryptor::SENSITIVE_REGEX.match?(serialized_params_string) | ||
| self.alert(SensitiveValueError.new) | ||
| end | ||
| end | ||
|
|
||
| def self.check_hash(hash, action) | ||
| hash.deep_transform_keys do |key| | ||
| if SessionEncryptor::SENSITIVE_KEYS.include?(key.to_s) | ||
| exception = SensitiveKeyError.new( | ||
| "#{key} unexpectedly appeared in #{action} Mailer args", | ||
| ) | ||
| self.alert(exception) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def self.alert(exception) | ||
| if IdentityConfig.store.session_encryptor_alert_enabled | ||
| NewRelic::Agent.notice_error(exception) | ||
| else | ||
| raise exception | ||
| end | ||
| end | ||
|
|
||
| class << self | ||
| include ::NewRelic::Agent::MethodTracer | ||
| add_method_tracer :check_for_sensitive_pii!, "Custom/#{name}/check_for_sensitive_pii!" | ||
| end | ||
| end |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 what if we only applied the lint to
UserMailerand its subclasses instead ofApplicationMailer?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that'd make sense, I don't know how to do that off-hand from a brief look at the current linter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok! I'll poke around at it for a follow-up