root/trunk/doc/secdownload.txt

Revision 1349, 3.7 kB (checked in by darix, 2 years ago)

- merged lighttpd-merge-1.4.x so we finally work in trunk/ ! yeah \o/

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