secure_link.t 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for nginx secure_link module.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. use Digest::MD5 qw/ md5 md5_hex /;
  9. use MIME::Base64 qw/ encode_base64 /;
  10. BEGIN { use FindBin; chdir($FindBin::Bin); }
  11. use lib 'lib';
  12. use Test::Nginx;
  13. ###############################################################################
  14. select STDERR; $| = 1;
  15. select STDOUT; $| = 1;
  16. my $t = Test::Nginx->new()->has(qw/http secure_link/)->plan(8);
  17. $t->write_file_expand('nginx.conf', <<'EOF');
  18. %%TEST_GLOBALS%%
  19. daemon off;
  20. events {
  21. }
  22. http {
  23. %%TEST_GLOBALS_HTTP%%
  24. server {
  25. listen 127.0.0.1:8080;
  26. server_name localhost;
  27. location / {
  28. # new style
  29. # /test.html?hash=BASE64URL
  30. secure_link $arg_hash;
  31. secure_link_md5 secret$uri;
  32. # invalid hash
  33. if ($secure_link = "") {
  34. return 403;
  35. }
  36. # expired
  37. if ($secure_link = "0") {
  38. return 403;
  39. }
  40. # $secure_link = "1"
  41. }
  42. location = /expires.html {
  43. # new style with expires
  44. # /test.html?hash=BASE64URL&expires=12345678
  45. secure_link $arg_hash,$arg_expires;
  46. secure_link_md5 secret$uri$arg_expires;
  47. # invalid hash
  48. if ($secure_link = "") {
  49. return 403;
  50. }
  51. # expired
  52. if ($secure_link = "0") {
  53. return 403;
  54. }
  55. # $secure_link = "1"
  56. }
  57. location /p/ {
  58. # old style
  59. # /p/d8e8fca2dc0f896fd7cb4cb0031ba249/test.html
  60. secure_link_secret secret;
  61. if ($secure_link = "") {
  62. return 403;
  63. }
  64. rewrite ^ /$secure_link break;
  65. }
  66. }
  67. }
  68. EOF
  69. $t->write_file('test.html', 'PASSED');
  70. $t->write_file('expires.html', 'PASSED');
  71. $t->run();
  72. ###############################################################################
  73. # new style
  74. like(http_get('/test.html?hash=q-5vpkjBkRXXtkUMXiJVHA=='),
  75. qr/PASSED/, 'request md5');
  76. like(http_get('/test.html?hash=q-5vpkjBkRXXtkUMXiJVHA'),
  77. qr/PASSED/, 'request md5 no padding');
  78. like(http_get('/test.html'), qr/^HTTP.*403/, 'request no hash');
  79. # new style with expires
  80. my ($expires, $hash);
  81. $expires = time() + 86400;
  82. $hash = encode_base64url(md5("secret/expires.html$expires"));
  83. like(http_get('/expires.html?hash=' . $hash . '&expires=' . $expires),
  84. qr/PASSED/, 'request md5 not expired');
  85. $expires = time() - 86400;
  86. $hash = encode_base64url(md5("secret/expires.html$expires"));
  87. like(http_get('/expires.html?hash=' . $hash . '&expires=' . $expires),
  88. qr/^HTTP.*403/, 'request md5 expired');
  89. # old style
  90. like(http_get('/p/' . md5_hex('test.html' . 'secret') . '/test.html'),
  91. qr/PASSED/, 'request old style');
  92. like(http_get('/p/' . md5_hex('fake') . '/test.html'), qr/^HTTP.*403/,
  93. 'request old style fake hash');
  94. like(http_get('/p/test.html'), qr/^HTTP.*403/, 'request old style no hash');
  95. ###############################################################################
  96. sub encode_base64url {
  97. my $e = encode_base64(shift, "");
  98. $e =~ s/=+\z//;
  99. $e =~ tr[+/][-_];
  100. return $e;
  101. }
  102. ###############################################################################