Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

monograph: add a strict csp

Abdullah Atta21760b2b74c8
diff --git a/apps/monograph/app/entry.server.tsx b/apps/monograph/app/entry.server.tsx
index 56d30663c..c57f66656 100644
--- a/apps/monograph/app/entry.server.tsx
+++ b/apps/monograph/app/entry.server.tsx
@@ -62,8 +62,42 @@ export default function handleRequest(
responseHeaders.set("Content-Type", "text/html; charset=utf-8");
+ const nonce =
+ remixContext.staticHandlerContext?.loaderData?.root?.cspScriptNonce;
+ if (nonce)
+ responseHeaders.set(
+ "Content-Security-Policy",
+ getContentSecurityPolicy(nonce)
+ );
+
return new Response(html, {
status: responseStatusCode,
headers: responseHeaders
});
}
+
+function getContentSecurityPolicy(nonce?: string) {
+ let script_src: string;
+ if (typeof nonce === "string") {
+ script_src = `'self' 'report-sample' 'nonce-${nonce}'`;
+ } else if (process.env.NODE_ENV === "development") {
+ // Allow the <LiveReload /> component to load without a nonce in the error pages
+ script_src = "'self' 'report-sample'";
+ } else {
+ script_src = "'self' 'report-sample'";
+ }
+
+ const connect_src =
+ process.env.NODE_ENV === "development"
+ ? "'self' ws://localhost:*"
+ : "'self'";
+
+ return (
+ `script-src ${script_src} 'strict-dynamic'; ` +
+ `connect-src ${connect_src}; ` +
+ "form-action 'self'; " +
+ "object-src 'none'; " +
+ "block-all-mixed-content; " +
+ "base-uri 'self'; manifest-src 'self'"
+ );
+}
diff --git a/apps/monograph/app/root.tsx b/apps/monograph/app/root.tsx
index 424bc571e..8148e4961 100644
--- a/apps/monograph/app/root.tsx
+++ b/apps/monograph/app/root.tsx
@@ -22,14 +22,22 @@ import {
Meta,
Outlet,
Scripts,
- ScrollRestoration
+ ScrollRestoration,
+ useLoaderData
} from "@remix-run/react";
import { BaseThemeProvider } from "./components/theme-provider";
import { Buffer } from "buffer";
import { ThemeDark, themeToCSS } from "@notesnook/theme";
+import { LoaderFunction } from "@remix-run/node";
globalThis.Buffer = Buffer;
+type RootLoaderData = { cspScriptNonce: string };
+export const loader: LoaderFunction = async () => {
+ const crypto = await import("node:crypto");
+ return { cspScriptNonce: crypto.randomBytes(16).toString("hex") };
+};
+
export function Head() {
return (
<>
@@ -40,13 +48,17 @@ export function Head() {
<Links />
<style
id="theme-colors"
- dangerouslySetInnerHTML={{ __html: colorsToCSS() }}
+ dangerouslySetInnerHTML={{ __html: themeToCSS(ThemeDark) }}
/>
</>
);
}
export default function App() {
+ const data = useLoaderData<RootLoaderData>();
+ const cspScriptNonce =
+ typeof document === "undefined" ? data.cspScriptNonce : "";
+
return (
<>
<BaseThemeProvider
@@ -56,29 +68,8 @@ export default function App() {
>
<Outlet />
</BaseThemeProvider>
- <ScrollRestoration />
- <Scripts />
+ <ScrollRestoration nonce={cspScriptNonce} />
+ <Scripts nonce={cspScriptNonce} />
</>
);
}
-
-function colorsToCSS() {
- // const colorScheme = JSON.parse(
- // window.localStorage.getItem("colorScheme") || '"light"'
- // );
- // const root = document.querySelector("html");
- // if (root) root.setAttribute("data-theme", colorScheme);
-
- // const theme =
- // colorScheme === "dark"
- // ? JSON.parse(window.localStorage.getItem("theme:dark") || "false") ||
- // ThemeDark
- // : JSON.parse(window.localStorage.getItem("theme:light") || "false") ||
- // ThemeLight;
- // const stylesheet = document.getElementById("theme-colors");
- // if (theme) {
- // const css = ;
- // if (stylesheet) stylesheet.innerHTML = css;
- // } else stylesheet?.remove();
- return themeToCSS(ThemeDark);
-}