red_alert 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86e3c291a2e0c7653e1e755e1a59486e5ce1c506
4
- data.tar.gz: 5e088483d9d67d5e930f49d27048bca9f6d931f1
3
+ metadata.gz: 835ff34cab230a96c3dc7a6972095cba661ca988
4
+ data.tar.gz: b00cc6f956ae826e0a7cb095ab11dc13679ed07e
5
5
  SHA512:
6
- metadata.gz: 46b6541473ba5adab9b5e591b27fd8997c068a6a327424117ec69b82ba531e199f25e86773e625feb69038ee6480d862410266bc2092cd6b4d3a9f2d4a4493a0
7
- data.tar.gz: c6aa5eaef678230029fe87fe50b544346353d6e24e9b8f37830d73a35137579b35862dbecf3e4543bf3ccd727972408d071dae49ad89feab396dcf0ed2688970
6
+ metadata.gz: c0357b0a2161ce655c08a6fefaea8546da251ddd1b30d6c89d62cd544f647fb04f0d10f0e211060da919e12f95c2f421022eecf9f0547c7fe7873f17e6cd9140
7
+ data.tar.gz: 3eb0419a57d2db742af88b3eebcce69b42615ce7cb5b5af3e5bec2fc58d017658ded88d4fb3174f2ffbd546d5d315f9e7c9093de3b8828b9ab3feb0feebb06aa
data/Rakefile CHANGED
@@ -2,7 +2,6 @@ require "bundler/gem_tasks"
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- puts ENV['TERM']
6
5
  t.libs = ['spec', 'lib']
7
6
  t.pattern = 'spec/**/*_spec.rb'
8
7
  end
@@ -1,5 +1,7 @@
1
1
  module RedAlert
2
2
  require 'red_alert/version'
3
+ require 'red_alert/const'
3
4
  require 'red_alert/notification'
4
5
  require 'red_alert/notifier'
6
+ require 'red_alert/cleaner'
5
7
  end
@@ -0,0 +1,35 @@
1
+ module RedAlert
2
+ class Cleaner
3
+ FILTERED_TEXT = '[REMOVED]'
4
+ RECURSIVE_TEXT = '[RECURSIVE STRUCTURE]'
5
+
6
+ attr_reader :filter_keys
7
+
8
+ def initialize(filter_keys)
9
+ @filter_keys = filter_keys.to_set
10
+ end
11
+
12
+ def scrub(params)
13
+ formatted = format(params)
14
+ formatted.each do |key, value|
15
+ if filter_keys.include? key
16
+ formatted[key] = FILTERED_TEXT
17
+ elsif value.respond_to? :to_hash
18
+ formatted[key] = scrub value
19
+ end
20
+ end
21
+ end
22
+
23
+ def format(value, stack = Set.new)
24
+ return RECURSIVE_TEXT if stack.include? value.object_id
25
+
26
+ if value.respond_to? :to_ary
27
+ value.map{|v| format v, stack + [value.object_id]}
28
+ elsif value.respond_to? :to_hash
29
+ value.each_with_object({}){|(k,v), memo| memo[k] = format v, stack + [value.object_id]}
30
+ else
31
+ value.nil? ? nil : value.to_s
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ module RedAlert
2
+ RACK_DEFAULT_FILTERS = %w{
3
+ rack.request.cookie_hash
4
+ rack.request.cookie_string
5
+ rack.request.form_vars
6
+ rack.session
7
+ rack.session.options
8
+ }.freeze
9
+
10
+ PARAMS_DEFAULT_FILTERS = %w{
11
+ password
12
+ password_confirm
13
+ password_confirmation
14
+ }.freeze
15
+ end
@@ -10,8 +10,14 @@ module RedAlert
10
10
  @notifier_settings ||= {}
11
11
  end
12
12
 
13
+ def filter_keys
14
+ PARAMS_DEFAULT_FILTERS
15
+ end
16
+
13
17
  def alert(exception, data = {})
