Security.pm

filter: Statementstmt 100% 102 / 102 Branchbran 84.2% 32 / 38 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 94.3% 167 / 177 SCAR 33.7
CC
29
Cov
95%
restriction_level
11.1
mixed_script_confusable
8.0
whole_script_confusable
5.0
CRAP 29.1
↑ Summary Security/Confusables.pm »
1 package Unicode::Security;
2  
3 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Unicode::Security::Confusables;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Unicode::Normalize qw(NFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Unicode::UCD qw(charinfo charscript);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 our $VERSION = '0.07';
13 7 $VERSION = eval $VERSION;
14  
15 7 our @EXPORT_OK = qw(
16 skeleton confusable soss restriction_level mixed_script
17 mixed_number mixed_num
18 whole_script_confusable mixed_script_confusable
19 ws_confusable ms_confusable
20 );
21  
22 7 our (%MA, %WS);
23  
24 use constant {
25 7 UNRESTRICTED => 0,
26 ASCII_ONLY => 1,
27 SINGLE_SCRIPT => 2,
28 HIGHLY_RESTRICTIVE => 3,
29 MODERATELY_RESTRICTIVE => 4,
30 MINIMALLY_RESTRICTIVE => 5,
31 7 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 my %recommended_script = map { $_ => \1 } qw(
34 Common Inherited Arabic Armenian Bengali Bopomofo Cyrillic Devanagari
35 Ethiopic Georgian Greek Gujarati Gurmukhi Han Hangul Hebrew Hiragana
36 Kannada Katakana Khmer Lao Latin Malayalam Myanmar Oriya Sinhala Tamil
37 Telugu Thaana Thai Tibetan
38 );
39  
40 7 my %aspirational_script = map { $_ => \1 } qw(
41 Canadian_Aboriginal Miao Mongolian Tifinagh Yi
42 );
43  
44 7 my %highly_restrictive = map { $_ => \1 } (
45 '', 'Hiragana', 'Katakana', 'Hiragana, Katakana', 'Bopomofo', 'Hangul',
46 );
47  
48  
49 sub skeleton {
50 10 ▶ my $str = NFD shift;
Subroutine skeleton: called CC=3 SCAR=11.0
Pod documented
51 10 ▶ my $m = $str =~ s{(.)}{ my $c = $MA{$1}; defined $c ? $c : $1 }eg;
Branch: 1 branch
BranchTrueFalseTotal
defined $c ? : 11 38 49
52 10 ▶ return $m ? NFD $str : $str;
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 10 0 10
53 }
54  
55  
56 sub confusable {
57 5 ▶ return skeleton($_[0]) eq skeleton($_[1]);
Subroutine confusable: called CC=1 SCAR=0.0
Pod documented
58 }
59  
60  
61 # Algorithm described here:
62 # http://www.unicode.org/reports/tr39/#Whole_Script_Confusables
63 sub whole_script_confusable {
64 3 ▶ my ($target, $str) = @_;
Subroutine whole_script_confusable: called CC=5 SCAR=16.2
Pod documented
65  
66 # Canonicalize the script name to match the format used in %WS.
67 3 $target = ucfirst lc $target;
68  
69 3 my %soss = soss(NFD $str);
70 3 delete @soss{qw(Common Inherited)};
71  
72 3 ▶ my $count = keys %soss or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $count = keys %soss or return "" 0 3 3
73 3 ▶ return if 1 < $count;
Branch: 1 branch
BranchTrueFalseTotal
if 1 < $count 0 3 3
74 3 my ($source) = keys %soss;
75  
76 3 my $chars = $WS{$source}{$target};
77 3 ▶ do { return 1 if $chars->{$_} } for keys %{ $soss{$source} };
Branch: 1 branch
BranchTrueFalseTotal
if $chars->{$_} 2 5 7
78 }
79 7 *ws_confusable = *ws_confusable = \&whole_script_confusable;
80  
81  
82 # Algorithm described here:
83 # http://www.unicode.org/reports/tr39/#Mixed_Script_Confusables
84 sub mixed_script_confusable {
85 3 ▶ my %soss = soss(NFD $_[0]);
Subroutine mixed_script_confusable: called CC=8 SCAR=20.8
Pod documented
86 3 delete @soss{qw(Common Inherited)};
87  
88 3 my @soss = keys %soss;
89 3 for my $source (@soss) {
90 4 my $sum = 0;
91 4 for my $target (@soss) {
92 8 ▶ next if $target eq $source;
Branch: 1 branch
BranchTrueFalseTotal
if $target eq $source 3 5 8
93  
94 5 my $nok = 0;
95 5 my $chars = $WS{$target}{$source};
96 5 for my $char (keys %{ $soss{$target} }) {
97 6 ▶ $nok = 1, last unless $chars->{$char};
Branch: 1 branch
BranchTrueFalseTotal
unless $chars->{$char} 2 4 6
98 }
99 5 ▶ last if $nok;
Branch: 1 branch
BranchTrueFalseTotal
if $nok 2 3 5
100 3 $sum++;
101 }
102  
103 4 ▶ return 1 if 1 == @soss - $sum;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == @soss - $sum 2 2 4
104 }
105  
106 1 return '';
107 }
108 7 *ms_confusable = *ms_confusable = \&mixed_script_confusable;
109  
110  
111 sub soss {
112 24 ▶ my %soss;
Subroutine soss: called CC=3 SCAR=11.0
Pod documented
113 24 for my $char (split //, $_[0]) {
114 146 my $script = charscript(ord($char));
115 146 ▶ $script = 'Unknown' unless defined $script;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $script 0 146 146
116 146 $soss{$script}{$char} = \1;
117 }
118 24 return %soss;
119 }
120  
121  
122 sub mixed_script {
123 4 ▶ my %soss = soss($_[0]);
Subroutine mixed_script: called CC=1 SCAR=0.0
Pod documented
124 4 delete @soss{qw(Common Inherited)};
125 4 return 1 < keys %soss;
126 }
127  
128  
129 sub mixed_number {
130 7 ▶ my %z;
Subroutine mixed_number: called CC=4 SCAR=13.9
Pod documented
131 7 for my $char (split //, $_[0]) {
132 36 ▶ my $info = charinfo(ord $char) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = charinfo(ord $char) or next 0 36 36
133  
134 36 my $num = $info->{decimal};
135 36 ▶ next unless length $num;
Branch: 1 branch
BranchTrueFalseTotal
unless length $num 8 28 36
136  
137 28 $z{ ord($char) - $num } = \1;
138 }
139  
140 7 return 1 < keys %z;
141 }
142 7 *mixed_num = *mixed_num = \&mixed_number;
143  
144  
145 # Algorithm described here:
146 # http://www.unicode.org/reports/tr39/#Restriction_Level_Detection
147 sub restriction_level {
148 17 ▶ my ($str, $non_id_regex) = @_;
Subroutine restriction_level: called CC=11 SCAR=24.0
Pod documented
149  
150 17 ▶ $non_id_regex = qr/\P{ID_Continue}/ unless defined $non_id_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $non_id_regex 15 2 17
151  
152 17 ▶ return UNRESTRICTED if $str =~ /$non_id_regex/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /$non_id_regex/ 1 16 17
153 16 ▶ return ASCII_ONLY if $str !~ /\P{ASCII}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\P{ASCII}/ 2 14 16
154  
155 14 my %soss = soss($str);
156 14 delete @soss{qw(Common Inherited)};
157 14 ▶ return SINGLE_SCRIPT if 1 == keys %soss;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == keys %soss 1 13 14
158  
159 13 delete $soss{Latin};
160 13 my %copy = %soss;
161 13 delete $copy{Han};
162 13 my $soss = join ', ', sort keys %copy;
163 13 ▶ return HIGHLY_RESTRICTIVE if $highly_restrictive{$soss};
Branch: 1 branch
BranchTrueFalseTotal
if $highly_restrictive{$soss} 7 6 13
164  
165 6 ▶ if (1 == keys %soss) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %soss) 6 0 6
166 6 my ($script) = keys %soss;
167 return MODERATELY_RESTRICTIVE
168 if ($recommended_script{$script} or $aspirational_script{$script})
169 6 ▶ and not ($soss{Cyrillic} or $soss{Greek});
Branch: 1 branch
BranchTrueFalseTotal
if $recommended_script{$script} || $aspirational_script{$script} and not $soss{"Cyrillic"} || $soss{"Greek"} 2 4 6
Condition77%7/9
$recommended_script{$script} || $aspirational_script{$script} and not $soss{"Cyrillic"} || $soss{"Greek"}
!ll&&!rl&&r
042
$recommended_script{$script} or $aspirational_script{$script}
l!l&&r!l&&!r
510
$soss{'Cyrillic'} || $soss{'Greek'}
l!l&&r!l&&!r
132
MC/DC50%2/4
$recommended_script{$script} || $aspirational_script{$script} and not $soss{"Cyrillic"} || $soss{"Greek"}
$recommended_script{$script} $aspirational_script{$script} $soss{'Cyrillic'} $soss{'Greek'}
Truth table57%4/7
A or B and not(C || D)
A= $recommended_script{$script}B= $aspirational_script{$script}C= $soss{'Cyrillic'}D= $soss{'Greek'}
ABCDresult
00XX0
1X1X0
1X010
011X0
01010
1X001
01001
170  
171 }
172  
173 4 return MINIMALLY_RESTRICTIVE;
174 }
175  
176  
177 7 1;
178  
179 __END__
↑ Summary Security/Confusables.pm »