Skip to content

Commit

Permalink
readd support for relative urls in ResultLink (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
samisayegh committed Nov 17, 2020
1 parent 990e605 commit ea0e273
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/ui/ResultLink/ResultLink.ts
Expand Up @@ -478,11 +478,10 @@ export class ResultLink extends Component {
);

private filterProtocol(uri: string) {
if (/^(https?|ftp|file|mailto|tel):/i.test(uri)) {
return uri;
}
const isAbsolute = /^(https?|ftp|file|mailto|tel):/i.test(uri);
const isRelative = /^\//.test(uri);

return '';
return isAbsolute || isRelative ? uri : '';
}

private getResultUri(): string {
Expand Down
12 changes: 12 additions & 0 deletions unitTests/ui/ResultLinkTest.ts
Expand Up @@ -359,6 +359,18 @@ export function ResultLinkTest() {
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.clickUri);
});

it('when the clickUri is a relative url (starts with slash), it sets the href to the uri', () => {
fakeResult.clickUri = '/casemgmt/sc_KnowledgeArticle?sfdcid=ka32C0000009t9CQAQ&type=Solution';
initHyperLink();
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.clickUri);
});

it('when the clickUri is a string containing but not starting with a slash, it sets the href to an empty string', () => {
fakeResult.clickUri = 'casemgmt/sc_KnowledgeArticle';
initHyperLink();
expect(test.cmp.element.getAttribute('href')).toEqual('');
});

it(`when the uri (clickUri) defined in the results contains the javascript protocol,
it clears the value to prevent XSS`, () => {
fakeResult.clickUri = 'JavaScript:void(0)';
Expand Down

0 comments on commit ea0e273

Please sign in to comment.