From f211edc879c6bb146276dfca99cf8c248c6a1f80 Mon Sep 17 00:00:00 2001 From: Christos Labrou Date: Wed, 6 Mar 2024 17:14:23 +0200 Subject: [PATCH] PROD-37017 Allow orka to accept s3 presigned URLs with region (#368) --- src/initializers/joi.ts | 6 +++++- test/initializers/joi.test.ts | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/initializers/joi.ts b/src/initializers/joi.ts index 4294b9f7..70f1b63e 100644 --- a/src/initializers/joi.ts +++ b/src/initializers/joi.ts @@ -18,7 +18,11 @@ export const isOwnS3Path = (bucket: string, val: string): boolean => { host.startsWith('s3.') && host.endsWith('.amazonaws.com') && pathname.startsWith(`/${bucket}/`); - return matchingProtocol && (s3Host || s3HostBucketInPath); + const s3HostContainsRegion = + host.startsWith(`${bucket}.s3`) && + host.endsWith('.amazonaws.com') && + host.split('.').length === 5; + return matchingProtocol && (s3Host || s3HostBucketInPath || s3HostContainsRegion); } catch (e) { logger.error(`Failed to parse url: ${val}`, e); return false; diff --git a/test/initializers/joi.test.ts b/test/initializers/joi.test.ts index fe716342..a78cf5ec 100644 --- a/test/initializers/joi.test.ts +++ b/test/initializers/joi.test.ts @@ -1,6 +1,6 @@ import should = require('should'); import Joi from '../../src/initializers/joi'; -import { isExpiredUrl } from '../../src/initializers/joi'; +import { isExpiredUrl, isOwnS3Path } from '../../src/initializers/joi'; import * as sinon from 'sinon'; describe('joi extensions', function () { @@ -259,4 +259,26 @@ describe('joi extensions', function () { isExpired.map((x) => x.should.be.false()); }); }); + + describe('isOwnS3Path', function () { + it('should return true for valid s3 path', function () { + const isOwnPath = [ + isOwnS3Path('some-bucket', 'https://s3.amazonaws.com/some-bucket/some-file-name'), + isOwnS3Path('some-bucket', 'https://some-bucket.s3.some-region-1.amazonaws.com/'), + isOwnS3Path('some-bucket', 'https://some-bucket.s3.amazonaws.com/'), + ]; + isOwnPath.map((x) => x.should.be.true()); + }); + + it('should return false for invalid s3 path', function () { + const isOwnPath = [ + isOwnS3Path('some-bucket', 'https://s3.amazonaws.com/some-other-bucket/some-file-name'), + isOwnS3Path('some-bucket', 'https://some-other-bucket.s3.some-region-1.amazonaws.com/'), + isOwnS3Path('some-bucket', 'https://some-other-bucket.s3.amazonaws.com/'), + isOwnS3Path('some-bucket', 'https://some-other-bucket.s3.some.deep.nested.subdomain.amazonaws.com/'), + isOwnS3Path('some-bucket', 'https://some-bucket.s3.some.deep.nested.subdomain.amazonaws.com/') + ]; + isOwnPath.map((x) => x.should.be.false()); + }); + }); });