Skip to content

Commit 7252abd

Browse files
committed
Use palloc instead of alloc. null terminate logged strings
1 parent 59bd2c8 commit 7252abd

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

Dockerfile

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ FROM centos:7
22

33
LABEL maintainer="TeslaGov" email="[email protected]"
44

5+
ARG NGINX_VERSION=1.12.0
6+
ARG JANSSON_VERSION=2.10
7+
ARG LIBJWT_VERSION=1.8.0
8+
59
COPY resources/nginx.repo /etc/yum.repos.d/nginx.repo
610

711
ENV LD_LIBRARY_PATH=/usr/local/lib
812

913
RUN yum -y update && \
1014
yum -y groupinstall 'Development Tools' && \
1115
yum -y install pcre-devel pcre zlib-devel openssl-devel wget cmake check-devel check && \
12-
yum -y install nginx-1.12.0
16+
yum -y install nginx-$NGINX_VERSION
17+
18+
# for compiling for rh-nginx110
19+
# yum -y install libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed
1320

1421
RUN mkdir -p /root/dl
1522
WORKDIR /root/dl
@@ -22,32 +29,41 @@ RUN wget https://github.com/TeslaGov/ngx-http-auth-jwt-module/archive/master.zip
2229
ln -sf ngx-http-auth-jwt-module-master ngx-http-auth-jwt-module
2330

2431
# build jansson
25-
RUN wget https://github.com/akheron/jansson/archive/v2.10.zip && \
26-
unzip v2.10.zip && \
27-
rm v2.10.zip && \
28-
ln -sf jansson-2.10 jansson && \
32+
RUN wget https://github.com/akheron/jansson/archive/v$JANSSON_VERSION.zip && \
33+
unzip v$JANSSON_VERSION.zip && \
34+
rm v$JANSSON_VERSION.zip && \
35+
ln -sf jansson-$JANSSON_VERSION jansson && \
2936
cd /root/dl/jansson && \
3037
cmake . -DJANSSON_BUILD_SHARED_LIBS=1 -DJANSSON_BUILD_DOCS=OFF && \
3138
make && \
3239
make check && \
3340
make install
3441

3542
# build libjwt
36-
RUN wget https://github.com/benmcollins/libjwt/archive/v1.8.0.zip && \
37-
unzip v1.8.0.zip && \
38-
rm v1.8.0.zip && \
39-
ln -sf libjwt-1.8.0 libjwt && \
43+
RUN wget https://github.com/benmcollins/libjwt/archive/v$LIBJWT_VERSION.zip && \
44+
unzip v$LIBJWT_VERSION.zip && \
45+
rm v$LIBJWT_VERSION.zip && \
46+
ln -sf libjwt-$LIBJWT_VERSION libjwt && \
4047
cd /root/dl/libjwt && \
4148
autoreconf -i && \
4249
./configure JANSSON_CFLAGS=/usr/local/include JANSSON_LIBS=/usr/local/lib && \
4350
make all && \
4451
make install
4552

53+
# after 1.11.5 use this command
54+
# ./configure --with-compat --add-dynamic-module=../ngx-http-auth-jwt-module --with-cc-opt='-std=gnu99'
4655
# build nginx module against nginx sources
47-
RUN wget http://nginx.org/download/nginx-1.12.0.tar.gz && \
48-
tar -xzf nginx-1.12.0.tar.gz && \
49-
rm nginx-1.12.0.tar.gz && \
50-
ln -sf nginx-1.12.0 nginx && \
56+
#
57+
# 1.10.2 from nginx by default use config flags... I had to add the -std=c99 and could not achieve "binary compatibility"
58+
# ./configure --add-dynamic-module=../ngx-http-auth-jwt-module --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-file-aio --with-threads --with-ipv6 --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -std=c99'
59+
#
60+
# rh-nginx110 uses these config flags
61+
# ./configure --add-dynamic-module=../ngx-http-auth-jwt-module --prefix=/opt/rh/rh-nginx110/root/usr/share/nginx --sbin-path=/opt/rh/rh-nginx110/root/usr/sbin/nginx --modules-path=/opt/rh/rh-nginx110/root/usr/lib64/nginx/modules --conf-path=/etc/opt/rh/rh-nginx110/nginx/nginx.conf --error-log-path=/var/opt/rh/rh-nginx110/log/nginx/error.log --http-log-path=/var/opt/rh/rh-nginx110/log/nginx/access.log --http-client-body-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/scgi --pid-path=/var/opt/rh/rh-nginx110/run/nginx/nginx.pid --lock-path=/var/opt/rh/rh-nginx110/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=c99' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
62+
63+
RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
64+
tar -xzf nginx-$NGINX_VERSION.tar.gz && \
65+
rm nginx-$NGINX_VERSION.tar.gz && \
66+
ln -sf nginx-$NGINX_VERSION nginx && \
5167
cd /root/dl/nginx && \
5268
./configure --with-compat --add-dynamic-module=../ngx-http-auth-jwt-module --with-cc-opt='-std=gnu99' && \
5369
make modules && \
@@ -58,7 +74,6 @@ COPY resources/nginx.conf /etc/nginx/nginx.conf
5874
COPY resources/test-jwt-nginx.conf /etc/nginx/conf.d/test-jwt-nginx.conf
5975
RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure
6076

