Skip to content

Commit

Permalink
fix(dsfr): ajout de la config pour supprimer le insafe-inline des `…
Browse files Browse the repository at this point in the history
…scripts` dans les `csp` (#6151)
  • Loading branch information
maxgfr authored Sep 26, 2024
1 parent 1935bcf commit b250d45
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/code-du-travail-frontend/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { defaultColorScheme } from "../src/modules/config/defaultColorScheme";
import { StartDsfr } from "../src/modules/config/StartDsfr";
import { Metadata } from "next/types";
import { SITE_URL } from "../src/config";
import { headers } from "next/headers";

export const metadata: Metadata = {
title: {
template: "%s - Code du travail numérique",
default: "Code du travail numérique",
},
description: "Posez votre question sur le droit du travail et obtenez une réponse personnalisée à vos questions (contrat de travail, congés payés, formation, démission, indemnités).",
description:
"Posez votre question sur le droit du travail et obtenez une réponse personnalisée à vos questions (contrat de travail, congés payés, formation, démission, indemnités).",
metadataBase: new URL(SITE_URL),
twitter: {
card: "summary",
Expand All @@ -26,6 +28,7 @@ export default function RootLayout({
children: React.ReactNode;
}) {
const lang = "fr";
const nonce = headers().get("x-nonce") ?? undefined;

return (
<html {...getHtmlAttributes({ defaultColorScheme, lang })}>
Expand All @@ -45,6 +48,7 @@ export default function RootLayout({
//"Spectral-Regular",
//"Spectral-ExtraBold"
]}
nonce={nonce}
/>
</head>
<body>
Expand Down
57 changes: 57 additions & 0 deletions packages/code-du-travail-frontend/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { NextResponse } from "next/server";

export function middleware(request) {
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
const ContentSecurityPolicy = `
img-src 'self' https://travail-emploi.gouv.fr https://www.service-public.fr https://cdtn-prod-public.s3.gra.io.cloud.ovh.net https://matomo.fabrique.social.gouv.fr data:;
script-src 'self' 'nonce-${nonce}' https://mon-entreprise.urssaf.fr https://matomo.fabrique.social.gouv.fr ${
process.env.NEXT_PUBLIC_APP_ENV !== "production" && "'unsafe-eval'"
};
frame-src 'self' https://mon-entreprise.urssaf.fr https://matomo.fabrique.social.gouv.fr *.dailymotion.com;
connect-src 'self' https://geo.api.gouv.fr https://sentry.fabrique.social.gouv.fr https://matomo.fabrique.social.gouv.fr;
worker-src 'self' blob:;
`;
// Replace newline characters and spaces
const contentSecurityPolicyHeaderValue = ContentSecurityPolicy.replace(
/\s{2,}/g,
" "
).trim();

const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-nonce", nonce);
requestHeaders.set(
"Content-Security-Policy",
contentSecurityPolicyHeaderValue
);

const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
response.headers.set(
"Content-Security-Policy",
contentSecurityPolicyHeaderValue
);

return response;
}

export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
{
source: "/((?!api|_next/static|_next/image|favicon.ico).*)",
missing: [
{ type: "header", key: "next-router-prefetch" },
{ type: "header", key: "purpose", value: "prefetch" },
],
},
],
};
2 changes: 1 addition & 1 deletion packages/code-du-travail-frontend/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MappingReplacement from "./redirects.json" assert { type: "json" };

const ContentSecurityPolicy = `
img-src 'self' https://travail-emploi.gouv.fr https://www.service-public.fr https://cdtn-prod-public.s3.gra.io.cloud.ovh.net https://matomo.fabrique.social.gouv.fr data:;
script-src 'self' https://mon-entreprise.urssaf.fr https://matomo.fabrique.social.gouv.fr 'unsafe-inline' https://tally.so ${
script-src 'self' https://mon-entreprise.urssaf.fr https://matomo.fabrique.social.gouv.fr https://tally.so ${
process.env.NEXT_PUBLIC_APP_ENV !== "production" && "'unsafe-eval'"
};
frame-src 'self' https://mon-entreprise.urssaf.fr https://matomo.fabrique.social.gouv.fr *.dailymotion.com https://tally.so;
Expand Down

0 comments on commit b250d45

Please sign in to comment.