Skip to content

Node.js based forum software built for the modern web

test: add more tests for xml uploads

Barış Soner Uşaklı2c1dcfb40939
diff --git a/src/file.js b/src/file.js
index fde147351c..d3c4b0c574 100644
--- a/src/file.js
+++ b/src/file.js
@@ -214,7 +214,7 @@ async function sanitizeXml(filePath) {
function walk(node) {
if (node.nodeType === 1) {
- const tag = node.tagName.toLowerCase();
+ const tag = (node.localName || node.tagName).toLowerCase();
if (FORBIDDEN.has(tag)) {
node.parentNode.removeChild(node);
@@ -222,15 +222,26 @@ async function sanitizeXml(filePath) {
}
for (const attr of [...node.attributes]) {
- const name = attr.name.toLowerCase();
- const value = attr.value.toLowerCase();
- // eslint-disable-next-line no-control-regex
- const normalized = value.trim().replace(/[\u0000-\u001F\u007F\s]+/g, '');
- if (
- name.startsWith('on') ||
- normalized.startsWith('javascript:')
- ) {
- node.removeAttribute(attr.name);
+ const name = (attr.localName || attr.name).toLowerCase();
+ const value = attr.value
+ .trim()
+ // eslint-disable-next-line no-control-regex
+ .replace(/[\u0000-\u001F\u007F\s]+/g, '')
+ .toLowerCase();
+ const fullName = attr.name.toLowerCase();
+
+ if (name.startsWith('on')) {
+ node.removeAttribute(fullName);
+ continue;
+ }
+
+ if (name === 'href' || name === 'src' || name === 'data' || name.endsWith(':href')) {
+ if (
+ value.startsWith('javascript:') ||
+ value.startsWith('data:text/html')
+ ) {
+ node.removeAttribute(fullName);
+ }
}
}
}
diff --git a/test/files/xss-dirty.xml b/test/files/xss-dirty.xml
index 7dcc39dc9f..01d0c8af32 100644
--- a/test/files/xss-dirty.xml
+++ b/test/files/xss-dirty.xml
@@ -1,10 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
-<a xmlns:a="http://www.w3.org/1999/xhtml">
- <a:body onload="alert('XSS')">
- <a:entry>
- <a:title>Test Entry</a:title>
- <a:content>This is a test entry.</a:content>
- <script>alert('XSS');</script>
- </a:entry>
- </a:body>
+<a xmlns:a="http://www.w3.org/1999/xhtml"
+ xmlns:x="http://example.com/x"
+ xmlns:xlink="http://www.w3.org/1999/xlink">
+ <a:body onload="alert('XSS')">
+ <a:entry>
+ <a:title>Test Entry</a:title>
+ <a:content>This is a test entry.</a:content>
+ <script>alert('XSS');</script>
+ <a:script>alert('xxs');</a:script>
+ <x:a xlink:href=" JAVASCRIPT:alert(1)">click</x:a>
+ </a:entry>
+ </a:body>
</a>
diff --git a/test/uploads.js b/test/uploads.js
index 8c53d86e37..28ba9e3bf2 100644
--- a/test/uploads.js
+++ b/test/uploads.js
@@ -484,6 +484,8 @@ describe('Upload Controllers', () => {
assert.equal(fileResponse.statusCode, 200);
assert.strictEqual(uploadedBody.includes('<script>'), false);
assert.strictEqual(uploadedBody.includes('onload="alert(\'XSS\')"'), false);
+ assert.strictEqual(uploadedBody.includes('<a:script>'), false);
+ assert.strictEqual(uploadedBody.includes('JAVASCRIPT:alert(1)'), false);
});
it('should keep valid xml file unchanged', async () => {