Skip to content
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

Fix #210: method to check content for secret keys #232

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/sendgrid/helpers/mail/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/sendgrid/helpers/mail/test_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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