Skip to content

Prevent links from being rendered in user bios #13643

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/amo/pages/UserProfile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ export class UserProfileBase extends React.Component<InternalProps> {
>
<div
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={sanitizeUserHTML(user.biography)}
dangerouslySetInnerHTML={sanitizeUserHTML(
user.biography,
false,
)}
/>
</Definition>
) : null}
Expand Down
14 changes: 10 additions & 4 deletions src/amo/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,11 @@ export function nl2br(text: ?string): string {
* Developer Hub when you hover over the *Some HTML Supported* link under
* the textarea field.
*/
export function sanitizeUserHTML(text: ?string): {| __html: string |} {
return sanitizeHTML(nl2br(text), [
'a',
export function sanitizeUserHTML(
text: ?string,
allowLinks: boolean = true,
): {| __html: string |} {
const allowTags = [
'abbr',
'acronym',
'b',
Expand All @@ -235,7 +237,11 @@ export function sanitizeUserHTML(text: ?string): {| __html: string |} {
'ol',
'strong',
'ul',
]);
];
if (allowLinks === true) {
allowTags.unshift('a');
}
return sanitizeHTML(nl2br(text), allowTags);
}

export function isAddonAuthor({
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/amo/pages/TestUserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,22 @@ describe(__filename, () => {
).toHaveTextContent(biographyText);
});

it('does not render links in biography', () => {
const biographyText = 'Veni vidi vici';
const biography = `<a href="//example.com"><b>${biographyText}</b></a>`;
signInUserAndRenderUserProfile({ biography });

expect(screen.getByText('Biography')).toBeInTheDocument();
expect(screen.getByClassName('UserProfile-biography')).toHaveTextContent(
biographyText,
);
expect(
within(screen.getByClassName('UserProfile-biography')).queryByTagName(
'a',
),
).not.toBeInTheDocument();
});

it('omits a null biography', () => {
signInUserAndRenderUserProfile({ biography: null });

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/amo/utils/test_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ describe(__filename, () => {
expect(sanitize(customHtml)).toEqual(customHtml);
});

it('can disallow links', () => {
const customHtml =
'<b>check</b> <i>out</i> <a href="http://mysite">my site</a>';
expect(sanitize(customHtml, false)).toEqual(
'<b>check</b> <i>out</i> my site',
);
});

it('does not allow certain tags', () => {
expect(
sanitize('<b>my add-on</b> <script>alert("does XSS")</script>'),
Expand Down