blib/lib/WWW/Pastebin/PastebinCa/Create.pm

filter: Statementstmt 91.8% 101 / 110 Branchbran 60.8% 28 / 46 Conditioncond 36.1% 13 / 36 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 94.4% 17 / 18 Podpod 100% 7 / 7 totaltotal 70.8% 168 / 237 CC 40 SCAR 32.3
CC
40
Cov
74%
paste
57.9
_solve_pow
16.4
new
5.2
CRAP 25.2
↑ Summary
1 package WWW::Pastebin::PastebinCa::Create;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '1.001001'; # VERSION
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # pastebin.ca was rebuilt in 2026. The old HTML paste form is gone; pastes
15 # are now created through a documented JSON API:
16 # GET /api/v1/pastes/pow-challenge -> proof-of-work challenge (for
17 # anonymous, key-less creation)
18 # POST /api/v1/pastes -> create the paste
19 # Anonymous creates must solve the proof-of-work challenge (in lieu of the
20 # browser Turnstile widget) and must set an expiry no longer than 90 days.
21 3 my $Base = 'https://pastebin.ca';
22 3 my $Max_Expire_Secs = 7_776_000; # 90 days: server-enforced anon maximum
23  
24 3 my %Valid_Langs = valid_langs();
25 3 my %Valid_Expires = map { $_ => $_ } valid_expires();
26  
27 # Map the historical numeric 'lang' codes to the syntax hint strings the
28 # rebuilt site understands. Unknown/Raw map to plain "text".
29 3 my %Lang_To_Syntax = (
30 1 => 'text', 2 => 'asterisk', 3 => 'c',
31 4 => 'cpp', 5 => 'php', 6 => 'perl',
32 7 => 'java', 8 => 'vb', 9 => 'csharp',
33 10 => 'ruby', 11 => 'python', 12 => 'pascal',
34 13 => 'mirc', 14 => 'pli', 15 => 'xml',
35 16 => 'sql', 17 => 'scheme', 18 => 'actionscript',
36 19 => 'ada', 20 => 'apache', 21 => 'nasm',
37 22 => 'asp', 23 => 'bash', 24 => 'css',
38 25 => 'delphi', 26 => 'html', 27 => 'javascript',
39 28 => 'lisp', 29 => 'lua', 30 => 'asm',
40 31 => 'objc', 32 => 'vbnet', 33 => 'log',
41 34 => 'diff',
42 );
43  
44 # Map the historical 'expire' strings to a number of seconds.
45 3 my %Expire_To_Secs = (
46 '5 minutes' => 5*60, '10 minutes' => 10*60,
47 '15 minutes' => 15*60, '30 minutes' => 30*60,
48 '45 minutes' => 45*60, '1 hour' => 60*60,
49 '2 hours' => 2*3600, '4 hours' => 4*3600,
50 '8 hours' => 8*3600, '12 hours' => 12*3600,
51 '1 day' => 86400, '2 days' => 2*86400,
52 '3 days' => 3*86400, '1 week' => 7*86400,
53 '2 weeks' => 14*86400, '3 weeks' => 21*86400,
54 '1 month' => 30*86400, '2 months' => 60*86400,
55 '3 months' => 90*86400, '4 months' => 120*86400,
56 '5 months' => 150*86400, '6 months' => 180*86400,
57 '1 year' => 365*86400,
58 );
59  
60 3 ▶use overload q|""| => sub { shift->paste_uri };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
63 2 my $self = bless {}, shift;
64 2 ▶ croak "Must have even number of arguments to new()"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 2 2
65 if @_ & 1;
66  
67 2 my %args = @_;
68 2 $args{ +lc } = delete $args{ $_ } for keys %args;
69  
70 2 ▶ $args{timeout} ||= 30;
Condition100%2/2
$args{"timeout"} ||= 30
l!l
11
MC/DC100%1/1
$args{"timeout"} ||= 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
Aresult
11
00
71 $args{mech} ||= WWW::Mechanize->new(
72 autocheck => 0,
73 timeout => $args{timeout},
74 2 ▶ agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12)'
Condition33%1/3
$args{"mech"} ||= "WWW::Mechanize"->new("autocheck", 0, "timeout", $args{"timeout"}, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12")
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{"mech"} ||= "WWW::Mechanize"->new("autocheck", 0, "timeout", $args{"timeout"}, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12")
$args{"mech"} "WWW::Mechanize"->new("autocheck", 0, "timeout", $args{"timeout"}, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12")
Truth table33%1/3
A ||= B
A= $args{"mech"}B= "WWW::Mechanize"->new("autocheck", 0, "timeout", $args{"timeout"}, "agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12")
ABresult
1X1
011
000
75 .' Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12',
76 );
77  
78 2 $self->mech( $args{mech} );
79  
80 2 return $self;
81 }
82  
83 1 ▶sub paste {
Subroutine paste: called CC=20 SCAR=40.6
Pod documented
84 1 my ( $self, $content ) = splice @_, 0, 2;
85  
86 1 $self->$_(undef) for qw(paste_uri error);
87  
88 1 ▶ defined $content
Branch: 1 branch
BranchTrueFalseTotal
defined $content or carp("first argument to paste() is not defined") and return 1 0 1
Condition20%1/5
defined $content || carp('first argument to paste() is not defined') && (return)
l!l&&r!l&&!r
100
carp("first argument to paste() is not defined") and return
!ll
00
MC/DC0%0/2
defined $content || carp('first argument to paste() is not defined') && (return)
defined $content carp("first argument to paste() is not defined")
Truth table33%1/3
A || B
A= defined $contentB= carp("first argument to paste() is not defined")
ABresult
1X1
011
000
89 or carp "first argument to paste() is not defined" and return;
90  
91 1 ▶ croak "Must have even number of optional arguments to paste()"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 1 1
92 if @_ & 1;
93  
94 1 my %args = @_;
95 1 $args{ +lc } = delete $args{ $_ } for keys %args;
96 1 %args = (
97 content => $content,
98 name => '',
99 desc => '',
100 tags => '',
101 lang => 1,
102 expire => '',
103 %args,
104 );
105  
106 croak q|Invalid 'lang' was specified to paste(). |
107 . q|print Dumper { WWW::Pastebin::PastebinCa::Create::valid_langs }|
108 1 ▶ unless exists $Valid_Langs{ $args{lang} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Valid_Langs{$args{"lang"}} 0 1 1
109  
110 croak q|Invalid 'expire' was specified to paste(). |
111 . q|print Dumper { WWW::Pastebin::PastebinCa::Create::valid_expires }|
112 if length $args{expire}
113 1 ▶ and not exists $Valid_Expires{ $args{expire} };
Branch: 1 branch
BranchTrueFalseTotal
if length $args{"expire"} and not exists $Valid_Expires{$args{"expire"}} 0 1 1
Condition33%1/3
length $args{"expire"} and not exists $Valid_Expires{$args{"expire"}}
!ll&&!rl&&r
010
MC/DC0%0/2
length $args{"expire"} and not exists $Valid_Expires{$args{"expire"}}
length $args{"expire"} not exists $Valid_Expires{$args{"expire"}}
Truth table33%1/3
A and not(B)
A= length $args{"expire"}B= not exists $Valid_Expires{$args{"expire"}}
ABresult
0X0
100
111
114  
115 1 my $mech = $self->mech;
116  
117 # 1) Obtain and solve a proof-of-work challenge so we can create
118 # anonymously without an account/API key.
119 1 ▶ my $pow = $self->_solve_pow
Branch: 1 branch
BranchTrueFalseTotal
my $pow = $self->_solve_pow or return 1 0 1
120 or return; # error already set
121  
122 # 2) Work out the expiry. Anonymous pastes must expire within 90 days,
123 # so an empty (historically "never") or too-long value is capped.
124 my $expire_secs = length $args{expire}
125 1 ▶ ? ( $Expire_To_Secs{ $args{expire} } || $Max_Expire_Secs )
Branch: 1 branch
BranchTrueFalseTotal
length $args{'expire'} ? : 1 0 1
Condition33%1/3
$Expire_To_Secs{$args{"expire"}} or $Max_Expire_Secs
l!l&&r!l&&!r
100
MC/DC0%0/2
$Expire_To_Secs{$args{"expire"}} or $Max_Expire_Secs
$Expire_To_Secs{$args{"expire"}} $Max_Expire_Secs
Truth table33%1/3
A or B
A= $Expire_To_Secs{$args{"expire"}}B= $Max_Expire_Secs
ABresult
1X1
011
000
126 : $Max_Expire_Secs;
127 1 ▶ $expire_secs = $Max_Expire_Secs if $expire_secs > $Max_Expire_Secs;
Branch: 1 branch
BranchTrueFalseTotal
if $expire_secs > $Max_Expire_Secs 0 1 1
128  
129 # 3) Build and send the create request.
130 my %payload = (
131 body => $args{content},
132 syntax_hint => ( $Lang_To_Syntax{ $args{lang} } || 'text' ),
133 visibility => 'unlisted',
134 expires_in_seconds => $expire_secs,
135 pow_challenge_id => $pow->{challenge_id},
136 1 ▶ pow_nonce => "$pow->{nonce}",
Condition50%1/2
$Lang_To_Syntax{$args{'lang'}} || 'text'
l!l
10
MC/DC0%0/1
$Lang_To_Syntax{$args{'lang'}} || 'text'
$Lang_To_Syntax{$args{'lang'}}
Truth table50%1/2
A
A= $Lang_To_Syntax{$args{'lang'}}
Aresult
11
00
137 );
138 $payload{title} = $args{name}
139 1 ▶ if length $args{name};
Branch: 1 branch
BranchTrueFalseTotal
if length $args{"name"} 1 0 1
140  
141 1 my $json = JSON::PP->new->utf8->canonical;
142 1 my $res = $mech->post(
143 "$Base/api/v1/pastes",
144 'Content-Type' => 'application/json',
145 Content => $json->encode( \%payload ),
146 );
147  
148 1 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 1 1
149 0 my $msg = $res->status_line;
150 0 my $body = eval { $json->decode( $res->decoded_content ) };
151 $msg .= " ($body->{error})"
152 0 ▶ if ref $body eq 'HASH' and defined $body->{error};
Branch: 1 branch
BranchTrueFalseTotal
if ref $body eq "HASH" and defined $body->{"error"} 0 0 0
Condition0%0/3
ref $body eq "HASH" and defined $body->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $body eq "HASH" and defined $body->{"error"}
ref $body eq "HASH" defined $body->{"error"}
Truth table0%0/3
A and B
A= ref $body eq "HASH"B= defined $body->{"error"}
ABresult
0X0
100
111
153 0 return $self->_set_error( "Network error: $msg" );
154 }
155  
156 1 my $data = eval { $json->decode( $res->decoded_content ) };
157 return $self->_set_error('Failed to parse response from pastebin.ca')
158 1 ▶ unless ref $data eq 'HASH' and defined $data->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and defined $data->{"url"} 0 1 1
Condition33%1/3
ref $data eq "HASH" and defined $data->{"url"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $data eq "HASH" and defined $data->{"url"}
ref $data eq "HASH" defined $data->{"url"}
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= defined $data->{"url"}
ABresult
0X0
100
111
159  
160 1 return $self->paste_uri( URI->new( $data->{url} ) );
161 }
162  
163 # Fetch a proof-of-work challenge and brute-force a nonce whose
164 # sha256(prefix . nonce) has `difficulty_bits` leading zero bits.
165 1 ▶sub _solve_pow {
Subroutine _solve_pow: called CC=14 SCAR=28.0
166 1 my $self = shift;
167 1 my $mech = $self->mech;
168  
169 1 my $res = $mech->get("$Base/api/v1/pastes/pow-challenge");
170 1 ▶ return $self->_set_error(
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 1 1
171 'Network error: ' . $res->status_line
172 ) unless $res->is_success;
173  
174 1 my $c = eval { JSON::PP->new->utf8->decode( $res->decoded_content ) };
175 return $self->_set_error('Failed to parse proof-of-work challenge')
176 unless ref $c eq 'HASH'
177 and defined $c->{prefix}
178 and defined $c->{challenge_id}
179 1 ▶ and defined $c->{difficulty_bits};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" and defined $c->{"prefix"} and defined $c->{"challenge_id"} and defined $c->{"difficulty_bits"} 0 1 1
Condition33%3/9
ref $c eq "HASH" and defined $c->{"prefix"} and defined $c->{"challenge_id"} and defined $c->{"difficulty_bits"}
!ll&&!rl&&r
001
ref $c eq "HASH" and defined $c->{"prefix"} and defined $c->{"challenge_id"}
!ll&&!rl&&r
001
ref $c eq "HASH" and defined $c->{"prefix"}
!ll&&!rl&&r
001
MC/DC0%0/4
ref $c eq "HASH" and defined $c->{"prefix"} and defined $c->{"challenge_id"} and defined $c->{"difficulty_bits"}
ref $c eq "HASH" defined $c->{"prefix"} defined $c->{"challenge_id"} defined $c->{"difficulty_bits"}
Truth table20%1/5
A and B and C and D
A= ref $c eq "HASH"B= defined $c->{"prefix"}C= defined $c->{"challenge_id"}D= defined $c->{"difficulty_bits"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
180  
181 1 my $bits = $c->{difficulty_bits};
182 1 my $prefix = $c->{prefix};
183 1 my $full_bytes = int( $bits / 8 );
184 1 my $rem_bits = $bits % 8;
185 1 ▶ my $rem_mask = $rem_bits ? ( ( 0xFF << ( 8 - $rem_bits ) ) & 0xFF ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rem_bits ? : 1 0 1
186  
187 1 ▶ for ( my $nonce = 0; $nonce <= 2_000_000_000; $nonce++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nonce <= 2000000000) 3113431 0 3113431
188 3113431 my $hash = Digest::SHA::sha256( $prefix . $nonce );
189 3113431 my $ok = 1;
190 3113431 for my $i ( 0 .. $full_bytes - 1 ) {
191 3125574 ▶ if ( ord substr $hash, $i, 1 ) { $ok = 0; last }
Branch: 1 branch
BranchTrueFalseTotal
if (ord substr($hash, $i, 1)) 3113389 12185 3125574
192 }
193 3113431 ▶ if ( $ok and $rem_mask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and $rem_mask) 42 3113389 3113431
Condition66%2/3
$ok and $rem_mask
!ll&&!rl&&r
3113389042
MC/DC50%1/2
$ok and $rem_mask
$ok $rem_mask
Truth table66%2/3
A and B
A= $okB= $rem_mask
ABresult
0X0
100
111
194 42 ▶ $ok = 0 if ord( substr $hash, $full_bytes, 1 ) & $rem_mask;
Branch: 1 branch
BranchTrueFalseTotal
if ord substr($hash, $full_bytes, 1) & $rem_mask 41 1 42
195 }
196 3113431 ▶ if ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 3113430 3113431
197 1 return { challenge_id => $c->{challenge_id}, nonce => $nonce };
198 }
199 }
200  
201 0 return $self->_set_error('Failed to solve proof-of-work challenge');
202 }
203  
204 0 ▶sub _set_error {
Subroutine _set_error: not called CC=1 SCAR=6.9
205 0 my ( $self, $error ) = @_;
206 0 $self->error( $error );
207 0 return;
208 }
209  
210 3 ▶sub valid_langs {
Subroutine valid_langs: called CC=1 SCAR=0.0
Pod documented
211 return (
212 3 1 => 'Raw',
213 2 => 'Asterisk Configuration',
214 3 => 'C Source',
215 4 => 'C++ Source',
216 5 => 'PHP Source',
217 6 => 'Perl Source',
218 7 => 'Java Source',
219 8 => 'Visual Basic Source',
220 9 => 'C# Source',
221 10 => 'Ruby Source',
222 11 => 'Python Source',
223 12 => 'Pascal Source',
224 13 => 'mIRC Script',
225 14 => 'PL/I Source',
226 15 => 'XML Document',
227 16 => 'SQL Statement',
228 17 => 'Scheme Source',
229 18 => 'Action Script',
230 19 => 'Ada Source',
231 20 => 'Apache Configuration',
232 21 => 'Assembly (NASM)',
233 22 => 'ASP',
234 23 => 'BASH Script',
235 24 => 'CSS',
236 25 => 'Delphi Source',
237 26 => 'HTML 4.0 Strict',
238 27 => 'JavaScript',
239 28 => 'LISP Source',
240 29 => 'Lua Source',
241 30 => 'Microprocessor ASM',
242 31 => 'Objective C',
243 32 => 'Visual Basic .NET',
244 33 => 'Script Log',
245 34 => 'Diff / Patch',
246 );
247 }
248  
249 3 ▶sub valid_expires {
Subroutine valid_expires: called CC=1 SCAR=0.0
Pod documented
250 return (
251 3 '2 hours',
252 '4 hours',
253 '1 year',
254 '2 weeks',
255 '45 minutes',
256 '2 months',
257 '30 minutes',
258 '1 week',
259 '1 hour',
260 '15 minutes',
261 '10 minutes',
262 '3 days',
263 '5 months',
264 '4 months',
265 '5 minutes',
266 '8 hours',
267 '2 days',
268 '3 months',
269 '1 day',
270 '12 hours',
271 '3 weeks',
272 '6 months',
273 '1 month',
274 );
275 }
276  
277 6 ▶sub mech {
Subroutine mech: called CC=2 SCAR=6.9
Pod documented
278 6 my $self = shift;
279 6 ▶ if ( @_ ) { $self->{MECH} = shift };
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 4 6
280 6 return $self->{MECH};
281 }
282  
283 4 ▶sub paste_uri {
Subroutine paste_uri: called CC=2 SCAR=6.9
Pod documented
284 4 my $self = shift;
285 4 ▶ if ( @_ ) { $self->{PASTE_URI} = shift };
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
286 4 return $self->{PASTE_URI};
287 }
288  
289 1 ▶sub error {
Subroutine error: called CC=2 SCAR=7.0
Pod documented
290 1 my $self = shift;
291 1 ▶ if ( @_ ) { $self->{ERROR} = shift };
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
292 1 return $self->{ERROR};
293 }
294  
295 3 1;
296 __END__
↑ Summary