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

Fio 7544 html sanitization issue #5559

Merged
merged 5 commits into from
Apr 16, 2024
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
14 changes: 8 additions & 6 deletions src/components/html/HTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ export default class HTMLComponent extends Component {
}

const submission = _.get(this.root, 'submission', {});
const content = this.component.content ? this.interpolate(this.component.content, {
metadata: submission.metadata || {},
submission: submission,
data: this.rootValue,
row: this.data
const content = this.component.content ? this.interpolate(
this.sanitize(this.component.content, this.shouldSanitizeValue),
{
metadata: submission.metadata || {},
submission: submission,
data: this.rootValue,
row: this.data
}) : '';
return this.sanitize(content, this.shouldSanitizeValue);
return content;
}

get singleTags() {
Expand Down
30 changes: 29 additions & 1 deletion src/components/html/HTML.unit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Webform from '../../Webform';
import Harness from '../../../test/harness';
import HTMLComponent from './HTML';
import sinon from 'sinon';
import assert from 'power-assert';

import {
comp1,
comp2
comp2,
comp3,
} from './fixtures';

describe('HTML Component', () => {
Expand All @@ -30,4 +32,30 @@ describe('HTML Component', () => {
assert.equal(emit.callCount, 0);
});
});

it('Should not execute scripts inside HTML component, but execute interpolation properly', (done) => {
const formElement = document.createElement('div');
const form = new Webform(formElement);

const alert = sinon.spy(window, 'alert');
form.setForm(comp3).then(() => {
setTimeout(() => {
assert.equal(alert.callCount, 0);
const div = form.element.querySelector('.myClass');
assert.equal(div.innerHTML.trim(), 'No Text');

const textField = form.getComponent(['textField']);
textField.setValue('apple', { modified: true });

setTimeout(() => {
const div = form.element.querySelector('.myClass');

assert.equal(div.innerHTML.trim(), 'apple');
assert.equal(div.className, 'myClass apple-class');
done();
}, 400);
}, 200);
})
.catch(done);
});
});
38 changes: 38 additions & 0 deletions src/components/html/fixtures/comp3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export default {
type: 'form',
display: 'form',
components: [
{
label: 'Text Field',
applyMaskOn: 'change',
tableView: true,
key: 'textField',
type: 'textfield',
input: true,
},
{
label: 'HTML',
attrs: [
{
attr: '',
value: '',
},
],
content: '<img src=1 onerror=alert("htmlContent")>\n<div class="myClass {{data.textField + \'-class\'}}">{{' +
' data.textField ? data.textField : \'No Text\'}}</div>',
refreshOnChange: true,
key: 'html',
type: 'htmlelement',
input: false,
tableView: false,
},
{
type: 'button',
label: 'Submit',
key: 'submit',
disableOnInvalid: true,
input: true,
tableView: false,
},
],
};
3 changes: 2 additions & 1 deletion src/components/html/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import comp1 from './comp1';
import comp2 from './comp2';
export { comp1, comp2 };
import comp3 from './comp3';
export { comp1, comp2, comp3 };
Loading