forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server/shortUrl] filter out invalid target urls
- Loading branch information
spalger
committed
Nov 4, 2016
1 parent
bbd6453
commit 21d2d7f
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Boom from 'boom'; | ||
|
||
import { shortUrlAssertValid } from '../short_url_assert_valid'; | ||
|
||
|
||
describe('shortUrlAssertValid()', () => { | ||
const invalid = [ | ||
['protocol', 'http://localhost:5601/app/kibana'], | ||
['protocol', 'https://localhost:5601/app/kibana'], | ||
['protocol', 'mailto:[email protected]'], | ||
['protocol', 'javascript:alert("hi")'], // eslint-disable-line no-script-url | ||
['hostname', 'localhost/app/kibana'], | ||
['hostname and port', 'local.host:5601/app/kibana'], | ||
['hostname and auth', 'user:[email protected]/app/kibana'], | ||
['path traversal', '/app/../../not-kibana'], | ||
['deep path', '/app/kibana/foo'], | ||
['deep path', '/app/kibana/foo/bar'], | ||
['base path', '/base/app/kibana'], | ||
]; | ||
|
||
invalid.forEach(([desc, url]) => { | ||
it(`fails when url has ${desc}`, () => { | ||
try { | ||
shortUrlAssertValid(url); | ||
throw new Error(`expected assertion to throw`); | ||
} catch (err) { | ||
if (!err || !err.isBoom) { | ||
throw err; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
const valid = [ | ||
'/app/kibana', | ||
'/app/monitoring#angular/route', | ||
'/app/text#document-id', | ||
'/app/some?with=query', | ||
'/app/some?with=query#and-a-hash', | ||
]; | ||
|
||
valid.forEach(url => { | ||
it(`allows ${url}`, () => { | ||
shortUrlAssertValid(url); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { parse } from 'url'; | ||
import { trim } from 'lodash'; | ||
import Boom from 'boom'; | ||
|
||
export function shortUrlAssertValid(url) { | ||
const { protocol, hostname, pathname } = parse(url); | ||
|
||
if (protocol) { | ||
throw Boom.notAcceptable(`Short url targets cannot have a protocol, found "${protocol}"`); | ||
} | ||
|
||
if (hostname) { | ||
throw Boom.notAcceptable(`Short url targets cannot have a hostname, found "${hostname}"`); | ||
} | ||
|
||
const pathnameParts = trim(pathname, '/').split('/'); | ||
if (pathnameParts.length !== 2) { | ||
throw Boom.notAcceptable(`Short url target path must be in the format "/app/{{appId}}", found "${pathname}"`); | ||
} | ||
} |