Skip to content
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

fix(refinementList): prevent XSS via routing #4344

Merged
merged 1 commit into from
Mar 5, 2020
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
1 change: 1 addition & 0 deletions src/components/RefinementList/RefinementList.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class RefinementList extends Component {
url,
attribute: this.props.attribute,
cssClasses: this.props.cssClasses,
isFromSearch: this.props.isFromSearch,
};

let { value: key } = facetValue;
Expand Down
72 changes: 72 additions & 0 deletions src/components/RefinementList/__tests__/RefinementList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { h } from 'preact';
import { render, fireEvent } from '@testing-library/preact';
import RefinementList from '../RefinementList';
import defaultTemplates from '../../../widgets/refinement-list/defaultTemplates';

const templates = {
item({ value, count, isRefined }) {
Expand Down Expand Up @@ -119,6 +120,77 @@ describe('RefinementList', () => {
expect(firstItem).not.toHaveAttribute('checked');
expect(secondItem).toHaveAttribute('checked');
});

it('should escape the items in the default template to prevent XSS', () => {
const props = {
...defaultProps,
facetValues: [
{
highlighted: 'Samsung"><iframe/onload=alert("xss")>',
value: 'Samsung"><iframe/onload=alert("xss")>',
label: 'Samsung"><iframe/onload=alert("xss")>',
count: 0,
isRefined: true,
},
{
highlighted: 'Apple',
value: 'Apple',
label: 'Apple',
count: 10,
isRefined: false,
},
],
templateProps: {
templates: defaultTemplates,
},
cssClasses: {
labelText: 'labelText',
},
};

const { container } = render(<RefinementList {...props} />);
const [firstItem, secondItem] = container.querySelectorAll('.labelText');

expect(firstItem.innerHTML).toEqual(
'Samsung"&gt;&lt;iframe/onload=alert("xss")&gt;'
);
expect(secondItem.innerHTML).toEqual('Apple');
});

it('should allow HTML in the items when SFFV', () => {
const props = {
...defaultProps,
isFromSearch: true,
facetValues: [
{
highlighted: '<mark>Sam</mark>sung',
value: 'Samsung',
label: 'Samsung',
count: 0,
isRefined: true,
},
{
highlighted: 'Apple',
value: 'Apple',
label: 'Apple',
count: 10,
isRefined: false,
},
],
templateProps: {
templates: defaultTemplates,
},
cssClasses: {
labelText: 'labelText',
},
};

const { container } = render(<RefinementList {...props} />);
const [firstItem, secondItem] = container.querySelectorAll('.labelText');

expect(firstItem.innerHTML).toEqual('<mark>Sam</mark>sung');
expect(secondItem.innerHTML).toEqual('Apple');
});
});

describe('count', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Object {
class=\\"{{cssClasses.checkbox}}\\"
value=\\"{{value}}\\"
{{#isRefined}}checked{{/isRefined}} />
<span class=\\"{{cssClasses.labelText}}\\">{{{highlighted}}}</span>
<span class=\\"{{cssClasses.labelText}}\\">{{#isFromSearch}}{{{highlighted}}}{{/isFromSearch}}{{^isFromSearch}}{{highlighted}}{{/isFromSearch}}</span>
<span class=\\"{{cssClasses.count}}\\">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>
</label>",
"loadingIndicator": "
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/refinement-list/defaultTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
class="{{cssClasses.checkbox}}"
value="{{value}}"
{{#isRefined}}checked{{/isRefined}} />
<span class="{{cssClasses.labelText}}">{{{highlighted}}}</span>
<span class="{{cssClasses.labelText}}">{{#isFromSearch}}{{{highlighted}}}{{/isFromSearch}}{{^isFromSearch}}{{highlighted}}{{/isFromSearch}}</span>
<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>
</label>`,
showMoreText: `
Expand Down