Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/node/routes/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WebsocketRequest } from "../../../typings/pluginapi"
import { HttpCode } from "../../common/http"
import { rootPath } from "../constants"
import { replaceTemplates } from "../http"
import { getMediaMime } from "../util"
import { escapeHtml, getMediaMime } from "../util"

const notFoundCodes = ["ENOENT", "EISDIR", "FileNotFound"]
export const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
Expand All @@ -29,7 +29,7 @@ export const errorHandler: express.ErrorRequestHandler = async (err, req, res, n
replaceTemplates(req, content)
.replace(/{{ERROR_TITLE}}/g, status)
.replace(/{{ERROR_HEADER}}/g, status)
.replace(/{{ERROR_BODY}}/g, err.message),
.replace(/{{ERROR_BODY}}/g, escapeHtml(err.message)),
)
} else {
res.json({
Expand Down
35 changes: 35 additions & 0 deletions test/unit/node/routes/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import express from "express"
import { errorHandler } from "../../../../src/node/routes/errors"

describe("error page is rendered for text/html requests", () => {
it("escapes any html in the error messages", async () => {
const next = jest.fn()
const err = {
code: "ENOENT",
statusCode: 404,
message: ";>hello<script>alert(1)</script>",
}
const req = createRequest()
const res = {
status: jest.fn().mockReturnValue(this),
send: jest.fn().mockReturnValue(this),
set: jest.fn().mockReturnValue(this),
} as unknown as express.Response

await errorHandler(err, req, res, next)
expect(res.status).toHaveBeenCalledWith(404)
expect(res.send).toHaveBeenCalledWith(expect.not.stringContaining("<script>"))
})
})

function createRequest(): express.Request {
return {
headers: {
accept: ["text/html"],
},
originalUrl: "http://example.com/test",
query: {
to: "test",
},
} as unknown as express.Request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer we have to do this but I don't see any better alternatives

}