-
-
Notifications
You must be signed in to change notification settings - Fork 288
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
Fix RCE in gateway.downloadBackup #2171
base: master
Are you sure you want to change the base?
Changes from all commits
95c7a36
25b207b
72e8f17
d6e3d60
c58a539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { expect } = require('chai'); | ||
const { isURL, validateUrl } = require('../../utils/url'); | ||
|
||
describe('url.validation', () => { | ||
it('should return true for a valid url', () => { | ||
const url = 'https://example.com'; | ||
expect(isURL(url)).to.equal(true); | ||
}); | ||
|
||
it('should return false for an invalid url', () => { | ||
const url = '/a/b'; | ||
expect(isURL(url)).to.equal(false); | ||
}); | ||
|
||
it('should return valid url', () => { | ||
const url = 'https://example.com/test'; | ||
expect(validateUrl(url)).to.be.equal('https://example.com/test'); | ||
}); | ||
|
||
it('should throw an error for malicious url', () => { | ||
const url = 'https://example.com/test?#a'; | ||
expect(() => { | ||
validateUrl(url); | ||
}).to.throw(Error); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const Joi = require('joi'); | ||
const { InvalidURL } = require('./coreErrors'); | ||
|
||
/** | ||
* @description Typeof url. | ||
* @param {string} str - The url of the backup. | ||
* @returns {boolean} Return true for valid url. | ||
* @example | ||
* isURL(); | ||
*/ | ||
const isURL = (str) => { | ||
try { | ||
const url = new URL(str); | ||
return url.protocol === 'http:' || url.protocol === 'https:'; | ||
} catch (_) { | ||
return false; | ||
} | ||
}; | ||
|
||
/** | ||
* @description Validate the url. | ||
* @param {string} url - The url of the backup. | ||
* @returns {string} Return a valid url. | ||
* @example | ||
* validateUrl(); | ||
*/ | ||
function validateUrl(url) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You created a generic function "validateUrl" in Will this be used elsewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What change should I make so that the GET parameter is removed before it’s passed to path.basename()? The validator I wrote, in my opinion, can be used in other places, but it’s largely tied to gateway.downloadBackup. |
||
const schema = Joi.string() | ||
.uri() | ||
.pattern(/^[^?#]*$/, ''); | ||
const { error, value } = schema.validate(url); | ||
if (error) { | ||
throw new InvalidURL('INVALID_URL'); | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
module.exports = { | ||
isURL, | ||
validateUrl, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it still works like that, for context, the fileUrl looks like that (it's a dummy URL) :
So the get parameters should be removed before doing path.basename()
Having the variable being name
value
isn't very clear