Skip to content

Commit 5441a7e

Browse files
committed
various coding style fixes in Jeremy Wohl's patch for cryptographically strong random string functions in github pull request #10.
1 parent c11f17f commit 5441a7e

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ src/module.h
6161
work/
6262
src/random.[ch]
6363
build1[01]
64+
analyze
65+
src/rotate.[ch]

src/ngx_http_set_misc_module.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ static ndk_set_var_t ngx_http_set_misc_set_decode_hex_filter = {
5252
NULL
5353
};
5454

55+
5556
static ndk_set_var_t ngx_http_set_misc_set_encode_hex_filter = {
5657
NDK_SET_VAR_VALUE,
5758
ngx_http_set_misc_set_encode_hex,
5859
1,
5960
NULL
6061
};
6162

63+
6264
#if NGX_OPENSSL
6365
static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = {
6466
NDK_SET_VAR_MULTI_VALUE,
@@ -68,6 +70,7 @@ static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = {
6870
};
6971
#endif
7072

73+
7174
#ifndef NGX_HTTP_SET_HASH
7275
static ndk_set_var_t ngx_http_set_misc_set_md5_filter = {
7376
NDK_SET_VAR_VALUE,
@@ -95,41 +98,47 @@ static ndk_set_var_t ngx_http_set_misc_unescape_uri_filter = {
9598
NULL
9699
};
97100

101+
98102
static ndk_set_var_t ngx_http_set_misc_escape_uri_filter = {
99103
NDK_SET_VAR_VALUE,
100104
ngx_http_set_misc_escape_uri,
101105
1,
102106
NULL
103107
};
104108

109+
105110
static ndk_set_var_t ngx_http_set_misc_decode_base32_filter = {
106111
NDK_SET_VAR_VALUE,
107112
ngx_http_set_misc_decode_base32,
108113
1,
109114
NULL
110115
};
111116

117+
112118
static ndk_set_var_t ngx_http_set_misc_quote_sql_str_filter = {
113119
NDK_SET_VAR_VALUE,
114120
ngx_http_set_misc_quote_sql_str,
115121
1,
116122
NULL
117123
};
118124

125+
119126
static ndk_set_var_t ngx_http_set_misc_quote_pgsql_str_filter = {
120127
NDK_SET_VAR_VALUE,
121128
ngx_http_set_misc_quote_pgsql_str,
122129
1,
123130
NULL
124131
};
125132

133+
126134
static ndk_set_var_t ngx_http_set_misc_quote_json_str_filter = {
127135
NDK_SET_VAR_VALUE,
128136
ngx_http_set_misc_quote_json_str,
129137
1,
130138
NULL
131139
};
132140

141+
133142
static ndk_set_var_t ngx_http_set_misc_encode_base32_filter = {
134143
NDK_SET_VAR_VALUE,
135144
ngx_http_set_misc_encode_base32,
@@ -145,27 +154,31 @@ static ndk_set_var_t ngx_http_set_misc_local_today_filter = {
145154
NULL
146155
};
147156

157+
148158
static ndk_set_var_t ngx_http_set_misc_set_random_filter = {
149159
NDK_SET_VAR_MULTI_VALUE,
150160
ngx_http_set_misc_set_random,
151161
2,
152162
NULL
153163
};
154164

165+
155166
static ndk_set_var_t ngx_http_set_misc_set_secure_random_alphanum_filter = {
156167
NDK_SET_VAR_VALUE,
157168
ngx_http_set_misc_set_secure_random_alphanum,
158169
1,
159170
NULL
160171
};
161172

173+
162174
static ndk_set_var_t ngx_http_set_misc_set_secure_random_lcalpha_filter = {
163175
NDK_SET_VAR_VALUE,
164176
ngx_http_set_misc_set_secure_random_lcalpha,
165177
1,
166178
NULL
167179
};
168180

181+
169182
static ngx_command_t ngx_http_set_misc_commands[] = {
170183
{ ngx_string ("set_encode_base64"),
171184
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF

src/ngx_http_set_secure_random.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
#include "ngx_http_set_secure_random.h"
88
#include <stdlib.h>
99

10-
const int MAX_RANDOM_STRING = 64;
1110

12-
const int ALPHANUM = 1;
13-
const int LCALPHA = 2;
11+
enum {
12+
MAX_RANDOM_STRING = 64,
13+
ALPHANUM = 1,
14+
LCALPHA = 2
15+
};
16+
1417

1518
ngx_int_t
16-
ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t *r,
17-
ngx_str_t *res, ngx_http_variable_value_t *v);
19+
ngx_http_set_misc_set_secure_random_common(int alphabet_type,
20+
ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v);
21+
1822

1923
ngx_int_t
2024
ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r,
@@ -23,50 +27,57 @@ ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r,
2327
return ngx_http_set_misc_set_secure_random_common(ALPHANUM, r, res, v);
2428
}
2529

30+
2631
ngx_int_t
2732
ngx_http_set_misc_set_secure_random_lcalpha(ngx_http_request_t *r,
2833
ngx_str_t *res, ngx_http_variable_value_t *v)
2934
{
3035
return ngx_http_set_misc_set_secure_random_common(LCALPHA, r, res, v);
3136
}
3237

38+
3339
ngx_int_t
34-
ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t *r,
35-
ngx_str_t *res, ngx_http_variable_value_t *v)
40+
ngx_http_set_misc_set_secure_random_common(int alphabet_type,
41+
ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v)
3642
{
37-
static u_char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
43+
static u_char alphabet[] = "abcdefghijklmnopqrstuvwxyz"
44+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
45+
3846
u_char entropy[MAX_RANDOM_STRING];
3947
u_char output[MAX_RANDOM_STRING];
40-
ngx_int_t length, fd, i;
48+
ngx_int_t length, i;
49+
ngx_fd_t fd;
4150
ssize_t n;
42-
4351

4452
length = ngx_atoi(v->data, v->len);
53+
4554
if (length == NGX_ERROR || length < 1 || length > MAX_RANDOM_STRING) {
4655
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
4756
"set_random: bad \"length\" argument: %v", v);
4857
return NGX_ERROR;
4958
}
5059

5160
fd = ngx_open_file("/dev/urandom", NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
52-
if (fd == -1) {
61+
if (fd == NGX_INVALID_FILE) {
5362
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
54-
"set_secure_random: could not open /dev/urandom");
63+
"set_secure_random: could not open /dev/urandom");
5564
return NGX_ERROR;
5665
}
57-
66+
5867
n = ngx_read_fd(fd, entropy, length);
5968
if (n != length) {
6069
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
61-
"set_secure_random: could not read all %d byte(s) from /dev/urandom", length);
70+
"set_secure_random: could not read all %i byte(s) from "
71+
"/dev/urandom", length);
6272
return NGX_ERROR;
6373
}
64-
74+
6575
ngx_close_file(fd);
66-
76+
6777
for (i = 0; i < length; i++) {
6878
if (alphabet_type == LCALPHA) {
6979
output[i] = entropy[i] % 26 + 'a';
80+
7081
} else {
7182
output[i] = alphabet[ entropy[i] % (sizeof alphabet - 1) ];
7283
}
@@ -78,13 +89,14 @@ ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t
7889
}
7990

