]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd.t
nntpd: reject control characters entirely
[public-inbox.git] / t / nntpd.t
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 foreach my $mod (qw(DBD::SQLite Search::Xapian Danga::Socket)) {
7         eval "require $mod";
8         plan skip_all => "$mod missing for nntpd.t" if $@;
9 }
10 require PublicInbox::SearchIdx;
11 require PublicInbox::Msgmap;
12 use Cwd;
13 use Email::Simple;
14 use IO::Socket;
15 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
16 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
17 use File::Temp qw/tempdir/;
18 use Net::NNTP;
19
20 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
21 my $home = "$tmpdir/pi-home";
22 my $err = "$tmpdir/stderr.log";
23 my $out = "$tmpdir/stdout.log";
24 my $maindir = "$tmpdir/main.git";
25 my $group = 'test-nntpd';
26 my $addr = $group . '@example.com';
27 my $cfgpfx = "publicinbox.$group";
28 my $nntpd = 'blib/script/public-inbox-nntpd';
29 my $init = 'blib/script/public-inbox-init';
30 use_ok 'PublicInbox::Import';
31 use_ok 'PublicInbox::Git';
32
33 my %opts = (
34         LocalAddr => '127.0.0.1',
35         ReuseAddr => 1,
36         Proto => 'tcp',
37         Type => SOCK_STREAM,
38         Listen => 1024,
39 );
40 my $sock = IO::Socket::INET->new(%opts);
41 my $pid;
42 my $len;
43 END { kill 'TERM', $pid if defined $pid };
44 {
45         local $ENV{HOME} = $home;
46         system($init, $group, $maindir, 'http://example.com/', $addr);
47         my $len;
48
49         # ensure successful message delivery
50         {
51                 my $mime = Email::MIME->new(<<EOF);
52 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
53 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
54 Cc: $addr
55 Message-Id: <nntp\@example.com>
56 Content-Type: text/plain; charset=utf-8
57 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
58 Date: Thu, 01 Jan 1970 06:06:06 +0000
59 Content-Transfer-Encoding: 8bit
60
61 This is a test message for El\xc3\xa9anor
62 EOF
63                 $mime->header_set('List-Id', "<$addr>");
64                 $len = length($mime->as_string);
65                 my $git = PublicInbox::Git->new($maindir);
66                 my $im = PublicInbox::Import->new($git, 'test', $addr);
67                 $im->add($mime);
68                 $im->done;
69                 my $s = PublicInbox::SearchIdx->new($maindir, 1);
70                 $s->index_sync;
71         }
72
73         ok($sock, 'sock created');
74         $! = 0;
75         my $fl = fcntl($sock, F_GETFD, 0);
76         ok(! $!, 'no error from fcntl(F_GETFD)');
77         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
78         $pid = fork;
79         if ($pid == 0) {
80                 use POSIX qw(dup2);
81                 # pretend to be systemd
82                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
83                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
84                 $ENV{LISTEN_PID} = $$;
85                 $ENV{LISTEN_FDS} = 1;
86                 exec $nntpd, "--stdout=$out", "--stderr=$err";
87                 die "FAIL: $!\n";
88         }
89         ok(defined $pid, 'forked nntpd process successfully');
90         $! = 0;
91         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
92         ok(! $!, 'no error from fcntl(F_SETFD)');
93         my $host_port = $sock->sockhost . ':' . $sock->sockport;
94         my $n = Net::NNTP->new($host_port);
95         my $list = $n->list;
96         is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
97         is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
98
99         %opts = (
100                 PeerAddr => $host_port,
101                 Proto => 'tcp',
102                 Type => SOCK_STREAM,
103                 Timeout => 1,
104         );
105         my $mid = '<nntp@example.com>';
106         my %xhdr = (
107                 'message-id' => $mid,
108                 subject => "Testing for El\xc3\xa9anor",
109                 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
110                 'from' => "El\xc3\xa9anor <me\@example.com>",
111                 'to' => "El\xc3\xa9anor <you\@example.com>",
112                 'cc' => $addr,
113                 'xref' => "example.com $group:1"
114         );
115
116         my $s = IO::Socket::INET->new(%opts);
117         sysread($s, my $buf, 4096);
118         is($buf, "201 server ready - post via email\r\n", 'got greeting');
119         $s->autoflush(1);
120
121         syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
122         is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
123
124         $s = IO::Socket::INET->new(%opts);
125         sysread($s, $buf, 4096);
126         is($buf, "201 server ready - post via email\r\n", 'got greeting');
127         $s->autoflush(1);
128
129         syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
130         $buf = read_til_dot($s);
131         like($buf, qr/\A231 list of /, 'newgroups OK');
132
133         while (my ($k, $v) = each %xhdr) {
134                 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
135                           "XHDR $k by message-id works");
136                 is_deeply($n->xhdr("$k 1"), { 1 => $v },
137                           "$k by article number works");
138                 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
139                           "$k by article range works");
140                 $buf = '';
141                 syswrite($s, "HDR $k $mid\r\n");
142                 $buf = read_til_dot($s);
143                 my @r = split("\r\n", $buf);
144                 like($r[0], qr/\A225 /, '225 response for HDR');
145                 is($r[1], "0 $v", 'got expected response for HDR');
146         }
147
148         {
149                 my $nogroup = Net::NNTP->new($host_port);
150                 while (my ($k, $v) = each %xhdr) {
151                         is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
152                                   "$k by message-id works without group");
153                 }
154         }
155
156         is_deeply($n->xover('1-'), {
157                 '1' => ["Testing for El\xc3\xa9anor",
158                         "El\xc3\xa9anor <me\@example.com>",
159                         'Thu, 01 Jan 1970 06:06:06 +0000',
160                         '<nntp@example.com>',
161                         '',
162                         $len,
163                         '1' ] }, "XOVER range works");
164
165         is_deeply($n->xover('1'), {
166                 '1' => ["Testing for El\xc3\xa9anor",
167                         "El\xc3\xa9anor <me\@example.com>",
168                         'Thu, 01 Jan 1970 06:06:06 +0000',
169                         '<nntp@example.com>',
170                         '',
171                         $len,
172                         '1' ] }, "XOVER by article works");
173
174         {
175                 syswrite($s, "OVER $mid\r\n");
176                 $buf = read_til_dot($s);
177                 my @r = split("\r\n", $buf);
178                 like($r[0], qr/^224 /, 'got 224 response for OVER');
179                 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
180                         "El\xc3\xa9anor <me\@example.com>\t" .
181                         "Thu, 01 Jan 1970 06:06:06 +0000\t" .
182                         "$mid\t\t$len\t1", 'OVER by Message-ID works');
183                 is($r[2], '.', 'correctly terminated response');
184         }
185
186         is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
187                  'XHDR Cc 1- works');
188         is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
189                  'XHDR References 1- works (empty string)');
190         is_deeply($n->xhdr(qw(list-id 1-)), {},
191                  'XHDR on invalid header returns empty');
192
193         {
194                 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
195                 syswrite($s, 'HDR List-id 1-');
196                 select(undef, undef, undef, 0.15);
197                 ok(kill('TERM', $pid), 'killed nntpd');
198                 select(undef, undef, undef, 0.15);
199                 syswrite($s, "\r\n");
200                 $buf = '';
201                 do {
202                         sysread($s, $buf, 4096, length($buf));
203                 } until ($buf =~ /\r\n\z/);
204                 my @r = split("\r\n", $buf);
205                 like($r[0], qr/^5\d\d /,
206                         'got 5xx response for unoptimized HDR');
207                 is(scalar @r, 1, 'only one response line');
208         }
209
210         is($pid, waitpid($pid, 0), 'nntpd exited successfully');
211         my $eout = eval {
212                 local $/;
213                 open my $fh, '<', $err or die "open $err failed: $!";
214                 <$fh>;
215         };
216         is($?, 0, 'no error in exited process');
217         unlike($eout, qr/wide/i, 'no Wide character warnings');
218 }
219
220 done_testing();
221
222 sub read_til_dot {
223         my ($s) = @_;
224         my $buf = '';
225         do {
226                 sysread($s, $buf, 4096, length($buf));
227         } until ($buf =~ /\r\n\.\r\n\z/);
228         $buf;
229 }
230
231 1;