Skip to content

Commit 1a50a7a

Browse files
committed
Fix problem where signature string is incorrect when a path other than root is used
1 parent f3d0a3a commit 1a50a7a

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

nginx_conf_fragment.txt

+16
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,20 @@
1111
proxy_set_header Authorization $s3_auth_token;
1212
proxy_set_header x-amz-date $aws_date;
1313
}
14+
15+
# This is an example that does not use the server root for the proxy root
16+
location /myfiles {
17+
proxy_pass http://your_s3_bucket.s3.amazonaws.com/;
18+
19+
aws_access_key your_aws_access_key;
20+
aws_secret_key the_secret_associated_with_the_above_access_key;
21+
s3_bucket your_s3_bucket;
22+
chop_prefix /myfiles; # Take out this part of the URL before signing it, since '/myfiles' will not be part of the URI sent to Amazon
23+
24+
25+
proxy_set_header Authorization $s3_auth_token;
26+
proxy_set_header x-amz-date $aws_date;
27+
}
28+
29+
1430
}

ngx_http_aws_auth.c

+35-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct {
2020
ngx_str_t access_key;
2121
ngx_str_t secret;
2222
ngx_str_t s3_bucket;
23+
ngx_str_t chop_prefix;
2324
} ngx_http_aws_auth_conf_t;
2425

2526

@@ -44,6 +45,13 @@ static ngx_command_t ngx_http_aws_auth_commands[] = {
4445
NGX_HTTP_LOC_CONF_OFFSET,
4546
offsetof(ngx_http_aws_auth_conf_t, s3_bucket),
4647
NULL },
48+
49+
{ ngx_string("chop_prefix"),
50+
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
51+
ngx_conf_set_str_slot,
52+
NGX_HTTP_LOC_CONF_OFFSET,
53+
offsetof(ngx_http_aws_auth_conf_t, chop_prefix),
54+
NULL },
4755

4856
ngx_null_command
4957
};
@@ -101,6 +109,7 @@ ngx_http_aws_auth_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
101109

102110
ngx_conf_merge_str_value(conf->access_key, prev->access_key, "");
103111
ngx_conf_merge_str_value(conf->secret, prev->secret, "");
112+
ngx_conf_merge_str_value(conf->chop_prefix, prev->chop_prefix, "");
104113

105114
return NGX_CONF_OK;
106115
}
@@ -113,12 +122,34 @@ ngx_http_aws_auth_variable_s3(ngx_http_request_t *r, ngx_http_variable_value_t *
113122
int t;
114123
unsigned int md_len;
115124
unsigned char md[EVP_MAX_MD_SIZE];
116-
117125
aws_conf = ngx_http_get_module_loc_conf(r, ngx_http_aws_auth_module);
118-
126+
127+
128+
/*
129+
* This Block of code added to deal with paths that are not on the root -
130+
* that is, via proxy_pass that are being redirected and the base part of
131+
* the proxy url needs to be taken off the beginning of the URI in order
132+
* to sign it correctly.
133+
*/
134+
u_char *uri = ngx_palloc(r->pool, r->uri.len);
135+
ngx_sprintf(uri,"%V",&r->uri);
136+
if(ngx_strcmp(aws_conf->chop_prefix.data, "")) {
137+
if(!ngx_strncmp(r->uri.data, aws_conf->chop_prefix.data, aws_conf->chop_prefix.len)) {
138+
uri += aws_conf->chop_prefix.len;
139+
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
140+
"chop_prefix '%V' chopped from URI",&aws_conf->chop_prefix);
141+
} else {
142+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
143+
"chop_prefix '%V' NOT in URI",&aws_conf->chop_prefix);
144+
}
145+
}
146+
119147
u_char *str_to_sign = ngx_palloc(r->pool, r->uri.len + aws_conf->s3_bucket.len + 200);
120-
ngx_sprintf(str_to_sign, "GET\n\n\n\nx-amz-date:%V\n/%V%V",
121-
&ngx_cached_http_time, &aws_conf->s3_bucket, &r->uri);
148+
ngx_sprintf(str_to_sign, "GET\n\n\n\nx-amz-date:%V\n/%V%s",
149+
&ngx_cached_http_time, &aws_conf->s3_bucket,uri);
150+
151+
152+
122153

123154
if (evp_md==NULL)
124155
{

0 commit comments

Comments
 (0)