/
DecodeShortURLs.pm
961 lines (754 loc) · 26.3 KB
/
DecodeShortURLs.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
=head1 NAME
DecodeShortURLs - Check for shortened URLs
=head1 SYNOPSIS
loadplugin Mail::SpamAssassin::Plugin::DecodeShortURLs
url_shortener tinyurl.com
url_shortener_get bit.ly
body HAS_SHORT_URL eval:short_url()
describe HAS_SHORT_URL Message has one or more shortened URLs
body SHORT_URL_REDIR eval:short_url_redir()
describe SHORT_URL_REDIR Message has shortened URL that resulted in a valid redirection
body SHORT_URL_CHAINED eval:short_url_chained()
describe SHORT_URL_CHAINED Message has shortened URL chained to other shorteners
body SHORT_URL_MAXCHAIN eval:short_url_maxchain()
describe SHORT_URL_MAXCHAIN Message has shortened URL that causes too many redirections
body SHORT_URL_LOOP eval:short_url_loop()
describe SHORT_URL_LOOP Message has short URL that loops back to itself
body SHORT_URL_200 eval:short_url_code('200') # Can check any non-redirect HTTP code
describe SHORT_URL_200 Message has shortened URL returning HTTP 200
body SHORT_URL_404 eval:short_url_code('404') # Can check any non-redirect HTTP code
describe SHORT_URL_404 Message has shortened URL returning HTTP 404
uri URI_TINYURL_BLOCKED m,https://tinyurl\.com/app/nospam,
describe URI_TINYURL_BLOCKED Message contains a tinyurl that has been disabled due to abuse
uri URI_BITLY_BLOCKED m,^https://bitly\.com/a/blocked,
describe URI_BITLY_BLOCKED Message contains a bit.ly URL that has been disabled due to abuse
=head1 DESCRIPTION
This plugin looks for URLs shortened by a list of URL shortening services.
Upon finding a matching URL, plugin will send a HTTP request to the
shortening service and retrieve the Location-header which points to the
actual shortened URL. It then adds this URL to the list of URIs extracted
by SpamAssassin which can then be accessed by uri rules and plugins such as
URIDNSBL.
This plugin will follow chained redirections, where a short URL redirects to
another short URL. Redirection depth limit can be set with
C<max_short_url_redirections>.
Maximum of C<max_short_urls> short URLs are checked in a message (10 by
default). Setting it to 0 disables HTTP requests, allowing only short_url()
test to work and report found shorteners.
All supported rule types for checking short URLs and redirection status are
documented in L<SYNOPSIS> section.
=head1 NOTES
This plugin runs at the check_dnsbl hook (priority -100) so that it may
modify the parsed URI list prior to normal uri rules or the URIDNSBL plugin.
=cut
package Mail::SpamAssassin::Plugin::DecodeShortURLs;
use Mail::SpamAssassin::Plugin;
use strict;
use warnings;
use vars qw(@ISA);
@ISA = qw(Mail::SpamAssassin::Plugin);
my $VERSION = 4.00;
use constant HAS_LWP_USERAGENT => eval { require LWP::UserAgent; require LWP::Protocol::https; };
sub dbg { my $msg = shift; return Mail::SpamAssassin::Logger::dbg("DecodeShortURLs: $msg", @_); }
sub info { my $msg = shift; return Mail::SpamAssassin::Logger::info("DecodeShortURLs: $msg", @_); }
sub new {
my $class = shift;
my $mailsaobject = shift;
$class = ref($class) || $class;
my $self = $class->SUPER::new($mailsaobject);
bless ($self, $class);
if ($mailsaobject->{local_tests_only}) {
dbg("local tests only, disabling HTTP requests");
$self->{net_disabled} = 1;
}
elsif (!HAS_LWP_USERAGENT) {
dbg("module LWP::UserAgent not installed, disabling HTTP requests");
$self->{net_disabled} = 1;
}
$self->set_config($mailsaobject->{conf});
$self->register_method_priority ('check_dnsbl', -10);
$self->register_eval_rule('short_url', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_redir', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_200', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_404', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_code', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_chained', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_maxchain', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_loop', $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->register_eval_rule('short_url_tests'); # for legacy plugin compatibility warning
return $self;
}
=head1 USER SETTINGS
=over 4
=item url_shortener domain [domain...] (default: none)
Domains that should be considered as an URL shortener. If the domain begins
with a '.', 3rd level tld of the main domain will be checked.
Example:
url_shortener tinyurl.com
url_shortener .page.link
=back
=over 4
=item url_shortener_get domain [domain...] (default: none)
Alias to C<url_shortener>. HTTP request will be done with GET method,
instead of default HEAD. Required for some services like bit.ly to return
blocked URL correctly.
Example:
url_shortener_get bit.ly
=back
=cut
sub set_config {
my($self, $conf) = @_;
my @cmds = ();
push (@cmds, {
setting => 'url_shortener',
default => {},
type => $Mail::SpamAssassin::Conf::CONF_TYPE_HASH_KEY_VALUE,
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value eq '') {
return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE;
}
foreach my $domain (split(/\s+/, $value)) {
$self->{url_shortener}->{lc $domain} = 1; # 1 == head
}
}
});
push (@cmds, {
setting => 'url_shortener_get',
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value eq '') {
return $Mail::SpamAssassin::Conf::MISSING_REQUIRED_VALUE;
}
foreach my $domain (split(/\s+/, $value)) {
$self->{url_shortener}->{lc $domain} = 2; # 2 == get
}
}
});
=over 4
=item clear_url_shortener [domain] [domain...]
Clear configured url_shortener and url_shortener_get domains, for example to
override default settings from an update channel. If domains are specified,
then only those are removed from list.
=back
=cut
push (@cmds, {
setting => 'clear_url_shortener',
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value eq '') {
$self->{url_shortener} = {};
} else {
foreach my $domain (split(/\s+/, $value)) {
delete $self->{url_shortener}->{lc $domain};
}
}
}
});
=head1 PRIVILEGED SETTINGS
=over 4
=item url_shortener_cache_type (default: none)
The cache type that is being utilized. Currently only supported value is
C<dbi> that implies C<url_shortener_cache_dsn> is a DBI connect string.
DBI module is required.
Example:
url_shortener_cache_type dbi
=back
=cut
push (@cmds, {
setting => 'url_shortener_cache_type',
default => '',
is_priv => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING
});
=over 4
=item url_shortener_cache_dsn (default: none)
The DBI dsn of the database to use.
For SQLite, the database will be created automatically if it does not
already exist, the supplied path and file must be read/writable by the
user running spamassassin or spamd.
For MySQL/MariaDB or PostgreSQL, see sql-directory for database table
creation clauses.
You will need to have the proper DBI module for your database. For example
DBD::SQLite, DBD::mysql, DBD::MariaDB or DBD::Pg.
Minimum required SQLite version is 3.24.0 (available from DBD::SQLite 1.59_01).
Examples:
url_shortener_cache_dsn dbi:SQLite:dbname=/var/lib/spamassassin/DecodeShortURLs.db
=back
=cut
push (@cmds, {
setting => 'url_shortener_cache_dsn',
default => '',
is_priv => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING
});
=over 4
=item url_shortener_cache_username (default: none)
The username that should be used to connect to the database. Not used for
SQLite.
=back
=cut
push (@cmds, {
setting => 'url_shortener_cache_username',
default => '',
is_priv => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING
});
=over 4
=item url_shortener_cache_password (default: none)
The password that should be used to connect to the database. Not used for
SQLite.
=back
=cut
push (@cmds, {
setting => 'url_shortener_cache_password',
default => '',
is_priv => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING
});
=over 4
=item url_shortener_cache_ttl (default: 86400)
The length of time a cache entry will be valid for in seconds.
Default is 86400 (1 day).
See C<url_shortener_cache_autoclean> for database cleaning.
=back
=cut
push (@cmds, {
setting => 'url_shortener_cache_ttl',
is_admin => 1,
default => 86400,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC
});
=head1 ADMINISTRATOR SETTINGS
=over 4
=item url_shortener_cache_autoclean (default: 1000)
Automatically purge old entries from database. Value describes a random run
chance of 1/x. The default value of 1000 means that cleaning is run
approximately once for every 1000 messages processed. Value of 1 would mean
database is cleaned every time a message is processed.
Set 0 to disable automatic cleaning and to do it manually.
=back
=cut
push (@cmds, {
setting => 'url_shortener_cache_autoclean',
is_admin => 1,
default => 1000,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC
});
=over 4
=item url_shortener_loginfo (default: 0 (off))
If this option is enabled (set to 1), then short URLs and the decoded URLs will be logged with info priority.
=back
=cut
push (@cmds, {
setting => 'url_shortener_loginfo',
is_admin => 1,
default => 0,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL
});
=over 4
=item url_shortener_timeout (default: 5)
Maximum time a short URL HTTP request can take, in seconds.
=back
=cut
push (@cmds, {
setting => 'url_shortener_timeout',
is_admin => 1,
default => 5,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC
});
=over 4
=item max_short_urls (default: 10)
Maximum amount of short URLs that will be looked up per message. Chained
redirections are not counted, only initial short URLs found.
Setting it to 0 disables HTTP requests, allowing only short_url() test to
work and report any found shortener URLs.
=back
=cut
push (@cmds, {
setting => 'max_short_urls',
is_admin => 1,
default => 10,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC
});
=over 4
=item max_short_url_redirections (default: 10)
Maximum depth of chained redirections that a short URL can generate.
=back
=cut
push (@cmds, {
setting => 'max_short_url_redirections',
is_admin => 1,
default => 10,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC
});
=over 4
=item url_shortener_user_agent (default: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36)
Set User-Agent header for HTTP requests. Some services require it to look
like a common browser.
=back
=cut
push (@cmds, {
setting => 'url_shortener_user_agent',
is_admin => 1,
default => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING
});
$conf->{parser}->register_commands(\@cmds);
}
=head1 ACKNOWLEDGEMENTS
Original DecodeShortURLs plugin was developed by Steve Freegard.
=cut
sub short_url_tests {
# Legacy compatibility warning done in finish_parsing_start
return 0;
}
sub finish_parsing_start {
my ($self, $opts) = @_;
if ($opts->{conf}->{eval_to_rule}->{short_url_tests}) {
warn "DecodeShortURLs: Legacy configuration format detected. ".
"Eval function short_url_tests() is no longer supported, ".
"please see documentation for the new rule format.\n";
}
}
sub initialise_url_shortener_cache {
my ($self, $conf) = @_;
return if $self->{dbh};
return if !$conf->{url_shortener_cache_type};
if (!$conf->{url_shortener_cache_dsn}) {
warn "DecodeShortURLs: invalid cache configuration\n";
return;
}
##
## SQLite
##
if ($conf->{url_shortener_cache_type} =~ /^(?:dbi|sqlite)$/i
&& $conf->{url_shortener_cache_dsn} =~ /^dbi:SQLite/)
{
eval {
local $SIG{'__DIE__'};
require DBI;
require DBD::SQLite;
DBD::SQLite->VERSION(1.59_01); # Required for ON CONFLICT
$self->{dbh} = DBI->connect_cached(
$conf->{url_shortener_cache_dsn}, '', '',
{RaiseError => 1, PrintError => 0, InactiveDestroy => 1, AutoCommit => 1}
);
$self->{dbh}->do("
CREATE TABLE IF NOT EXISTS short_url_cache (
short_url TEXT PRIMARY KEY NOT NULL,
decoded_url TEXT NOT NULL,
hits INTEGER NOT NULL DEFAULT 1,
created INTEGER NOT NULL,
modified INTEGER NOT NULL
)
");
# Maintaining index for cleaning is likely more expensive than occasional full table scan
#$self->{dbh}->do("
# CREATE INDEX IF NOT EXISTS short_url_modified
# ON short_url_cache(created)
#");
$self->{sth_insert} = $self->{dbh}->prepare("
INSERT INTO short_url_cache (short_url, decoded_url, created, modified)
VALUES (?,?,strftime('%s','now'),strftime('%s','now'))
ON CONFLICT(short_url) DO UPDATE
SET decoded_url = excluded.decoded_url,
modified = excluded.modified,
hits = hits + 1
");
$self->{sth_select} = $self->{dbh}->prepare("
SELECT decoded_url FROM short_url_cache
WHERE short_url = ?
");
$self->{sth_delete} = $self->{dbh}->prepare("
DELETE FROM short_url_cache
WHERE short_url = ? AND created < strftime('%s','now') - $conf->{url_shortener_cache_ttl}
");
$self->{sth_clean} = $self->{dbh}->prepare("
DELETE FROM short_url_cache
WHERE created < strftime('%s','now') - $conf->{url_shortener_cache_ttl}
");
};
}
##
## MySQL/MariaDB
##
elsif (lc $conf->{url_shortener_cache_type} eq 'dbi'
&& $conf->{url_shortener_cache_dsn} =~ /^dbi:(?:mysql|MariaDB)/i)
{
eval {
local $SIG{'__DIE__'};
require DBI;
$self->{dbh} = DBI->connect_cached(
$conf->{url_shortener_cache_dsn},
$conf->{url_shortener_cache_username},
$conf->{url_shortener_cache_password},
{RaiseError => 1, PrintError => 0, InactiveDestroy => 1, AutoCommit => 1}
);
$self->{sth_insert} = $self->{dbh}->prepare("
INSERT INTO short_url_cache (short_url, decoded_url, created, modified)
VALUES (?,?,UNIX_TIMESTAMP(),UNIX_TIMESTAMP())
ON DUPLICATE KEY UPDATE
decoded_url = VALUES(decoded_url),
modified = VALUES(modified),
hits = hits + 1
");
$self->{sth_select} = $self->{dbh}->prepare("
SELECT decoded_url FROM short_url_cache
WHERE short_url = ?
");
$self->{sth_delete} = $self->{dbh}->prepare("
DELETE FROM short_url_cache
WHERE short_url = ? AND created < UNIX_TIMESTAMP() - $conf->{url_shortener_cache_ttl}
");
$self->{sth_clean} = $self->{dbh}->prepare("
DELETE FROM short_url_cache
WHERE created < UNIX_TIMESTAMP() - $conf->{url_shortener_cache_ttl}
");
};
}
##
## PostgreSQL
##
elsif (lc $conf->{url_shortener_cache_type} eq 'dbi'
&& $conf->{url_shortener_cache_dsn} =~ /^dbi:Pg/i)
{
eval {
local $SIG{'__DIE__'};
require DBI;
$self->{dbh} = DBI->connect_cached(
$conf->{url_shortener_cache_dsn},
$conf->{url_shortener_cache_username},
$conf->{url_shortener_cache_password},
{RaiseError => 1, PrintError => 0, InactiveDestroy => 1, AutoCommit => 1}
);
$self->{sth_insert} = $self->{dbh}->prepare("
INSERT INTO short_url_cache (short_url, decoded_url, created, modified)
VALUES (?,?,CAST(EXTRACT(epoch FROM NOW()) AS INT),CAST(EXTRACT(epoch FROM NOW()) AS INT))
ON CONFLICT (short_url) DO UPDATE SET
decoded_url = EXCLUDED.decoded_url,
modified = EXCLUDED.modified,
hits = short_url_cache.hits + 1
");
$self->{sth_select} = $self->{dbh}->prepare("
SELECT decoded_url FROM short_url_cache
WHERE short_url = ?
");
$self->{sth_delete} = $self->{dbh}->prepare("
DELETE FROM short_url_cache
WHERE short_url = ? AND created < CAST(EXTRACT(epoch FROM NOW()) AS INT) - $conf->{url_shortener_cache_ttl}
");
$self->{sth_clean} = $self->{dbh}->prepare("
DELETE FROM short_url_cache
WHERE created < CAST(EXTRACT(epoch FROM NOW()) AS INT) - $conf->{url_shortener_cache_ttl}
");
};
##
## ...
##
} else {
warn "DecodeShortURLs: invalid cache configuration\n";
return;
}
if ($@ || !$self->{sth_clean}) {
warn "DecodeShortURLs: cache connect failed: $@\n";
undef $self->{dbh};
undef $self->{sth_insert};
undef $self->{sth_select};
undef $self->{sth_delete};
undef $self->{sth_clean};
}
}
sub short_url {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url} ? 1 : 0;
}
sub short_url_redir {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url_redir} ? 1 : 0;
}
sub short_url_200 {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url_200} ? 1 : 0;
}
sub short_url_404 {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url_404} ? 1 : 0;
}
sub short_url_code {
my ($self, $pms, undef, $code) = @_;
# Make sure checks are run
$self->_check_short($pms);
return 0 unless defined $code && $code =~ /^\d{3}$/;
return $pms->{"short_url_$code"} ? 1 : 0;
}
sub short_url_chained {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url_chained} ? 1 : 0;
}
sub short_url_maxchain {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url_maxchain} ? 1 : 0;
}
sub short_url_loop {
my ($self, $pms) = @_;
# Make sure checks are run
$self->_check_short($pms);
return $pms->{short_url_loop} ? 1 : 0;
}
sub _check_shortener_uri {
my ($uri, $conf) = @_;
local($1,$2);
return 0 unless $uri =~ m{^
https?:// # Only http
(?:[^\@/?#]*\@)? # Ignore user:pass@
([^/?#:]+) # (Capture hostname)
(?::\d+)? # Possible port
(.*?\w)? # Some path wanted
}ix;
my $host = lc $1;
my $has_path = defined $2;
my $levels = $host =~ tr/.//;
# No point looking at single level "xxx.yy" without a path
return if $levels == 1 && !$has_path;
if (exists $conf->{url_shortener}->{$host}) {
return {
'uri' => $uri,
'method' => $conf->{url_shortener}->{$host} == 1 ? 'head' : 'get',
};
}
# if domain is a 3rd level domain check if there is a url shortener
# on the www domain
elsif($levels == 2 && $host =~ /^www\.([^.]+\.[^.]+)$/i) {
my $domain = $1;
if(($host eq "www.$domain") and exists $conf->{url_shortener}->{$domain}) {
dbg("Found internal www redirection for domain $domain");
return {
'uri' => $uri,
'method' => $conf->{url_shortener}->{$domain} == 1 ? 'head' : 'get',
};
}
}
# if domain is a 3rd level domain check if there is a url shortener
# on the 2nd level tld
elsif ($levels == 2 && $host =~ /^(?!www)[^.]+(\.[^.]+\.[^.]+)$/i &&
exists $conf->{url_shortener}->{$1}) {
return {
'uri' => $uri,
'method' => $conf->{url_shortener}->{$1} == 1 ? 'head' : 'get',
};
}
return;
}
sub check_dnsbl {
my ($self, $opts) = @_;
$self->_check_short($opts->{permsgstatus});
}
sub _check_short {
my ($self, $pms) = @_;
return if $pms->{short_url_checked}++;
my $conf = $pms->{conf};
# Sort short URLs into hash to de-dup them
my %short_urls;
my $uris = $pms->get_uri_detail_list();
while (my($uri, $info) = each %{$uris}) {
next unless $info->{domains} && $info->{cleaned};
# Remove anchors and parameters from shortened uris
$uri =~ s/(?:\#|\?).*//g;
if (my $short_url_info = _check_shortener_uri($uri, $conf)) {
$short_urls{$uri} = $short_url_info;
last if scalar keys %short_urls >= $conf->{max_short_urls};
}
}
# Bail out if no shortener was found
return unless %short_urls;
# Mark that a URL shortener was found
$pms->{short_url} = 1;
# Bail out if network lookups not enabled or max_short_urls 0
return if $self->{net_disabled};
return if !$conf->{max_short_urls};
# Initialize cache
$self->initialise_url_shortener_cache($conf);
# Initialize LWP
my $ua = LWP::UserAgent->new(
'agent' => $conf->{url_shortener_user_agent},
'max_redirect' => 0,
'timeout' => $conf->{url_shortener_timeout},
);
$ua->env_proxy;
# Launch HTTP requests
foreach my $uri (keys %short_urls) {
$self->recursive_lookup($short_urls{$uri}, $pms, $ua);
}
# Automatically purge old entries
if ($self->{dbh} && $conf->{url_shortener_cache_autoclean}
&& rand() < 1/$conf->{url_shortener_cache_autoclean})
{
dbg("cleaning stale cache entries");
eval { $self->{sth_clean}->execute(); };
if ($@) { dbg("cache cleaning failed: $@"); }
}
}
sub recursive_lookup {
my ($self, $short_url_info, $pms, $ua, %been_here) = @_;
my $conf = $pms->{conf};
my $count = scalar keys %been_here;
dbg("redirection count $count") if $count;
if ($count >= $conf->{max_short_url_redirections}) {
dbg("found more than $conf->{max_short_url_redirections} shortener redirections");
# Fire test
$pms->{short_url_maxchain} = 1;
return;
}
my $short_url = $short_url_info->{uri};
my $location;
if (defined($location = $self->cache_get($short_url))) {
if ($conf->{url_shortener_loginfo}) {
info("found cached $short_url => $location");
} else {
dbg("found cached $short_url => $location");
}
# Cached http code?
if ($location =~ /^\d{3}$/) {
$pms->{"short_url_$location"} = 1;
# Update cache
$self->cache_add($short_url, $location);
return;
}
} else {
# Not cached; do lookup
my $method = $short_url_info->{method};
my $response = $ua->$method($short_url);
if (!$response->is_redirect) {
dbg("URL is not redirect: $short_url = ".$response->status_line);
my $rcode = $response->code;
if ($rcode =~ /^\d{3}$/) {
$pms->{"short_url_$rcode"} = 1;
# Update cache
$self->cache_add($short_url, $rcode);
}
return;
}
$location = $response->headers->{location};
if ($conf->{url_shortener_loginfo}) {
info("found $short_url => $location");
} else {
dbg("found $short_url => $location");
}
}
# Update cache
$self->cache_add($short_url, $location);
# Bail out if $short_url redirects to itself
if ($short_url eq $location) {
dbg("URL is redirect to itself");
return;
}
# At this point we have a valid redirection and new URL in $response
$pms->{short_url_redir} = 1;
# Set chained here otherwise we might mark a disabled page or
# redirect back to the same host as chaining incorrectly.
$pms->{short_url_chained} = 1 if $count;
# Check if it is a redirection to a relative URI
# Make it an absolute URI and chain to it in that case
if ($location !~ m{^[a-z]+://}i) {
my $orig_location = $location;
my $orig_short_url = $short_url;
# Strip to..
if (index($location, '/') == 0) {
$short_url =~ s{^([a-z]+://.*?)[/?#].*}{$1}; # ..absolute path base is http://example.com
} else {
$short_url =~ s{^([a-z]+://.*/)}{$1}; # ..relative path base is http://example.com/a/b/
}
$location = "$short_url$location";
dbg("looks like a redirection to a relative URI: $orig_short_url => $location ($orig_location)");
}
if (exists $been_here{$location}) {
# Loop detected
dbg("error: loop detected: $location");
$pms->{short_url_loop} = 1;
return;
}
$been_here{$location} = 1;
$pms->add_uri_detail_list($location) if !$pms->{uri_detail_list}->{$location};
# Check for recursion
if (my $short_url_info = _check_shortener_uri($location, $conf)) {
# Recurse...
$self->recursive_lookup($short_url_info, $pms, $ua, %been_here);
}
}
sub cache_add {
my ($self, $short_url, $decoded_url) = @_;
return if !$self->{dbh};
return if length($short_url) > 256 || length($decoded_url) > 512;
# Upsert
eval { $self->{sth_insert}->execute($short_url, $decoded_url); };
if ($@) {
dbg("could not add to cache: $@");
}
return;
}
sub cache_get {
my ($self, $key) = @_;
return if !$self->{dbh};
# Make sure expired entries are gone. Just a quick check for primary key,
# not that expensive.
eval { $self->{sth_delete}->execute($key); };
if ($@) {
dbg("cache delete failed: $@");
return;
}
# Now try to get it (don't bother parsing if something was deleted above,
# it would be rare event anyway)
eval { $self->{sth_select}->execute($key); };
if ($@) {
dbg("cache get failed: $@");
return;
}
my @row = $self->{sth_select}->fetchrow_array();
if (@row) {
return $row[0];
}
return;
}
# Version features
sub has_short_url { 1 }
sub has_autoclean { 1 }
sub has_short_url_code { 1 }
sub has_user_agent { 1 } # url_shortener_user_agent
sub has_get { 1 } # url_shortener_get
sub has_clear { 1 } # clear_url_shortener
sub has_timeout { 1 } # url_shortener_timeout
sub has_max_redirections { 1 } # max_short_url_redirections
# short_url() will always hit if matching url_shortener was found, even
# without HTTP requests. To check if a valid HTTP redirection response was
# seen, use short_url_redir().
sub has_short_url_redir { 1 }
1;