File: basic.t

package info (click to toggle)
libplack-middleware-crossorigin-perl 0.014-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 205; makefile: 2; sh: 1
file content (377 lines) | stat: -rw-r--r-- 14,818 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use Test::More 0.88;

use Plack::Middleware::CrossOrigin;
use Plack::Test;
use Plack::Builder;

test_psgi
    app => builder {
        enable 'CrossOrigin',
            origins => '*',
            headers => '*',
            methods => '*',
            credentials => 0,
            max_age => 60*60*24*30,
            expose_headers => 'X-Exposed-Header',
        ;
        sub { [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ] };
    },
    client => sub {
        my $cb = shift;
        my $req;
        my $res;

        $req = HTTP::Request->new(GET => 'http://localhost/');
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'No CORS headers added with no Origin header';
        is $res->header('Vary'), 'Origin', '... but Vary header added';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), '*', 'Access-Control-Allow-Origin header added';
        is $res->header('Access-Control-Expose-Headers'), 'X-Exposed-Header', 'Access-Control-Expose-Headers header added';
        is $res->header('Access-Control-Max-Age'), undef, 'No Max-Age header for simple request';
        is $res->header('Vary'), 'Origin', 'Vary header added';
        is $res->content, 'Hello World', "CORS handling doesn't interfere with request content";

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), '*', 'Access-Control-Allow-Origin header added for preflight';
        is $res->header('Access-Control-Allow-Methods'), 'POST', 'Access-Control-Allow-Methods header added for preflight';
        is $res->header('Vary'), 'Origin', 'Vary header added for preflight';
        is $res->header('Access-Control-Max-Age'), 60*60*24*30, 'Max-Age header added for preflight';

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Access-Control-Request-Headers' => 'X-Extra-Header',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        ok $res->header('Access-Control-Allow-Origin'), 'Request with extra headers allowed';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Referer' => 'http://www.example.com/page',
            'User-Agent' => 'AppleWebKit/534.16',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), '*', 'Buggy GET request from WebKit includes Allow-Origin header';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Referer' => 'http://www.example.com/page',
            'User-Agent' => 'AppleWebKit/534.19',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'New versions of WebKit don\'t trigger referer workaround';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        my @warnings;
        local $SIG{__WARN__} = sub { push @warnings, join '', @_ };
        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'User-Agent' => 'AppleWebKit/534.16',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'Buggy GET request from WebKit without Referer does not include Allow-Origin header';
        is $res->header('Vary'), 'Origin', 'Vary header added';
        is_deeply \@warnings, [], 'No warnings from buggy WebKit request';
    };

test_psgi
    app => builder {
        enable 'CrossOrigin',
            origins => [ 'http://www.example.com' ],
            methods => ['GET', 'POST'],
            headers => ['X-Extra-Header', 'X-Extra-Header-2'],
            max_age => 60*60*24*30,
            expose_headers => '*',
        ;
        sub { [ 200, [
            'Content-Type' => 'text/plain',
            'X-Some-Other-Header' => 'true',
        ], [ 'Hello World' ] ] };
    },
    client => sub {
        my $cb = shift;
        my $req;
        my $res;

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Access-Control-Request-Headers' => 'X-Extra-Header',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        ok $res->header('Access-Control-Allow-Origin'), 'Request with explicitly listed extra header allowed';
        is $res->header('Access-Control-Allow-Origin'), 'http://www.example.com', 'Explicitly listed origin returned';
        is $res->header('Access-Control-Allow-Headers'), 'X-Extra-Header, X-Extra-Header-2', 'Allowed headers returned';
        is $res->header('Access-Control-Allow-Methods'), 'GET, POST', 'Allowed methods returned';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Access-Control-Request-Headers' => 'X-Extra-Header-Other',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'Request with unmatched extra header rejected';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Origin' => 'http://www.example2.com',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'Request with unmatched origin rejected';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'DELETE',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'Request with unmatched method rejected';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->content, 'Hello World', 'OPTIONS request without Allow-Origin processes as normal';
        is $res->header('Access-Control-Expose-Headers'), 'Vary, X-Some-Other-Header', 'Wildcard expose headers returned';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Referer' => 'http://www.example.com/page',
            'User-Agent' => 'AppleWebKit/534.16',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), 'http://www.example.com', 'Buggy GET request from WebKit includes Allow-Origin header based on referer';
        is $res->header('Vary'), 'Origin', 'Vary header added';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Referer' => 'http://www.example.com/page',
            'User-Agent' => 'AppleWebKit/534.19',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Origin'), undef, 'New versions of WebKit don\'t trigger referer workaround';
        is $res->header('Vary'), 'Origin', 'Vary header added';
    };

test_psgi
    app => builder {
        enable 'CrossOrigin',
            origins => '*',
            methods => '*',
            credentials => 1,
        ;
        sub { [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ] };
    },
    client => sub {
        my $cb = shift;
        my $req;
        my $res;

        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->header('Access-Control-Allow-Credentials'), 'true', 'Resource with credentials adds correct header';
        is $res->header('Access-Control-Allow-Origin'), 'http://www.example.com', '... and an explicit origin';
        is $res->header('Vary'), 'Origin', '... and the Vary header';
    };