14
- notification = Notification.build notifier_settings[:subject], template, exception, data
18
+ cleaner = Cleaner.new(filter_keys)
19
+ cleaned_data = cleaner.scrub data
20
+ notification = Notification.build notifier_settings[:subject], template, exception, cleaned_data
15
21
  mail = Mail.new(
16
22
  to: notifier_settings[:to],
17
23
  from: notifier_settings[:from],
@@ -3,6 +3,11 @@ module RedAlert
3
3
  class Notifier
4
4
  include RedAlert::Notifier
5
5
 
6
+ def filter_keys
7
+ RACK_DEFAULT_FILTERS + PARAMS_DEFAULT_FILTERS
8
+ end
9
+
10
+
6
11
  def template
7
12
  <<-EMAIL
8
13
  A <%= exception.class %> occured: <%= exception %>
@@ -1,3 +1,3 @@
1
1
  module RedAlert
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe RedAlert::Cleaner do
4
+ let(:filter_keys) { ['foo', 'bar'] }
5
+
6
+ subject { RedAlert::Cleaner.new(filter_keys) }
7
+
8
+ it 'filters keys from a hash' do
9
+ result = subject.scrub('foo' => 'secret', 'baz' => 'cool')
10
+ result['foo'].must_equal RedAlert::Cleaner::FILTERED_TEXT
11
+ result['baz'].must_equal 'cool'
12
+ end
13
+
14
+ it 'returns a new hash' do
15
+ input = {'qux' => 'val'}
16
+ subject.scrub(input).wont_be_same_as input
17
+ end
18
+
19
+ it 'filters nested hashes' do
20
+ input = {'zing' => {'foo' => 'secret'}}
21
+ result = subject.scrub(input)
22
+ result['zing']['foo'].must_equal RedAlert::Cleaner::FILTERED_TEXT
23
+ end
24
+
25
+ it 'stringifys with unserializable data' do
26
+ bad_data = lambda { puts 'hello' }
27
+ input = {'zing' => bad_data}
28
+ result = subject.scrub(input)
29
+ assert_equal result['zing'], bad_data.to_s
30
+ end
31
+
32
+ it 'stringifys with unserializable data in arrays' do
33
+ bad_data = lambda { puts 'hello' }
34
+ input = {'zing' => [bad_data]}
35
+ result = subject.scrub(input)
36
+ assert_equal result['zing'].first, bad_data.to_s
37
+ end
38
+
39
+ it 'stringifys ints' do
40
+ input = {'zing' => 1}
41
+ result = subject.scrub(input)
42
+ result['zing'].must_equal '1'
43
+ end
44
+
45
+ it 'handles recursive arrays' do
46
+ a = []
47
+ a << a
48
+ input = {'zing' => a}
49
+ result = subject.scrub(input)
50
+ result['zing'].first.must_equal RedAlert::Cleaner::RECURSIVE_TEXT
51
+ end
52
+
53
+ it 'handles recursive hashes' do
54
+ input = {}
55
+ input['zing'] = {'self' => input}
56
+ result = subject.scrub(input)
57
+ result['zing']['self'].must_equal RedAlert::Cleaner::RECURSIVE_TEXT
58
+ end
59
+
60
+ it 'handles nil' do
61
+ input = {'zing' => nil}
62
+ result = subject.scrub(input)
63
+ result['zing'].must_equal nil
64
+ end
65
+ end
@@ -47,7 +47,7 @@ describe RedAlert::Notifier do
47
47
  enable_starttls_auto: true
48
48
  } }
49
49
 
50
- subject { TestNotifier.new 'test template <%= exception %> |<%= data[:stuff] %>|' }
50
+ subject { TestNotifier.new 'test template <%= exception %> |<%= data %>|' }
51
51
 
52
52
  before do
53
53
  subject.to to
@@ -67,12 +67,19 @@ describe RedAlert::Notifier do
67
67
  notification.to.must_include to
68
68
  notification.from.must_include from
69
69
  notification.subject.must_equal 'test subject something bad happened'
70
- notification.body.to_s.must_equal 'test template something bad happened |here|'
70
+ notification.body.to_s.must_equal 'test template something bad happened |{:stuff=>"here"}|'
71
71
  end
72
72
 
73
73
  it 'uses settings' do
74
74
  result = subject.alert(exception).delivery_method.settings
75
75
  result.must_equal settings
76
76
  end
77
+
78
+ it 'strips sensitive params' do
79
+ data['password'] = 'secret'
80
+ subject.alert exception, data
81
+ message = deliveries.first
82
+ message.body.to_s.must_include RedAlert::Cleaner::FILTERED_TEXT
83
+ end
77
84
  end
78
85
  end
@@ -12,11 +12,24 @@ describe RedAlert::Rack::Notifier do
12
12
  after { deliveries.clear }
13
13
 
14
14
  it 'alerts' do
15
+ expected = SecureRandom.hex
15
16
  begin
16
17
  raise 'boom'
17
18
  rescue => e
18
- subject.alert(e, request: 'data', env: { 'in' => 'out' }).body.wont_be_nil
19
- deliveries.length.must_be :>, 0
19
+ subject.alert(e, request: 'data', env: { 'in' => expected })
20
+ message = deliveries.first
21
+ message.body.to_s.must_include expected
22
+ end
23
+ end
24
+
25
+ it 'removes sensitive rack params' do
26
+ expected = SecureRandom.hex
27
+ begin
28
+ raise 'boom'
29
+ rescue => e
30
+ subject.alert(e, request: 'data', env: { 'rack.session' => expected })
31
+ message = deliveries.first
32
+ message.body.to_s.wont_include expected
20
33
  end
21
34
  end
22
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red_alert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vyrak.bunleang@gmail.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-22 00:00:00.000000000 Z
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -37,6 +37,8 @@ files:
37
37
  - README.md
38
38
  - Rakefile
39
39
  - lib/red_alert.rb
40
+ - lib/red_alert/cleaner.rb
41
+ - lib/red_alert/const.rb
40
42
  - lib/red_alert/notification.rb
41
43
  - lib/red_alert/notifier.rb
42
44
  - lib/red_alert/rack.rb
@@ -47,6 +49,7 @@ files:
47
49
  - lib/red_alert/sidekiq/notifier.rb
48
50
  - lib/red_alert/version.rb
49
51
  - red_alert.gemspec
52
+ - spec/red_alert/cleaner_spec.rb
50
53
  - spec/red_alert/notification_spec.rb
51
54
  - spec/red_alert/notifier_spec.rb
52
55
  - spec/red_alert/rack/middleware_spec.rb
@@ -78,6 +81,7 @@ signing_key:
78
81
  specification_version: 4
79
82
  summary: Middlewares for mailing errors
80
83
  test_files:
84
+ - spec/red_alert/cleaner_spec.rb
81
85
  - spec/red_alert/notification_spec.rb
82
86
  - spec/red_alert/notifier_spec.rb
83
87
  - spec/red_alert/rack/middleware_spec.rb