Skip to content

Commit 0e90184

Browse files
JohnMcLearclaude
andauthored
fix(export): surface checkValidRev error message on bad :rev (#7792)
* fix(export): surface checkValidRev error message in response body A non-numeric :rev (e.g. /p/foo/test1/export/txt) was reaching checkValidRev, which throws CustomError('rev is not a number', 'apierror'). The error fell through the route handler's .catch(next), so Express's default error renderer kicked in and returned a 500 with the generic HTML page <title>Error</title> / <pre>Internal Server Error</pre>. The thrown message never made it to the body, so callers had no way to tell why the request failed. Catch the apierror in the route handler and send err.message as a text/plain 500 body. Other errors still propagate to next(err) so unrelated failures keep their existing handling. Also retitle the test (was "is 403" while asserting expect(500)) — leftover label from an earlier expectation. Fixes #7788 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(export): validate rev before attachment, broaden error catch Qodo feedback on #7792: 1) ExportHandler.doExport set Content-Disposition (via res.attachment) before calling checkValidRev. If the rev was invalid, the route-level catch returned a plain-text 500 — but the attachment header was still in place, so browsers offered to save the error message as a file. Move checkValidRev to the top of doExport so an invalid rev never touches the attachment header. 2) The catch only converted CustomError('...', 'apierror') into a plain-text response. Other export errors (conversion failures, fs issues, soffice problems) still fell through to Express's default HTML renderer — non-deterministic for API callers and confusing when they were already half-downloading a file. Surface every export failure as a deterministic text/plain 500. apierrors carry user-facing messages, so send err.message verbatim. For other errors, log the full stack server-side and still emit err.message (or 'Internal Server Error' if absent) so the response body is never the Express HTML stack page. Also clear Content-Disposition in the catch as a safety net. Backend tests (importexportGetPost.ts): 58 passing, 0 failing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fba4a17 commit 0e90184

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/node/handler/ExportHandler.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ const tempDirectory = os.tmpdir();
4545
* @param {String} type the type to export
4646
*/
4747
exports.doExport = async (req: any, res: any, padId: string, readOnlyId: string, type:string) => {
48+
// Validate :rev BEFORE setting Content-Disposition. A bad rev causes
49+
// checkValidRev to throw, which the route handler catches and renders as a
50+
// plain-text 500. If we set the attachment header first, the browser would
51+
// download the error message as a file instead of displaying it.
52+
if (req.params.rev !== undefined) {
53+
// modify req, as we use it in a later call to exportConvert
54+
req.params.rev = checkValidRev(req.params.rev);
55+
}
56+
4857
// avoid naming the read-only file as the original pad's id
4958
let fileName = readOnlyId ? readOnlyId : padId;
5059

@@ -59,12 +68,6 @@ exports.doExport = async (req: any, res: any, padId: string, readOnlyId: string,
5968
// tell the browser that this is a downloadable file
6069
res.attachment(`${fileName}.${type}`);
6170

62-
if (req.params.rev !== undefined) {
63-
// ensure revision is a number
64-
// modify req, as we use it in a later call to exportConvert
65-
req.params.rev = checkValidRev(req.params.rev);
66-
}
67-
6871
// if this is a plain text export, we can do this directly
6972
// We have to over engineer this because tabs are stored as attributes and not plain text
7073
if (type === 'etherpad') {

src/node/hooks/express/importexport.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,24 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
7171
console.log(`Exporting pad "${req.params.pad}" in ${req.params.type} format`);
7272
await exportHandler.doExport(req, res, padId, readOnlyId, req.params.type);
7373
}
74-
})().catch((err) => next(err || new Error(err)));
74+
})().catch((err) => {
75+
// Send a deterministic plain-text body for every export failure.
76+
// checkValidRev throws CustomError('...', 'apierror') for a bad :rev,
77+
// but conversion / fs / soffice errors also reach this handler — and
78+
// without an explicit response, all of them would fall through to
79+
// Express's default HTML error renderer, which is hostile to API
80+
// callers (and would be saved as a file by the browser because of
81+
// the attachment header set in doExport for non-apierror cases).
82+
if (res.headersSent) return next(err || new Error(err));
83+
// Clear the download header so the error body renders inline instead
84+
// of being saved as the requested filename.
85+
res.removeHeader('Content-Disposition');
86+
// Log the full error server-side for operators. apierrors are
87+
// user-facing validation errors and not worth a server-side log line.
88+
if (!err || err.name !== 'apierror') console.error('Export error:', err);
89+
const msg = (err && err.message) || 'Internal Server Error';
90+
return res.status(500).type('text/plain').send(msg);
91+
});
7592
});
7693

7794
// handle import requests

src/tests/backend/specs/api/importexportGetPost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ describe(__filename, function () {
614614
.expect((res:any) => assert.equal(res.text, 'ofoo\n'));
615615
});
616616

617-
it('txt request rev test1 is 403', async function () {
617+
it('txt request rev test1 returns 500 with error message', async function () {
618618
await agent.get(`/p/${testPadId}/test1/export/txt`)
619619
.set("authorization", await common.generateJWTToken())
620620
.expect(500)

0 commit comments

Comments
 (0)