File: 05-tampering.t

package info (click to toggle)
libcgi-application-plugin-linkintegrity-perl 0.06-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: perl: 269; sh: 7; makefile: 2
file content (447 lines) | stat: -rw-r--r-- 14,218 bytes parent folder | download | duplicates (4)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

use Test::More 'no_plan';
use strict;
$ENV{CGI_APP_RETURN_ONLY} = 1;

use URI;
use URI::QueryParam;

my $Created_Link;

{
    package WebApp;
    use CGI;
    use CGI::Application;
    use vars qw(@ISA);
    use URI;
    use URI::Escape;
    @ISA = ('CGI::Application');

    use Test::More;
    use CGI::Application::Plugin::LinkIntegrity;

    sub setup {
        my $self = shift;

        $self->header_type('none');
        $self->run_modes([qw(
            link_okay
            create_link
            bad_user_no_biscuit
            link_tampered
        )]);

        my %li_config = (
            secret  => 'extree seekrit',
        );
        if ($self->param('custom_rm')) {
            $li_config{'link_tampered_run_mode'} = 'bad_user_no_biscuit';
        }
        if ($self->param('check')) {
            $li_config{'checksum_param'} = $self->param('check');
        }
        if ($self->param('create_link')) {
            $self->start_mode('create_link');
            $li_config{'disable'} = 1;
        }
        else {
            $self->start_mode('link_okay');
        }
        $self->link_integrity_config(
            %li_config,
        );
    }

    sub link_okay {
        my $self = shift;
        return 'rm=link_okay';
    }
    sub create_link {
        my $self = shift;
        return $self->link($self->param('create_link'));
    }
    sub link_tampered {
        my $self = shift;
        return 'rm=link_tampered';
    }
    sub bad_user_no_biscuit {
        my $self = shift;
        return 'rm=bad_user_no_biscuit';
    }

}

###########################################################################
# Entry point: Build a link without parameters

my $meth;

my $link = URI->new('http://www.example.com/script.cgi');

$ENV{'REQUEST_METHOD'} = $meth = 'GET';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = $link->query || '';

CGI::initialize_globals();
is(WebApp->new->run, 'rm=link_okay', "[$meth] entry_point (no params) link_okay");

local *STDIN;
open(STDIN, '<', '/dev/null');

$ENV{'REQUEST_METHOD'} = $meth = 'POST';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = $link->query || '';

CGI::initialize_globals();
is(WebApp->new->run, 'rm=link_okay', "[$meth] entry_point (no params) link_okay");

$ENV{'REQUEST_METHOD'} = $meth = 'POST';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = 'foo+bar+baz+boom';

CGI::initialize_globals();
is(WebApp->new->run, 'rm=link_okay', "[$meth] entry_point (no named params but with keywords) link_okay");


###########################################################################
# Bad entry point #1: Build a link with a non-blank keyword parameter

$link = URI->new('http://www.example.com/script.cgi?keywords=x');

# Validate it
$ENV{'REQUEST_METHOD'} = $meth = 'GET';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = $link->query || '';
# print STDERR "[$link] qs: $ENV{'QUERY_STRING'}\n";

CGI::initialize_globals();
is(WebApp->new->run, 'rm=link_tampered', "[$meth] bad entry_point 1 (non-blank 'keywords' param) link_tampered");


###########################################################################
# Bad entry point #2: Build a link with a zero (still non-blank) keyword parameter

$link = URI->new('http://www.example.com/script.cgi?keywords=0');

# Validate it
$ENV{'REQUEST_METHOD'} = $meth = 'GET';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = $link->query;
# print STDERR "[$link] qs: $ENV{'QUERY_STRING'}\n";

CGI::initialize_globals();
is(WebApp->new->run, 'rm=link_tampered', "[$meth] bad entry_point 2 (false but non-blank 'keywords' param) link_tampered");


###########################################################################
# Bad entry point #3: Build a link with actual keywords

$link = URI->new('http://www.example.com/script.cgi');

# Validate it
$ENV{'REQUEST_METHOD'} = $meth = 'POST';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = 'foo+bar+baz boom';
# print STDERR "[$link] qs: $ENV{'QUERY_STRING'}\n";

is(WebApp->new->run, 'rm=link_tampered', "[$meth] bad entry_point 4 (URI contains actual keywords) link_tampered");


# Build the link
$ENV{'REQUEST_METHOD'} = 'POST';
$ENV{'SERVER_PORT'}    = '80';
$ENV{'SCRIPT_NAME'}    = '/cgi-bin/app.cgi';
$ENV{'SERVER_NAME'}    = 'www.example.com';
$ENV{'PATH_INFO'}      = '/my/happy/pathy/info';
$ENV{'QUERY_STRING'}   = 'zap=zoom&zap=zub&guff=gubbins&zap=zuzzu';

$link = URI->new(WebApp->new(PARAMS => {
    create_link => 'http://www.example.com/script.cgi/path/info?p1=v1&p2=v2&p2=v3',
})->run);

# Validate it

$ENV{'REQUEST_METHOD'} = 'POST';
$ENV{'SERVER_PORT'}    = $link->port;
$ENV{'SCRIPT_NAME'}    = $link->path;
$ENV{'SERVER_NAME'}    = $link->authority;
$ENV{'PATH_INFO'}      = '';
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_okay', '[POST] link_okay');


# remove the _checksum from the query - this should invalidate it
my $checksum = $link->query_param_delete('_checksum');
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[POST] link_tampered (checksum removed)');

# add a bogus checksum - this should invalidate it
my $qf = $link->query_form_hash;
$qf->{'_checksum'} = 'xxxx';
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[POST] link_tampered (checksum changed)');


# restore original checksum - this should revalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = $checksum;
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_okay', '[POST] link_okay (original checksum added again)');

