Skip to content

Commit 046f8a4

Browse files
committed
Fix nginx#23
When running in S3, inconsistency in encoding unicode characters let to certain resources being unavailable. This change fixes the issue.
1 parent 8a172e0 commit 046f8a4

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

common/etc/nginx/include/s3gateway.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,12 @@ function _s3DirQueryParams(uriPath, method) {
265265
return '';
266266
}
267267

268-
var path = 'delimiter=%2F'
268+
let path = 'delimiter=%2F'
269269

270270
if (uriPath !== '/') {
271-
var without_leading_slash = uriPath.charAt(0) === '/' ?
272-
uriPath.substring(1, uriPath.length) : uriPath;
271+
let decodedUriPath = decodeURIComponent(uriPath);
272+
let without_leading_slash = decodedUriPath.charAt(0) === '/' ?
273+
decodedUriPath.substring(1, decodedUriPath.length) : decodedUriPath;
273274
path += '&prefix=' + encodeURIComponent(without_leading_slash);
274275
}
275276

@@ -329,7 +330,7 @@ function signatureV2(r, bucket, credentials) {
329330

330331
var s3signature = hmac.update(stringToSign).digest('base64');
331332

332-
return 'AWS ' + credentials.accessKeyId + ':' + s3signature;
333+
return `AWS ${credentials.accessKeyId}:${s3signature}`;
333334
}
334335

335336
/**
@@ -595,7 +596,9 @@ function _eightDigitDate(timestamp) {
595596
var month = timestamp.getUTCMonth() + 1;
596597
var day = timestamp.getUTCDate();
597598

598-
return ''.concat(_padWithLeadingZeros(year, 4), _padWithLeadingZeros(month, 2), _padWithLeadingZeros(day, 2));
599+
return ''.concat(_padWithLeadingZeros(year, 4),
600+
_padWithLeadingZeros(month,2),
601+
_padWithLeadingZeros(day,2));
599602
}
600603

601604
/**
@@ -643,9 +646,11 @@ function _padWithLeadingZeros(num, size) {
643646
* @private
644647
*/
645648
function _escapeURIPath(uri) {
646-
var components = [];
649+
// Check to see if the URI path was already encoded. If so, we decode it.
650+
let decodedUri = (uri.indexOf('%') >= 0) ? decodeURIComponent(uri) : uri;
651+
let components = [];
647652

648-
uri.split('/').forEach(function (item, i) {
653+
decodedUri.split('/').forEach(function (item, i) {
649654
components[i] = encodeURIComponent(item);
650655
});
651656

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Filename encoding issues are hard.

test/integration/test_api.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ assertHttpRequestEquals "HEAD" "b/c/@" "200"
135135
assertHttpRequestEquals "HEAD" "a/c/あ" "200"
136136
assertHttpRequestEquals "HEAD" "b/クズ箱/ゴミ.txt" "200"
137137
assertHttpRequestEquals "HEAD" "системы/system.txt" "200"
138+
assertHttpRequestEquals "HEAD" "%D1%81%D0%B8%D1%81%D1%82%D0%B5%D0%BC%D1%8B/%25bad%25file%25name%25" "200"
138139

139140
# Expected 400s
140141
assertHttpRequestEquals "HEAD" "request with unencoded spaces" "400"
@@ -170,6 +171,7 @@ assertHttpRequestEquals "GET" "a/c/あ" "data/bucket-1/a/c/あ"
170171
assertHttpRequestEquals "GET" "b/ブツブツ.txt" "data/bucket-1/b/ブツブツ.txt"
171172
assertHttpRequestEquals "GET" "b/クズ箱/ゴミ.txt" "data/bucket-1/b/クズ箱/ゴミ.txt"
172173
assertHttpRequestEquals "GET" "системы/system.txt" "data/bucket-1/системы/system.txt"
174+
assertHttpRequestEquals "GET" "%D1%81%D0%B8%D1%81%D1%82%D0%B5%D0%BC%D1%8B/%25bad%25file%25name%25" "data/bucket-1/системы/%bad%file%name%"
173175

174176
if [ "${allow_directory_list}" == "1" ]; then
175177
assertHttpRequestEquals "GET" "/" "200"

0 commit comments

Comments
 (0)