my $has_run;
test_psgi
    app => builder {
        enable 'CrossOrigin',
            origins => 'http://localhost',
        ;
        sub {
            $has_run = 1;
            [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
        };
    },
    client => sub {
        my $cb = shift;
        my $req;
        my $res;

        $req = HTTP::Request->new(POST => 'http://localhost/', [
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        is $res->code, 403, 'Disallowed simple request returns 403 error';
        ok ! $has_run, ' ... and aborts before running main app';
        is $res->header('Vary'), 'Origin', ' ... but still adds the Vary header';

        $has_run = 0;
        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Referer' => 'http://www.example.com/page',
            'User-Agent' => 'AppleWebKit/534.16',
        ]);
        $res = $cb->($req);
        ok $has_run, 'WebKit workaround always allows app to run';
        is $res->header('Vary'), 'Origin', 'Vary header added';
    };

test_psgi
    app => builder {
        enable 'CrossOrigin',
            origins => 'http://localhost',
            continue_on_failure => 1,
        ;
        sub {
            $has_run = 1;
            [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
        };
    },
    client => sub {
        my $cb = shift;
        my $req;
        my $res;

        $has_run = 0;
        $req = HTTP::Request->new(POST => 'http://localhost/', [
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        ok $has_run, 'continue_on_failure allows main app to run for simple requests';
        is $res->code, 200, ' ... and passes through results';
        is $res->header('Access-Control-Allow-Origin'), undef, ' ... and doesn\'t add headers to allow CORS';
        is $res->header('Vary'), 'Origin', ' ... but adds the Vary header';

        $has_run = 0;
        $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
            'Access-Control-Request-Method' => 'POST',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);
        ok ! $has_run, 'continue_on_failure doesn\'t run main app for preflighted request';
        is $res->header('Vary'), 'Origin', ' ... but adds the Vary header';
    };

{
    my @headers = ('Content-Type' => 'text/plain', 'Vary' => 'Accept-Language');
    test_psgi
        app => builder {
            enable 'CrossOrigin',
                origins => 'http://localhost',
            ;
            sub {
                [ 200, [ @headers ], [ 'Hello World' ] ];
            };
        },
        client => sub {
            my $cb = shift;
            my $req;
            my $res;

            $req = HTTP::Request->new(GET => 'http://localhost/', [
                'Origin' => 'http://localhost',
            ]);
            $res = $cb->($req);
            is $res->header('Vary'), 'Accept-Language, Origin', 'Vary header extended';

            unshift @headers, 'Vary' => 'Origin';
            $req = HTTP::Request->new(GET => 'http://localhost/', [
                'Origin' => 'http://localhost',
            ]);
            $res = $cb->($req);
            is $res->header('Vary'), 'Origin, Accept-Language', 'Vary header not duplicated';
        };
}

{
   # Test that the access control headers are returned as single headers
   # with comma-separated values. IE 11 (at least) appears to only evaluate
   # the first 'Access-Control-Allow-Headers' header.
   #
   # We can't use test_psgi for this test because after the PSGI response
   # is parsed by HTTP::Response we can no longer tell how the headers were
   # actually formatted.
   my $app = builder {
        enable 'CrossOrigin',
            origins => [ 'http://www.example.com' ],
            methods => ['GET', 'POST'],
            headers => ['X-Extra-Header', 'X-Extra-Header-2'],
            expose_headers => ['X-Exposed-Header', 'X-Exposed-Header2'],
        ;
        sub { [ 200, [
            'Content-Type' => 'text/plain',
        ], [ 'Hello World' ] ] };
    };

   my $req = HTTP::Request->new(OPTIONS => 'http://localhost/', [
      'Access-Control-Request-Method' => 'POST',
      'Origin' => 'http://www.example.com',
   ]);

   my $res = $app->($req->to_psgi);
   is_deeply($res, [
      200,
      [
         'Content-Type'                  => 'text/plain',
         'Vary'                          => 'Origin',
         'Access-Control-Allow-Origin'   => 'http://www.example.com',
         'Access-Control-Allow-Methods'  => 'GET, POST',
         'Access-Control-Allow-Headers'  => 'X-Extra-Header, X-Extra-Header-2',
         'Access-Control-Expose-Headers' => 'X-Exposed-Header, X-Exposed-Header2'
      ],
      []
   ], 'headers returned as comma separated values for the benenfit of IE');
}

test_psgi
    app => builder {
        enable 'CrossOrigin',
            origins => [ 'http://*.example.com' ],
        ;
        sub { [ 200, [
            'Content-Type' => 'text/plain',
        ], [ 'Hello World' ] ] };
    },
    client => sub {
        my $cb = shift;
        my $req;
        my $res;

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Access-Control-Request-Method' => 'GET',
            'Origin' => 'http://www.example.com',
        ]);
        $res = $cb->($req);

        is $res->header('Access-Control-Allow-Origin'), 'http://www.example.com',
          'wildcard as partial domain allowed';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Access-Control-Request-Method' => 'GET',
            'Origin' => 'http://www2.example.com',
        ]);
        $res = $cb->($req);

        is $res->header('Access-Control-Allow-Origin'), 'http://www2.example.com',
          'wildcard as partial domain matches numbers';

        $req = HTTP::Request->new(GET => 'http://localhost/', [
            'Access-Control-Request-Method' => 'GET',
            'Origin' => 'http://www.example2.com',
        ]);
        $res = $cb->($req);

        ok !$res->header('Access-Control-Allow-Origin'),
          'non-matching origin not allowed with wildcard';

    },
;

done_testing;