+sub test_site3_we_specifically_want_everything_to_be_secure {
+ diag("test_site3_we_specifically_want_everything_to_be_secure");
+ write_setup_file(
+ url => "https://example.com/wiki/",
+ cgiurl => "https://example.com/cgi-bin/ikiwiki.cgi",
+ );
+ thoroughly_rebuild();
+ check_cgi_mode_bits();
+ # url and cgiurl are on the same host so the cgiurl is host-relative
+ check_generated_content(qr{<a[^>]+href="/cgi-bin/ikiwiki.cgi\?do=prefs"});
+
+ # when accessed via HTTPS, links are secure
+ my %bits = parse_cgi_content(run_cgi(is_https => 1));
+ like($bits{basehref}, qr{^(?:(?:https:)?//example\.com)?/wiki/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:/wiki|\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+ check_goto(qr{^https://example\.com/wiki/a/b/c/$}, is_https => 1);
+
+ # when not accessed via HTTPS, links should still be secure
+ # (but if this happens, that's a sign of web server misconfiguration)
+ %bits = parse_cgi_content(run_cgi());
+ like($bits{tophref}, qr{^(?:/wiki|\.)/$});
+ TODO: {
+ local $TODO = "treat https in configured url, cgiurl as required?";
+ is($bits{basehref}, "https://example.com/wiki/");
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+ }
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+ check_goto(qr{^https://example\.com/wiki/a/b/c/$}, is_https => 0);
+
+ # when accessed via a different hostname, links stay on that host
+ %bits = parse_cgi_content(run_cgi(is_https => 1, HTTP_HOST => 'staging.example.net'));
+ like($bits{basehref}, qr{^(?:(?:https:)?//staging\.example\.net)?/wiki/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//staging.example.net)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:/wiki|\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$});
+ check_goto(qr{^https://staging\.example\.net/wiki/a/b/c/$}, is_https => 1,
+ HTTP_HOST => 'staging.example.net');
+
+ # previewing a page
+ %bits = parse_cgi_content(run_cgi(is_preview => 1, is_https => 1));
+ like($bits{basehref}, qr{^(?:(?:https:)?//example\.com)?/wiki/a/b/c/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+}
+
+sub test_site4_cgi_is_secure_static_content_doesnt_have_to_be {
+ diag("test_site4_cgi_is_secure_static_content_doesnt_have_to_be");
+ # (NetBSD wiki)
+ write_setup_file(
+ url => "http://example.com/wiki/",
+ cgiurl => "https://example.com/cgi-bin/ikiwiki.cgi",
+ );
+ thoroughly_rebuild();
+ check_cgi_mode_bits();
+ # url and cgiurl are on the same host but different schemes
+ check_generated_content(qr{<a[^>]+href="https://example.com/cgi-bin/ikiwiki.cgi\?do=prefs"});
+
+ # when accessed via HTTPS, links are secure (to avoid mixed-content)
+ my %bits = parse_cgi_content(run_cgi(is_https => 1));
+ like($bits{basehref}, qr{^(?:(?:https:)?//example\.com)?/wiki/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:/wiki|\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+ check_goto(qr{^https://example\.com/wiki/a/b/c/$}, is_https => 1);
+
+ # FIXME: when not accessed via HTTPS, should the static content be
+ # forced to https anyway? For now we accept either
+ %bits = parse_cgi_content(run_cgi());
+ like($bits{basehref}, qr{^(?:(?:https?)?://example\.com)?/wiki/$});
+ like($bits{stylehref}, qr{^(?:(?:https?:)?//example.com)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:(?:https?://example.com)?/wiki|\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+ check_goto(qr{^https://example\.com/wiki/a/b/c/$}, is_https => 0);
+
+ # when accessed via a different hostname, links stay on that host
+ %bits = parse_cgi_content(run_cgi(is_https => 1, HTTP_HOST => 'staging.example.net'));
+ # because the static and dynamic stuff is on the same server, we assume that
+ # both are also on the staging server
+ like($bits{basehref}, qr{^(?:(?:https:)?//staging\.example\.net)?/wiki/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//staging.example.net)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:(?:(?:https:)?//staging.example.net)?/wiki|\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//(?:staging\.example\.net|example\.com))?/cgi-bin/ikiwiki.cgi$});
+ TODO: {
+ local $TODO = "this should really point back to itself but currently points to example.com";
+ like($bits{cgihref}, qr{^(?:(?:https:)?//staging.example.net)?/cgi-bin/ikiwiki.cgi$});
+ }
+ check_goto(qr{^https://staging\.example\.net/wiki/a/b/c/$}, is_https => 1,
+ HTTP_HOST => 'staging.example.net');
+
+ # previewing a page
+ %bits = parse_cgi_content(run_cgi(is_preview => 1, is_https => 1));
+ like($bits{basehref}, qr{^(?:(?:https:)?//example\.com)?/wiki/a/b/c/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+ like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+}
+
+sub test_site5_w3mmode {
+ diag("test_site5_w3mmode");
+ # as documented in [[w3mmode]]
+ write_setup_file(
+ url => undef,
+ cgiurl => "ikiwiki.cgi",
+ w3mmode => 1,
+ );
+ thoroughly_rebuild();
+ check_cgi_mode_bits();
+ # FIXME: does /$LIB/ikiwiki-w3m.cgi work under w3m?
+ check_generated_content(qr{<a[^>]+href="(?:file://)?/\$LIB/ikiwiki-w3m.cgi/ikiwiki.cgi\?do=prefs"});
+
+ my %bits = parse_cgi_content(run_cgi(PATH_INFO => '/ikiwiki.cgi', SCRIPT_NAME => '/cgi-bin/ikiwiki-w3m.cgi'));
+ my $pwd = getcwd();
+ like($bits{tophref}, qr{^(?:\Q$pwd\E/t/tmp/out|\.)/$});
+ like($bits{cgihref}, qr{^(?:file://)?/\$LIB/ikiwiki-w3m.cgi/ikiwiki.cgi$});
+ like($bits{basehref}, qr{^(?:(?:file:)?//)?\Q$pwd\E/t/tmp/out/$});
+ like($bits{stylehref}, qr{^(?:(?:(?:file:)?//)?\Q$pwd\E/t/tmp/out|\.)/style.css$});
+
+ my $redirect = run_cgi(goto => 1, PATH_INFO => '/ikiwiki.cgi',
+ SCRIPT_NAME => '/cgi-bin/ikiwiki-w3m.cgi');
+ like($redirect, qr{^Content-type: text/plain\r?\n}m);
+ like($redirect, qr{^W3m-control: GOTO (?:file://)?\Q$pwd\E/t/tmp/out/a/b/c/\r?\n}m);
+}
+
+sub test_site6_behind_reverse_proxy {
+ diag("test_site6_behind_reverse_proxy");
+ write_setup_file(
+ url => "https://example.com/wiki/",
+ cgiurl => "https://example.com/cgi-bin/ikiwiki.cgi",
+ reverse_proxy => 1,
+ );
+ thoroughly_rebuild();
+ check_cgi_mode_bits();
+ # url and cgiurl are on the same host so the cgiurl is host-relative
+ check_generated_content(qr{<a[^>]+href="/cgi-bin/ikiwiki.cgi\?do=prefs"});
+
+ # because we are behind a reverse-proxy we must assume that
+ # we're being accessed by the configured cgiurl
+ my %bits = parse_cgi_content(run_cgi(HTTP_HOST => 'localhost'));
+ like($bits{tophref}, qr{^(?:/wiki|\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+ like($bits{basehref}, qr{^(?:(?:https:)?//example\.com)?/wiki/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+ check_goto(qr{^https://example\.com/wiki/a/b/c/$}, HTTP_HOST => 'localhost');
+
+ # previewing a page
+ %bits = parse_cgi_content(run_cgi(is_preview => 1, HTTP_HOST => 'localhost'));
+ like($bits{tophref}, qr{^(?:/wiki|\.\./\.\./\.\.)/$});
+ like($bits{cgihref}, qr{^(?:(?:https:)?//example.com)?/cgi-bin/ikiwiki.cgi$});
+ like($bits{basehref}, qr{^(?:(?:https)?://example\.com)?/wiki/a/b/c/$});
+ like($bits{stylehref}, qr{^(?:(?:https:)?//example.com)?/wiki/style.css$});
+}
+
+test_startup();
+
+test_site1_perfectly_ordinary_ikiwiki();
+test_site2_static_content_and_cgi_on_different_servers();
+test_site3_we_specifically_want_everything_to_be_secure();
+test_site4_cgi_is_secure_static_content_doesnt_have_to_be();
+test_site5_w3mmode();
+test_site6_behind_reverse_proxy();
+
+done_testing();