diff --git a/lib/sendgrid/helpers/mail/mail.rb b/lib/sendgrid/helpers/mail/mail.rb index 37db5629..023b2833 100644 --- a/lib/sendgrid/helpers/mail/mail.rb +++ b/lib/sendgrid/helpers/mail/mail.rb @@ -60,6 +60,13 @@ def add_content(content) @contents << content.to_json end + def check_for_secrets(patterns) + contents = @contents.map { |content| content['value'] }.join(' ') + patterns.each do |pattern| + raise SecurityError.new('Content contains sensitive information.') if contents.match(pattern) + end + end + def add_attachment(attachment) @attachments << attachment.to_json end diff --git a/test/sendgrid/helpers/mail/test_mail.rb b/test/sendgrid/helpers/mail/test_mail.rb index 664322ec..f057e57c 100644 --- a/test/sendgrid/helpers/mail/test_mail.rb +++ b/test/sendgrid/helpers/mail/test_mail.rb @@ -235,4 +235,12 @@ def test_add_invalid_category mail.add_category('foo') end end + + def test_check_for_secrets + mail = Mail.new + mail.add_content(Content.new(type: 'text/plain', value: 'Sensitive information: SG.a123b456')) + assert_raises(SecurityError) do + mail.check_for_secrets([/SG.[a-zA-Z0-9_-]*/]) + end + end end