Skip to content

Commit

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

* FIO-7544: Fixes an issue where scripts inside HTML component will be executed during interpolation

* Refactoring
  • Loading branch information
alexandraRamanenka authored Dec 21, 2023
1 parent 230af91 commit cde5f97
Show file tree
Hide file tree
Showing 4 changed files with 56 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
18 changes: 17 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,18 @@ describe('HTML Component', () => {
assert.equal(emit.callCount, 0);
});
});

it('Should not execute scripts inside HTML component', (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);
done();
}, 200);
})
.catch(done);
});
});
29 changes: 29 additions & 0 deletions src/components/html/fixtures/comp3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default {
type: 'form',
display: 'form',
components: [
{
label: 'HTML',
attrs: [
{
attr: '',
value: '',
},
],
content: '<img src=1 onerror=alert("htmlContent")>',
refreshOnChange: false,
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 cde5f97

Please sign in to comment.