61-
WORKDIR /etc/nginx
62-
CMD ["nginx"]
77+
ENTRYPOINT ["/usr/sbin/nginx"]
6378

6479
EXPOSE 8000

src/ngx_http_auth_jwt_module.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,12 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
123123
}
124124

125125
// the cookie data is not necessarily null terminated... we need a null terminated character pointer
126-
jwtCookieValChrPtr = ngx_alloc(jwtCookieVal.len + 1, r->connection->log);
127-
ngx_memcpy(jwtCookieValChrPtr, jwtCookieVal.data, jwtCookieVal.len);
128-
*(jwtCookieValChrPtr+jwtCookieVal.len) = '\0';
126+
jwtCookieValChrPtr = ngx_str_t_to_char_ptr(r->pool, jwtCookieVal);
129127

130128
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "rampartjwt: %s %d", jwtCookieValChrPtr, jwtCookieVal.len);
131129

132130
// convert key from hex to binary
133-
keyBinary = ngx_alloc(jwtcf->auth_jwt_key.len / 2, r->connection->log);
131+
keyBinary = ngx_palloc(r->pool, jwtcf->auth_jwt_key.len / 2);
134132
if (0 != hex_to_binary((char *)jwtcf->auth_jwt_key.data, keyBinary, jwtcf->auth_jwt_key.len))
135133
{
136134
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to turn hex key into binary");
@@ -207,10 +205,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
207205
uri.len = request_uri_var->len;
208206
ngx_memcpy(uri.data, request_uri_var->data, request_uri_var->len);
209207

210-
211-
char * tmp = ngx_alloc(uri.len + 1, r->connection->log);
212-
ngx_memcpy(tmp, uri.data, uri.len);
213-
*(tmp+uri.len) = '\0';
208+
char * tmp = ngx_str_t_to_char_ptr(r->pool, uri);
214209

215210
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "found uri with querystring %s", tmp);
216211
}
@@ -229,7 +224,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
229224
ngx_escape_uri(uri_escaped.data, uri.data, uri.len, NGX_ESCAPE_URI);
230225

231226
r->headers_out.___location->value.len = loginlen + sizeof("?return_url=") - 1 + strlen(scheme) + sizeof("://") - 1 + server.len + uri_escaped.len;
232-
return_url = ngx_alloc(r->headers_out.___location->value.len, r->connection->log);
227+
return_url = ngx_palloc(r->pool, r->headers_out.___location->value.len);
233228
ngx_memcpy(return_url, jwtcf->auth_jwt_loginurl.data, jwtcf->auth_jwt_loginurl.len);
234229
int return_url_idx = jwtcf->auth_jwt_loginurl.len;
235230
ngx_memcpy(return_url+return_url_idx, "?return_url=", sizeof("?return_url=") - 1);
@@ -244,7 +239,9 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
244239
return_url_idx += uri_escaped.len;
245240
r->headers_out.___location->value.data = (u_char *)return_url;
246241

247-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "return_url: %s", return_url);
242+
char* return_url_printable = ngx_str_t_to_char_ptr(r->pool, return_url);
243+
244+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "return_url: %s", return_url_printable);
248245
}
249246
else
250247
{
@@ -335,7 +332,8 @@ hex_char_to_binary( char ch, char* ret )
335332
}
336333

337334
static int
338-
hex_to_binary( const char* str, u_char* buf, int len ) {
335+
hex_to_binary( const char* str, u_char* buf, int len )
336+
{
339337
u_char
340338
*cpy = buf;
341339
char
@@ -357,3 +355,13 @@ hex_to_binary( const char* str, u_char* buf, int len ) {
357355
return 0;
358356
}
359357

358+
/** copies an nginx string structure to a newly allocated character pointer */
359+
static char* ngx_str_t_to_char_ptr(ngx_pool_t *pool, ngx_str_t str)
360+
{
361+
char* char_ptr = ngx_palloc(pool, str.len + 1);
362+
ngx_memcpy(char_ptr, str.data, str.len);
363+
*(char_ptr + str.len) = '\0';
364+
return char_ptr;
365+
}
366+
367+

0 commit comments

Comments
 (0)