• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H19-Apr-2018-834587

t/H19-Apr-2018-319250

util/H19-Apr-2018-3524

.gitattributesH A D19-Apr-201827 21

.gitignoreH A D19-Apr-2018577 6160

.travis.ymlH A D19-Apr-20183.1 KiB7365

README.mdH A D19-Apr-201811 KiB369257

configH A D19-Apr-20181.7 KiB4639

valgrind.suppressH A D19-Apr-20182.2 KiB116115

README.md

1Name
2====
3
4encrypted-session-nginx-module - encrypt and decrypt nginx variable values
5
6*This module is not distributed with the Nginx source.* See the
7installation instructions.
8
9Table of Contents
10=================
11
12* [Name](#name)
13* [Status](#status)
14* [Synopsis](#synopsis)
15* [Description](#description)
16* [Directives](#directives)
17    * [encrypted_session_key](#encrypted_session_key)
18    * [encrypted_session_iv](#encrypted_session_iv)
19    * [encrypted_session_expires](#encrypted_session_expires)
20    * [set_encrypt_session](#set_encrypt_session)
21    * [set_decrypt_session](#set_decrypt_session)
22* [Installation](#installation)
23    * [Building as a dynamic module](#building-as-a-dynamic-module)
24* [Compatibility](#compatibility)
25* [Report Bugs](#report-bugs)
26* [Source Repository](#source-repository)
27* [Getting involved](#getting-involved)
28* [Author](#author)
29* [Copyright & License](#copyright--license)
30* [See Also](#see-also)
31
32Status
33======
34
35This module is production ready.
36
37Synopsis
38========
39
40```nginx
41# key must be of 32 bytes long
42encrypted_session_key "abcdefghijklmnopqrstuvwxyz123456";
43
44# iv must not be longer than 16 bytes
45#   default: "deadbeefdeadbeef" (w/o quotes)
46encrypted_session_iv "1234567812345678";
47
48# default: 1d (1 day)
49encrypted_session_expires 2; # in sec
50
51location /encrypt {
52    set $raw 'text to encrypted'; # from the ngx_rewrite module
53    set_encrypt_session $session $raw;
54    set_encode_base32 $session; # from the ngx_set_misc module
55
56    add_header Set-Cookie 'my_login=$session';  # from the ngx_headers module
57
58    # your content handler goes here...
59}
60
61location /decrypt {
62    set_decode_base32 $session $cookie_my_login; # from the ngx_set_misc module
63    set_decrypt_session $raw $session;
64
65    if ($raw = '') {
66        # bad session
67    }
68
69    # your content handler goes here...
70}
71```
72
73Description
74===========
75
76This module provides encryption and decryption support for
77nginx variables based on AES-256 with Mac.
78
79This module is usually used with the [ngx_set_misc module](http://github.com/agentzh/set-misc-nginx-module)
80and the standard rewrite module's directives.
81
82This module can be used to implement simple user login and ACL.
83
84Usually, you just decrypt data in nginx level, and pass the unencrypted
85data to your FastCGI/HTTP backend, as in
86
87```nginx
88location /blah {
89    set_decrypt_session $raw_text $encrypted;
90
91    # this directive is from the ngx_set_misc module
92    set_escape_uri $escaped_raw_text $raw_text;
93
94    fastcgi_param QUERY_STRING "uid=$uid";
95    fastcgi_pass unix:/path/to/my/php/or/python/fastcgi.sock;
96}
97```
98
99Lua web applications running directly on [ngx_lua](https://github.com/openresty/lua-nginx-module) can call
100this module's directives directly from within Lua code:
101
102```lua
103local raw_text = ndk.set_var.set_decrypt_session(encrypted_text)
104```
105
106[Back to TOC](#table-of-contents)
107
108Directives
109==========
110
111[Back to TOC](#table-of-contents)
112
113encrypted_session_key
114---------------------
115**syntax:** *encrypted_session_key <key>*
116
117**default:** *no*
118
119**context:** *http, server, server if, location, location if*
120
121Sets the key for the cipher (must be 32 bytes long). For example,
122
123```nginx
124encrypted_session_key "abcdefghijklmnopqrstuvwxyz123456";
125```
126
127[Back to TOC](#table-of-contents)
128
129encrypted_session_iv
130--------------------
131**syntax:** *encrypted_session_iv <iv>*
132
133**default:** *encrypted_session_iv "deadbeefdeadbeef";*
134
135**context:** *http, server, server if, location, location if*
136
137Sets the initial vector used for the cipher (must be *no longer* than 16 bytes).
138
139For example,
140
141```nginx
142encrypted_session_iv "12345678";
143```
144
145[Back to TOC](#table-of-contents)
146
147encrypted_session_expires
148-------------------------
149**syntax:** *encrypted_session_expires <time>*
150
151**default:** *encrypted_session_expires 1d;*
152
153**context:** *http, server, server if, location, location if*
154
155Sets expiration time difference (in seconds by default).
156
157For example, consider the following configuration:
158
159```nginx
160encypted_session_expires 1d;
161```
162
163When your session is being generated, ngx_encrypted_session will plant
164an expiration time (1 day in the future in this example) into the
165encrypted session string, such that when the session is being decrypted
166later, the server can pull the expiration time out of the session and
167compare it with the server's current system time. No matter how you
168transfer and store your session, like using cookies, or URI query arguments,
169or whatever.
170
171People may confuse this setting with the expiration date of HTTP
172cookies. This directive simply controls when the session gets expired;
173it knows nothing about HTTP cookies. Even if the end user intercepted
174this session from cookie by himself and uses it later manually, the
175server will still reject it when the expiration time gets passed.
176
177[Back to TOC](#table-of-contents)
178
179set_encrypt_session
180-------------------
181**syntax:** *set_encrypt_session $target <value>*
182
183**default:** *no*
184
185**context:** *http, server, server if, location, location if*
186
187Encrypts the string value specified by the `value` argument and saves the result into
188the variable specified by `$target`.
189
190For example,
191
192```nginx
193set_encrypt_session $res $value;
194```
195
196will encrypts the value in the variable $value into the target variable `$res`.
197
198The `value` argument can also be an nginx string value, for example,
199
200```nginx
201set_encrypt_session $res "my value = $value";
202```
203
204The resulting data can later be decrypted via the [set_decrypt_session](#set_decrypt_session) directive.
205
206[Back to TOC](#table-of-contents)
207
208set_decrypt_session
209-------------------
210**syntax:** *set_decrypt_session $target <value>*
211
212**default:** *no*
213
214**context:** *http, server, server if, location, location if*
215
216Similar to [set_encrypt_session](#set_encrypt_session), but performs the inverse operation, that is,
217to decrypt things.
218
219[Back to TOC](#table-of-contents)
220
221Installation
222============
223
224You're recommended to install this module (as well as the Nginx core and many other goodies) via the [ngx_openresty bundle](http://openresty.org). See [the detailed instructions](http://openresty.org/#Installation) for downloading and installing ngx_openresty into your system. This is the easiest and most safe way to set things up.
225
226Alternatively, you can install this module manually with the Nginx source:
227
228Grab the nginx source code from [nginx.org](http://nginx.org/), for example,
229the version 1.13.6 (see [nginx compatibility](#compatibility)), and then build the source with this module:
230
231```bash
232wget 'http://nginx.org/download/nginx-1.13.6.tar.gz'
233tar -xzvf nginx-1.13.6.tar.gz
234cd nginx-1.13.6/
235
236Here we assume you would install you nginx under /opt/nginx/.
237./configure --prefix=/opt/nginx \
238    --with-http_ssl_module \
239    --add-module=/path/to/encrypted-session-nginx-module
240
241make -j2
242make install
243```
244
245Download the latest version of the release tarball of this module from [encrypted-session-nginx-module file list](https://github.com/openresty/encrypted-session-nginx-module/tags).
246
247Also, this module is included and enabled by default in the [ngx_openresty bundle](http://openresty.org).
248
249OpenSSL should not be disabled in your Nginx build.
250
251[Back to TOC](#table-of-contents)
252
253Building as a dynamic module
254----------------------------
255
256Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the `--add-dynamic-module=PATH` option instead of `--add-module=PATH` on the
257`./configure` command line above. And then you can explicitly load the module in your `nginx.conf` via the [load_module](http://nginx.org/en/docs/ngx_core_module.html#load_module)
258directive, for example,
259
260```nginx
261load_module /path/to/modules/ndk_http_module.so;  # assuming NDK is built as a dynamic module too
262load_module /path/to/modules/ngx_http_encrypted_session_module.so;
263```
264
265[Back to TOC](#table-of-contents)
266
267Compatibility
268=============
269
270The following versions of Nginx should work with this module:
271
272* **1.13.x** (last tested: 1.13.6)
273* **1.12.x**
274* **1.11.x** (last tested: 1.11.2)
275* **1.10.x**
276* **1.9.x** (last tested: 1.9.15)
277* **1.8.x**
278* **1.7.x** (last tested: 1.7.10)
279* **1.6.x**
280* **1.5.x** (last tested: 1.5.12)
281* **1.4.x** (last tested: 1.4.4)
282* **1.2.x** (last tested: 1.2.9)
283* **1.1.x** (last tested: 1.1.5)
284* **1.0.x** (last tested: 1.0.11)
285* **0.9.x** (last tested: 0.9.4)
286* **0.8.x** (last tested: 0.8.54)
287* **0.7.x >= 0.7.46** (last tested: 0.7.68)
288
289Earlier versions of Nginx like 0.6.x and 0.5.x will *not* work.
290
291If you find that any particular version of Nginx above 0.7.44 does not
292work with this module, please consider reporting a bug.
293
294[Back to TOC](#table-of-contents)
295
296Report Bugs
297===========
298
299Although a lot of effort has been put into testing and code tuning,
300there must be some serious bugs lurking somewhere in this module. So
301whenever you are bitten by any quirks, please don't hesitate to
302
3031.  send a bug report or even patches to <agentzh@gmail.com>,
3042.  or create a ticket on the [issue tracking interface](http://github.com/openresty/encrypted-session-nginx-module/issues)
305provided by GitHub.
306
307[Back to TOC](#table-of-contents)
308
309Source Repository
310=================
311
312Available on github at [openresty/encrypted-session-nginx-module](http://github.com/openresty/encrypted-session-nginx-module).
313
314[Back to TOC](#table-of-contents)
315
316Getting involved
317================
318
319You'll be very welcomed to submit patches to the author or just ask for
320a commit bit to the source repository on GitHub.
321
322[Back to TOC](#table-of-contents)
323
324Author
325======
326
327Yichun "agentzh" Zhang (章亦春) &lt;agentzh@gmail.com&gt;
328
329[Back to TOC](#table-of-contents)
330
331Copyright & License
332===================
333
334Copyright (c) 2009-2018, Yichun Zhang (agentzh) &lt;agentzh@gmail.com&gt;, OpenResty Inc.
335
336This module is licensed under the terms of the BSD license.
337
338Redistribution and use in source and binary forms, with or without
339modification, are permitted provided that the following conditions are
340met:
341
342* Redistributions of source code must retain the above copyright
343notice, this list of conditions and the following disclaimer.
344* Redistributions in binary form must reproduce the above copyright
345notice, this list of conditions and the following disclaimer in the
346documentation and/or other materials provided with the distribution.
347
348THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
349IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
350TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
351PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
352HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
353SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
354TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
355PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
356LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
357NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
358SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
359
360[Back to TOC](#table-of-contents)
361
362See Also
363=========
364* [NDK](http://github.com/simpl-it/ngx_devel_kit)
365* [ngx_set_misc module](http://github.com/agentzh/set-misc-nginx-module)
366
367[Back to TOC](#table-of-contents)
368
369