Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format error messages as text to avoid XSS vulnerabilities #2161

Merged
merged 2 commits into from
Nov 12, 2024
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
2 changes: 1 addition & 1 deletion apps/passport-server/src/routing/pcdHttpError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function respondWithError(
if (!e.message) {
res.sendStatus(e.code);
} else {
res.status(e.code).send(e.message);
res.status(e.code).type("text").send(e.message);
}
} else if (e instanceof PCDHTTPJSONError) {
res.status(e.code).json(e.json);
Expand Down
4 changes: 2 additions & 2 deletions apps/passport-server/src/routing/routes/accountRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export function initAccountRoutes(
if (result.success) {
res.status(200).json(result.value);
} else {
res.status(403).send(result.error);
res.status(403).type("text").send(result.error);
}
}
);
Expand All @@ -274,7 +274,7 @@ export function initAccountRoutes(
if (result.success) {
res.status(200).json(result.value);
} else {
res.status(403).send(result.error);
res.status(403).type("text").send(result.error);
}
}
);
Expand Down
3 changes: 3 additions & 0 deletions apps/passport-server/src/routing/routes/healthCheckRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function initHealthcheckRoutes(
async (req: Request, res: Response) => {
res
.status(200)
.type("text")
.send(`Zupass Server - Cluster Test OK! PID: ${process.pid}`);
}
);
Expand All @@ -46,6 +47,7 @@ export function initHealthcheckRoutes(
async (req: Request, res: Response) => {
res
.status(200)
.type("text")
.send(
`Zupass Server - Cluster Test OK! PID: ${
process.pid
Expand All @@ -62,6 +64,7 @@ export function initHealthcheckRoutes(
async (req: Request, res: Response) => {
res
.status(200)
.type("text")
.send(
`Zupass Server - Cluster Test OK! PID: ${
process.pid
Expand Down
9 changes: 8 additions & 1 deletion apps/passport-server/src/util/telegramWebApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ export const closeWebviewHtml = `
`;

export const errorHtmlWithDetails = (error: Error): string => {
const errorMessage = error ? error.message.replace(/\n/g, "<br>") : "";
// Reformat linebreaks in error messages as <br>. Also replace any <> to
// avoid including of HTML (accidental, or XSS risks).
const errorMessage = error
? error.message
.replace(/\n/g, "<br>")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
: "";
const errorStack =
error instanceof Error && error.stack
? error.stack.replace(/\n/g, "<br>")
Expand Down
2 changes: 1 addition & 1 deletion apps/zupoll-server/src/routing/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function startServer(
) => {
console.error(`[ERROR] ${req.method} ${req.url}`);
console.error(err.stack);
res.status(500).send(err.message);
res.status(500).type("text").send(err.message);
}
);

Expand Down
Loading