aboutsummaryrefslogtreecommitdiff
path: root/t/git-untrusted.t
blob: 5fd2efb9eeac61c0498530ebdfbb954e9badc4e4 (plain)
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
#!/usr/bin/perl
use warnings;
use strict;

use File::Temp;
use Test::More;

BEGIN {
	my $git = `which git`;
	chomp $git;
	plan(skip_all => 'git not available') unless -x $git;

	plan(skip_all => "IPC::Run not available")
		unless eval q{
			use IPC::Run qw(run start);
			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 $tmp = File::Temp->newdir(CLEANUP => 0);

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 %params = @_;
	my %setup = (
		wikiname => 'this is the name of my wiki',
		srcdir => "$tmp/srcdir",
		destdir => "$tmp/out",
		url => 'http://example.com',
		cgiurl => 'http://example.com/cgi-bin/ikiwiki.cgi',
		cgi_wrapper => "$tmp/ikiwiki.cgi",
		cgi_wrappermode => '0755',
		add_plugins => [qw(anonok attachment lockedit recentchanges)],
		disable_plugins => [qw(emailauth openid passwordauth)],
		anonok_pagespec => 'writable/*',
		locked_pages => '!writable/*',
		rcs => 'git',
		git_wrapper => "$tmp/repo.git/hooks/post-update",
		git_wrappermode => '0755',
		gitorigin_branch => 'test-repo',
		gitmaster_branch => 'master',
		untrusted_committers => [$params{trustme} ? 'nobody' : scalar getpwuid($<)],
		git_test_receive_wrapper => "$tmp/repo.git/hooks/pre-receive",
		ENV => { LC_ALL => 'C' },
		verbose => 1,
	);
	unless ($installed) {
		$setup{ENV}{'PERL5LIB'} = getcwd.'/blib/lib';
	}
	writefile("test.setup", "$tmp",
		"# IkiWiki::Setup::Yaml - YAML formatted setup file\n" .
		Dump(\%setup));
}

sub thoroughly_rebuild {
	ok(unlink("$tmp/ikiwiki.cgi") || $!{ENOENT});
	ok(unlink("$tmp/repo.git/hooks/post-update") || $!{ENOENT});
	ok(unlink("$tmp/repo.git/hooks/pre-receive") || $!{ENOENT});
	ok(! system(@command, qw(--setup), "$tmp/test.setup", qw(--rebuild --wrappers)));
}

sub try_run_git {
	my $args = shift;
	my %params = @_;
	my $git_dir = $params{chdir} || "$tmp/srcdir";
	my ($in, $out, $err);
	my @redirections = ('>', \$in);
	if ($params{capture_stdout}) {
		push @redirections, '>', \$out;
	}
	else {
		push @redirections, '>', \*STDERR;
	}
	push @redirections, '2>', \$err if $params{capture_stderr};
	my $h = start(['git', @$args], @redirections, init => sub {
		chdir $git_dir 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;
		}
	});
	while ($h->pump) {};
	$h->finish;
	return $h, $out, $err;
}

sub run_git {
	my (undef, $filename, $line) = caller;
	my $args = shift;
	my %params = @_;
	my $git_dir = $params{chdir} || "$tmp/srcdir";
	my $desc = $params{desc} || join(' ', 'git', @$args);
	my ($h, $out, $err) = try_run_git($args, capture_stdout => 1, %params);
	is($h->full_result(0), 0, "'$desc' in $git_dir at $filename:$line");
	return $out;
}

sub test {
	my ($h, $out, $err);
	my $sha1;

	write_old_file('.gitignore', "$tmp/srcdir", "/.ikiwiki/\n");
	write_old_file('writable/one.mdwn', "$tmp/srcdir", 'This is the first test page');
	write_old_file('writable/two.bin', "$tmp/srcdir", 'An attachment');

	unless ($installed) {
		ok(! system(qw(cp -pRL doc/wikiicons), "$tmp/srcdir/"));
		ok(! system(qw(cp -pRL doc/recentchanges.mdwn), "$tmp/srcdir/"));
	}

	ok(mkdir "$tmp/repo.git");
	run_git(['init', '--bare'], chdir => "$tmp/repo.git");

	run_git(['init']);
	run_git(['add', '.']);
	run_git(['commit', '-m', 'Initial commit']);
	my $initial_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
	run_git(['remote', 'add', 'test-repo', "$tmp/repo.git"]);
	run_git(['push', 'test-repo', 'master:master']);
	run_git(['branch', '--set-upstream-to=test-repo/master']);

	run_git(['clone', '-otest-repo', "$tmp/repo.git", "$tmp/clone"], chdir => $tmp);
	writefile('writable/untrusted_user_says_hi.mdwn', "$tmp/clone", 'Hi!');
	run_git(['add', 'writable'], chdir => "$tmp/clone");
	run_git(['commit', '-m', 'Hi'], chdir => "$tmp/clone");
	my $allowed_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD'],
		chdir => "$tmp/clone");

	diag 'Pushing unrestricted change as untrusted user';
	write_setup_file(trustme => 0);
	thoroughly_rebuild();

	$out = run_git([
		'push', 'test-repo', 'master:master',
	], chdir => "$tmp/clone");
	$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD'], chdir => "$tmp/repo.git");
	is($sha1, $allowed_sha1, 'allowed commit was pushed');
	$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
	is($sha1, $allowed_sha1, 'allowed commit was pushed');
	ok(-e "$tmp/srcdir/writable/untrusted_user_says_hi.mdwn");
	ok(-e "$tmp/out/writable/untrusted_user_says_hi/index.html");

	diag 'Pushing restricted change as untrusted user';
	writefile('staff_only.mdwn', "$tmp/clone", 'Hi!');
	run_git(['add', 'staff_only.mdwn'], chdir => "$tmp/clone");
	run_git(['commit', '-m', 'Hi'], chdir => "$tmp/clone");
	my $proposed_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD'],
		chdir => "$tmp/clone");

	($h, $out, $err) = try_run_git([
		'push', 'test-repo', 'master:master',
	], chdir => "$tmp/clone", capture_stdout => 1, capture_stderr => 1);
	isnt($h->full_result(0), 0);
	is($out, '');
	like($err, qr{remote: <.*>staff only</.*> is locked and cannot be edited});
	$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD'], chdir => "$tmp/repo.git");
	is($sha1, $allowed_sha1, 'proposed commit was not pushed');
	$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
	is($sha1, $allowed_sha1, 'proposed commit was not pushed');
	ok(! -e "$tmp/srcdir/staff_only.mdwn");
	ok(! -e "$tmp/out/staff_only/index.html");

	diag 'Pushing restricted change as trusted user';
	write_setup_file(trustme => 1);
	thoroughly_rebuild();

	$out = run_git([
		'push', 'test-repo', 'master:master',
	], chdir => "$tmp/clone");
	$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD'], chdir => "$tmp/repo.git");
	is($sha1, $proposed_sha1, 'proposed commit was pushed');
	$sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
	is($sha1, $proposed_sha1, 'proposed commit was pushed');
	ok(-e "$tmp/srcdir/staff_only.mdwn");
	ok(-e "$tmp/out/staff_only/index.html");
}

test();

done_testing();