-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dsfr): ajout de la config pour supprimer le
insafe-inline
des `…
…scripts` dans les `csp` (#6151)
- Loading branch information
Showing
3 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters