Skip to content

Commit

Permalink
Merge branch 'feat/resource-add-integrity' into deploy/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoel committed Sep 14, 2024
2 parents dbe759d + c6a82a6 commit e3257b7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
11 changes: 10 additions & 1 deletion web/app/components/base/ga/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FC } from 'react'
import React from 'react'
import Script from 'next/script'
import { headers } from 'next/headers'
import { IS_CE_EDITION } from '@/config'

export enum GaType {
Expand All @@ -23,9 +24,16 @@ const GA: FC<IGAProps> = ({
if (IS_CE_EDITION)
return null

const nonce = process.env.NODE_ENV === 'production' ? headers().get('x-nonce') : ''

return (
<>
<Script strategy="beforeInteractive" async src={`https://www.googletagmanager.com/gtag/js?id=${gaIdMaps[gaType]}`}></Script>
<Script
strategy="beforeInteractive"
async
src={`https://www.googletagmanager.com/gtag/js?id=${gaIdMaps[gaType]}`}
nonce={nonce!}
></Script>
<Script
id="ga-init"
dangerouslySetInnerHTML={{
Expand All @@ -36,6 +44,7 @@ gtag('js', new Date());
gtag('config', '${gaIdMaps[gaType]}');
`,
}}
nonce={nonce!}
>
</Script>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const VarReferencePicker: FC<Props> = ({
const handleVarReferenceChange = useCallback((value: ValueSelector, varInfo: Var) => {
// sys var not passed to backend
const newValue = produce(value, (draft) => {
if (draft[1] && draft[1].startsWith('sys')) {
if (draft[1] && draft[1].startsWith('sys.')) {
draft.shift()
const paths = draft[0].split('.')
paths.forEach((p, i) => {
Expand Down
67 changes: 67 additions & 0 deletions web/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

export function middleware(request: NextRequest) {
const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
// style-src 'self' 'nonce-${nonce}';
const csp = process.env.NODE_ENV === 'production' ? `'nonce-${nonce}'` : '\'unsafe-eval\' \'unsafe-inline\''

const cspHeader = `
default-src 'self';
connect-src 'self' https://cloud.dify.dev/ https://cloud.dify.ai/ https://analytics.google.com ;
script-src 'self' ${csp} https://www.googletagmanager.com;
style-src 'self' ${csp};
img-src 'self' blob: data:;
font-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests;
`
// Replace newline characters and spaces
const contentSecurityPolicyHeaderValue = cspHeader
.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).*)',
source: '/((?!_next/static|_next/image|favicon.ico).*)',
// source: '/(.*)',
// missing: [
// { type: 'header', key: 'next-router-prefetch' },
// { type: 'header', key: 'purpose', value: 'prefetch' },
// ],
},
],
}

0 comments on commit e3257b7

Please sign in to comment.