raven-processor-sanitizessn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ ## Attribution
2
+ # This class is a modified version of the sanitizedata.rb processor
3
+ # from the sentry-raven gem. All source licensing applies.
4
+ #
5
+ # https://github.com/getsentry/raven-ruby/blob/master/lib/raven/processors/sanitizedata.rb
6
+ #
7
+
8
+ require 'raven/processor'
9
+ require 'json'
10
+
11
+ module Raven
12
+ module Processor
13
+ class SanitizeSSN < Processor
14
+
15
+ MASK = '********'
16
+ FIELDS_RE = /(ssn|social(.*)?sec)/i
17
+ VALUES_RE = /^\d{16}$/
18
+
19
+ def apply(value, key = nil, visited = [], &block)
20
+ if value.is_a?(Hash)
21
+
22
+ return "{...}" if visited.include?(value.__id__)
23
+ visited += [value.__id__]
24
+
25
+ value.each.reduce({}) do |memo, (k, v)|
26
+ memo[k] = apply(v, k, visited, &block)
27
+ memo
28
+ end
29
+ elsif value.is_a?(Array)
30
+ return "[...]" if visited.include?(value.__id__)
31
+ visited += [value.__id__]
32
+
33
+ value.map do |value_|
34
+ apply(value_, key, visited, &block)
35
+ end
36
+ elsif json_data = parse_as_json(value) && json_data.is_a?(Hash)
37
+ apply(json_data, key, visited, &block)
38
+ else
39
+ block.call(key, value)
40
+ end
41
+ end
42
+
43
+ def sanitize(key, value)
44
+ if !value.is_a?(String) || value.empty?
45
+ value
46
+ elsif VALUES_RE.match(clean_invalid_utf8_bytes(value)) || FIELDS_RE.match(key)
47
+ MASK
48
+ else
49
+ clean_invalid_utf8_bytes(value)
50
+ end
51
+ end
52
+
53
+ def process(data)
54
+ apply(data) do |key, value|
55
+ sanitize(key, value)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def parse_as_json(value)
62
+ begin
63
+ JSON.parse(value)
64
+ rescue JSON::ParserError
65
+ value
66
+ end
67
+ end
68
+
69
+ def clean_invalid_utf8_bytes(text)
70
+ if RUBY_VERSION <= '1.8.7'
71
+ text
72
+ else
73
+ text.encode(
74
+ 'UTF-8',
75
+ 'binary',
76
+ :invalid => :replace,
77
+ :undef => :replace,
78
+ :replace => ''
79
+ )
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Raven
2
+ module Processor
3
+ module SanitizeSSNVersion
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raven-processor-sanitizessn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cozy Services Ltd.
9
+ - Matt Greensmith
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-04-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sentry-raven
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '0.4'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '0.4'
31
+ description:
32
+ email:
33
+ - opensource@cozy.co
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/raven/processors/sanitizessn.rb
39
+ - lib/version.rb
40
+ homepage: http://github.com/cozyco/raven-processor-sanitizessn
41
+ licenses:
42
+ - Apache
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A processor plugin for the Sentry Raven gem that sanitizes Social Security
65
+ Number fields.
66
+ test_files: []