Skip to content

Commit

Permalink
Fio 7544 html sanitization issue (#5559)
Browse files Browse the repository at this point in the history
* FIO-7544: Fixes an issue where scripts inside HTML component will be executed during interpolation

* Refactoring

* Added test for interpolation in HTML component
  • Loading branch information
alexandraRamanenka authored Apr 16, 2024
1 parent 5649728 commit 0ffd33b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
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 };

0 comments on commit 0ffd33b

Please sign in to comment.