Skip to content

Commit 9df46f8

Browse files
pjeanjeanmichitux
authored andcommitted
XWIKI-19611: Improve filename escaping in attachment upload
* Use escapeHTML() when displaying filenames in upload.js * Add a test in AttachmentIT to check for proper HTML escaping during upload (cherry picked from commit 910a501)
1 parent 53d22f4 commit 9df46f8

File tree

3 files changed

+26
-5
lines changed
  • xwiki-platform-core
    • xwiki-platform-flamingo/xwiki-platform-flamingo-skin/xwiki-platform-flamingo-skin-test/xwiki-platform-flamingo-skin-test-docker/src/test
    • xwiki-platform-web/xwiki-platform-web-war/src/main/webapp/resources/uicomponents/widgets

3 files changed

+26
-5
lines changed

xwiki-platform-core/xwiki-platform-flamingo/xwiki-platform-flamingo-skin/xwiki-platform-flamingo-skin-test/xwiki-platform-flamingo-skin-test-docker/src/test/it/org/xwiki/flamingo/test/docker/AttachmentIT.java

+19
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class AttachmentIT
6565

6666
private static final String SECOND_ATTACHMENT = "SmallAttachment2.txt";
6767

68+
private static final String ESCAPED_ATTACHMENT = "<strong>EscapedAttachment.txt";
69+
6870
private static final String IMAGE_ATTACHMENT = "image.gif";
6971

7072
private static final String SMALL_SIZE_ATTACHMENT = "SmallSizeAttachment.png";
@@ -446,6 +448,23 @@ void deleteAttachmentWithSpecialChar(TestUtils setup, TestReference testReferenc
446448
basePage.getXWikiMessageContent());
447449
}
448450

451+
@Test
452+
@Order(9)
453+
void checkEscapingInAttachmentName(TestUtils setup, TestReference testReference,
454+
TestConfiguration testConfiguration)
455+
{
456+
setup.loginAsSuperAdmin();
457+
setup.createPage(testReference, "Empty content");
458+
AttachmentsPane attachmentsPane = new AttachmentsViewPage().openAttachmentsDocExtraPane();
459+
460+
attachmentsPane.setFileToUpload(getFileToUpload(testConfiguration, ESCAPED_ATTACHMENT).getAbsolutePath());
461+
attachmentsPane.waitForUploadToFinish(ESCAPED_ATTACHMENT);
462+
attachmentsPane.clickHideProgress();
463+
464+
assertTrue(attachmentsPane.attachmentExistsByFileName(ESCAPED_ATTACHMENT));
465+
attachmentsPane.deleteAttachmentByFileByName(ESCAPED_ATTACHMENT);
466+
}
467+
449468
private String getAttachmentsMacroContent(DocumentReference docRef)
450469
{
451470
StringBuilder sb = new StringBuilder();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an attachment whose name should be escaped.

xwiki-platform-core/xwiki-platform-web/xwiki-platform-web-war/src/main/webapp/resources/uicomponents/widgets/upload.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var XWiki = (function(XWiki) {
124124

125125
if (this.options.enableFileInfo) {
126126
statusUI.FILE_INFO = UploadUtils.createDiv('file-info');
127-
(statusUI.FILE_NAME = UploadUtils.createSpan('file-name', this.file.name)).title = this.file.type;
127+
(statusUI.FILE_NAME = UploadUtils.createSpan('file-name', this.file.name.escapeHTML())).title = this.file.type;
128128
statusUI.FILE_SIZE = UploadUtils.createSpan('file-size', ' (' + UploadUtils.bytesToSize(this.file.size) + ')');
129129
statusUI.FILE_CANCEL = UploadUtils.createButton("$services.localization.render('core.widgets.html5upload.item.cancel')", this.cancelUpload.bindAsEventListener(this));
130130
// TODO MIME type icon?
@@ -295,7 +295,7 @@ var XWiki = (function(XWiki) {
295295
this.statusUI.FILE_CANCEL.addClassName('hidden');
296296
}
297297
this.formData.input.fire('xwiki:html5upload:message', {content: 'UPLOAD_FINISHING', type: 'inprogress', source: this,
298-
parameters : {name : this.file.name}
298+
parameters : {name : this.file.name.escapeHTML()}
299299
});
300300
},
301301

@@ -324,7 +324,7 @@ var XWiki = (function(XWiki) {
324324
}
325325
}
326326
this.formData.input.fire('xwiki:html5upload:message', {content: 'UPLOAD_FINISHED', type: 'done', source: this,
327-
parameters : {name : this.file.name, size : UploadUtils.bytesToSize(this.file.size)}
327+
parameters : {name : this.file.name.escapeHTML(), size : UploadUtils.bytesToSize(this.file.size)}
328328
});
329329
this.formData.input.fire('xwiki:html5upload:fileFinished', {source: this});
330330
clearInterval(this.timer);
@@ -354,7 +354,8 @@ var XWiki = (function(XWiki) {
354354
*/
355355
abnormalUploadFinish : function (message) {
356356
clearInterval(this.timer);
357-
this.formData.input.fire('xwiki:html5upload:message', {content: message, type: 'error', source: this, parameters : {name : this.file.name}});
357+
this.formData.input.fire('xwiki:html5upload:message', {content: message, type: 'error', source: this, parameters :
358+
{name : this.file.name.escapeHTML()}});
358359
this.formData.input.fire('xwiki:html5upload:fileFinished', {source: this});
359360
}
360361
});
@@ -517,7 +518,7 @@ var XWiki = (function(XWiki) {
517518
}
518519
} catch (ex) {
519520
this.showMessage(ex, 'error', {size : UploadUtils.bytesToSize(this.options && this.options.maxFilesize),
520-
name : file.name, type: file.type
521+
name : file.name.escapeHTML(), type: file.type
521522
});
522523
}
523524
}

0 commit comments

Comments
 (0)