credit_card_sanitizer 0.3.1 → 0.3.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/credit_card_sanitizer.rb +28 -18
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 006b623e0a5d6f3ce1a9c4d51a31e271592f1e4f
4
- data.tar.gz: 79a2c6ae31ce9a9ce0481a0ed29bf50515bc8aab
3
+ metadata.gz: 931e6e4a5625866a098125ac6b7d19b48107f6ee
4
+ data.tar.gz: 95736bfd3c27a5807f3ae2527b5580e0f399f875
5
5
  SHA512:
6
- metadata.gz: 5c04220031bba7cf26fd2a061ef6a5f773286e862a194e292c1d22f086119679d4579a35cbb452919afbf806878dce925269f31c705c041172e7984e20c4a0e6
7
- data.tar.gz: b7204bb4ab4699a447dd215edd2ed1d7c19120a35d36ebcb7841e9a8c7e4560a8249f99b020052e3d43bd6e378c42f30ec962289a07ee8b174b8253cc02b533c
6
+ metadata.gz: 46b52b3e0a9906cef8969498fba3341a0e130407f21eb40d90b9e6e7edea2864fe7bddcc1e0f94638d856d7e9d8b7bffb84bd7a737f4bb32c7611672db429ebe
7
+ data.tar.gz: 06be394926ce1bd28c494669f85c0e577800d61d8bd3ebd442bf9739697aa789855f28fafbd4bcd542a0ba30fd7488a6d5d3bb977ea12025912742adbfda250d
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'luhn_checksum'
4
+ require 'securerandom'
4
5
 
5
6
  class CreditCardSanitizer
6
7
 
@@ -20,9 +21,10 @@ class CreditCardSanitizer
20
21
  'laser' => /^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/
21
22
  }
22
23
  VALID_COMPANY_PREFIXES = Regexp.union(*CARD_COMPANIES.values)
24
+ EXPIRATION_DATE = /\s(?:0?[1-9]|1[0-2])(?:\/|-)(?:\d{4}|\d{2})(?:\s|$)/
23
25
  LINE_NOISE = /[^\w_\n,()\/:]{,5}/
24
- SCHEME = /((?:[a-zA-Z][\-+.a-zA-Z\d]{,9}):\S+)/
25
- NUMBERS_WITH_LINE_NOISE = /#{SCHEME}?\d(?:#{LINE_NOISE}\d#{LINE_NOISE}){10,17}\d/
26
+ SCHEME_OR_PLUS = /(\+|(?:[a-zA-Z][\-+.a-zA-Z\d]{,9}):\S+)/
27
+ NUMBERS_WITH_LINE_NOISE = /#{SCHEME_OR_PLUS}?\d(?:#{LINE_NOISE}\d#{LINE_NOISE}){10,17}\d/
26
28
 
27
29
  attr_reader :replacement_token, :expose_first, :expose_last
28
30
 
@@ -60,16 +62,19 @@ class CreditCardSanitizer
60
62
  to_utf8!(text)
61
63
 
62
64
  redacted = nil
63
- text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
64
- next match if $1
65
- @numbers = match.tr('^0-9', '')
66
65
 
67
- if valid_numbers?
68
- redacted = true
69
- redact_numbers!(match)
70
- end
66
+ without_expiration(text) do
67
+ text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
68
+ next match if $1
69
+ @numbers = match.tr('^0-9', '')
70
+
71
+ if valid_numbers?
72
+ redacted = true
73
+ redact_numbers!(match)
74
+ end
71
75
 
72
- match
76
+ match
77
+ end
73
78
  end
74
79
 
75
80
  redacted && text
@@ -84,12 +89,12 @@ class CreditCardSanitizer
84
89
  # Rails.app.config.filter_parameters = [:password, CreditCardSanitizer.parameter_filter]
85
90
  #
86
91
  # env = {
87
- # "action_dispatch.request.parameters" => {"credit_card_number" => "123 4512 3451 2348", "password" => "123"},
88
- # "action_dispatch.parameter_filter" => Rails.app.config.filter_parameters
92
+ # "action_dispatch.request.parameters" => {"credit_card_number" => "4111 1111 1111 1111", "password" => "123"},
93
+ # "action_dispatch.parameter_filter" => Rails.app.config.filter_parameters
89
94
  # }
90
95
  #
91
96
  # >> ActionDispatch::Request.new(env).filtered_parameters
92
- # => {"credit_card_number" => "123 451X XXXX 2348", "password" => "[FILTERED]"}
97
+ # => {"credit_card_number" => "4111 11▇▇ ▇▇▇▇ 1111", "password" => "[FILTERED]"}
93
98
  #
94
99
  # Returns a Proc that takes the key/value of the request parameter.
95
100
  def self.parameter_filter
@@ -107,10 +112,8 @@ class CreditCardSanitizer
107
112
  end
108
113
 
109
114
  def redact_numbers!(text)
110
- digit_index = 0
111
-
112
- text.gsub!(/\d/) do |number|
113
- if within_redaction_range?(digit_index += 1)
115
+ text.gsub!(/\d/).with_index do |number, digit_index|
116
+ if within_redaction_range?(digit_index)
114
117
  replacement_token
115
118
  else
116
119
  number
@@ -119,7 +122,14 @@ class CreditCardSanitizer
119
122
  end
120
123
 
121
124
  def within_redaction_range?(digit_index)
122
- digit_index > expose_first && digit_index <= @numbers.size - expose_last
125
+ digit_index >= expose_first && digit_index < @numbers.size - expose_last
126
+ end
127
+
128
+ def without_expiration(text)
129
+ expiration_date_boundary = SecureRandom.hex.tr('0123456789', 'ABCDEFGHIJ')
130
+ text.gsub!(EXPIRATION_DATE) { |expiration_date| "#{expiration_date_boundary}#{expiration_date}#{expiration_date_boundary}" }
131
+ yield
132
+ text.gsub!(expiration_date_boundary, '')
123
133
  end
124
134
 
125
135
  if ''.respond_to?(:scrub)
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.3.1
4
+ version: 0.3.2
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: 2014-10-23 00:00:00.000000000 Z
13
+ date: 2014-11-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appraisal