Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROD-30208] - add check on s3 url for @ after domain to prevent security risks #336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/initializers/joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ const Joi: JoiWithExtensions = _Joi.extend(
}
],
validate(value, helpers, {bucket}) {
if (!isOwnS3Path(bucket, value)) {
const { hostname, protocol, pathname} = new URL(value);
if (!isOwnS3Path(bucket, `${protocol}//${hostname}${pathname}`)) {
Comment on lines +193 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever! 💯
I think we could also simplify it by doing

Suggested change
const { hostname, protocol, pathname} = new URL(value);
if (!isOwnS3Path(bucket, `${protocol}//${hostname}${pathname}`)) {
const { origin } = new URL(value);
if (!isOwnS3Path(bucket, origin)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As talked offline we need the path name also to make the validation because s3 can have the bucket in the path after domain

return helpers.error('string.notInS3');
}
return value;
Expand Down
16 changes: 15 additions & 1 deletion test/initializers/joi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,22 @@ describe('joi extensions', function () {
should(underTest.error).be.undefined();
underTest.value.should.equal(url);
});
it('s3 url with @ after domain should throw error', function () {
const urls = [
'https://[email protected]/tmp/123-456-789',
'https://[email protected]/tmp/123-456-789',
'https://application-form.s3.amazonaws.com@joecup/tmp/123-456-789',
'https://application-form.s3.amazonaws.com@hello',
'https://application-form.s3.amazonaws.com@4nsex02yymx4wzjzc0ljcmsar1xsli97.oastify.com'
];
urls.forEach(url => {
const underTest = Joi.urlInOwnS3()
.bucket('application-form')
.validate(url);
underTest.error.message.should.equal('Invalid path provided');
});
});
});

describe('url', function () {
it('rejects URLs with less than two domain fragments', function () {
Joi.url().validate('https://foo').error.message.should.equal('"value" must contain a valid domain name');
Expand Down
Loading