Skip to content

Commit 31d5823

Browse files
mauricode-asherjsjoeio
authored
Escape HTML from messages in error page (#4430)
Co-authored-by: Asher <[email protected]> Co-authored-by: Joe Previte <[email protected]>
1 parent 605c3c6 commit 31d5823

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/node/routes/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { WebsocketRequest } from "../../../typings/pluginapi"
66
import { HttpCode } from "../../common/http"
77
import { rootPath } from "../constants"
88
import { replaceTemplates } from "../http"
9-
import { getMediaMime } from "../util"
9+
import { escapeHtml, getMediaMime } from "../util"
1010

1111
const notFoundCodes = ["ENOENT", "EISDIR", "FileNotFound"]
1212
export const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
@@ -29,7 +29,7 @@ export const errorHandler: express.ErrorRequestHandler = async (err, req, res, n
2929
replaceTemplates(req, content)
3030
.replace(/{{ERROR_TITLE}}/g, status)
3131
.replace(/{{ERROR_HEADER}}/g, status)
32-
.replace(/{{ERROR_BODY}}/g, err.message),
32+
.replace(/{{ERROR_BODY}}/g, escapeHtml(err.message)),
3333
)
3434
} else {
3535
res.json({

test/unit/node/routes/errors.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import express from "express"
2+
import { errorHandler } from "../../../../src/node/routes/errors"
3+
4+
describe("error page is rendered for text/html requests", () => {
5+
it("escapes any html in the error messages", async () => {
6+
const next = jest.fn()
7+
const err = {
8+
code: "ENOENT",
9+
statusCode: 404,
10+
message: ";>hello<script>alert(1)</script>",
11+
}
12+
const req = createRequest()
13+
const res = {
14+
status: jest.fn().mockReturnValue(this),
15+
send: jest.fn().mockReturnValue(this),
16+
set: jest.fn().mockReturnValue(this),
17+
} as unknown as express.Response
18+
19+
await errorHandler(err, req, res, next)
20+
expect(res.status).toHaveBeenCalledWith(404)
21+
expect(res.send).toHaveBeenCalledWith(expect.not.stringContaining("<script>"))
22+
})
23+
})
24+
25+
function createRequest(): express.Request {
26+
return {
27+
headers: {
28+
accept: ["text/html"],
29+
},
30+
originalUrl: "http://example.com/test",
31+
query: {
32+
to: "test",
33+
},
34+
} as unknown as express.Request
35+
}

0 commit comments

Comments
 (0)