Skip to content

Commit f14ce2e

Browse files
authored
Merge pull request #8873 from shirady/presignurl-expires-negative
Presigned URL | Add an Error on Expiry with a Negative Number
2 parents bd1aab4 + 77cf081 commit f14ce2e

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

docs/NooBaaNonContainerized/S3Ops.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ Currently we support a couple of options:
9999
2. Principal by account name: `"Principal": { "AWS": [ "<account-name-1>", "<account-name-2>", ... ,"<account-name-n>"] }`
100100
3. Principal by account ID: `"Principal": { "AWS": [ "<account-ID-1>", "<account-ID-2>", ... ,"<account-ID-n>"] }`
101101

102+
### Presigned URL Support
103+
An account can use presigned URLs to grant time-limited access to objects in its bucket without updating the bucket policy. The credentials used by the presigned URL are those of the user who generated the URL (more information in [AWS docs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html)).
104+
105+
#### Simple Test Using AWS CLI and curl:
106+
1. Pre-requisites: Create account and a bucket, noobaa service should be running, and upload an object to the bucket.
107+
2. Create a presigned URL for a minute (the expiration is in seconds): `aws s3 presign s3://<bucket-name>/<key-name> --expires-in 60 --endpoint-url <endpoint-address>` (if needed you can add `--region us-east-1`).
108+
3. Then you can use: `curl --insecure <URL>` to test the output (`-v` if you want verbose output).
109+
102110
### Anonymous Requests Support
103111

104112
Anonymous requests are S3 requests made without an access key or a secret key -

src/endpoint/s3/s3_errors.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,16 @@ S3Error.InvalidEncodingType = Object.freeze({
546546
message: 'Invalid Encoding Method specified in Request',
547547
http_code: 400,
548548
});
549-
S3Error.AuthorizationQueryParametersError = Object.freeze({
549+
S3Error.AuthorizationQueryParametersErrorWeek = Object.freeze({
550550
code: 'AuthorizationQueryParametersError',
551551
message: 'X-Amz-Expires must be less than a week (in seconds); that is, the given X-Amz-Expires must be less than 604800 seconds',
552552
http_code: 400,
553553
});
554+
S3Error.AuthorizationQueryParametersErrorNonNegative = Object.freeze({
555+
code: 'AuthorizationQueryParametersError',
556+
message: 'X-Amz-Expires must be non-negative',
557+
http_code: 400,
558+
});
554559
S3Error.RequestExpired = Object.freeze({
555560
code: 'AccessDenied',
556561
message: 'Request has expired',

src/test/unit_tests/test_nsfs_integration.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,15 +2239,23 @@ mocha.describe('Presigned URL tests', function() {
22392239
it('fetch invalid presigned URL - expiry expoch - expire in bigger than limit', async () => {
22402240
const invalid_expiry = 604800 + 10;
22412241
const invalid_expiry_presigned_url = cloud_utils.get_signed_url(presigned_url_params, invalid_expiry);
2242-
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersError);
2242+
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersErrorWeek);
22432243
await assert_throws_async(fetchData(invalid_expiry_presigned_url), expected_err.message);
22442244
});
22452245

22462246
it('fetch invalid presigned URL - expire in bigger than limit', async () => {
22472247
const now = new Date();
22482248
const invalid_expiry = 604800 + 10;
22492249
const invalid_expiry_presigned_url = cloud_utils.get_signed_url(presigned_url_params, invalid_expiry) + '&X-Amz-Date=' + now.toISOString() + '&X-Amz-Expires=' + invalid_expiry;
2250-
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersError);
2250+
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersErrorWeek);
2251+
await assert_throws_async(fetchData(invalid_expiry_presigned_url), expected_err.message);
2252+
});
2253+
2254+
it('fetch invalid presigned URL - throw an error on exipray with a negative number', async () => {
2255+
const now = new Date();
2256+
const invalid_expiry = -7;
2257+
const invalid_expiry_presigned_url = cloud_utils.get_signed_url(presigned_url_params, invalid_expiry) + '&X-Amz-Date=' + now.toISOString() + '&X-Amz-Expires=' + invalid_expiry;
2258+
const expected_err = new S3Error(S3Error.AuthorizationQueryParametersErrorNonNegative);
22512259
await assert_throws_async(fetchData(invalid_expiry_presigned_url), expected_err.message);
22522260
});
22532261

src/util/signature_utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ function make_auth_token_from_request(req) {
286286
*/
287287
function check_request_expiry(req) {
288288
if (req.query['X-Amz-Date'] && req.query['X-Amz-Expires']) {
289+
_check_expiry_non_negative(req.query['X-Amz-Expires']);
289290
_check_expiry_limit(req.query['X-Amz-Expires']);
290291
_check_expiry_query_v4(req.query['X-Amz-Date'], req.query['X-Amz-Expires']);
291292
} else if (req.query.Expires) {
@@ -298,7 +299,20 @@ function check_request_expiry(req) {
298299
// expiry_seconds limit is 7 days = 604800 seconds
299300
function _check_expiry_limit(expiry_seconds) {
300301
if (Number(expiry_seconds) > 604800) {
301-
throw new S3Error(S3Error.AuthorizationQueryParametersError);
302+
throw new S3Error(S3Error.AuthorizationQueryParametersErrorWeek);
303+
}
304+
}
305+
306+
/**
307+
* _check_expiry_non_negative converts the expiry_seconds that we got
308+
* from eq.query['X-Amz-Expires'] to number and checks that it is non negative number
309+
* (throws an error otherwise)
310+
* Note: we don't run it in the condition of epoch time (req.query.expires)
311+
* @param {string} expiry_seconds
312+
*/
313+
function _check_expiry_non_negative(expiry_seconds) {
314+
if (Number(expiry_seconds) < 0) {
315+
throw new S3Error(S3Error.AuthorizationQueryParametersErrorNonNegative);
302316
}
303317
}
304318

0 commit comments

Comments
 (0)