credit_card_sanitizer 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/credit_card_sanitizer.rb +26 -11
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8640635e8befbc2048e354e705bd824eda92ddf
4
- data.tar.gz: 447736aa16f358f1c470e3fe6ad07307a954debe
3
+ metadata.gz: 3d2b64979bb9b0c1ca2f9faecfdab2e3f7087210
4
+ data.tar.gz: 53ed4ff7f20f8d2b9b623bf40855df5a021e02ab
5
5
  SHA512:
6
- metadata.gz: 8e372b34945a260da35685a148a47dcb1fc4b6e02fa69fa5aab7823e19e4f141d01d3f3a14188b041eca00d62bb37fb18aa43dbea8700a175515a08ea2ec37ec
7
- data.tar.gz: 1741723f389d568c9555f5436d812ab47651e6c1a6c4b24979ff1bf6de804869040cd54d29cc32cc54e92ef7e65ed7dddd431a9f9027c9c550998eb5ff7d7626
6
+ metadata.gz: 0c2f62467f9c15db699fd4df40e1b08775b6de4b7480d71eb765e0feca9d8f69aaa0c9e7bc9ca52a4004181c0a4a3ac0c7da24d224090b121d5d67dc3d8dfeb2
7
+ data.tar.gz: 144a363a9968daf603b359698545a3632b6023ea54fd8202a048516678ec7de468397f15172caeb0566e31c4e001dd5dd9cc499d86a32ccf89c7ff0fc245d7ba
@@ -37,6 +37,9 @@ class CreditCardSanitizer
37
37
  'laser' => [[4, 4, 4, 4]]
38
38
  }
39
39
 
40
+ ACCEPTED_PREFIX = /(?:cc|card|visa|amex)\z/i
41
+ ACCEPTED_POSTFIX = /\Aex/i
42
+ ALPHANUMERIC = /[[:alnum:]]/i
40
43
  VALID_COMPANY_PREFIXES = Regexp.union(*CARD_COMPANIES.values)
41
44
  EXPIRATION_DATE = /\s(?:0?[1-9]|1[0-2])(?:\/|-)(?:\d{4}|\d{2})(?:\D|$)/
42
45
  LINE_NOISE_CHAR = /[^\w\n,()&.\/:;<>]/
@@ -44,8 +47,6 @@ class CreditCardSanitizer
44
47
  NONEMPTY_LINE_NOISE = /#{LINE_NOISE_CHAR}{1,5}/
45
48
  SCHEME_OR_PLUS = /((?:&#43;|\+)|(?:[a-zA-Z][\-+.a-zA-Z\d]{,9}):[^\s>]+)/
46
49
  NUMBERS_WITH_LINE_NOISE = /#{SCHEME_OR_PLUS}?\d(?:#{LINE_NOISE}\d){10,30}/
47
- HTML_TAGS = /(<\w+(?:(?:\s+\w+(?:\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+\s*|\s*)>)/
48
- SKIP_HTML_TAGS = Regexp.union(HTML_TAGS, NUMBERS_WITH_LINE_NOISE)
49
50
 
50
51
  DEFAULT_OPTIONS = {
51
52
  replacement_token: '▇',
@@ -53,12 +54,12 @@ class CreditCardSanitizer
53
54
  expose_last: 4,
54
55
  use_groupings: false,
55
56
  exclude_tracking_numbers: false,
56
- exclude_html_tags: false
57
+ parse_flanking: false
57
58
  }
58
59
 
59
60
  attr_reader :settings
60
61
 
61
- Candidate = Struct.new(:text, :numbers)
62
+ Candidate = Struct.new(:text, :numbers, :prefix, :postfix)
62
63
 
63
64
  # Create a new CreditCardSanitizer
64
65
  #
@@ -95,15 +96,15 @@ class CreditCardSanitizer
95
96
 
96
97
  text.force_encoding(Encoding::UTF_8)
97
98
  text.scrub!('�')
98
- regex = options[:exclude_html_tags] ? SKIP_HTML_TAGS : NUMBERS_WITH_LINE_NOISE
99
99
  redacted = nil
100
+
100
101
  without_expiration(text) do
101
- text.gsub!(regex) do |match|
102
- next match if $1 || $2
102
+ text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
103
+ next match if $1
103
104
 
104
- candidate = Candidate.new(match, match.tr('^0-9', ''))
105
+ candidate = Candidate.new(match, match.tr('^0-9', ''), $`, $')
105
106
 
106
- if valid_numbers?(candidate, options)
107
+ if valid_context?(candidate, options) && valid_numbers?(candidate, options)
107
108
  redacted = true
108
109
  redact_numbers(candidate, options)
109
110
  else
@@ -138,7 +139,7 @@ class CreditCardSanitizer
138
139
 
139
140
  private
140
141
 
141
- def valid_prefix?(numbers)
142
+ def valid_company_prefix?(numbers)
142
143
  !!(numbers =~ VALID_COMPANY_PREFIXES)
143
144
  end
144
145
 
@@ -170,7 +171,21 @@ class CreditCardSanitizer
170
171
  end
171
172
 
172
173
  def valid_numbers?(candidate, options)
173
- LuhnChecksum.valid?(candidate.numbers) && valid_prefix?(candidate.numbers) && valid_grouping?(candidate, options) && !is_tracking?(candidate, options)
174
+ LuhnChecksum.valid?(candidate.numbers) && valid_company_prefix?(candidate.numbers) && valid_grouping?(candidate, options) && !is_tracking?(candidate, options)
175
+ end
176
+
177
+ def valid_context?(candidate, options)
178
+ !options[:parse_flanking] || valid_prefix?(candidate.prefix) && valid_postfix?(candidate.postfix)
179
+ end
180
+
181
+ def valid_prefix?(prefix)
182
+ return true if prefix.nil? || !!ACCEPTED_PREFIX.match(prefix)
183
+ !ALPHANUMERIC.match(prefix[-1])
184
+ end
185
+
186
+ def valid_postfix?(postfix)
187
+ return true if postfix.nil? || !!ACCEPTED_POSTFIX.match(postfix)
188
+ !ALPHANUMERIC.match(postfix[0])
174
189
  end
175
190
 
176
191
  def redact_numbers(candidate, options)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credit_card_sanitizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Chapweske
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-02-15 00:00:00.000000000 Z
13
+ date: 2017-06-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -88,14 +88,14 @@ dependencies:
88
88
  requirements:
89
89
  - - "~>"
90
90
  - !ruby/object:Gem::Version
91
- version: 0.10.2
91
+ version: 0.10.3
92
92
  type: :runtime
93
93
  prerelease: false
94
94
  version_requirements: !ruby/object:Gem::Requirement
95
95
  requirements:
96
96
  - - "~>"
97
97
  - !ruby/object:Gem::Version
98
- version: 0.10.2
98
+ version: 0.10.3
99
99
  description: Credit card sanitizer
100
100
  email:
101
101
  - ggrossman@zendesk.com
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.5.1
127
+ rubygems_version: 2.6.8
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Credit card sanitizer