secdownload.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ===========================
  2. Secure and Fast Downloading
  3. ===========================
  4. -----------------------
  5. Module: mod_secdownload
  6. -----------------------
  7. :Author: Jan Kneschke
  8. :Date: $Date: 2004/08/01 07:01:29 $
  9. :Revision: $Revision: 1.1 $
  10. :abstract:
  11. authenticated file requests and a countermeasure against
  12. deep-linking can be achieved easily by using mod_secdownload
  13. .. meta::
  14. :keywords: lighttpd, secure, fast, downloads
  15. .. contents:: Table of Contents
  16. Options
  17. =======
  18. ::
  19. secdownload.secret = <string>
  20. secdownload.document-root = <string>
  21. secdownload.uri-prefix = <string> (default: /)
  22. secdownload.timeout = <short> (default: 60 seconds)
  23. Description
  24. ===========
  25. there are multiple ways to handle secured download mechanisms:
  26. 1. use the webserver and the internal HTTP authentication
  27. 2. use the application to authenticate and send the file
  28. through the application
  29. Both ways have limitations:
  30. webserver:
  31. - ``+`` fast download
  32. - ``+`` no additional system load
  33. - ``-`` inflexible authentication handling
  34. application:
  35. - ``+`` integrated into the overall layout
  36. - ``+`` very flexible permission management
  37. - ``-`` the download occupies an application thread/process
  38. A simple way to combine the two ways could be:
  39. 1. app authenticates user and checks permissions to
  40. download the file.
  41. 2. app redirects user to the file accessible by the webserver
  42. for further downloading.
  43. 3. the webserver transfers the file to the user.
  44. As the webserver doesn't know anything about the permissions
  45. used in the app, the resulting URL would be available to every
  46. user who knows the URL.
  47. mod_secdownload removes this problem by introducing a way to
  48. authenticate a URL for a specified time. The application has
  49. to generate a token and a timestamp which are checked by the
  50. webserver before it allows the file to be downloaded by the
  51. webserver.
  52. The generated URL has to have the format:
  53. <uri-prefix><token>/<timestamp-in-hex><rel-path>
  54. <token> is an MD5 of
  55. 1. a secret string (user supplied)
  56. 2. <rel-path> (starts with /)
  57. 3. <timestamp-in-hex>
  58. As you can see, the token is not bound to the user at all. The
  59. only limiting factor is the timestamp which is used to
  60. invalidate the URL after a given timeout (secdownload.timeout).
  61. .. Note::
  62. Be sure to choose a another secret than the one used in the
  63. examples, as this is the only part of the token that is not
  64. known to the user.
  65. If the user tries to fake the URL by choosing a random token,
  66. status 403 'Forbidden' will be sent out.
  67. If the timeout is reached, status 408 'Request Timeout' will be
  68. sent. (This does not really conform to the standard, but should
  69. do the trick).
  70. If token and timeout are valid, the <rel-path> is appended to
  71. the configured (secdownload.document-root) and passed to the
  72. normal internal file transfer functionality. This might lead to
  73. status 200 or 404.
  74. Example
  75. =======
  76. Application
  77. -----------
  78. Your application has to generate the correct URLs. The following sample
  79. code for PHP should be easily adaptable to any other language: ::
  80. <?php
  81. $secret = "verysecret";
  82. $uri_prefix = "/dl/";
  83. # filename
  84. $f = "/secret-file.txt";
  85. # current timestamp
  86. $t = time();
  87. $t_hex = sprintf("%08x", $t);
  88. $m = md5($secret.$f.$t_hex);
  89. # generate link
  90. printf('<a href="%s%s/%s%s">%s</a>',
  91. $uri_prefix, $m, $t_hex, $f, $f);
  92. ?>
  93. Webserver
  94. ---------
  95. The server has to be configured in the same way. The URI prefix and
  96. secret have to match: ::
  97. server.modules = ( ..., "mod_secdownload", ... )
  98. secdownload.secret = "verysecret"
  99. secdownload.document-root = "/home/www/servers/download-area/"
  100. secdownload.uri-prefix = "/dl/"
  101. secdownload.timeout = 120
  102. secdownload.algorithm = "md5"