]> sipb.mit.edu Git - ikiwiki.git/blob - t/relativity.t
Avoid mixed content when cgiurl is https but url is not
[ikiwiki.git] / t / relativity.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Cwd qw(getcwd);
6 use Errno qw(ENOENT);
7
8 BEGIN {
9         if (!eval q{
10                 use IPC::Run qw(run);
11                 1;
12         }) {
13                 eval q{use Test::More skip_all => "IPC::Run not available"};
14         }
15         else {
16                 eval q{use Test::More};
17         }
18         use_ok("IkiWiki");
19 }
20
21 my $PERL5LIB = 'blib/lib:blib/arch';
22 my $pwd = getcwd();
23
24 # Black-box (ish) test for relative linking between CGI and static content
25
26 my $blob;
27 my ($content, $in, %bits);
28
29 sub parse_cgi_content {
30         my %bits;
31         if ($content =~ qr{<base href="([^"]+)" */>}) {
32                 $bits{basehref} = $1;
33         }
34         if ($content =~ qr{href="([^"]+/style.css)"}) {
35                 $bits{stylehref} = $1;
36         }
37         if ($content =~ qr{class="parentlinks">\s+<a href="([^"]+)">this is the name of my wiki</a>/}s) {
38                 $bits{tophref} = $1;
39         }
40         if ($content =~ qr{<a[^>]+href="([^"]+)\?do=prefs"}) {
41                 $bits{cgihref} = $1;
42         }
43         return %bits;
44 }
45
46 ok(! system("make -s ikiwiki.out"));
47 ok(! system("rm -rf t/tmp"));
48 ok(! system("mkdir t/tmp"));
49
50 sub write_old_file {
51         my $name = shift;
52         my $content = shift;
53
54         writefile($name, "t/tmp/in", $content);
55         ok(utime(333333333, 333333333, "t/tmp/in/$name"));
56 }
57
58 write_old_file("a.mdwn", "A");
59 write_old_file("a/b.mdwn", "B");
60 write_old_file("a/b/c.mdwn",
61 "* A: [[a]]\n".
62 "* B: [[b]]\n".
63 "* E: [[a/d/e]]\n");
64 write_old_file("a/d.mdwn", "D");
65 write_old_file("a/d/e.mdwn", "E");
66
67 #######################################################################
68 # site 1: a perfectly ordinary ikiwiki
69
70 writefile("test.setup", "t/tmp", <<EOF
71 # IkiWiki::Setup::Yaml - YAML formatted setup file
72 wikiname: this is the name of my wiki
73 srcdir: t/tmp/in
74 destdir: t/tmp/out
75 templatedir: templates
76 url: "http://example.com/wiki/"
77 cgiurl: "http://example.com/cgi-bin/ikiwiki.cgi"
78 cgi_wrapper: t/tmp/ikiwiki.cgi
79 cgi_wrappermode: 0754
80 # make it easier to test previewing
81 add_plugins:
82 - anonok
83 anonok_pagespec: "*"
84 ENV: { 'PERL5LIB': '$PERL5LIB' }
85 EOF
86 );
87
88 ok(unlink("t/tmp/ikiwiki.cgi") || $!{ENOENT});
89 ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers"));
90
91 # CGI wrapper should be exactly the requested mode
92 my (undef, undef, $mode, undef, undef,
93         undef, undef, undef, undef, undef,
94         undef, undef, undef) = stat("t/tmp/ikiwiki.cgi");
95 is($mode & 07777, 0754);
96
97 ok(-e "t/tmp/out/a/b/c/index.html");
98 $content = readfile("t/tmp/out/a/b/c/index.html");
99 # no <base> on static HTML
100 unlike($content, qr{<base\W});
101 # url and cgiurl are on the same host so the cgiurl is host-relative
102 like($content, qr{<a[^>]+href="/cgi-bin/ikiwiki.cgi\?do=prefs"});
103 # cross-links between static pages are relative
104 like($content, qr{<li>A: <a href="../../">a</a></li>});
105 like($content, qr{<li>B: <a href="../">b</a></li>});
106 like($content, qr{<li>E: <a href="../../d/e/">e</a></li>});
107
108 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
109         $ENV{REQUEST_METHOD} = 'GET';
110         $ENV{SERVER_PORT} = '80';
111         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
112         $ENV{QUERY_STRING} = 'do=prefs';
113         $ENV{HTTP_HOST} = 'example.com';
114 });
115 %bits = parse_cgi_content($content);
116 is($bits{basehref}, "http://example.com/wiki/");
117 like($bits{stylehref}, qr{^(?:(?:http:)?//example.com)?/wiki/style.css$});
118 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
119 like($bits{cgihref}, qr{^(?:(?:http:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
120
121 # when accessed via HTTPS, links are secure
122 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
123         $ENV{REQUEST_METHOD} = 'GET';
124         $ENV{SERVER_PORT} = '443';
125         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
126         $ENV{QUERY_STRING} = 'do=prefs';
127         $ENV{HTTP_HOST} = 'example.com';
128         $ENV{HTTPS} = 'on';
129 });
130 %bits = parse_cgi_content($content);
131 is($bits{basehref}, "https://example.com/wiki/");
132 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
133 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
134 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
135
136 # when accessed via a different hostname, links stay on that host
137 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
138         $ENV{REQUEST_METHOD} = 'GET';
139         $ENV{SERVER_PORT} = '80';
140         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
141         $ENV{QUERY_STRING} = 'do=prefs';
142         $ENV{HTTP_HOST} = 'staging.example.net';
143 });
144 %bits = parse_cgi_content($content);
145 is($bits{basehref}, "http://staging.example.net/wiki/");
146 like($bits{stylehref}, qr{^(?:(?:http:)?//staging.example.net)?/wiki/style.css$});
147 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
148 like($bits{cgihref}, qr{^(?:(?:http:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$});
149
150 # previewing a page
151 $in = 'do=edit&page=a/b/c&Preview';
152 run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub {
153         $ENV{REQUEST_METHOD} = 'POST';
154         $ENV{SERVER_PORT} = '80';
155         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
156         $ENV{HTTP_HOST} = 'example.com';
157         $ENV{CONTENT_LENGTH} = length $in;
158 });
159 %bits = parse_cgi_content($content);
160 is($bits{basehref}, "http://example.com/wiki/a/b/c/");
161 like($bits{stylehref}, qr{^(?:(?:http:)?//example.com)?/wiki/style.css$});
162 like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
163 like($bits{cgihref}, qr{^(?:(?:http:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
164
165 #######################################################################
166 # site 2: static content and CGI are on different servers
167
168 writefile("test.setup", "t/tmp", <<EOF
169 # IkiWiki::Setup::Yaml - YAML formatted setup file
170 wikiname: this is the name of my wiki
171 srcdir: t/tmp/in
172 destdir: t/tmp/out
173 templatedir: templates
174 url: "http://static.example.com/"
175 cgiurl: "http://cgi.example.com/ikiwiki.cgi"
176 cgi_wrapper: t/tmp/ikiwiki.cgi
177 cgi_wrappermode: 0754
178 # make it easier to test previewing
179 add_plugins:
180 - anonok
181 anonok_pagespec: "*"
182 ENV: { 'PERL5LIB': '$PERL5LIB' }
183 EOF
184 );
185
186 ok(unlink("t/tmp/ikiwiki.cgi"));
187 ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers"));
188
189 # CGI wrapper should be exactly the requested mode
190 (undef, undef, $mode, undef, undef,
191         undef, undef, undef, undef, undef,
192         undef, undef, undef) = stat("t/tmp/ikiwiki.cgi");
193 is($mode & 07777, 0754);
194
195 ok(-e "t/tmp/out/a/b/c/index.html");
196 $content = readfile("t/tmp/out/a/b/c/index.html");
197 # no <base> on static HTML
198 unlike($content, qr{<base\W});
199 # url and cgiurl are not on the same host so the cgiurl has to be
200 # protocol-relative or absolute
201 like($content, qr{<a[^>]+href="(?:http:)?//cgi.example.com/ikiwiki.cgi\?do=prefs"});
202 # cross-links between static pages are still relative
203 like($content, qr{<li>A: <a href="../../">a</a></li>});
204 like($content, qr{<li>B: <a href="../">b</a></li>});
205 like($content, qr{<li>E: <a href="../../d/e/">e</a></li>});
206
207 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
208         $ENV{REQUEST_METHOD} = 'GET';
209         $ENV{SERVER_PORT} = '80';
210         $ENV{SCRIPT_NAME} = '/ikiwiki.cgi';
211         $ENV{QUERY_STRING} = 'do=prefs';
212         $ENV{HTTP_HOST} = 'cgi.example.com';
213 });
214 %bits = parse_cgi_content($content);
215 like($bits{basehref}, qr{^http://static.example.com/$});
216 like($bits{stylehref}, qr{^(?:(?:http:)?//static.example.com)?/style.css$});
217 like($bits{tophref}, qr{^(?:http:)?//static.example.com/$});
218 like($bits{cgihref}, qr{^(?:(?:http:)?//cgi.example.com)?/ikiwiki.cgi$});
219
220 # when accessed via HTTPS, links are secure
221 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
222         $ENV{REQUEST_METHOD} = 'GET';
223         $ENV{SERVER_PORT} = '443';
224         $ENV{SCRIPT_NAME} = '/ikiwiki.cgi';
225         $ENV{QUERY_STRING} = 'do=prefs';
226         $ENV{HTTP_HOST} = 'cgi.example.com';
227         $ENV{HTTPS} = 'on';
228 });
229 %bits = parse_cgi_content($content);
230 like($bits{basehref}, qr{^https://static.example.com/$});
231 like($bits{stylehref}, qr{^(?:(?:https:)?//static.example.com)?/style.css$});
232 like($bits{tophref}, qr{^(?:https:)?//static.example.com/$});
233 like($bits{cgihref}, qr{^(?:(?:https:)?//cgi.example.com)?/ikiwiki.cgi$});
234
235 # when accessed via a different hostname, links to the CGI (only) should
236 # stay on that host?
237 $in = 'do=edit&page=a/b/c&Preview';
238 run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub {
239         $ENV{REQUEST_METHOD} = 'POST';
240         $ENV{SERVER_PORT} = '80';
241         $ENV{SCRIPT_NAME} = '/ikiwiki.cgi';
242         $ENV{HTTP_HOST} = 'staging.example.net';
243         $ENV{CONTENT_LENGTH} = length $in;
244 });
245 %bits = parse_cgi_content($content);
246 like($bits{basehref}, qr{^http://static.example.com/a/b/c/$});
247 like($bits{stylehref}, qr{^(?:(?:http:)?//static.example.com|\.\./\.\./\.\.)/style.css$});
248 like($bits{tophref}, qr{^(?:(?:http:)?//static.example.com|\.\./\.\./\.\.)/$});
249 TODO: {
250 local $TODO = "use self-referential CGI URL?";
251 like($bits{cgihref}, qr{^(?:(?:http:)?//staging.example.net)?/ikiwiki.cgi$});
252 }
253
254 #######################################################################
255 # site 3: we specifically want everything to be secure
256
257 writefile("test.setup", "t/tmp", <<EOF
258 # IkiWiki::Setup::Yaml - YAML formatted setup file
259 wikiname: this is the name of my wiki
260 srcdir: t/tmp/in
261 destdir: t/tmp/out
262 templatedir: templates
263 url: "https://example.com/wiki/"
264 cgiurl: "https://example.com/cgi-bin/ikiwiki.cgi"
265 cgi_wrapper: t/tmp/ikiwiki.cgi
266 cgi_wrappermode: 0754
267 # make it easier to test previewing
268 add_plugins:
269 - anonok
270 anonok_pagespec: "*"
271 ENV: { 'PERL5LIB': '$PERL5LIB' }
272 EOF
273 );
274
275 ok(unlink("t/tmp/ikiwiki.cgi"));
276 ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers"));
277
278 # CGI wrapper should be exactly the requested mode
279 (undef, undef, $mode, undef, undef,
280         undef, undef, undef, undef, undef,
281         undef, undef, undef) = stat("t/tmp/ikiwiki.cgi");
282 is($mode & 07777, 0754);
283
284 ok(-e "t/tmp/out/a/b/c/index.html");
285 $content = readfile("t/tmp/out/a/b/c/index.html");
286 # no <base> on static HTML
287 unlike($content, qr{<base\W});
288 # url and cgiurl are on the same host so the cgiurl is host-relative
289 like($content, qr{<a[^>]+href="/cgi-bin/ikiwiki.cgi\?do=prefs"});
290 # cross-links between static pages are relative
291 like($content, qr{<li>A: <a href="../../">a</a></li>});
292 like($content, qr{<li>B: <a href="../">b</a></li>});
293 like($content, qr{<li>E: <a href="../../d/e/">e</a></li>});
294
295 # when accessed via HTTPS, links are secure
296 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
297         $ENV{REQUEST_METHOD} = 'GET';
298         $ENV{SERVER_PORT} = '443';
299         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
300         $ENV{QUERY_STRING} = 'do=prefs';
301         $ENV{HTTP_HOST} = 'example.com';
302         $ENV{HTTPS} = 'on';
303 });
304 %bits = parse_cgi_content($content);
305 is($bits{basehref}, "https://example.com/wiki/");
306 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
307 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
308 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
309
310 # when not accessed via HTTPS, links should still be secure
311 # (but if this happens, that's a sign of web server misconfiguration)
312 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
313         $ENV{REQUEST_METHOD} = 'GET';
314         $ENV{SERVER_PORT} = '80';
315         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
316         $ENV{QUERY_STRING} = 'do=prefs';
317         $ENV{HTTP_HOST} = 'example.com';
318 });
319 %bits = parse_cgi_content($content);
320 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
321 TODO: {
322 local $TODO = "treat https in configured url, cgiurl as required?";
323 is($bits{basehref}, "https://example.com/wiki/");
324 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
325 }
326 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
327
328 # when accessed via a different hostname, links stay on that host
329 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
330         $ENV{REQUEST_METHOD} = 'GET';
331         $ENV{SERVER_PORT} = '443';
332         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
333         $ENV{QUERY_STRING} = 'do=prefs';
334         $ENV{HTTP_HOST} = 'staging.example.net';
335         $ENV{HTTPS} = 'on';
336 });
337 %bits = parse_cgi_content($content);
338 is($bits{basehref}, "https://staging.example.net/wiki/");
339 like($bits{stylehref}, qr{^(?:(?:https:)?//staging.example.net)?/wiki/style.css$});
340 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
341 like($bits{cgihref}, qr{^(?:(?:https:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$});
342
343 # previewing a page
344 $in = 'do=edit&page=a/b/c&Preview';
345 run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub {
346         $ENV{REQUEST_METHOD} = 'POST';
347         $ENV{SERVER_PORT} = '443';
348         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
349         $ENV{HTTP_HOST} = 'example.com';
350         $ENV{CONTENT_LENGTH} = length $in;
351         $ENV{HTTPS} = 'on';
352 });
353 %bits = parse_cgi_content($content);
354 is($bits{basehref}, "https://example.com/wiki/a/b/c/");
355 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
356 like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
357 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
358
359 #######################################################################
360 # site 4 (NetBSD wiki): CGI is secure, static content doesn't have to be
361
362 writefile("test.setup", "t/tmp", <<EOF
363 # IkiWiki::Setup::Yaml - YAML formatted setup file
364 wikiname: this is the name of my wiki
365 srcdir: t/tmp/in
366 destdir: t/tmp/out
367 templatedir: templates
368 url: "http://example.com/wiki/"
369 cgiurl: "https://example.com/cgi-bin/ikiwiki.cgi"
370 cgi_wrapper: t/tmp/ikiwiki.cgi
371 cgi_wrappermode: 0754
372 # make it easier to test previewing
373 add_plugins:
374 - anonok
375 anonok_pagespec: "*"
376 ENV: { 'PERL5LIB': '$PERL5LIB' }
377 EOF
378 );
379
380 ok(unlink("t/tmp/ikiwiki.cgi"));
381 ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers"));
382
383 # CGI wrapper should be exactly the requested mode
384 (undef, undef, $mode, undef, undef,
385         undef, undef, undef, undef, undef,
386         undef, undef, undef) = stat("t/tmp/ikiwiki.cgi");
387 is($mode & 07777, 0754);
388
389 ok(-e "t/tmp/out/a/b/c/index.html");
390 $content = readfile("t/tmp/out/a/b/c/index.html");
391 # no <base> on static HTML
392 unlike($content, qr{<base\W});
393 # url and cgiurl are on the same host but different schemes
394 like($content, qr{<a[^>]+href="https://example.com/cgi-bin/ikiwiki.cgi\?do=prefs"});
395 # cross-links between static pages are relative
396 like($content, qr{<li>A: <a href="../../">a</a></li>});
397 like($content, qr{<li>B: <a href="../">b</a></li>});
398 like($content, qr{<li>E: <a href="../../d/e/">e</a></li>});
399
400 # when accessed via HTTPS, links are secure (to avoid mixed-content)
401 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
402         $ENV{REQUEST_METHOD} = 'GET';
403         $ENV{SERVER_PORT} = '443';
404         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
405         $ENV{QUERY_STRING} = 'do=prefs';
406         $ENV{HTTP_HOST} = 'example.com';
407         $ENV{HTTPS} = 'on';
408 });
409 %bits = parse_cgi_content($content);
410 is($bits{basehref}, "https://example.com/wiki/");
411 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
412 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
413 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
414
415 # when not accessed via HTTPS, ???
416 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
417         $ENV{REQUEST_METHOD} = 'GET';
418         $ENV{SERVER_PORT} = '80';
419         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
420         $ENV{QUERY_STRING} = 'do=prefs';
421         $ENV{HTTP_HOST} = 'example.com';
422 });
423 %bits = parse_cgi_content($content);
424 like($bits{basehref}, qr{^https?://example.com/wiki/$});
425 like($bits{stylehref}, qr{^(?:(?:https?:)?//example.com)?/wiki/style.css$});
426 like($bits{tophref}, qr{^(?:(?:https?://example.com)?/wiki|\.)/$});
427 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
428
429 # when accessed via a different hostname, links stay on that host
430 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
431         $ENV{REQUEST_METHOD} = 'GET';
432         $ENV{SERVER_PORT} = '443';
433         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
434         $ENV{QUERY_STRING} = 'do=prefs';
435         $ENV{HTTP_HOST} = 'staging.example.net';
436         $ENV{HTTPS} = 'on';
437 });
438 %bits = parse_cgi_content($content);
439 # because the static and dynamic stuff is on the same server, we assume that
440 # both are also on the staging server
441 like($bits{basehref}, qr{^https://staging.example.net/wiki/$});
442 like($bits{stylehref}, qr{^(?:(?:https:)?//staging.example.net)?/wiki/style.css$});
443 like($bits{tophref}, qr{^(?:(?:(?:https:)?//staging.example.net)?/wiki|\.)/$});
444 TODO: {
445 local $TODO = "this should really point back to itself but currently points to example.com";
446 like($bits{cgihref}, qr{^(?:(?:https:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$});
447 }
448
449 # previewing a page
450 $in = 'do=edit&page=a/b/c&Preview';
451 run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub {
452         $ENV{REQUEST_METHOD} = 'POST';
453         $ENV{SERVER_PORT} = '443';
454         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
455         $ENV{HTTP_HOST} = 'example.com';
456         $ENV{CONTENT_LENGTH} = length $in;
457         $ENV{HTTPS} = 'on';
458 });
459 %bits = parse_cgi_content($content);
460 is($bits{basehref}, "https://example.com/wiki/a/b/c/");
461 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
462 like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
463 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
464
465 # Deliberately not testing https static content with http cgiurl,
466 # because that makes remarkably little sense.
467
468 #######################################################################
469 # site 5: w3mmode, as documented in [[w3mmode]]
470
471 writefile("test.setup", "t/tmp", <<EOF
472 # IkiWiki::Setup::Yaml - YAML formatted setup file
473 wikiname: this is the name of my wiki
474 srcdir: t/tmp/in
475 destdir: t/tmp/out
476 templatedir: templates
477 cgiurl: ikiwiki.cgi
478 w3mmode: 1
479 cgi_wrapper: t/tmp/ikiwiki.cgi
480 cgi_wrappermode: 0754
481 add_plugins:
482 - anonok
483 anonok_pagespec: "*"
484 ENV: { 'PERL5LIB': '$PERL5LIB' }
485 EOF
486 );
487
488 ok(unlink("t/tmp/ikiwiki.cgi"));
489 ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers"));
490
491 # CGI wrapper should be exactly the requested mode
492 (undef, undef, $mode, undef, undef,
493         undef, undef, undef, undef, undef,
494         undef, undef, undef) = stat("t/tmp/ikiwiki.cgi");
495 is($mode & 07777, 0754);
496
497 ok(-e "t/tmp/out/a/b/c/index.html");
498 $content = readfile("t/tmp/out/a/b/c/index.html");
499 # no <base> on static HTML
500 unlike($content, qr{<base\W});
501 # FIXME: does /$LIB/ikiwiki-w3m.cgi work under w3m?
502 like($content, qr{<a[^>]+href="(?:file://)?/\$LIB/ikiwiki-w3m.cgi/ikiwiki.cgi\?do=prefs"});
503 # cross-links between static pages are still relative
504 like($content, qr{<li>A: <a href="../../">a</a></li>});
505 like($content, qr{<li>B: <a href="../">b</a></li>});
506 like($content, qr{<li>E: <a href="../../d/e/">e</a></li>});
507
508 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
509         $ENV{REQUEST_METHOD} = 'GET';
510         $ENV{PATH_INFO} = '/ikiwiki.cgi';
511         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki-w3m.cgi';
512         $ENV{QUERY_STRING} = 'do=prefs';
513 });
514 %bits = parse_cgi_content($content);
515 like($bits{tophref}, qr{^(?:\Q$pwd\E/t/tmp/out|\.)/$});
516 like($bits{cgihref}, qr{^(?:file://)?/\$LIB/ikiwiki-w3m.cgi/ikiwiki.cgi$});
517 like($bits{basehref}, qr{^(?:(?:file:)?//)?\Q$pwd\E/t/tmp/out/$});
518 like($bits{stylehref}, qr{^(?:(?:(?:file:)?//)?\Q$pwd\E/t/tmp/out|\.)/style.css$});
519
520 #######################################################################
521 # site 6: we're behind a reverse-proxy
522
523 writefile("test.setup", "t/tmp", <<EOF
524 # IkiWiki::Setup::Yaml - YAML formatted setup file
525 wikiname: this is the name of my wiki
526 srcdir: t/tmp/in
527 destdir: t/tmp/out
528 templatedir: templates
529 url: "https://example.com/wiki/"
530 cgiurl: "https://example.com/cgi-bin/ikiwiki.cgi"
531 cgi_wrapper: t/tmp/ikiwiki.cgi
532 cgi_wrappermode: 0754
533 # make it easier to test previewing
534 add_plugins:
535 - anonok
536 anonok_pagespec: "*"
537 reverse_proxy: 1
538 ENV: { 'PERL5LIB': '$PERL5LIB' }
539 EOF
540 );
541
542 ok(unlink("t/tmp/ikiwiki.cgi"));
543 ok(! system("./ikiwiki.out --setup t/tmp/test.setup --rebuild --wrappers"));
544
545 # CGI wrapper should be exactly the requested mode
546 (undef, undef, $mode, undef, undef,
547         undef, undef, undef, undef, undef,
548         undef, undef, undef) = stat("t/tmp/ikiwiki.cgi");
549 is($mode & 07777, 0754);
550
551 ok(-e "t/tmp/out/a/b/c/index.html");
552 $content = readfile("t/tmp/out/a/b/c/index.html");
553 # no <base> on static HTML
554 unlike($content, qr{<base\W});
555 # url and cgiurl are on the same host so the cgiurl is host-relative
556 like($content, qr{<a[^>]+href="/cgi-bin/ikiwiki.cgi\?do=prefs"});
557 # cross-links between static pages are relative
558 like($content, qr{<li>A: <a href="../../">a</a></li>});
559 like($content, qr{<li>B: <a href="../">b</a></li>});
560 like($content, qr{<li>E: <a href="../../d/e/">e</a></li>});
561
562 # because we are behind a reverse-proxy we must assume that
563 # we're being accessed by the configured cgiurl
564 run(["./t/tmp/ikiwiki.cgi"], \undef, \$content, init => sub {
565         $ENV{REQUEST_METHOD} = 'GET';
566         $ENV{SERVER_PORT} = '80';
567         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
568         $ENV{QUERY_STRING} = 'do=prefs';
569         $ENV{HTTP_HOST} = 'localhost';
570 });
571 %bits = parse_cgi_content($content);
572 like($bits{tophref}, qr{^(?:/wiki|\.)/$});
573 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
574 TODO: {
575 local $TODO = "reverse-proxy support needed";
576 is($bits{basehref}, "https://example.com/wiki/");
577 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
578 }
579
580 # previewing a page
581 $in = 'do=edit&page=a/b/c&Preview';
582 run(["./t/tmp/ikiwiki.cgi"], \$in, \$content, init => sub {
583         $ENV{REQUEST_METHOD} = 'POST';
584         $ENV{SERVER_PORT} = '80';
585         $ENV{SCRIPT_NAME} = '/cgi-bin/ikiwiki.cgi';
586         $ENV{HTTP_HOST} = 'localhost';
587         $ENV{CONTENT_LENGTH} = length $in;
588 });
589 %bits = parse_cgi_content($content);
590 like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
591 like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
592 TODO: {
593 local $TODO = "reverse-proxy support needed";
594 is($bits{basehref}, "https://example.com/wiki/a/b/c/");
595 like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
596 }
597
598 done_testing;