Skip to content

Commit

Permalink
Move to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxachun committed Nov 12, 2024
1 parent 84cbbbc commit ca2d0e6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
43 changes: 10 additions & 33 deletions site/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NextResponse } from "next/server";

import { GQLRedirectScope } from "./graphql.generated";
import { createRedirects } from "./redirects/redirects";
import { configureResponse } from "./util/configureResponse";
import { getHostByHeaders, getSiteConfigForHost, getSiteConfigs } from "./util/siteConfig";

export async function middleware(request: NextRequest) {
Expand Down Expand Up @@ -50,41 +51,17 @@ export async function middleware(request: NextRequest) {
return NextResponse.rewrite(new URL(rewrite.destination, request.url));
}

const response = NextResponse.rewrite(
new URL(
`/${siteConfig.scope.domain}${request.nextUrl.pathname}${
request.nextUrl.searchParams.toString().length > 0 ? `?${request.nextUrl.searchParams.toString()}` : ""
}`,
request.url,
return configureResponse(
NextResponse.rewrite(
new URL(
`/${siteConfig.scope.domain}${request.nextUrl.pathname}${
request.nextUrl.searchParams.toString().length > 0 ? `?${request.nextUrl.searchParams.toString()}` : ""
}`,
request.url,
),
{ request: { headers } },
),
{ request: { headers } },
);

response.headers.set(
"Content-Security-Policy",
`
default-src 'self';
form-action 'self';
object-src 'none';
img-src 'self' https: data:${process.env.NODE_ENV === "development" ? " http:" : ""};
media-src 'self' https: data:${process.env.NODE_ENV === "development" ? " http:" : ""};
style-src 'self' 'unsafe-inline';
font-src 'self' https: data:;
script-src 'self' 'unsafe-inline' https:${process.env.NODE_ENV === "development" ? " 'unsafe-eval'" : ""};
connect-src 'self' https:${process.env.NODE_ENV === "development" ? " http:" : ""};
frame-ancestors ${process.env.ADMIN_URL};
upgrade-insecure-requests;
block-all-mixed-content;
frame-src 'self' https://*.youtube.com https://*.youtube-nocookie.com;
`
.replace(/\s{2,}/g, " ")
.trim(),
);
if (process.env.ADMIN_URL) {
response.headers.set("Access-Control-Allow-Origin", process.env.ADMIN_URL);
}

return response;
}

type RewritesMap = Map<string, Rewrite>;
Expand Down
28 changes: 28 additions & 0 deletions site/src/util/configureResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NextResponse } from "next/server";

export function configureResponse(response: NextResponse) {
response.headers.set(
"Content-Security-Policy1",
`
default-src 'self';
form-action 'self';
object-src 'none';
img-src 'self' https: data:${process.env.NODE_ENV === "development" ? " http:" : ""};
media-src 'self' https: data:${process.env.NODE_ENV === "development" ? " http:" : ""};
style-src 'self' 'unsafe-inline';
font-src 'self' https: data:;
script-src 'self' 'unsafe-inline' https:${process.env.NODE_ENV === "development" ? " 'unsafe-eval'" : ""};
connect-src 'self' https:${process.env.NODE_ENV === "development" ? " http:" : ""};
frame-ancestors ${process.env.ADMIN_URL ?? "none"};
upgrade-insecure-requests;
block-all-mixed-content;
frame-src 'self' https://*.youtube.com https://*.youtube-nocookie.com;
`
.replace(/\s{2,}/g, " ")
.trim(),
);
if (process.env.ADMIN_URL) {
response.headers.set("Access-Control-Allow-Origin", process.env.ADMIN_URL);
}
return response;
}

0 comments on commit ca2d0e6

Please sign in to comment.