Skip to content

Commit e612b04

Browse files
committed
Compatibility with ngx_http_secure_link_module
1 parent ac9bd5a commit e612b04

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
Nginx HMAC Secure Link Module
2-
--
2+
=============================
33

44
Description:
5-
--
5+
============
66

77
The Nginx HMAC secure link module enhances the security and functionality of the standard secure link module.
88
Secure token is created using secure HMAC construction with an arbitrary hash algorithm supported by OpenSSL, e.g., `md5`, `sha1`, `sha256`, `sha512`. Furthermore, secure token is created as described in RFC2104, that is, `H(secret_key XOR opad,H(secret_key XOR ipad, message))` instead of a simple `MD5(secret_key,message, expire)`.
99

1010
Installation:
11-
--
11+
=============
1212

1313
You'll need to re-compile Nginx from source to include this module.
1414
Modify your compile of Nginx by adding the following directive (modified to suit your path of course):
1515

1616
Static module (built-in nginx binary)
1717

18-
./configure --add-module=/absolute/path/to/nginx-hmac-secure-link
18+
./configure --add-module=/absolute/path/to/ngx_http_hmac_secure_link_module
1919

2020
Dynamic nginx module `ngx_http_hmac_secure_link_module.so` module
2121

22-
./configure --add-dynamic-module=/absolute/path/to/nginx-hmac-secure-link
22+
./configure --add-dynamic-module=/absolute/path/to/ngx_http_hmac_secure_link_module
2323

2424
Build Nginx
2525

2626
make
2727
make install
2828

2929
Usage:
30-
--
30+
======
3131

3232
Message to be hashed is defined by `secure_link_hmac_message`, `secret_key` is given by `secure_link_hmac_secret`, and hashing algorithm H is defined by `secure_link_hmac_algorithm`.
3333

