Debian Codegraph @

cassandane/Cassandane/Cyrus/URLAuth.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2020 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::URLAuth;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44 use File::Path qw(mkpath);
45 use DateTime;
46 use Data::Dumper;
47 use Digest::SHA qw(hmac_sha1_hex);
48
49 use lib '.';
50 use base qw(Cassandane::Cyrus::TestCase);
51 use Cassandane::Util::Log;
52 use Cassandane::Util::NetString;
53
54
55 sub new
56 {
57 my $class = shift;
58
59 my $config = Cassandane::Config->default()->clone();
60 $config->set(servername => "127.0.0.1"); # urlauth needs matching servername
61
62 return $class->SUPER::new({
63 config => $config,
64 adminstore => 1,
65 services => ['imap']
66 }, @_);
67 }
68
69 sub set_up
70 {
71 my ($self) = @_;
72 $self->SUPER::set_up();
73 }
74
75 sub tear_down
76 {
77 my ($self) = @_;
78 $self->SUPER::tear_down();
79 }
80
81 sub test_urlfetch
82 {
83 my ($self) = @_;
84
85 my %exp_sub;
86 my $store = $self->{store};
87 my $talk = $store->get_client();
88
89 $store->set_folder("INBOX");
90 $store->_select();
91 $self->{gen}->set_next_uid(1);
92
93 my $body;
94
95 # Subpart 1
96 $body = "--047d7b33dd729737fe04d3bde348\r\n"
97 . "Content-Type: text/plain; charset=UTF-8\r\n"
98 . "\r\n"
99 . "body1"
100 . "\r\n";
101
102 # Subpart 2
103 $body .= "--047d7b33dd729737fe04d3bde348\r\n"
104 . "Content-Type: multipart/mixed;boundary=frontier\r\n"
105 . "\r\n";
106
107 # Subpart 2.1
108 $body .= "--frontier\r\n"
109 . "Content-Type: text/plain\r\n"
110 . "\r\n"
111 . "body21"
112 . "\r\n";
113
114 # End subpart 2
115 $body .= "--frontier--\r\n";
116
117 # Subpart 3
118 my $msg3 = ""
119 . "Return-Path: <Ava.Nguyen\@local>\r\n"
120 . "Mime-Version: 1.0\r\n"
121 . "Content-Type: text/plain\r\n"
122 . "Content-Transfer-Encoding: 7bit\r\n"
123 . "Subject: bar\r\n"
124 . "From: Ava T. Nguyen <Ava.Nguyen\@local>\r\n"
125 . "Message-ID: <fake.1475639947.6507\@local>\r\n"
126 . "Date: Wed, 05 Oct 2016 14:59:07 +1100\r\n"
127 . "To: Test User <test\@local>\r\n"
128 . "\r\n"
129 . "body3";
130
131 $body .= "--047d7b33dd729737fe04d3bde348\r\n"
132 . "Content-Type: message/rfc822\r\n"
133 . "\r\n"
134 . $msg3
135 . "\r\n";
136
137 # End body
138 $body .= "--047d7b33dd729737fe04d3bde348--";
139
140 $self->make_message("foo",
141 mime_type => "multipart/mixed",
142 mime_boundary => "047d7b33dd729737fe04d3bde348",
143 body => $body
144 );
145
146 my $data;
147 my %handlers =
148 (
149 urlfetch => sub
150 {
151 my ($cmd, $params) = @_;
152 $data = ${$params}[1];
153 },
154 );
155
156 my $url = $talk->_imap_cmd('genurlauth', 0, "genurlauth",
157 "imap://cassandane\@127.0.0.1/INBOX/;uid=1/;section=3.TEXT;partial=1.3;urlauth=user+cassandane",
158 'INTERNAL');
159
160 my $res = $talk->_imap_cmd('urlfetch', 0, \%handlers, substr($url, 1, -1));
161
162 $self->assert_str_equals($data, "ody");
163 }
164
165 sub test_perms
166 :UnixHierarchySep :AltNamespace
167 {
168 my ($self) = @_;
169
170 $self->{instance}->create_user('other');
171
172 my $msg = $self->make_message("Message A");
173
174 my $url;
175 my %handlers = (
176 genurlauth => sub {
177 my ($cmd, $params) = @_;
178 $url = $params->[0];
179 }
180 );
181
182 my $svc = $self->{instance}->get_service('imap');
183 my $store = $svc->create_store(username => 'other');
184 my $talk = $store->get_client();
185
186 $talk->_imap_cmd(
187 'genurlauth', 0, \%handlers,
188 "imap://other\@127.0.0.1/Other%20Users%2fcassandane/;uid=1;urlauth=user+other",
189 "INTERNAL",
190 );
191
192 # 'other' has no rights on cassandane's mailbox, so the server must refuse
193 # to mint a URLAUTH token for it. Without this check, cmd_urlfetch would
194 # happily hand over the message: it skips ACLs whenever urlauth.access is
195 # set, trusting that a valid token implies the authorizer had the rights
196 # to delegate.
197 $self->assert_null($url);
198 }
199
200 sub test_revoked_access_invalidates_url
201 :UnixHierarchySep :AltNamespace
202 {
203 my ($self) = @_;
204
205 my $admintalk = $self->{adminstore}->get_client;
206
207 $self->{instance}->create_user('other');
208
209 $self->make_message("Secret Message");
210
211 # Grant 'other' read+lookup so that genurlauth will mint a token.
212 $admintalk->setacl('user/cassandane', 'other', 'lrs')
213 or die "setacl: " . $admintalk->get_last_error;
214
215 my $svc = $self->{instance}->get_service('imap');
216 my $store = $svc->create_store(username => 'other');
217 my $other_talk = $store->get_client();
218
219 # 'other' mints a URLAUTH token while authorized.
220 my $url;
221 $other_talk->_imap_cmd(
222 'genurlauth', 0,
223 { genurlauth => sub { $url = $_[1]->[0] } },
224 'imap://[email protected]/Other%20Users%2fcassandane/;uid=1;urlauth=user+other',
225 'INTERNAL',
226 );
227 $self->assert_not_null($url, 'genurlauth should succeed while authorized');
228
229 # Captured value is wrapped in quotes; strip them for urlfetch.
230 (my $bare_url = $url) =~ s/\A"(.*)"\z/$1/;
231
232 # Sanity check: the token works while access is in place.
233 my $data;
234 $other_talk->_imap_cmd(
235 'urlfetch', 0,
236 { urlfetch => sub { $data = $_[1]->[1] } },
237 $bare_url,
238 );
239 $self->assert_not_null($data, 'URL should fetch while authorizer is authorized');
240
241 # Revoke 'other's access entirely.
242 $admintalk->setacl('user/cassandane', 'other', '')
243 or die "setacl: " . $admintalk->get_last_error;
244
245 # The token is cryptographically intact, but the authorizer's right to
246 # delegate the message is gone. cmd_urlfetch must re-check ACLs at fetch
247 # time and refuse, instead of trusting the token alone.
248 my $stolen;
249 $other_talk->_imap_cmd(
250 'urlfetch', 0,
251 { urlfetch => sub { $stolen = $_[1]->[1] } },
252 $bare_url,
253 );
254 $self->assert_null($stolen, 'URL must not fetch after authorizer access revoked');
255 }
256
257 sub test_forged_empty_key
258 :UnixHierarchySep :AltNamespace
259 {
260 my ($self) = @_;
261
262 # The victim has an INBOX and a second folder. The secret we don't want
263 # leaked lives in the second folder.
264 my $talk = $self->{store}->get_client;
265 $talk->create("Archive") or die "create Archive: " . $talk->get_last_error;
266
267 $self->{store}->set_folder("INBOX");
268 $self->make_message("Public INBOX message");
269
270 $self->{store}->set_folder("Archive");
271 $self->make_message("Secret archived message");
272
273 # Cause the victim's mboxkey.db to be created, but with an entry only for
274 # the INBOX -- *not* for the Archive folder we're about to attack. An
275 # mboxkey.db appears in the wild whenever a user has ever issued a
276 # GENURLAUTH (or a RESETKEY for a specific mailbox).
277 my $seed;
278 $talk->_imap_cmd(
279 'genurlauth', 0,
280 { genurlauth => sub { $seed = $_[1]->[0] } },
281 'imap://[email protected]/INBOX/;uid=1;urlauth=user+cassandane',
282 'INTERNAL',
283 );
284 $self->assert_not_null($seed, 'victim should be able to URLAUTH their own INBOX');
285
286 # Now the attacker forges a token for the *Archive* folder. Because
287 # mboxkey_read() returns {key=NULL, keylen=0} for any mailbox it has never
288 # stored, cmd_urlfetch computes HMAC-SHA1 with an empty key -- which is
289 # entirely attacker-computable.
290 $self->{instance}->create_user('attacker');
291
292 my $rump = 'imap://[email protected]/Archive/;uid=1;urlauth=user+attacker';
293 my $token = '00' . hmac_sha1_hex($rump, '');
294 my $forged = "$rump:internal:$token";
295
296 my $svc = $self->{instance}->get_service('imap');
297 my $store = $svc->create_store(username => 'attacker');
298 my $attacker = $store->get_client();
299
300 my $stolen;
301 $attacker->_imap_cmd(
302 'urlfetch', 0,
303 { urlfetch => sub { $stolen = $_[1]->[1] } },
304 $forged,
305 );
306
307 $self->assert_null($stolen);
308 }
309
310 1;