Skip to content

Commit

Permalink
DN-26: Added unit test for HTML sanitizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Austerschulte committed Apr 24, 2024
1 parent 7b1280b commit cdc0274
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/js/bundles/dn_welcome/tests/WelcomeWidgetTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,73 @@ describe(module.id, function () {
const buttonWrapper = wrapper.findComponent({ name: 'v-btn' });
assert.isFalse(buttonWrapper.vm.$props.disabled);
});

const sanitizerTests = [
{
message: "Event attributes should be removed from info text",
testInfoText: "Welcome! <p onclick='runEvilCode()'>Click here!</p>",
shouldNotInclude: "onclick"
},
{
message: "'script' tags should be removed from info text",
testInfoText: "Welcome! <script type=\"text/javascript\">runEvilCode()</script>",
shouldNotInclude: "script"
},
{
message: "Uppercase 'SCRIPT' tags should be removed from info text",
testInfoText: "Welcome! <SCRIPT type=\"text/javascript\">runEvilCode()</SCRIPT>",
shouldNotInclude: "SCRIPT"
},
{
message: "'iframe' tags should be removed from info text",
testInfoText: "Welcome! <iframe src='evil.html'></iframe>",
shouldNotInclude: "iframe"
},
{
message: "'object' tags should be removed from info text",
testInfoText: "Welcome! <object></object>",
shouldNotInclude: "object"
},
{
message: "'embed' tags should be removed from info text",
testInfoText: "Welcome! <embed></embed>",
shouldNotInclude: "embed"
},
{
message: "'i' tags should be preserved in info text",
testInfoText: "<i>Welcome!</i>",
shouldInclude: "<i>"
},
{
message: "'style' attributes on tags should be preserved in info text",
testInfoText: "<p style='font-size: 12px'>Welcome</p>",
shouldInclude: "style"
}
] as SanitizerTestCase[];

sanitizerTests.forEach(testCase => {
it(testCase.message, async function () {
const wrapper = mount(WelcomeWidget);
await wrapper.setData({
infoText: testCase.testInfoText
});
const infoTextWrapper = wrapper.find(".dn-welcome-widget__info-text");

const html = infoTextWrapper.html();

if (testCase.shouldNotInclude) {
assert.notInclude(html, testCase.shouldNotInclude);
}
if (testCase.shouldInclude) {
assert.include(html, testCase.shouldInclude);
}
});
});
});

interface SanitizerTestCase {
message: string;
testInfoText: string;
shouldNotInclude?: string;
shouldInclude?: string;
}

0 comments on commit cdc0274

Please sign in to comment.