Skip to content

Commit 2eaf11c

Browse files
fix issue with "Bearer" being removed from header (#106)
1 parent a23ed3c commit 2eaf11c

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/ngx_http_auth_jwt_module.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ static ngx_int_t load_public_key(ngx_conf_t *cf, auth_jwt_conf_t *conf)
612612
static char *get_jwt(ngx_http_request_t *r, ngx_str_t jwt_location)
613613
{
614614
static const char *HEADER_PREFIX = "HEADER=";
615-
static const char *BEARER_PREFIX = "Bearer ";
616615
static const char *COOKIE_PREFIX = "COOKIE=";
617616
char *jwtPtr = NULL;
618617

@@ -629,13 +628,21 @@ static char *get_jwt(ngx_http_request_t *r, ngx_str_t jwt_location)
629628

630629
if (jwtHeaderVal != NULL)
631630
{
631+
static const char *BEARER_PREFIX = "Bearer ";
632+
632633
if (ngx_strncmp(jwtHeaderVal->value.data, BEARER_PREFIX, sizeof(BEARER_PREFIX) - 1) == 0)
633634
{
634-
jwtHeaderVal->value.data += sizeof(BEARER_PREFIX) - 1;
635-
jwtHeaderVal->value.len -= sizeof(BEARER_PREFIX) - 1;
636-
}
635+
ngx_str_t jwtHeaderValWithoutBearer = jwtHeaderVal->value;
636+
637+
jwtHeaderValWithoutBearer.data += sizeof(BEARER_PREFIX) - 1;
638+
jwtHeaderValWithoutBearer.len -= sizeof(BEARER_PREFIX) - 1;
637639

638-
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtHeaderVal->value);
640+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtHeaderValWithoutBearer);
641+
}
642+
else
643+
{
644+
jwtPtr = ngx_str_t_to_char_ptr(r->pool, jwtHeaderVal->value);
645+
}
639646
}
640647
}
641648
else if (jwt_location.len > sizeof(COOKIE_PREFIX) && ngx_strncmp(jwt_location.data, COOKIE_PREFIX, sizeof(COOKIE_PREFIX) - 1) == 0)

test/etc/nginx/conf.d/test.conf

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ server {
9090
try_files index.html =404;
9191
}
9292

93+
location /secure/auth-header/default/proxy-header {
94+
auth_jwt_enabled on;
95+
auth_jwt_redirect off;
96+
auth_jwt_location HEADER=Authorization;
97+
98+
add_header "Test-Authorization" "$http_authorization";
99+
100+
alias /usr/share/nginx/html/;
101+
try_files index.html =404;
102+
}
103+
93104
location /secure/auth-header/rs256 {
94105
auth_jwt_enabled on;
95106
auth_jwt_redirect on;

test/test.sh

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ main() {
119119
-c '200' \
120120
-x "--header \"Authorization: Bearer ${JWT_HS256_VALID}\""
121121

122+
run_test -n 'when auth enabled with Authorization header with Bearer, should keep header intact' \
123+
-p '/secure/auth-header/default/proxy-header' \
124+
-c '200' \
125+
-r "< Test-Authorization: Bearer ${JWT_HS256_VALID}" \
126+
-x "--header \"Authorization: Bearer ${JWT_HS256_VALID}\""
127+
122128
run_test -n 'when auth enabled with default algorithm and no JWT cookie, returns 302' \
123129
-p '/secure/cookie/default' \
124130
-c '302'

0 commit comments

Comments
 (0)