lib/At/Protocol/Handle.pm

filter: Statementstmt 90.4% 57 / 63 Branchbran 92.3% 24 / 26 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 92.8% 13 / 14 Podpod 100% 7 / 7 totaltotal 92.6% 114 / 123 CC 19 SCAR 16.3
CC
19
Cov
92%
ensureValidHandle
10.0
isValidHandle
6.0
new
5.0
CRAP 5.1
« lib/At/Protocol/NSID.pm ↑ Summary lib/At/Protocol/DID.pm »
1 6 package At::Protocol::Handle v1.8.0 {
2 6 ▶ use v5.42;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶ use At::Error qw[register throw];
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶ use parent -norequire => 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶ use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶ no warnings qw[experimental::try];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use overload
8 6 ▶ '""' => sub ( $s, $u, $q ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9 27 $$s;
10 6 };
11 6 our %EXPORT_TAGS = (
12 all => [
13 our @EXPORT_OK
14 = qw[
15 ensureValidHandle ensureValidHandleRegex
16 normalizeHandle normalizeAndEnsureValidHandle
17 isValidHandle isValidTld]
18 ]
19 );
20 #
21 6 my $INVALID_HANDLE = 'handle.invalid';
22  
23 #~ Currently these are registration-time restrictions, not protocol-level
24 #~ restrictions. We have a couple accounts in the wild that we need to clean up
25 #~ before hard-disallow.
26 #~ See also: https://en.wikipedia.org/wiki/Top-level_domain#Reserved_domains
27 6 my @DISALLOWED_TLDS = (
28 '.local', '.arpa', '.invalid', '.localhost', '.internal', '.example', '.alt',
29  
30 # policy could concievably change on ".onion" some day
31 '.onion',
32  
33 #~ NOTE: .test is allowed in testing and devopment. In practical terms
34 #~ "should" "never" actually resolve and get registered in production
35 );
36  
37 22 ▶ sub new( $class, $id ) {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
38 22 ▶ throw UnsupportedDomainError('invalid TLD') unless isValidTld($id);
Branch: 1 branch
BranchTrueFalseTotal
unless isValidTld($id) 3 19 22
39 19 ensureValidHandle($id);
40 10 ensureValidHandleRegex($id);
41 10 ▶ CORE::state $warned //= 0;
Condition100%2/2
state $warned //= 0
l!l
91
MC/DC100%1/1
state $warned //= 0
state $warned
Truth table100%2/2
A
A= state $warned
Aresult
11
00
42 10 ▶ if ( $id =~ /\.(test)$/ && !$warned ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /\.(test)$/u and not $warned) 1 9 10
Condition100%3/3
$id =~ /\.(test)$/u and not $warned
!ll&&!rl&&r
811
MC/DC100%2/2
$id =~ /\.(test)$/u and not $warned
$id =~ /\.(test)$/u not $warned
Truth table100%3/3
A and not(B)
A= $id =~ /\.(test)$/uB= not $warned
ABresult
0X0
100
111
43 1 require Carp;
44 1 Carp::carp 'development or testing TLD used in handle: ' . $id;
45 1 $warned = 1;
46 }
47 10 bless \$id, $class;
48 }
49  
50 # Taken from https://github.com/bluesky-social/atproto/blob/main/packages/syntax/src/handle.ts
51 # Handle constraints, in English:
52 # - must be a possible domain name
53 # - RFC-1035 is commonly referenced, but has been updated. eg, RFC-3696,
54 # section 2. and RFC-3986, section 3. can now have leading numbers (eg,
55 # 4chan.org)
56 # - "labels" (sub-names) are made of ASCII letters, digits, hyphens
57 # - can not start or end with a hyphen
58 # - TLD (last component) should not start with a digit
59 # - can't end with a hyphen (can end with digit)
60 # - each segment must be between 1 and 63 characters (not including any periods)
61 # - overall length can't be more than 253 characters
62 # - separated by (ASCII) periods; does not start or end with period
63 # - case insensitive
64 # - domains (handles) are equal if they are the same lower-case
65 # - punycode allowed for internationalization
66 # - no whitespace, null bytes, joining chars, etc
67 # - does not validate whether domain or TLD exists, or is a reserved or
68 # special TLD (eg, .onion or .local)
69 # - does not validate punycode
70 291 ▶ sub ensureValidHandle ($handle) {
Subroutine ensureValidHandle: called CC=10 SCAR=23.0
Pod documented
71  
72 # check that all chars are boring ASCII
73 291 ▶ throw InvalidHandleError('Disallowed characters in handle (ASCII letters, digits, dashes, periods only)') if $handle !~ /^[a-zA-Z0-9.-]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle =~ /^[a-zA-Z0-9.-]*$/u 48 243 291
74 #
75 243 ▶ throw InvalidHandleError('Handle is too long (253 chars max)') if length $handle > 253;
Branch: 1 branch
BranchTrueFalseTotal
if length $handle > 253 3 240 243
76 #
77 240 my @labels = split /\./, $handle, -1; # negative limit, ftw
78 240 ▶ throw InvalidHandleError('Handle domain needs at least two parts') if scalar @labels < 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @labels < 2 19 221 240
79 221 for my $i ( 0 .. $#labels ) {
80 512 my $l = $labels[$i];
81 512 ▶ throw InvalidHandleError('Handle parts can not be empty') if !length $l;
Branch: 1 branch
BranchTrueFalseTotal
unless length $l 16 496 512
82 496 ▶ throw InvalidHandleError('Handle part too long (max 63 chars)') if length $l > 63;
Branch: 1 branch
BranchTrueFalseTotal
if length $l > 63 2 494 496
83 494 ▶ throw InvalidHandleError('Handle parts can not start or end with hyphens') if $l =~ /^-|-$/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^-|-$/u 19 475 494
84 475 ▶ throw InvalidHandleError('Handle final component (TLD) must start with ASCII letter') if $i == $#labels && $l !~ /^[a-zA-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#labels and not $l =~ /^[a-zA-Z]/u 18 457 475
Condition100%3/3
$i == $#labels and not $l =~ /^[a-zA-Z]/u
!ll&&!rl&&r
29116618
MC/DC100%2/2
$i == $#labels and not $l =~ /^[a-zA-Z]/u
$i == $#labels not $l =~ /^[a-zA-Z]/u
Truth table100%3/3
A and not(B)
A= $i == $#labelsB= not $l =~ /^[a-zA-Z]/u
ABresult
0X0
100
111
85 }
86 166 1;
87 }
88  
89 357 ▶ sub ensureValidHandleRegex ($handle) {
Subroutine ensureValidHandleRegex: called CC=3 SCAR=11.0
Pod documented
90 357 ▶ throw InvalidHandleError(q[Handle didn't validate via regex])
Branch: 1 branch
BranchTrueFalseTotal
unless $handle =~ /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/u 197 160 357
91 unless $handle =~ /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
92 160 ▶ throw InvalidHandleError('Handle is too long (253 chars max)') if length $handle > 253;
Branch: 1 branch
BranchTrueFalseTotal
if length $handle > 253 3 157 160
93 157 1;
94 }
95  
96 2 ▶ sub normalizeHandle ($handle) {
Subroutine normalizeHandle: called CC=1 SCAR=0.0
Pod documented
97 2 lc $handle;
98 }
99  
100 2 ▶ sub normalizeAndEnsureValidHandle($handle) {
Subroutine normalizeAndEnsureValidHandle: called CC=1 SCAR=0.0
Pod documented
101 2 my $normalized = normalizeHandle($handle);
102 2 ensureValidHandle($normalized);
103 1 $normalized;
104 }
105  
106 0 ▶ sub isValidHandle ($handle) {
Subroutine isValidHandle: not called CC=2 SCAR=17.9
Pod documented
107 0 try {
108 0 ensureValidHandle($handle)
109 }
110 catch ($err) { # TODO: I want this to work by checking the type of thrown error but this is perl...
111 0 ▶ if ( $err =~ /Handle/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /Handle/u) 0 0 0
112 0 return 0;
113 }
114 0 die $err;
115 }
116 0 1;
117 }
118  
119 22 ▶ sub isValidTld($handle) {
Subroutine isValidTld: called CC=3 SCAR=11.0
Pod documented
120 22 for my $tld (@DISALLOWED_TLDS) {
121 163 ▶ return 0 if $handle =~ /${tld}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $handle =~ /$tld$/u 3 160 163
122 }
123 19 1;
124 }
125  
126 # All fatal errors
127 6 register 'InvalidHandleError', (), 1;
128 6 register 'ReservedHandleError', (), 1;
129 6 register 'UnsupportedDomainError', (), 1;
130 6 register 'DisallowedDomainError', (), 1;
131 };
132 6 1;
133 __END__
« lib/At/Protocol/NSID.pm ↑ Summary lib/At/Protocol/DID.pm »