@@ -40,7 +40,7 @@ Configuration example for server side.
4040
```nginx
4141
location ^~ /files/ {
4242
# Variable to be passed are secure token, timestamp, expiration period (optional)
43-
secure_link $arg_st,$arg_ts,$arg_e;
43+
secure_link_hmac $arg_st,$arg_ts,$arg_e;
4444
4545
# Secret key
4646
secure_link_hmac_secret my_secret_key;
@@ -51,13 +51,13 @@ location ^~ /files/ {
5151
# Cryptographic hash function to be used
5252
secure_link_hmac_algorithm sha256;
5353
54-
# If the hash is incorrect then $secure_link is a null string.
55-
# If the hash is correct but the link has already expired then $secure_link is zero.
56-
# If the hash is correct and the link has not expired then $secure_link is one.
54+
# If the hash is incorrect then $secure_link_hmac is a null string.
55+
# If the hash is correct but the link has already expired then $secure_link_hmac is zero.
56+
# If the hash is correct and the link has not expired then $secure_link_hmac is one.
5757
5858
# In production environment, we should not reveal to potential attacker
5959
# why hmac authentication has failed
60-
if ($secure_link != "1") {
60+
if ($secure_link_hmac != "1") {
6161
return 404;
6262
}
6363
@@ -108,7 +108,7 @@ $loc = "https://{$host}/files/top_secret.pdf?st={$hashmac}&ts={$timestamp}&e={$e
108108

109109
It is also possible to use this module with a Nginx acting as proxy server.
110110

111-
The string to be signed is defined in `secure_link_hmac_message`, the `secure_link_token` variable contains then a secure token to be passed to backend server.
111+
The string to be signed is defined in `secure_link_hmac_message`, the `secure_link_hmac_token` variable contains then a secure token to be passed to backend server.
112112

113113
```nginx
114114
location ^~ /backend_location/ {
@@ -118,14 +118,21 @@ location ^~ /backend_location/ {
118118
secure_link_hmac_secret "my_very_secret_key";
119119
secure_link_hmac_algorithm sha256;
120120
121-
proxy_pass "http://backend_server$uri?st=$secure_link_token&ts=$time_iso8601&e=$expire";
121+
proxy_pass "http://backend_server$uri?st=$secure_link_hmac_token&ts=$time_iso8601&e=$expire";
122122
}
123123
```
124124

125125

126+
Embedded Variables
127+
==================
128+
* `$secure_link_hmac` -
129+
* `$secure_link_hmac_token` -
130+
* `$secure_link_hmac_expires` - The lifetime of a link passed in a request.
131+
132+
126133
Contributing:
127-
--
134+
=============
128135

129-
Git source repositories: http://github.com/nginx-modules/nginx-hmac-secure-link/tree/master
136+
Git source repositories: http://github.com/nginx-modules/ngx_http_hmac_secure_link_module/tree/master
130137

131138
Please feel free to fork the project at GitHub and submit pull requests or patches.

ngx_http_hmac_secure_link_module.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf);
3535

3636
static ngx_command_t ngx_http_hmac_secure_link_commands[] = {
3737

38-
{ ngx_string("secure_link"),
38+
{ ngx_string("secure_link_hmac"),
3939
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
4040
ngx_http_set_complex_value_slot,
4141
NGX_HTTP_LOC_CONF_OFFSET,
@@ -99,13 +99,13 @@ ngx_module_t ngx_http_hmac_secure_link_module = {
9999

100100

101101
static ngx_http_variable_t ngx_http_secure_link_vars[] = {
102-
{ ngx_string("secure_link"), NULL,
102+
{ ngx_string("secure_link_hmac"), NULL,
103103
ngx_http_secure_link_variable, 0, NGX_HTTP_VAR_CHANGEABLE, 0 },
104104

105-
{ ngx_string("secure_link_expires"), NULL,
105+
{ ngx_string("secure_link_hmac_expires"), NULL,
106106
ngx_http_secure_link_expires_variable, 0, NGX_HTTP_VAR_CHANGEABLE, 0 },
107107

108-
{ ngx_string("secure_link_token"), NULL,
108+
{ ngx_string("secure_link_hmac_token"), NULL,
109109
ngx_http_secure_link_token_variable, 0, NGX_HTTP_VAR_CHANGEABLE, 0 },
110110

111111
{ ngx_null_string, NULL, NULL, 0, 0, 0}
@@ -124,7 +124,7 @@ ngx_http_secure_link_variable(ngx_http_request_t *r,
124124
u_char hash_buf[EVP_MAX_MD_SIZE], hmac_buf[EVP_MAX_MD_SIZE];
125125
u_int hmac_len;
126126
time_t timestamp, expires, gmtoff;
127-
int_t year, month, mday, hour, min, sec, gmtoff_hour, gmtoff_min;
127+
int year, month, mday, hour, min, sec, gmtoff_hour, gmtoff_min;
128128
char gmtoff_sign;
129129

130130
conf = ngx_http_get_module_loc_conf(r, ngx_http_hmac_secure_link_module);
@@ -157,8 +157,10 @@ ngx_http_secure_link_variable(ngx_http_request_t *r,
157157
sizeof("1970-09-28T12:00:00+06:00")-1, p);
158158

159159
/* Parse timestamp in ISO8601 format */
160-
if (sscanf((char *)p, "%d-%d-%dT%d:%d:%d%c%d:%d",
161-
&year, &month, &mday, &hour, &min, &sec,
160+
if (sscanf((char *)p, "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
161+
(ngx_tm_year_t *) &year, (ngx_tm_mon_t *) &month,
162+
(ngx_tm_mday_t *) &mday, (ngx_tm_hour_t *) &hour,
163+
(ngx_tm_min_t *) &min, (ngx_tm_sec_t *) &sec,
162164
&gmtoff_sign, &gmtoff_hour, &gmtoff_min) < 9) {
163165
goto not_found;
164166
}

0 commit comments

Comments
 (0)