lib/ForgeOps/Tracker/PiiScrubber.pm

filter: Statementstmt 98.9% 90 / 91 Branchbran 94.1% 32 / 34 Conditioncond 72.2% 13 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 2 totaltotal 90.9% 151 / 166 CC 29 SCAR 16.3
CC
29
Cov
94%
scrub
7.4
_scrub_card_numbers_in
7.0
_is_iban
6.2
CRAP 5.1
« lib/ForgeOps/Tracker/EventBuilder.pm ↑ Summary blib/lib/ForgeOps/Tracker/Integrations/PSGI.pm »
1 package ForgeOps::Tracker::PiiScrubber;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Redacts likely-sensitive content out of a payload before it ever leaves this process: the
8 # same patterns ForgeOps itself applies again on arrival (defense in depth: this layer keeps the
9 # data off the wire and out of any request logging in between; the server-side layer is what
10 # actually protects the database). Ported from app/services/pii_scrubber.rb: same key list, same
11 # 8 regex patterns, same "[LABEL FILTERED]" replacement format, same REDACTED constant.
12 # Deliberately does NOT support Project#additional_sensitive_keys: confirmed server-side only
13 # (see that file's own header comment: extending the pattern list to arbitrary customer regexes is
14 # a ReDoS risk best kept out of every client).
15 3 our @EXPORT_OK = qw(scrub scrub_string REDACTED);
16  
17 3 ▶use constant REDACTED => '[FILTERED]';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 my @SENSITIVE_KEYS = qw(
20 password passwd pwd
21 secret apisecret clientsecret secretkey
22 token accesstoken refreshtoken apikey apitoken authorization authtoken bearer sessiontoken csrftoken
23 creditcard cardnumber cardnum cvv cvv2 cvc
24 iban accountnumber routingnumber bankaccount
25 ssn socialsecuritynumber socialsecurity
26 privatekey
27 );
28  
29 # Order matters no more than it does in any other port: each pattern is applied independently
30 # to the same string, left to right, same as every other language's version.
31 3 my @PATTERNS = (
32 ['EMAIL', qr/[a-zA-Z0-9._%+-]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/],
33 ['SSN', qr/\b\d{3}-\d{2}-\d{4}\b/],
34 ['CREDIT CARD', qr/\b\d{4}[ -]\d{4}[ -]\d{4}[ -]\d{1,4}\b/],
35 ['BEARER TOKEN', qr/\bBearer\s+[A-Za-z0-9\-._~+\/]+=*/i],
36 ['JWT', qr/\bey[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/],
37 ['AWS KEY', qr/\bAKIA[0-9A-Z]{16}\b/],
38 ['STRIPE KEY', qr/\b[sr]k_(?:live|test)_[A-Za-z0-9]{10,}\b/],
39 ['GITHUB TOKEN', qr/\bgh[pousr]_[A-Za-z0-9]{20,}\b/],
40 );
41  
42 # scrub($value, $key): $value may be a scalar, an arrayref, or a hashref (Perl's own equivalent
43 # of Ruby's Hash/Array/String/other dispatch). $key is the enclosing hash key $value was found
44 # under (undef for a bare top-level value, or an array element), and is what the key-name check
45 # runs against, mirroring app/services/pii_scrubber.rb's own recursive `scrub` exactly.
46 105 ▶sub scrub {
Subroutine scrub: called CC=7 SCAR=20.0
Pod undocumented
47 105 my ($value, $key) = @_;
48  
49 105 ▶ if (_is_sensitive_key($key) && defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_sensitive_key($key) and defined $value) 12 93 105
Condition66%2/3
_is_sensitive_key($key) and defined $value
!ll&&!rl&&r
93012
MC/DC50%1/2
_is_sensitive_key($key) and defined $value
_is_sensitive_key($key) defined $value
Truth table66%2/3
A and B
A= _is_sensitive_key($key)B= defined $value
ABresult
0X0
100
111
50 12 return REDACTED;
51 }
52  
53 93 my $ref = ref $value;
54 93 ▶ if ($ref eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 81 12 93
elsif ($ref eq 'ARRAY') { } 1 11 12
elsif ($ref eq '' and defined $value) { } 11 0 11
55 81 return { map { $_ => scrub($value->{$_}, $_) } keys %$value };
56 }
57 elsif ($ref eq 'ARRAY') {
58 1 return [ map { scrub($_, $key) } @$value ];
59 }
60 ▶ elsif ($ref eq '' && defined $value) {
Condition33%1/3
$ref eq "" and defined $value
!ll&&!rl&&r
0011
MC/DC0%0/2
$ref eq "" and defined $value
$ref eq "" defined $value
Truth table33%1/3
A and B
A= $ref eq ""B= defined $value
ABresult
0X0
100
111
61 11 return scrub_string($value);
62 }
63  
64 0 return $value;
65 }
66  
67 # Card numbers in any other shape, and IBANs: see sdks/PII_SCRUBBING.md, which every SDK follows,
68 # and sdks/pii_scrubbing_vectors.json (a copy is in t/fixtures), which pins the results. Word
69 # characters and digits are spelled out as ASCII classes: on a character string Perl's own \w, \d
70 # and \b are Unicode-aware, and the rule is ASCII only.
71 3 my $DIGIT_RUN = qr/(?<![A-Za-z0-9_])[0-9](?:[ -]?[0-9])*(?![A-Za-z0-9_])/;
72 3 my $CARD_PREFIXES = qr/\A(?:4|5[1-5]|2(?:22[1-9]|2[3-9][0-9]|[3-6][0-9][0-9]|7[01][0-9]|720)|3[47]|6011|64[4-9]|65|35(?:2[89]|[3-8][0-9])|36|30[0-5]|62)/;
73 3 my $IBAN = qr/(?<![A-Za-z0-9_])[A-Z]{2}[0-9]{2}(?:[A-Z0-9]{11,30}|(?: [A-Z0-9]{4}){2,7}(?: [A-Z0-9]{1,4})?)(?![A-Za-z0-9_])/;
74  
75 173 ▶sub scrub_string {
Subroutine scrub_string: called CC=3 SCAR=11.0
Pod undocumented
76 173 my ($text) = @_;
77 # IBANs first: a spaced IBAN's middle groups look like a formatted card number.
78 173 ▶ $text =~ s/($IBAN)/_is_iban($1) ? '[IBAN FILTERED]' : $1/ge;
Branch: 1 branch
BranchTrueFalseTotal
_is_iban($1) ? : 7 1 8
79 173 for my $pattern (@PATTERNS) {
80 1384 my ($label, $regex) = @$pattern;
81 1384 $text =~ s/$regex/[$label FILTERED]/g;
82 }
83 173 $text =~ s/($DIGIT_RUN)/_scrub_card_numbers_in($1)/ge;
84 173 return $text;
85 }
86  
87 # From each group of the run on, the longest stretch of whole groups that is a card number is
88 # replaced, so a card followed by its CVV, two cards in a row, and Amex's 4-6-5 are all found.
89 68 ▶sub _scrub_card_numbers_in {
Subroutine _scrub_card_numbers_in: called CC=7 SCAR=19.5
90 68 my ($run) = @_;
91 68 my @parts = split /([ -])/, $run;
92 68 my (@digits, @separators);
93 68 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 95 68 163
94 95 push @digits, shift @parts;
95 95 ▶ push @separators, @parts ? shift @parts : '';
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 27 68 95
96 }
97  
98 68 my $out = '';
99 68 my $i = 0;
100 68 ▶ while ($i < @digits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @digits) 78 68 146
101 78 my $found;
102 78 ▶ for (my $last = $#digits; $last >= $i; $last -= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last >= $i) 91 47 138
103 91 ▶ if (_is_card_number(join '', @digits[$i .. $last])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_card_number(join("", @digits[$i .. $last]))) 31 60 91
104 31 $found = $last;
105 31 last;
106 }
107 }
108 78 ▶ if (defined $found) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) { } 31 47 78
109 31 $out .= '[CREDIT CARD FILTERED]' . $separators[$found];
110 31 $i = $found + 1;
111 }
112 else {
113 47 $out .= $digits[$i] . $separators[$i];
114 47 $i++;
115 }
116 }
117 68 return $out;
118 }
119  
120 91 ▶sub _is_card_number {
Subroutine _is_card_number: called CC=4 SCAR=13.9
121 91 my ($digits) = @_;
122 91 my $length = length $digits;
123 91 ▶ return $length >= 13 && $length <= 19 && $digits =~ $CARD_PREFIXES && _passes_luhn($digits);
Condition100%9/9
$length >= 13 && $length <= 19 && $digits =~ /$CARD_PREFIXES/ && _passes_luhn($digits)
!ll&&!rl&&r
56431
$length >= 13 and $length <= 19 and $digits =~ /$CARD_PREFIXES/
!ll&&!rl&&r
53335
$length >= 13 and $length <= 19
!ll&&!rl&&r
49438
MC/DC100%4/4
$length >= 13 && $length <= 19 && $digits =~ /$CARD_PREFIXES/ && _passes_luhn($digits)
$length >= 13 $length <= 19 $digits =~ /$CARD_PREFIXES/ _passes_luhn($digits)
Truth table100%5/5
A and B and C && D
A= $length >= 13B= $length <= 19C= $digits =~ /$CARD_PREFIXES/D= _passes_luhn($digits)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
124 }
125  
126 35 ▶sub _passes_luhn {
Subroutine _passes_luhn: called CC=4 SCAR=13.9
127 35 my ($digits) = @_;
128 35 my $sum = 0;
129 35 my $index = 0;
130 35 for my $char (reverse split //, $digits) {
131 562 my $value = ord($char) - ord('0');
132 562 ▶ if ($index % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index % 2) 278 284 562
133 278 $value *= 2;
134 278 ▶ $value -= 9 if $value > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $value > 9 41 237 278
135 }
136 562 $sum += $value;
137 562 $index++;
138 }
139 35 return $sum % 10 == 0;
140 }
141  
142 # The IBAN check, a digit at a time: the first four characters moved to the end, letters as two
143 # digits (A is 10), and the remainder by 97 is 1.
144 8 ▶sub _is_iban {
Subroutine _is_iban: called CC=6 SCAR=18.2
145 8 my ($candidate) = @_;
146 8 (my $compact = $candidate) =~ s/ //g;
147 8 my $length = length $compact;
148 8 ▶ return 0 unless $length >= 15 && $length <= 34;
Branch: 1 branch
BranchTrueFalseTotal
unless $length >= 15 and $length <= 34 0 8 8
Condition33%1/3
$length >= 15 and $length <= 34
!ll&&!rl&&r
008
MC/DC0%0/2
$length >= 15 and $length <= 34
$length >= 15 $length <= 34
Truth table33%1/3
A and B
A= $length >= 15B= $length <= 34
ABresult
0X0
100
111
149  
150 8 my $rearranged = substr($compact, 4) . substr($compact, 0, 4);
151 8 my $remainder = 0;
152 8 for my $char (split //, $rearranged) {
153 181 ▶ my $number = $char =~ /\A[A-Z]\z/ ? ord($char) - 55 : $char;
Branch: 1 branch
BranchTrueFalseTotal
$char =~ /\A[A-Z]\z/ ? : 25 156 181
154 181 for my $digit (split //, $number) {
155 206 $remainder = ($remainder * 10 + ord($digit) - ord('0')) % 97;
156 }
157 }
158 8 return $remainder == 1;
159 }
160  
161 105 ▶sub _is_sensitive_key {
Subroutine _is_sensitive_key: called CC=4 SCAR=13.9
162 105 my ($key) = @_;
163 105 ▶ return 0 unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 79 26 105
164  
165 26 my $normalized = lc $key;
166 26 $normalized =~ s/[^a-z0-9]//g;
167  
168 26 for my $sensitive (@SENSITIVE_KEYS) {
169 649 ▶ return 1 if index($normalized, $sensitive) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($normalized, $sensitive) != -1 12 637 649
170 }
171 14 return 0;
172 }
173  
174 3 1;
« lib/ForgeOps/Tracker/EventBuilder.pm ↑ Summary blib/lib/ForgeOps/Tracker/Integrations/PSGI.pm »