Better csp
This commit is contained in:
@@ -1,8 +1,19 @@
|
|||||||
import { App, csp, csrf, staticFiles } from "fresh";
|
import { App, csp, csrf, staticFiles } from "fresh";
|
||||||
|
|
||||||
import { define, type State } from "./utils.ts";
|
import { define, type State } from "./utils.ts";
|
||||||
|
import { UPLOAD_CSP } from "@/src/storage.ts";
|
||||||
|
|
||||||
export const app = new App<State>();
|
export const app = new App<State>();
|
||||||
|
|
||||||
|
// csp() overwrites any policy a route sets, so the stricter one for uploaded files is put back here
|
||||||
|
app.use(define.middleware(async (ctx) => {
|
||||||
|
const res = await ctx.next();
|
||||||
|
if (res.headers.get("Content-Disposition") === "attachment") {
|
||||||
|
res.headers.set("Content-Security-Policy", UPLOAD_CSP);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}));
|
||||||
|
|
||||||
// Vite's dev client injects CSS through <style> tags without a nonce
|
// Vite's dev client injects CSS through <style> tags without a nonce
|
||||||
app.use(csrf(), csp({ useNonce: !import.meta.env.DEV }));
|
app.use(csrf(), csp({ useNonce: !import.meta.env.DEV }));
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { FILE_UPLOAD_PATH } from "./server_constants.ts";
|
|||||||
|
|
||||||
export class UploadTooLargeError extends Error {}
|
export class UploadTooLargeError extends Error {}
|
||||||
|
|
||||||
|
export const UPLOAD_CSP = "sandbox; default-src 'none'";
|
||||||
|
|
||||||
function upload_path(storage_id: string) {
|
function upload_path(storage_id: string) {
|
||||||
if (!FILE_UPLOAD_PATH) {
|
if (!FILE_UPLOAD_PATH) {
|
||||||
throw new Error("FILE_UPLOAD_PATH isn't set");
|
throw new Error("FILE_UPLOAD_PATH isn't set");
|
||||||
@@ -49,9 +51,13 @@ export async function upload_response(storage_id: string) {
|
|||||||
if (!stored) {
|
if (!stored) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
// The body is whatever the uploader sent, so browsers must never render it on this origin
|
||||||
return new Response(stored.readable, {
|
return new Response(stored.readable, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/octet-stream",
|
"Content-Type": "application/octet-stream",
|
||||||
|
"Content-Disposition": "attachment",
|
||||||
|
"X-Content-Type-Options": "nosniff",
|
||||||
|
"Content-Security-Policy": UPLOAD_CSP,
|
||||||
"Cache-Control": "no-store",
|
"Cache-Control": "no-store",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user