]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_bad_mids.t
config: we always have {-section_order}
[public-inbox.git] / t / psgi_bad_mids.t
1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use File::Temp qw/tempdir/;
7 use PublicInbox::MIME;
8 use PublicInbox::Config;
9 use PublicInbox::WWW;
10 my @mods = qw(DBD::SQLite HTTP::Request::Common Plack::Test
11                 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for psgi_bad_mids.t" if $@;
15 }
16 use_ok($_) for @mods;
17 use_ok 'PublicInbox::V2Writable';
18 my $mainrepo = tempdir('pi-bad-mids-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $cfgpfx = "publicinbox.bad-mids";
20 my $ibx = {
21         mainrepo => $mainrepo,
22         name => 'bad-mids',
23         version => 2,
24         -primary_address => 'test@example.com',
25         indexlevel => 'basic',
26 };
27 $ibx = PublicInbox::Inbox->new($ibx);
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
29 $im->{parallel} = 0;
30
31 my $msgs = <<'';
32 F1V5OR6NMF.3M649JTLO9IXD@tux.localdomain/hehe1"'<foo
33 F1V5NB0PTU.3U0DCVGAJ750Z@tux.localdomain"'<>/foo
34 F1V5MIHGCU.2ABINKW6WBE8N@tux.localdomain/raw
35 F1V5LF9D9C.2QT5PGXZQ050E@tux.localdomain/t.atom
36 F1V58X3CMU.2DCCVAKQZGADV@tux.localdomain/../../../../foo
37 F1TVKINT3G.2S6I36MXMHYG6@tux.localdomain" onclick="alert(1)"
38
39 my @mids = split(/\n/, $msgs);
40 my $i = 0;
41 foreach my $mid (@mids) {
42         my $data = << "";
43 Subject: test
44 Message-ID: <$mid>
45 From: a\@example.com
46 To: b\@example.com
47 Date: Fri, 02 Oct 1993 00:00:0$i +0000
48
49
50         my $mime = PublicInbox::MIME->new(\$data);
51         ok($im->add($mime), "added $mid");
52         $i++
53 }
54 $im->done;
55
56 my $cfg = <<EOF;
57 $cfgpfx.address=$ibx->{-primary_address}
58 $cfgpfx.mainrepo=$mainrepo
59 EOF
60 my $config = PublicInbox::Config->new(\$cfg);
61 my $www = PublicInbox::WWW->new($config);
62 test_psgi(sub { $www->call(@_) }, sub {
63         my ($cb) = @_;
64         my $res = $cb->(GET('/bad-mids/'));
65         is($res->code, 200, 'got 200 OK listing');
66         my $raw = $res->content;
67         foreach my $mid (@mids) {
68                 ok(index($raw, $mid) < 0, "escaped $mid");
69         }
70
71         my (@xmids) = ($raw =~ m!\bhref="([^"]+)/t\.mbox\.gz"!sg);
72         is(scalar(@xmids), scalar(@mids),
73                 'got escaped links to all messages');
74
75         @xmids = reverse @xmids;
76         foreach my $i (0..$#xmids) {
77                 $res = $cb->(GET("/bad-mids/$xmids[$i]/raw"));
78                 is($res->code, 200, 'got 200 OK raw message');
79                 like($res->content, qr/Message-ID: <\Q$mids[$i]\E>/s,
80                         'retrieved correct message');
81         }
82 });
83
84 done_testing();
85
86 1;