Skip to content
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
11 changes: 10 additions & 1 deletion src/disco/components/Addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ import {

import 'disco/css/Addon.scss';

purify.addHook('afterSanitizeAttributes', (node) => {
// Set all elements owning target to target=_blank
// and add rel="noreferrer".
if ('target' in node) {
node.setAttribute('target', '_blank');
node.setAttribute('rel', 'noreferrer');
}
});

function sanitizeHTML(text, allowTags = []) {
// TODO: Accept tags to allow and run through dom-purify.
return {
Expand Down Expand Up @@ -213,7 +222,7 @@ export class Addon extends React.Component {
<h2
ref="heading"
className="heading"
dangerouslySetInnerHTML={sanitizeHTML(heading, ['span'])} />
dangerouslySetInnerHTML={sanitizeHTML(heading, ['a', 'span'])} />
{this.getDescription()}
</div>
<div className="install-button">
Expand Down
28 changes: 28 additions & 0 deletions src/disco/css/Addon.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,38 @@ $addon-padding: 20px;
font-weight: medium;
margin: 0;

a:link,
a:visited,
a:focus,
a:hover,
a:active {
color: $primary-font-color;
text-decoration: none;
}

a:focus,
a:hover {
text-decoration: underline;
}

span {
color: $secondary-font-color;
font-size: 14px;
font-weight: normal;

a:link,
a:visited,
a:focus,
a:hover,
a:active {
color: $secondary-font-color;
text-decoration: none;
}

a:focus,
a:hover {
text-decoration: underline;
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/client/disco/components/TestAddon.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ describe('<Addon />', () => {
assert.include(root.refs.heading.innerHTML, 'Hey! This is <span>an add-on</span>');
});

it('purifies the heading with a link and adds link attrs', () => {
root = renderAddon({
...result,
heading: 'This is <span>an <a href="https://addons.mozilla.org">add-on</a>/span>',
});
const link = root.refs.heading.querySelector('a');
assert.equal(link.getAttribute('rel'), 'noreferrer');
assert.equal(link.getAttribute('target'), '_blank');
});

it('purifies the heading with a bad link', () => {
root = renderAddon({
...result,
heading: 'This is <span>an <a href="javascript:alert(1)">add-on</a>/span>',
});
const link = root.refs.heading.querySelector('a');
assert.equal(link.getAttribute('href'), null);
});

it('purifies the editorial description', () => {
root = renderAddon({
...result,
Expand Down