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
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
BEGIN {
my $git = `which git`;
chomp $git;
plan(skip_all => 'git not available') unless -x $git;
plan(skip_all => "CGI not available")
unless eval q{
use CGI qw();
1;
};
plan(skip_all => "IPC::Run not available")
unless eval q{
use IPC::Run qw(run);
1;
};
use_ok('IkiWiki');
use_ok('YAML::XS');
}
# We check for English error messages
$ENV{LC_ALL} = 'C';
use Cwd qw(getcwd);
use Errno qw(ENOENT);
my $installed = $ENV{INSTALLED_TESTS};
my @command;
if ($installed) {
@command = qw(ikiwiki);
}
else {
ok(! system("make -s ikiwiki.out"));
@command = ("perl", "-I".getcwd."/blib/lib", './ikiwiki.out',
'--underlaydir='.getcwd.'/underlays/basewiki',
'--set', 'underlaydirbase='.getcwd.'/underlays',
'--templatedir='.getcwd.'/templates');
}
sub write_old_file {
my $name = shift;
my $dir = shift;
my $content = shift;
writefile($name, $dir, $content);
ok(utime(333333333, 333333333, "$dir/$name"));
}
sub write_setup_file {
my %setup = (
wikiname => 'this is the name of my wiki',
srcdir => getcwd.'/t/tmp/in/doc',
destdir => getcwd.'/t/tmp/out',
url => 'http://example.com',
cgiurl => 'http://example.com/cgi-bin/ikiwiki.cgi',
cgi_wrapper => getcwd.'/t/tmp/ikiwiki.cgi',
cgi_wrappermode => '0751',
add_plugins => [qw(anonok attachment lockedit recentchanges)],
disable_plugins => [qw(emailauth openid passwordauth)],
anonok_pagespec => 'writable/*',
locked_pages => '!writable/*',
rcs => 'git',
git_wrapper => getcwd.'/t/tmp/in/.git/hooks/post-commit',
git_wrappermode => '0754',
gitorigin_branch => '',
);
unless ($installed) {
$setup{ENV} = { 'PERL5LIB' => getcwd.'/blib/lib' };
}
writefile("test.setup", "t/tmp",
"# IkiWiki::Setup::Yaml - YAML formatted setup file\n" .
Dump(\%setup));
}
sub thoroughly_rebuild {
ok(unlink("t/tmp/ikiwiki.cgi") || $!{ENOENT});
ok(unlink("t/tmp/in/.git/hooks/post-commit") || $!{ENOENT});
ok(! system(@command, qw(--setup t/tmp/test.setup --rebuild --wrappers)));
}
sub check_cgi_mode_bits {
my $mode;
(undef, undef, $mode, undef, undef,
undef, undef, undef, undef, undef,
undef, undef, undef) = stat('t/tmp/ikiwiki.cgi');
is ($mode & 07777, 0751);
(undef, undef, $mode, undef, undef,
undef, undef, undef, undef, undef,
undef, undef, undef) = stat('t/tmp/in/.git/hooks/post-commit');
is ($mode & 07777, 0754);
}
sub run_cgi {
my (%args) = @_;
my ($in, $out);
my $method = $args{method} || 'GET';
my $environ = $args{environ} || {};
my $params = $args{params} || { do => 'prefs' };
my %defaults = (
SCRIPT_NAME => '/cgi-bin/ikiwiki.cgi',
HTTP_HOST => 'example.com',
);
my $cgi = CGI->new($args{params});
my $query_string = $cgi->query_string();
if ($method eq 'POST') {
$defaults{REQUEST_METHOD} = 'POST';
$in = $query_string;
$defaults{CONTENT_LENGTH} = length $in;
} else {
$defaults{REQUEST_METHOD} = 'GET';
$defaults{QUERY_STRING} = $query_string;
}
my %envvars = (
%defaults,
%$environ,
);
run(["./t/tmp/ikiwiki.cgi"], \$in, \$out, init => sub {
map {
$ENV{$_} = $envvars{$_}
} keys(%envvars);
});
return $out;
}
sub run_git {
my (undef, $filename, $line) = caller;
my $args = shift;
my $desc = shift || join(' ', 'git', @$args);
my ($in, $out);
ok(run(['git', @$args], \$in, \$out, init => sub {
chdir 't/tmp/in' or die $!;
my $name = 'The IkiWiki Tests';
my $email = 'nobody@ikiwiki-tests.invalid';
if ($args->[0] eq 'commit') {
$ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
$ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
}
}), "$desc at $filename:$line");
return $out;
}
sub test {
my $content;
my $status;
ok(! system(qw(rm -rf t/tmp)));
ok(! system(qw(mkdir t/tmp)));
write_old_file('.gitignore', 't/tmp/in', "/doc/.ikiwiki/\n");
write_old_file('doc/writable/one.mdwn', 't/tmp/in', 'This is the first test page');
write_old_file('doc/writable/two.mdwn', 't/tmp/in', 'This is the second test page');
write_old_file('doc/writable/three.mdwn', 't/tmp/in', 'This is the third test page');
write_old_file('doc/writable/three.bin', 't/tmp/in', 'An attachment');
unless ($installed) {
ok(! system(qw(cp -pRL doc/wikiicons t/tmp/in/doc/)));
ok(! system(qw(cp -pRL doc/recentchanges.mdwn t/tmp/in/doc/)));
}
run_git(['init']);
run_git(['add', '.']);
run_git(['commit', '-m', 'Initial commit']);
write_setup_file();
thoroughly_rebuild();
check_cgi_mode_bits();
ok(-e 't/tmp/out/writable/one/index.html');
$content = readfile('t/tmp/out/writable/one/index.html');
like($content, qr{This is the first test page});
my $orig_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
# We have to wait 1 second here so that new writes are guaranteed
# to have a strictly larger mtime.
sleep 1;
# Test the git hook, which accepts git commits
writefile('doc/writable/one.mdwn', 't/tmp/in',
'This is new content for the first test page');
run_git(['add', '.']);
run_git(['commit', '-m', 'Git commit']);
my $first_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
isnt($orig_sha1, $first_revertable_sha1);
ok(-e 't/tmp/out/writable/one/index.html');
$content = readfile('t/tmp/out/writable/one/index.html');
like($content, qr{This is new content for the first test page});
# Test a web commit
$content = run_cgi(method => 'POST',
params => {
do => 'edit',
page => 'writable/two',
type => 'mdwn',
editmessage => 'Web commit',
editcontent => 'Here is new content for the second page',
_submit => 'Save Page',
_submitted => '1',
},
);
like($content, qr{^Status:\s*302\s}m);
like($content, qr{^Location:\s*http://example\.com/writable/two/\?updated}m);
my $second_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
isnt($orig_sha1, $second_revertable_sha1);
isnt($first_revertable_sha1, $second_revertable_sha1);
ok(-e 't/tmp/out/writable/two/index.html');
$content = readfile('t/tmp/out/writable/two/index.html');
like($content, qr{Here is new content for the second page});
# Another edit
writefile('doc/writable/three.mdwn', 't/tmp/in',
'Also new content for the third page');
unlink('t/tmp/in/doc/writable/three.bin');
writefile('doc/writable/three.bin', 't/tmp/in',
'Changed attachment');
run_git(['add', '.']);
run_git(['commit', '-m', 'Git commit']);
ok(-e 't/tmp/out/writable/three/index.html');
$content = readfile('t/tmp/out/writable/three/index.html');
like($content, qr{Also new content for the third page});
$content = readfile('t/tmp/out/writable/three.bin');
like($content, qr{Changed attachment});
my $third_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
isnt($orig_sha1, $third_revertable_sha1);
isnt($second_revertable_sha1, $third_revertable_sha1);
run_git(['mv', 'doc/writable/one.mdwn', 'doc/one.mdwn']);
run_git(['mv', 'doc/writable/two.mdwn', 'two.mdwn']);
run_git(['commit', '-m', 'Rename files to test CVE-2016-10026']);
ok(! -e 't/tmp/out/writable/two/index.html');
ok(! -e 't/tmp/out/writable/one/index.html');
ok(-e 't/tmp/out/one/index.html');
my $sha1_before_revert = run_git(['rev-list', '--max-count=1', 'HEAD']);
isnt($sha1_before_revert, $third_revertable_sha1);
$content = run_cgi(method => 'post',
params => {
do => 'revert',
revertmessage => 'CVE-2016-10026',
rev => $first_revertable_sha1,
_submit => 'Revert',
_submitted_revert => '1',
},
);
like($content, qr{is locked and cannot be edited});
# The tree is left clean
run_git(['diff', '--exit-code']);
run_git(['diff', '--cached', '--exit-code']);
my $sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
is($sha1, $sha1_before_revert);
ok(-e 't/tmp/out/one/index.html');
ok(! -e 't/tmp/in/doc/writable/one.mdwn');
ok(-e 't/tmp/in/doc/one.mdwn');
$content = readfile('t/tmp/out/one/index.html');
like($content, qr{This is new content for the first test page});
$content = run_cgi(method => 'post',
params => {
do => 'revert',
revertmessage => 'CVE-2016-10026',
rev => $second_revertable_sha1,
_submit => 'Revert',
_submitted_revert => '1',
},
);
like($content, qr{you are not allowed to change two\.mdwn});
run_git(['diff', '--exit-code']);
run_git(['diff', '--cached', '--exit-code']);
$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
is($sha1, $sha1_before_revert);
ok(! -e 't/tmp/out/writable/two/index.html');
ok(! -e 't/tmp/out/two/index.html');
ok(! -e 't/tmp/in/doc/writable/two.mdwn');
ok(-e 't/tmp/in/two.mdwn');
$content = readfile('t/tmp/in/two.mdwn');
like($content, qr{Here is new content for the second page});
# This one can legitimately be reverted
$content = run_cgi(method => 'post',
params => {
do => 'revert',
revertmessage => 'not CVE-2016-10026',
rev => $third_revertable_sha1,
_submit => 'Revert',
_submitted_revert => '1',
},
);
like($content, qr{^Status:\s*302\s}m);
like($content, qr{^Location:\s*http://example\.com/recentchanges/}m);
run_git(['diff', '--exit-code']);
run_git(['diff', '--cached', '--exit-code']);
ok(-e 't/tmp/out/writable/three/index.html');
$content = readfile('t/tmp/out/writable/three/index.html');
like($content, qr{This is the third test page});
$content = readfile('t/tmp/out/writable/three.bin');
like($content, qr{An attachment});
}
test();
done_testing();
|