Skip to content

Commit 60982ad

Browse files
committed
XWIKI-23151: Cleanup the PDF export job request after the job is finished
1 parent 769aa49 commit 60982ad

File tree

2 files changed

+31
-0
lines changed
  • xwiki-platform-core/xwiki-platform-export/xwiki-platform-export-pdf/xwiki-platform-export-pdf-api/src

2 files changed

+31
-0
lines changed

xwiki-platform-core/xwiki-platform-export/xwiki-platform-export-pdf/xwiki-platform-export-pdf-api/src/main/java/org/xwiki/export/pdf/internal/job/PDFExportJob.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ public class PDFExportJob extends AbstractPDFExportJob
7676

7777
@Override
7878
protected void runInternal() throws Exception
79+
{
80+
try {
81+
exportAsPDF();
82+
} finally {
83+
cleanup();
84+
}
85+
}
86+
87+
private void cleanup()
88+
{
89+
// Avoid saving sensitive information along with the PDF export job status. This information is not needed
90+
// anymore after the PDF export job is done (whether the PDF is generated client-side or server-side).
91+
getRequest().getContext().keySet().removeAll(List.of("request.session", "request.headers", "request.cookies"));
92+
}
93+
94+
private void exportAsPDF() throws Exception
7995
{
8096
if (!this.request.getDocuments().isEmpty()) {
8197
this.requiredSkinExtensionsRecorder.start();

xwiki-platform-core/xwiki-platform-export/xwiki-platform-export-pdf/xwiki-platform-export-pdf-api/src/test/java/org/xwiki/export/pdf/internal/job/PDFExportJobTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.xwiki.export.pdf.internal.job;
2121

2222
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
2324
import static org.junit.jupiter.api.Assertions.assertNull;
2425
import static org.junit.jupiter.api.Assertions.assertSame;
2526
import static org.junit.jupiter.api.Assertions.fail;
@@ -159,6 +160,7 @@ void runServerSide() throws Exception
159160
when(this.pdfPrinter.print(printPreviewURL)).thenReturn(pdfContent);
160161

161162
this.request.setServerSide(true);
163+
this.request.getContext().put("request.session", "session");
162164
this.pdfExportJob.initialize(this.request);
163165
this.pdfExportJob.runInternal();
164166

@@ -170,11 +172,14 @@ void runServerSide() throws Exception
170172

171173
TemporaryResourceReference pdfFileReference = jobStatus.getPDFFileReference();
172174
verify(this.temporaryResourceStore).createTemporaryFile(pdfFileReference, pdfContent);
175+
176+
assertFalse(this.request.getContext().containsKey("request.session"));
173177
}
174178

175179
@Test
176180
void runClientSide() throws Exception
177181
{
182+
this.request.getContext().put("request.headers", "headers");
178183
this.pdfExportJob.initialize(this.request);
179184
this.pdfExportJob.runInternal();
180185

@@ -188,6 +193,8 @@ void runClientSide() throws Exception
188193
assertEquals(2, renderingResults.size());
189194
assertSame(this.firstPageRendering, renderingResults.get(0));
190195
assertSame(this.secondPageRendering, renderingResults.get(1));
196+
197+
assertFalse(this.request.getContext().containsKey("request.headers"));
191198
}
192199

193200
@Test
@@ -216,6 +223,7 @@ void runWithTemplateSpecified() throws Exception
216223
@Test
217224
void runWithoutDocuments() throws Exception
218225
{
226+
this.request.getContext().put("request.cookies", "cookies");
219227
this.request.setDocuments(Collections.emptyList());
220228
this.pdfExportJob.initialize(this.request);
221229
this.pdfExportJob.runInternal();
@@ -224,6 +232,8 @@ void runWithoutDocuments() throws Exception
224232
assertNull(jobStatus.getPDFFileReference());
225233
assertNull(jobStatus.getRequiredSkinExtensions());
226234
assertEquals(0, jobStatus.getDocumentRenderingResults().size());
235+
236+
assertFalse(this.request.getContext().containsKey("request.cookies"));
227237
}
228238

229239
@Test
@@ -233,6 +243,8 @@ void runWithContentSizeLimitExceeded() throws Exception
233243
new XDOM(Collections.singletonList(new WordBlock("second"))), StringUtils.repeat('x', 1000));
234244
when(this.documentRenderer.render(this.secondPageReference, this.rendererParameters)).thenReturn(largeResult);
235245

246+
this.request.getContext().put("request.cookies", "cookies");
247+
this.request.getContext().put("request.foo", "bar");
236248
this.pdfExportJob.initialize(this.request);
237249
try {
238250
this.pdfExportJob.runInternal();
@@ -243,6 +255,9 @@ void runWithContentSizeLimitExceeded() throws Exception
243255
+ " or disable this limit from the PDF Export administration section or from XWiki properties.",
244256
e.getMessage());
245257
}
258+
259+
assertFalse(this.request.getContext().containsKey("request.cookies"));
260+
assertEquals("bar", this.request.getContext().get("request.foo"));
246261
}
247262

248263
@Test

0 commit comments

Comments
 (0)