Skip to content

Commit

Permalink
Merge pull request #6 from change/chris/fix-domain-vulnerability
Browse files Browse the repository at this point in the history
Fix domain vulnerability
  • Loading branch information
quaelin authored Jun 4, 2019
2 parents 54f4d5f + 8f294cb commit f1d68a2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions api/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ class HttpError extends Error {
}

async function validate(longUrl) {
let host;
let parsed;
debugLog(`validating longUrl ${longUrl}`);
try {
({ host } = url.parse(longUrl));
parsed = url.parse(longUrl);
} catch (ex) {
throw new HttpError(400, 'Not a valid URL');
}
const { host, path } = parsed;
if (path.charAt(0) !== '/') {
// Disallow anything with a doctored path
throw new HttpError(400, 'Not a valid URL for shortening');
}
const suffixMatcher = suffix => suffix === host || endsWith(host, `.${suffix}`);
if (!find(domainSafeList, suffixMatcher)) {
throw new HttpError(400, `Not an allowed domain for shortening: ${host}`);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@change-org/longlinks",
"version": "0.1.2",
"version": "0.1.3",
"description": "A serverless URL shortener that leverages Amazon Lambda for short link creation, and S3 for link storage and redirection.",
"author": "Change Engineering",
"license": "MIT",
Expand Down
5 changes: 5 additions & 0 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ describe('When the `url` property', () => {
() => expectStatusForUrl('https://evildomain1.com', 400)
);

test(
'even more sneakily prepends the allowed domain to a malicious domain with an encoded character',
() => expectStatusForUrl('https://www.domain1.com%2Eevil.com?foo=bar', 400)
);

test(
'contains an allowed domain string, but in the path. Nice try but still 400',
() => expectStatusForUrl('https://www.evil.com/https://www.domain1.com', 400)
Expand Down

0 comments on commit f1d68a2

Please sign in to comment.