8091
ngx_memcpy(res->data, output, length);
81-
92+
8293
res->len = length;
8394

84-
/* Set all required params */
95+
/* set all required params */
8596
v->valid = 1;
8697
v->no_cacheable = 0;
8798
v->not_found = 0;
8899

89100
return NGX_OK;
90101
}
102+

t/secure-random.t

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# vi:filetype=perl
1+
# vi:filetype=
22

33
use Test::Nginx::Socket;
44

5-
repeat_each(100);
5+
repeat_each(2);
66

77
plan tests => repeat_each() * 2 * blocks();
88

@@ -25,6 +25,8 @@ __DATA__
2525
GET /alphanum
2626
--- response_body_like: ^[a-zA-Z0-9]{32}$
2727

28+
29+
2830
=== TEST 2: a 16-character alphanum
2931
--- config
3032
location /alphanum {
@@ -36,6 +38,8 @@ __DATA__
3638
GET /alphanum
3739
--- response_body_like: ^[a-zA-Z0-9]{16}$
3840

41+
42+
3943
=== TEST 3: a 1-character alphanum
4044
--- config
4145
location /alphanum {
@@ -47,6 +51,8 @@ __DATA__
4751
GET /alphanum
4852
--- response_body_like: ^[a-zA-Z0-9]{1}$
4953

54+
55+
5056
=== TEST 4: length less than <= 0 should fail
5157
--- config
5258
location /alphanum {
@@ -59,6 +65,8 @@ __DATA__
5965
--- response_body_like: 500 Internal Server Error
6066
--- error_code: 500
6167

68+
69+
6270
=== TEST 5: length less than <= 0 should fail
6371
--- config
6472
location /alphanum {
@@ -71,6 +79,8 @@ __DATA__
7179
--- response_body_like: 500 Internal Server Error
7280
--- error_code: 500
7381

82+
83+
7484
=== TEST 6: non-numeric length should fail
7585
--- config
7686
location /alphanum {
@@ -83,6 +93,8 @@ __DATA__
8393
--- response_body_like: 500 Internal Server Error
8494
--- error_code: 500
8595

96+
97+
8698
=== TEST 7: a 16-character lcalpha
8799
--- config
88100
location /lcalpha {
@@ -93,3 +105,4 @@ __DATA__
93105
--- request
94106
GET /lcalpha
95107
--- response_body_like: ^[a-z]{16}$
108+

0 commit comments

Comments
 (0)