credit_card_sanitizer 0.7.0 → 1.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 +4 -4
- data/lib/credit_card_sanitizer/version.rb +3 -0
- data/lib/credit_card_sanitizer.rb +49 -39
- metadata +4 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e35438028266d3036c2b8e5b67e7bf7e113e3dd77980a25a1a18257e0b20c8f1
|
4
|
+
data.tar.gz: e2e3999eda40193e5d055e35d8b930f147e5273896161bf11eaad378128ecc7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f724f3f0f94e690072f018a12bc69353bfb542999341b254c2b269f589e2933a70241aa204a4cb7cc98b096c8e9774768627b71733770d27af19f6077b86922
|
7
|
+
data.tar.gz: 761f7d79806e0efe4706852a479a1c97fecee2c6bbb4180f7887831f0364bcae3445fdccdacc25ed6aefc9d3b756cd98a0a93d9a45c71027de7166c214a8469f
|
@@ -1,39 +1,48 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require 'securerandom'
|
5
|
-
require 'tracking_number'
|
1
|
+
require "luhn_checksum"
|
2
|
+
require "securerandom"
|
3
|
+
require "tracking_number"
|
6
4
|
|
7
5
|
class CreditCardSanitizer
|
8
|
-
# https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb#L5-L18
|
9
6
|
CARD_COMPANIES = {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
7
|
+
"visa" => /^4\d{12}(\d{3})?(\d{3})?$/,
|
8
|
+
"master" => /^(5[1-5]\d{4}|677189|222[1-9]\d{2}|22[3-9]\d{3}|2[3-6]\d{4}|27[01]\d{3}|2720\d{2})\d{10}$/,
|
9
|
+
"discover" => /^((6011\d{12})|(65[4-9]\d{13})|(64[4-9]\d{13})|(622(?:12[6-9]|1[3-9]\d|[2-8]\d{2}|9[01]\d|92[0-5])\d{10}))$/,
|
10
|
+
"american_express" => /^3[47]\d{13}$/,
|
11
|
+
"diners_club" => /^3(0[0-5]|[68]\d)\d{11}$/,
|
12
|
+
"jcb" => /^35(28|29|[3-8]\d)\d{12}$/,
|
13
|
+
"switch" => /^(6759\d{12}(\d{2,3})?|(4903|4905|4911|4936|6333|6759)\d{12}|(4903|4905|4911|4936|6333|6759)\d{14}|(4903|4905|4911|4936|6333|6759)\d{15}|564182\d{10}|564182\d{12}|564182\d{13}|633110\d{10}|633110\d{12}|633110\d{13})$/,
|
14
|
+
"solo" => /^(6767\d{12}(\d{2,3})?|6334\d{12}|6334\d{14}|6334\d{15}|6767\d{14}|6767\d{15})$/,
|
15
|
+
"dankort" => /^5019\d{12}$/,
|
16
|
+
"maestro" => /^(5[06-8]\d{10,17}|6\d\d{10,17}|5018|5020|5038|5893|6304|6759|6761|6762|6763\d{8,15})$/,
|
17
|
+
"forbrugsforeningen" => /^600722\d{10}$/,
|
18
|
+
"laser" => /^(6304|6706|6709|6771(?!89))(\d{12,15}|\d{8}(\d{4}|\d{6,7})?)$/,
|
19
|
+
"bc_global" => /^(6541|6556)\d{12}$/,
|
20
|
+
"carte_blanche" => /^389\d{11}$/,
|
21
|
+
"insta_payment" => /^63[7-9]\d{13}$/,
|
22
|
+
"korean_local" => /^9\d{15}$/,
|
23
|
+
"union_pay" => /^62\d{14,17}$/,
|
24
|
+
"visa_master" => /^(4\d{12}(\d{3})?|5[1-5]\d{14})$/
|
22
25
|
}.freeze
|
23
26
|
|
24
27
|
CARD_NUMBER_GROUPINGS = {
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
"visa" => [[4, 4, 4, 4]],
|
29
|
+
"master" => [[4, 4, 4, 4]],
|
30
|
+
"discover" => [[4, 4, 4, 4]],
|
31
|
+
"american_express" => [[4, 6, 5]],
|
32
|
+
"diners_club" => [[4, 6, 4]],
|
33
|
+
"jcb" => [[4, 4, 4, 4]],
|
34
|
+
"switch" => [[4, 4, 4, 4]],
|
35
|
+
"solo" => [[4, 4, 4, 4], [4, 4, 4, 4, 2], [4, 4, 4, 4, 3]],
|
36
|
+
"dankort" => [[4, 4, 4, 4]],
|
37
|
+
"maestro" => [[4], [5], [4, 4, 4, 4], [4, 4, 4, 4, 1], [4, 4, 4, 4, 2], [4, 4, 4, 4, 3]],
|
38
|
+
"forbrugsforeningen" => [[4, 4, 4, 4]],
|
39
|
+
"laser" => [[4, 4, 4, 4], [4, 4, 4, 4, 1], [4, 4, 4, 4, 2], [4, 4, 4, 4, 3]],
|
40
|
+
"bc_global" => [[4, 4, 4, 4]],
|
41
|
+
"carte_blanche" => [[4, 6, 4]],
|
42
|
+
"insta_payment" => [[4, 4, 4, 4]],
|
43
|
+
"korean_local" => [[4, 4, 4, 4]],
|
44
|
+
"union_pay" => [[4, 4, 4, 4], [4, 4, 4, 4, 1], [4, 4, 4, 4, 2], [4, 4, 4, 4, 3]],
|
45
|
+
"visa_master" => [[4, 4, 4, 4], [4, 4, 4, 4, 3]]
|
37
46
|
}.freeze
|
38
47
|
|
39
48
|
ACCEPTED_PREFIX = /(?:cc|card|visa|amex)\z/i
|
@@ -44,11 +53,11 @@ class CreditCardSanitizer
|
|
44
53
|
LINE_NOISE_CHAR = /[^\w\n,()&.\/:;<>]/
|
45
54
|
LINE_NOISE = /#{LINE_NOISE_CHAR}{,5}/
|
46
55
|
NONEMPTY_LINE_NOISE = /#{LINE_NOISE_CHAR}{1,5}/
|
47
|
-
SCHEME_OR_PLUS = /((?:+|\+|\/)|(?:[a-zA-Z][
|
56
|
+
SCHEME_OR_PLUS = /((?:+|\+|\/)|(?:[a-zA-Z][-+.a-zA-Z\d]{,9}):[^\s>]+)/
|
48
57
|
NUMBERS_WITH_LINE_NOISE = /#{SCHEME_OR_PLUS}?\d(?:#{LINE_NOISE}\d){10,30}/
|
49
58
|
|
50
59
|
DEFAULT_OPTIONS = {
|
51
|
-
replacement_token:
|
60
|
+
replacement_token: "▇",
|
52
61
|
expose_first: 6,
|
53
62
|
expose_last: 4,
|
54
63
|
use_groupings: false,
|
@@ -96,15 +105,16 @@ class CreditCardSanitizer
|
|
96
105
|
def sanitize!(text, options = {})
|
97
106
|
options = @settings.merge(options)
|
98
107
|
|
108
|
+
text = text.dup if text.frozen?
|
99
109
|
text.force_encoding(Encoding::UTF_8)
|
100
|
-
text.scrub!(
|
110
|
+
text.scrub!("�")
|
101
111
|
changes = nil
|
102
112
|
|
103
113
|
without_expiration(text) do
|
104
114
|
text.gsub!(NUMBERS_WITH_LINE_NOISE) do |match|
|
105
115
|
next match if $1
|
106
116
|
|
107
|
-
candidate = Candidate.new(match, match.tr(
|
117
|
+
candidate = Candidate.new(match, match.tr("^0-9", ""), $`, $')
|
108
118
|
|
109
119
|
if valid_context?(candidate, options) && valid_numbers?(candidate, options)
|
110
120
|
redact_numbers(candidate, options).tap do |redacted_text|
|
@@ -153,16 +163,16 @@ class CreditCardSanitizer
|
|
153
163
|
|
154
164
|
def find_company(numbers)
|
155
165
|
CARD_COMPANIES.each do |company, pattern|
|
156
|
-
return company if numbers
|
166
|
+
return company if pattern.match?(numbers)
|
157
167
|
end
|
158
168
|
end
|
159
169
|
|
160
170
|
def valid_grouping?(candidate, options)
|
161
171
|
if options[:use_groupings]
|
162
|
-
if company = find_company(candidate.numbers)
|
172
|
+
if (company = find_company(candidate.numbers))
|
163
173
|
groupings = candidate.text.split(NONEMPTY_LINE_NOISE).map(&:length)
|
164
174
|
return true if groupings.length == 1
|
165
|
-
if company_groupings = CARD_NUMBER_GROUPINGS[company]
|
175
|
+
if (company_groupings = CARD_NUMBER_GROUPINGS[company])
|
166
176
|
company_groupings.each do |company_grouping|
|
167
177
|
return true if groupings.take(company_grouping.length) == company_grouping
|
168
178
|
end
|
@@ -211,12 +221,12 @@ class CreditCardSanitizer
|
|
211
221
|
end
|
212
222
|
|
213
223
|
def without_expiration(text)
|
214
|
-
expiration_date_boundary = SecureRandom.hex.tr(
|
224
|
+
expiration_date_boundary = SecureRandom.hex.tr("0123456789", "ABCDEFGHIJ")
|
215
225
|
text.gsub!(EXPIRATION_DATE) do |expiration_date|
|
216
226
|
match = expiration_date.match(/(?<whitespace>\s*)(?<rest>.*)/m)
|
217
227
|
"#{match[:whitespace]}#{expiration_date_boundary}#{match[:rest]}#{expiration_date_boundary}"
|
218
228
|
end
|
219
229
|
yield
|
220
|
-
text.gsub!(expiration_date_boundary,
|
230
|
+
text.gsub!(expiration_date_boundary, "")
|
221
231
|
end
|
222
232
|
end
|
metadata
CHANGED
@@ -1,31 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: credit_card_sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Chapweske
|
8
8
|
- Gary Grossman
|
9
9
|
- Victor Kmita
|
10
|
-
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: irb
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
-
requirements:
|
19
|
-
- - ">="
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
version: '0'
|
29
14
|
- !ruby/object:Gem::Dependency
|
30
15
|
name: luhn_checksum
|
31
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,11 +47,11 @@ extensions: []
|
|
62
47
|
extra_rdoc_files: []
|
63
48
|
files:
|
64
49
|
- lib/credit_card_sanitizer.rb
|
50
|
+
- lib/credit_card_sanitizer/version.rb
|
65
51
|
homepage: https://github.com/zendesk/credit_card_sanitizer
|
66
52
|
licenses:
|
67
53
|
- Apache License Version 2.0
|
68
54
|
metadata: {}
|
69
|
-
post_install_message:
|
70
55
|
rdoc_options: []
|
71
56
|
require_paths:
|
72
57
|
- lib
|
@@ -81,8 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
66
|
- !ruby/object:Gem::Version
|
82
67
|
version: '0'
|
83
68
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
85
|
-
signing_key:
|
69
|
+
rubygems_version: 3.6.9
|
86
70
|
specification_version: 4
|
87
71
|
summary: Credit card sanitizer
|
88
72
|
test_files: []
|