~ps10gel/ubuntu/xenial/nginx/add-with-http_slice_module

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/t/024-access/exit.t

  • Committer: ps10gel at gmail
  • Author(s): Christos Trochalakis, Christos Trochalakis
  • Date: 2016-05-31 19:01:25 UTC
  • Revision ID: ps10gel@gmail.com-20160531190125-o9xy03d29nhzti33
Tags: 1.10.1-1
[ Christos Trochalakis ]
* New upstream release (1.10.1)
  Fixes CVE-2016-4450
  NULL pointer dereference while writing client request body.
  (Closes: #825960)

* debian/control:
  + Generate dbgsym packages only if dh_strip supports it.
    Thanks Faidon Liambotis for the hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim:set ft= ts=4 sw=4 et fdm=marker:
 
2
 
 
3
use Test::Nginx::Socket::Lua;
 
4
 
 
5
#repeat_each(20000);
 
6
repeat_each(2);
 
7
 
 
8
#master_on();
 
9
#workers(1);
 
10
#log_level('debug');
 
11
#log_level('warn');
 
12
#worker_connections(1024);
 
13
 
 
14
plan tests => repeat_each() * (blocks() * 2 + 2);
 
15
 
 
16
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
 
17
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;
 
18
 
 
19
our $LuaCpath = $ENV{LUA_CPATH} ||
 
20
    '/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;';
 
21
 
 
22
no_long_string();
 
23
 
 
24
run_tests();
 
25
 
 
26
__DATA__
 
27
 
 
28
=== TEST 1: throw 403
 
29
--- config
 
30
    location /lua {
 
31
        access_by_lua "ngx.exit(403);ngx.say('hi')";
 
32
        content_by_lua 'ngx.exit(ngx.OK)';
 
33
    }
 
34
--- request
 
35
GET /lua
 
36
--- error_code: 403
 
37
--- response_body_like: 403 Forbidden
 
38
 
 
39
 
 
40
 
 
41
=== TEST 2: throw 404
 
42
--- config
 
43
    location /lua {
 
44
        access_by_lua "ngx.exit(404);ngx.say('hi');";
 
45
        content_by_lua 'ngx.exit(ngx.OK)';
 
46
    }
 
47
--- request
 
48
GET /lua
 
49
--- error_code: 404
 
50
--- response_body_like: 404 Not Found
 
51
 
 
52
 
 
53
 
 
54
=== TEST 3: throw 404 after sending the header and partial body
 
55
--- config
 
56
    location /lua {
 
57
        access_by_lua "ngx.say('hi');ngx.exit(404);ngx.say(', you')";
 
58
        content_by_lua 'ngx.exit(ngx.OK)';
 
59
    }
 
60
--- request
 
61
GET /lua
 
62
--- response_body
 
63
hi
 
64
--- no_error_log
 
65
[alert]
 
66
--- error_log
 
67
attempt to set status 404 via ngx.exit after sending out the response status 200
 
68
 
 
69
 
 
70
 
 
71
=== TEST 4: working with ngx_auth_request (succeeded)
 
72
--- config
 
73
    location /auth {
 
74
        access_by_lua "
 
75
            if ngx.var.user == 'agentzh' then
 
76
                ngx.eof();
 
77
            else
 
78
                ngx.exit(403)
 
79
            end";
 
80
        content_by_lua 'ngx.exit(ngx.OK)';
 
81
    }
 
82
    location /api {
 
83
        set $user $arg_user;
 
84
        auth_request /auth;
 
85
 
 
86
        echo "Logged in";
 
87
    }
 
88
--- request
 
89
GET /api?user=agentzh
 
90
--- error_code: 200
 
91
--- response_body
 
92
Logged in
 
93
 
 
94
 
 
95
 
 
96
=== TEST 5: working with ngx_auth_request (failed)
 
97
--- config
 
98
    location /api {
 
99
        set $user $arg_user;
 
100
        access_by_lua "
 
101
            if ngx.var.user == 'agentzh' then
 
102
                ngx.eof();
 
103
            else
 
104
                ngx.exit(403)
 
105
            end";
 
106
 
 
107
        echo "Logged in";
 
108
    }
 
109
--- request
 
110
GET /api?user=agentz
 
111
--- error_code: 403
 
112
--- response_body_like: 403 Forbidden
 
113
 
 
114
 
 
115
 
 
116
=== TEST 6: working with ngx_auth_request (simplest form, w/o ngx_memc)
 
117
--- http_config eval
 
118
"
 
119
    lua_package_cpath '$::LuaCpath';
 
120
    upstream backend {
 
121
        drizzle_server 127.0.0.1:\$TEST_NGINX_MYSQL_PORT protocol=mysql
 
122
                       dbname=ngx_test user=ngx_test password=ngx_test;
 
123
        drizzle_keepalive max=300 mode=single overflow=ignore;
 
124
    }
 
125
"
 
126
--- config
 
127
    location /memc {
 
128
        internal;
 
129
 
 
130
        set $memc_key $arg_key;
 
131
        set $memc_exptime $arg_exptime;
 
132
 
 
133
        memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
 
134
    }
 
135
 
 
136
    location /conv-uid-mysql {
 
137
        internal;
 
138
 
 
139
        set $key "conv-uid-$arg_uid";
 
140
 
 
141
        #srcache_fetch GET /memc key=$key;
 
142
        #srcache_store PUT /memc key=$key;
 
143
 
 
144
        default_type 'application/json';
 
145
 
 
146
        drizzle_query "select new_uid as uid from conv_uid where old_uid=$arg_uid";
 
147
        drizzle_pass backend;
 
148
 
 
149
        rds_json on;
 
150
    }
 
151
 
 
152
    location /api {
 
153
        set $uid $arg_uid;
 
154
        access_by_lua_file 'html/foo.lua';
 
155
 
 
156
        echo "Logged in $uid";
 
157
    }
 
158
--- user_files
 
159
>>> foo.lua
 
160
local cjson = require('cjson');
 
161
local old_uid = ngx.var.uid
 
162
print('about to run sr')
 
163
local res = ngx.location.capture('/conv-uid-mysql?uid=' .. old_uid)
 
164
print('just have run sr' .. res.body)
 
165
if (res.status ~= ngx.HTTP_OK) then
 
166
    -- ngx.exit(res.status)
 
167
end
 
168
res = cjson.decode(res.body)
 
169
if (not res or not res[1] or not res[1].uid or
 
170
        not string.match(res[1].uid, '^%d+$')) then
 
171
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 
172
end
 
173
ngx.var.uid = res[1].uid;
 
174
-- print('done')
 
175
--- request
 
176
GET /api?uid=32
 
177
--- response_body
 
178
Logged in 56
 
179
--- timeout: 3
 
180
 
 
181
 
 
182
 
 
183
=== TEST 7: working with ngx_auth_request (simplest form)
 
184
--- http_config eval
 
185
"
 
186
    lua_package_cpath '$::LuaCpath';
 
187
    upstream backend {
 
188
        drizzle_server 127.0.0.1:\$TEST_NGINX_MYSQL_PORT protocol=mysql
 
189
                       dbname=ngx_test user=ngx_test password=ngx_test;
 
190
        drizzle_keepalive max=300 mode=single overflow=ignore;
 
191
    }
 
192
"
 
193
--- config
 
194
    location /memc {
 
195
        internal;
 
196
 
 
197
        set $memc_key $arg_key;
 
198
        set $memc_exptime $arg_exptime;
 
199
 
 
200
        memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
 
201
    }
 
202
 
 
203
    location /conv-uid-mysql {
 
204
        internal;
 
205
 
 
206
        set $key "conv-uid-$arg_uid";
 
207
 
 
208
        #srcache_fetch GET /memc key=$key;
 
209
        #srcache_store PUT /memc key=$key;
 
210
 
 
211
        default_type 'application/json';
 
212
 
 
213
        drizzle_query "select new_uid as uid from conv_uid where old_uid=$arg_uid";
 
214
        drizzle_pass backend;
 
215
 
 
216
        rds_json on;
 
217
    }
 
218
 
 
219
    location /api {
 
220
        set $uid $arg_uid;
 
221
        access_by_lua_file html/foo.lua;
 
222
 
 
223
        echo "Logged in $uid";
 
224
    }
 
225
--- user_files
 
226
>>> foo.lua
 
227
local cjson = require('cjson');
 
228
local old_uid = ngx.var.uid
 
229
-- print('about to run sr')
 
230
local res = ngx.location.capture('/conv-uid-mysql?uid=' .. old_uid)
 
231
-- print('just have run sr' .. res.body)
 
232
if (res.status ~= ngx.HTTP_OK) then
 
233
    ngx.exit(res.status)
 
234
end
 
235
res = cjson.decode(res.body)
 
236
if (not res or not res[1] or not res[1].uid or
 
237
        not string.match(res[1].uid, '^%d+$')) then
 
238
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 
239
end
 
240
ngx.var.uid = res[1].uid;
 
241
-- print('done')
 
242
--- request
 
243
GET /api?uid=32
 
244
--- response_body
 
245
Logged in 56
 
246
 
 
247
 
 
248
 
 
249
=== TEST 8: working with ngx_auth_request
 
250
--- http_config eval
 
251
"
 
252
    lua_package_cpath '$::LuaCpath';
 
253
    upstream backend {
 
254
        drizzle_server 127.0.0.1:\$TEST_NGINX_MYSQL_PORT protocol=mysql
 
255
                       dbname=ngx_test user=ngx_test password=ngx_test;
 
256
        drizzle_keepalive max=300 mode=single overflow=ignore;
 
257
    }
 
258
 
 
259
    upstream memc_a {
 
260
        server 127.0.0.1:\$TEST_NGINX_MEMCACHED_PORT;
 
261
    }
 
262
 
 
263
    upstream memc_b {
 
264
        server 127.0.0.1:\$TEST_NGINX_MEMCACHED_PORT;
 
265
    }
 
266
 
 
267
    upstream_list memc_cluster memc_a memc_b;
 
268
"
 
269
--- config
 
270
    location /memc {
 
271
        internal;
 
272
 
 
273
        set $memc_key $arg_key;
 
274
        set $memc_exptime $arg_exptime;
 
275
 
 
276
        set_hashed_upstream $backend memc_cluster $arg_key;
 
277
        memc_pass $backend;
 
278
    }
 
279
 
 
280
    location /conv-uid-mysql {
 
281
        internal;
 
282
 
 
283
        set $key "conv-uid-$arg_uid";
 
284
 
 
285
        #srcache_fetch GET /memc key=$key;
 
286
        #srcache_store PUT /memc key=$key;
 
287
 
 
288
        default_type 'application/json';
 
289
 
 
290
        drizzle_query "select new_uid as uid from conv_uid where old_uid=$arg_uid";
 
291
        drizzle_pass backend;
 
292
 
 
293
        rds_json on;
 
294
    }
 
295
 
 
296
    location /api {
 
297
        set $uid $arg_uid;
 
298
        access_by_lua_file html/foo.lua;
 
299
 
 
300
        echo "Logged in $uid";
 
301
    }
 
302
--- user_files
 
303
>>> foo.lua
 
304
local cjson = require('cjson');
 
305
local old_uid = ngx.var.uid
 
306
-- print('about to run sr')
 
307
local res = ngx.location.capture('/conv-uid-mysql?uid=' .. old_uid)
 
308
-- print('just have run sr' .. res.body)
 
309
if (res.status ~= ngx.HTTP_OK) then
 
310
    ngx.exit(res.status)
 
311
end
 
312
res = cjson.decode(res.body)
 
313
if (not res or not res[1] or not res[1].uid or
 
314
        not string.match(res[1].uid, '^%d+$')) then
 
315
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 
316
end
 
317
ngx.var.uid = res[1].uid;
 
318
-- print('done')
 
319
--- request
 
320
GET /api?uid=32
 
321
--- response_body
 
322
Logged in 56
 
323
 
 
324
 
 
325
 
 
326
=== TEST 9: working with ngx_auth_request
 
327
--- http_config
 
328
    upstream backend {
 
329
        drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql
 
330
                       dbname=ngx_test user=ngx_test password=ngx_test;
 
331
        drizzle_keepalive max=300 mode=single overflow=ignore;
 
332
    }
 
333
 
 
334
    upstream memc_a {
 
335
        server 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
 
336
        keepalive 300;
 
337
    }
 
338
 
 
339
    #upstream_list memc_cluster memc_a memc_b;
 
340
 
 
341
--- config
 
342
    location /memc {
 
343
        internal;
 
344
 
 
345
        set $memc_key $arg_key;
 
346
        set $memc_exptime $arg_exptime;
 
347
 
 
348
        #set_hashed_upstream $backend memc_cluster $arg_key;
 
349
        memc_pass memc_a;
 
350
    }
 
351
 
 
352
    location /conv-mysql {
 
353
        internal;
 
354
 
 
355
        set $key "conv-uri-$query_string";
 
356
 
 
357
        #srcache_fetch GET /memc key=$key;
 
358
        #srcache_store PUT /memc key=$key;
 
359
 
 
360
        default_type 'application/json';
 
361
 
 
362
        set_quote_sql_str $seo_uri $query_string;
 
363
        drizzle_query "select url from my_url_map where seo_url=$seo_uri";
 
364
        drizzle_pass backend;
 
365
 
 
366
        rds_json on;
 
367
    }
 
368
 
 
369
    location /conv-uid {
 
370
        internal;
 
371
        access_by_lua_file 'html/foo.lua';
 
372
        content_by_lua 'ngx.exit(ngx.OK)';
 
373
    }
 
374
 
 
375
    location /baz {
 
376
        set $my_uri $uri;
 
377
        auth_request /conv-uid;
 
378
 
 
379
        echo_exec /jump $my_uri;
 
380
    }
 
381
 
 
382
    location /jump {
 
383
        internal;
 
384
        rewrite ^ $query_string? redirect;
 
385
    }
 
386
--- user_files
 
387
>>> foo.lua
 
388
local cjson = require('cjson');
 
389
local seo_uri = ngx.var.my_uri
 
390
-- print('about to run sr')
 
391
local res = ngx.location.capture('/conv-mysql?' .. seo_uri)
 
392
if (res.status ~= ngx.HTTP_OK) then
 
393
    ngx.exit(res.status)
 
394
end
 
395
res = cjson.decode(res.body)
 
396
if (not res or not res[1] or not res[1].url) then
 
397
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
 
398
end
 
399
ngx.var.my_uri = res[1].url;
 
400
-- print('done')
 
401
--- request
 
402
GET /baz
 
403
--- response_body_like: 302
 
404
--- error_code: 302
 
405
--- response_headers
 
406
Location: http://localhost:$ServerPort/foo/bar
 
407
--- SKIP
 
408
 
 
409
 
 
410
 
 
411
=== TEST 10: throw 0
 
412
--- config
 
413
    location /lua {
 
414
        access_by_lua "ngx.say('Hi'); ngx.eof(); ngx.exit(0);ngx.say('world')";
 
415
        content_by_lua 'ngx.exit(ngx.OK)';
 
416
    }
 
417
--- request
 
418
GET /lua
 
419
--- error_code: 200
 
420
--- response_body
 
421
Hi
 
422
 
 
423
 
 
424
 
 
425
=== TEST 11: throw ngx.OK does *not* skip other later phase handlers
 
426
--- config
 
427
    location /lua {
 
428
        access_by_lua "ngx.exit(ngx.OK)";
 
429
        set $foo hello;
 
430
        echo $foo;
 
431
    }
 
432
--- request
 
433
GET /lua
 
434
--- response_body
 
435
hello
 
436
 
 
437
 
 
438
 
 
439
=== TEST 12: throw ngx.HTTP_OK *does* skip other later phase handlers (by inlined code)
 
440
--- config
 
441
    location /lua {
 
442
        access_by_lua "ngx.exit(ngx.HTTP_OK)";
 
443
        set $foo hello;
 
444
        echo $foo;
 
445
    }
 
446
--- request
 
447
GET /lua
 
448
--- response_body
 
449
 
 
450
 
 
451
 
 
452
=== TEST 13: throw ngx.HTTP_OK *does* skip other rewrite phase handlers (by inlined code + partial output)
 
453
--- config
 
454
    location /lua {
 
455
        rewrite_by_lua "ngx.say('hiya') ngx.exit(ngx.HTTP_OK)";
 
456
        set $foo hello;
 
457
        echo $foo;
 
458
    }
 
459
--- request
 
460
GET /lua
 
461
--- response_body
 
462
hiya
 
463
 
 
464
 
 
465
 
 
466
=== TEST 14: throw ngx.HTTP_OK *does* skip other later phase handlers (by file)
 
467
--- config
 
468
    location /lua {
 
469
        access_by_lua_file html/foo.lua;
 
470
        set $foo hello;
 
471
        echo $foo;
 
472
    }
 
473
--- user_files
 
474
>>> foo.lua
 
475
ngx.exit(ngx.HTTP_OK)
 
476
--- request
 
477
GET /lua
 
478
--- response_body
 
479
 
 
480
 
 
481
 
 
482
=== TEST 15: throw ngx.HTTP_OK *does* skip other rewrite phase handlers (by file + partial output)
 
483
--- config
 
484
    location /lua {
 
485
        rewrite_by_lua_file html/foo.lua;
 
486
        set $foo hello;
 
487
        echo $foo;
 
488
    }
 
489
--- user_files
 
490
>>> foo.lua
 
491
ngx.say("morning")
 
492
ngx.exit(ngx.HTTP_OK)
 
493
--- request
 
494
GET /lua
 
495
--- response_body
 
496
morning
 
497
 
 
498
 
 
499
 
 
500
=== TEST 16: error page with custom body
 
501
--- config
 
502
    error_page 410 @err;
 
503
    location @err {
 
504
        echo blah blah;
 
505
    }
 
506
    location /foo {
 
507
        access_by_lua '
 
508
            ngx.status = ngx.HTTP_GONE
 
509
            ngx.say("This is our own content")
 
510
            -- to cause quit the whole request rather than the current phase handler
 
511
            ngx.exit(ngx.HTTP_OK)
 
512
        ';
 
513
        echo Hello;
 
514
    }
 
515
--- request
 
516
    GET /foo
 
517
--- response_body
 
518
This is our own content
 
519
--- error_code: 410
 
520
 
 
521
 
 
522
 
 
523
=== TEST 17: exit(404) after I/O
 
524
--- config
 
525
    error_page 400 /400.html;
 
526
    error_page 404 /404.html;
 
527
    location /foo {
 
528
        access_by_lua '
 
529
            ngx.location.capture("/sleep")
 
530
            ngx.exit(ngx.HTTP_NOT_FOUND)
 
531
        ';
 
532
        echo Hello;
 
533
    }
 
534
 
 
535
    location /sleep {
 
536
        echo_sleep 0.002;
 
537
    }
 
538
--- user_files
 
539
>>> 400.html
 
540
Bad request, dear...
 
541
>>> 404.html
 
542
Not found, dear...
 
543
--- request
 
544
    GET /bah
 
545
--- response_body
 
546
Not found, dear...
 
547
--- error_code: 404
 
548