Files
webstudio/apps/builder/app/services/csrf-session.server.ts
T
Ivan Starkov 766d7eb95f chore: Add CSRF protection to prevent Canvas script execution vulnerability (#4047)
## Description

how it works
https://github.com/webstudio-is/webstudio-saas/issues/317#issuecomment-2317121292

- attack example
https://p-845e975a-4e84-4186-b312-431f3a6f99ea-dot-main.development.webstudio.is/
- attack fixed
https://p-845e975a-4e84-4186-b312-431f3a6f99ea-dot-csrf.staging.webstudio.is/

corresponding PR #4060 (Add postMessage token to secure against Canvas
script execution)

Additionally

csp on iframe (see comments)
destination checks - to prevent including loaders/pages into iframes

<img width="1015" alt="image"
src="https://github.com/user-attachments/assets/2d8c6840-e195-424e-942f-8b8d05203e18">


## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-09-04 17:08:08 +03:00

128 lines
4.3 KiB
TypeScript

import { createCookieSessionStorage } from "@remix-run/node";
import env from "~/env/env.server";
import { extractAuthFromRequest } from "~/shared/context.server";
import { allowedDestinations } from "./destinations.server";
type CsrfSessionData = {
hash: string;
token: string;
};
const getCsrfSessionCookieNameVersion = () => {
// IMPORTANT: If you see an error here, you need to increase the version number.
// Explanation:
// Changing the CsrfSessionData type will cause all existing user sessions to not work as expected.
// There is no logic to validate or clean up sessions, so we avoid session migration issues by changing the session cookie name.
// This ensures that old sessions are invalidated and new sessions are created with the updated structure.
const obj: CsrfSessionData = { token: "", hash: "" };
obj.hash = "";
// IMPORTANT: Change version in the SaaS platform as well!
// IMPORTANT: Changing the version will cause all users to be logged out.
return "1";
};
const csrfSessionStorage = createCookieSessionStorage({
cookie: {
// Using the __Host- prefix to prevent a malicious user from setting another person's session cookie
// on all subdomains of apps.webstudio.is, e.g., setting Domain=.apps.webstudio.is.
// For more information, see: https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/Cookies#name
name: `__Host-_csrf_${getCsrfSessionCookieNameVersion()}`,
sameSite: "lax",
path: "/",
httpOnly: true,
secrets: env.AUTH_WS_CLIENT_SECRET
? [env.AUTH_WS_CLIENT_SECRET]
: undefined,
secure: true,
},
});
const toBase64Url = (buffer: ArrayBuffer) => {
return Buffer.from(buffer).toString("base64url");
};
export const getRequestAuthHash = async (request: Request) => {
const data = await extractAuthFromRequest(request);
if (data.isServiceCall) {
throw new Error("Service calls are not allowed to use CSRF tokens.");
}
// Because of cookie session we don't have session id in the request, so we derive it from the auth credentials data.
const sessionId = data.authToken ?? data.sessionData?.userId ?? "anonymous";
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(sessionId);
const hashBuffer = await crypto.subtle.digest("SHA-256", dataBuffer);
return toBase64Url(hashBuffer.slice(0, 16));
};
/**
* We use a hash to ensure that any change in authentication credentials also changes the CSRF token.
*
* If setCookieValue is defined, it means that we need to set the cookie.
*/
export const getCsrfTokenAndCookie = async (
request: Request
): Promise<[csrfToken: string, setCookieValue: string | undefined]> => {
const hash = await getRequestAuthHash(request);
const csrfSession = await csrfSessionStorage.getSession(
request.headers.get("Cookie")
);
let sessionCreateCookieValue: string | undefined = undefined;
if (csrfSession.get("hash") !== hash) {
const csrfTokenLength = 16;
const array = new Uint8Array(csrfTokenLength);
crypto.getRandomValues(array);
const token = toBase64Url(array);
csrfSession.set("hash", hash);
csrfSession.set("token", token);
sessionCreateCookieValue =
await csrfSessionStorage.commitSession(csrfSession);
}
return [csrfSession.get("token"), sessionCreateCookieValue];
};
export const checkCsrf = async (request: Request) => {
if (
request.headers.get("sec-fetch-mode") === "navigate" &&
request.method === "GET"
) {
// Do not check CSRF for GET navigation requests to allow logged-in users to view the data.
// However, prevent loading the data from an iframe.
allowedDestinations(request, ["document"]);
return;
}
const [token, setCookieValue] = await getCsrfTokenAndCookie(request);
if (setCookieValue) {
throw new Response(
"Authentication credentials have changed. Please reload and try again.",
{
status: 403,
statusText:
"Authentication credentials have changed. Please reload and try again.",
}
);
}
const csrfToken = request.headers.get("X-CSRF-Token");
if (token !== csrfToken) {
throw new Response(
`Forbidden: "The CSRF token is invalid. Please reload and try again.`,
{
status: 403,
statusText: `Forbidden: "The CSRF token is invalid. Please reload and try again.`,
}
);
}
};