rewrite_unescape.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for escaping/unescaping in rewrite module.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. BEGIN { use FindBin; chdir($FindBin::Bin); }
  9. use lib 'lib';
  10. use Test::Nginx;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. my $t = Test::Nginx->new()->has(qw/http rewrite/)->plan(9)
  15. ->write_file_expand('nginx.conf', <<'EOF');
  16. %%TEST_GLOBALS%%
  17. master_process off;
  18. daemon off;
  19. events {
  20. }
  21. http {
  22. %%TEST_GLOBALS_HTTP%%
  23. server {
  24. listen 127.0.0.1:8080;
  25. server_name localhost;
  26. location /t1 {
  27. rewrite ^ $arg_r? redirect;
  28. }
  29. location /t2 {
  30. rewrite ^ http://example.com$request_uri? redirect;
  31. }
  32. location /t3 {
  33. rewrite ^ http://example.com$uri redirect;
  34. }
  35. location /t4 {
  36. rewrite ^(.*) http://example.com$1 redirect;
  37. }
  38. location /t5 {
  39. rewrite ^ http://example.com/blah%20%3Fblah redirect;
  40. }
  41. location /t6 {
  42. rewrite ^ http://example.com/blah%20%2Fblah redirect;
  43. }
  44. }
  45. }
  46. EOF
  47. mkdir($t->testdir() . '/directory');
  48. $t->run();
  49. ###############################################################################
  50. # Some rewrites and expected (?) behaviour
  51. #
  52. # /t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom
  53. # rewrite ^ $arg_r? redirect;
  54. # expected: http://example.com/?from
  55. # got: http://example.com/?from
  56. #
  57. # /t1?r=http%3A%2F%2Fexample.com%0D%0Asplit
  58. # rewrite ^ $arg_r? redirect;
  59. # expected: http://example.com%0D%0Asplit
  60. # got: http://example.com%0D%0Asplit
  61. #
  62. # /t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom%3Dblah
  63. # rewrite ^ $arg_r? redirect;
  64. # expected: http://example.com/?from=blah
  65. # got: http://example.com/?from%3Dblah
  66. #
  67. # /blah%3Fblah
  68. # rewrite ^ http://example.com$request_uri? redirect;
  69. # expected: http://example.com/blah%3Fblah
  70. # got: http://example.com/blah?blah
  71. #
  72. # /blah%3Fblah
  73. # rewrite ^ http://example.com$uri redirect;
  74. # expected: http://example.com/blah%3Fblah
  75. # got: http://example.com/blah?blah
  76. #
  77. # /blah%3Fblah
  78. # rewrite ^(.*) http://example.com$1 redirect;
  79. # expected: http://example.com/blah%3Fblah
  80. # got: http://example.com/blah?blah
  81. #
  82. # /
  83. # rewrite ^ http://example.com/blah%3Fblah redirect;
  84. # expected: http://example.com/blah%3Fblah
  85. # got: http://example.com/blah?blah
  86. #
  87. location('/t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom',
  88. 'http://example.com/?from', 'escaped argument');
  89. location('/t1?r=http%3A%2F%2Fexample.com%0D%0Asplit',
  90. 'http://example.com%0D%0Asplit', 'escaped argument header splitting');
  91. TODO: {
  92. local $TODO = 'not yet';
  93. # Fixing this cases will require major changes to the whole aproach and
  94. # likely to break some currently working cases. On the other hand, current
  95. # behaviour is far from acceptable. Should be carefully thought.
  96. location('/t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom%3Dblah',
  97. 'http://example.com/?from=blah', 'escaped argument with complex query');
  98. location('/t2/blah%20%3Fblah',
  99. 'http://example.com/t2/blah%20%3Fblah', 'escaped $request_uri');
  100. location('/t3/blah%20%3Fblah',
  101. 'http://example.com/t3/blah%20%3Fblah', 'escaped $uri');
  102. location('/t4/blah%20%3Fblah',
  103. 'http://example.com/t4/blah%20%3Fblah', 'escaped $1');
  104. location('/t5',
  105. 'http://example.com/blah%20%3Fblah', 'escaped static');
  106. location('/t5?arg=blah',
  107. 'http://example.com/blah%20%3Fblah?arg=blah',
  108. 'escaped static with argument');
  109. location('/t6',
  110. 'http://example.com/blah%20%2Fblah', 'escaped static slash');
  111. }
  112. ###############################################################################
  113. sub location {
  114. my ($url, $value, $name) = @_;
  115. my $data = http_get($url);
  116. if ($data !~ qr!^Location: (.*?)\x0d?$!ms) {
  117. fail($name);
  118. return;
  119. }
  120. my $location = $1;
  121. is($location, $value, $name);
  122. }
  123. ###############################################################################