###########################################################################
# Same tests with method=GET
$ENV{'REQUEST_METHOD'} = 'GET';

is(WebApp->new->run, 'rm=link_okay', '[GET] link_okay');

# remove the _checksum from the query - this should invalidate it
$checksum = $link->query_param_delete('_checksum');
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[GET] link_tampered (checksum removed)');

# add a bogus checksum - this should invalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = 'xxxx';
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[GET] link_tampered (checksum changed)');


# restore original checksum - this should revalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = $checksum;
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=link_okay', '[GET] link_okay (original checksum added again)');


###########################################################################
# Same tests with method=XXX
$ENV{'REQUEST_METHOD'} = 'XXX';

is(WebApp->new->run, 'rm=link_okay', '[XXX] link_okay');

# remove the _checksum from the query - this should invalidate it
$checksum = $link->query_param_delete('_checksum');
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[XXX] link_tampered (checksum removed)');

# add a bogus checksum - this should invalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = 'xxxx';
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[XXX] link_tampered (checksum changed)');


# restore original checksum - this should revalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = $checksum;
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=link_okay', '[XXX] link_okay (original checksum added again)');


###########################################################################
# Same tests with method=BARNEY
$ENV{'REQUEST_METHOD'} = 'PUT';

is(WebApp->new->run, 'rm=link_okay', '[PUT] link_okay');

# remove the _checksum from the query - this should invalidate it
$checksum = $link->query_param_delete('_checksum');
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[PUT] link_tampered (checksum removed)');

# add a bogus checksum - this should invalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = 'xxxx';
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new->run, 'rm=link_tampered', '[PUT] link_tampered (checksum changed)');


# restore original checksum - this should revalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = $checksum;
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=link_okay', '[PUT] link_okay (original checksum added again)');



###########################################################################
# Same tests with custom link_tampered_run_mode
$ENV{'REQUEST_METHOD'} = 'GET';

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=link_okay', '[CUSTOM RM] link_okay');

# remove the _checksum from the query - this should invalidate it
$checksum = $link->query_param_delete('_checksum');
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=bad_user_no_biscuit', '[CUSTOM RM] bad_user (checksum removed)');

# add a bogus checksum - this should invalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = 'xxxx';
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=bad_user_no_biscuit', '[CUSTOM RM] bad_user (checksum changed)');


# restore original checksum - this should revalidate it
$qf = $link->query_form_hash;
$qf->{'_checksum'} = $checksum;
$qf = $link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'custom_rm' => 1 })->run, 'rm=link_okay', '[CUSTOM RM] link_okay (original checksum added again)');


###########################################################################
# Same tests with custom checksum_param
$ENV{'REQUEST_METHOD'} = 'GET';

is(WebApp->new(PARAMS => { 'check' => '_checksum' })->run, 'rm=link_okay', '[custom checksum] link_okay');
is(WebApp->new(PARAMS => { 'check' => '**frobnicate**' })->run, 'rm=link_tampered', '[custom checksum] link_tampered (changed checksum_param)');

# remove the _checksum from the query - this should invalidate it
$qf = $link->query_form_hash;
delete $qf->{'_checksum'};
$qf->{'**frobnicate**'} = 'xxxx';
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'check' => '**frobnicate**' })->run, 'rm=link_tampered', '[custom checksum] link_tampered (checksum removed)');

# add a bogus checksum - this should invalidate it
delete $qf->{'**frobnicate**'};
$qf->{'xxx_frobozz_xxx'} = 'xxxx';
$link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'check' => 'xxx_frobozz_xxx' })->run, 'rm=link_tampered', '[custom checksum] link_tampered (checksum changed)');

# restore original checksum - this should revalidate it
delete $qf->{'xxx_frobozz_xxx'};
$qf->{'xxx_fizzle_***'} = $checksum;
$link->query_form_hash($qf);

$ENV{'QUERY_STRING'}   = $link->query;

is(WebApp->new(PARAMS => { 'check' => 'xxx_fizzle_***' })->run, 'rm=link_okay', '[custom checksum] link_okay (original checksum added again)');

###########################################################################
# Leave the checksum, alter a param

delete $qf->{'xxx_fizzle_***'};
$qf->{'_checksum'} = $checksum;
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');

# add a param
$qf->{'yyy'} = 'xxxx';
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_tampered', '[alter param] link_tampered (added param)');

# change a param
delete $qf->{'yyy'};
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');

$qf->{'p1'} = 'v1b';
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_tampered', '[alter param] link_tampered (changed param)');

$qf->{'p1'} = 'v1';
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');

# change a value
$qf->{'p2'} = ['v2', 'v3b'];
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_tampered', "[alter param] link_tampered (changed a param's value)");

$qf->{'p2'} = ['v2', 'v3'];
$link->query_form_hash($qf);
$ENV{'QUERY_STRING'}   = $link->query;
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');



# change host
$ENV{'SERVER_NAME'}    = 'www.badhost.com';
is(WebApp->new->run, 'rm=link_tampered', "[alter param] link_tampered (changed a host)");

$link->query_form_hash($qf);
$ENV{'SERVER_NAME'}    = 'www.example.com';
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');

# change port
$ENV{'SERVER_PORT'}    = '88';
is(WebApp->new->run, 'rm=link_tampered', "[alter param] link_tampered (changed a host)");

$link->query_form_hash($qf);
$ENV{'SERVER_PORT'}    = '80';
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');

# change scheme
$ENV{'HTTPS'}    = 'on';
is(WebApp->new->run, 'rm=link_tampered', "[alter param] link_tampered (changed a host)");

$link->query_form_hash($qf);
delete $ENV{'HTTPS'};
is(WebApp->new->run, 'rm=link_okay', '[alter param] link_okay');