Spaces:
Sleeping
Sleeping
tumberger commited on
Commit ·
22da39f
1
Parent(s): 0836b60
Add security headers to allow Hugging Face iframe embedding
Browse files- app/(auth)/auth.config.ts +0 -28
- middleware.ts +2 -14
- next.config.ts +33 -0
app/(auth)/auth.config.ts
CHANGED
|
@@ -10,32 +10,4 @@ export const authConfig = {
|
|
| 10 |
// while this file is also used in non-Node.js environments
|
| 11 |
],
|
| 12 |
callbacks: {},
|
| 13 |
-
cookies: {
|
| 14 |
-
sessionToken: {
|
| 15 |
-
name: `__Secure-next-auth.session-token`,
|
| 16 |
-
options: {
|
| 17 |
-
httpOnly: true,
|
| 18 |
-
sameSite: 'none',
|
| 19 |
-
path: '/',
|
| 20 |
-
secure: true,
|
| 21 |
-
},
|
| 22 |
-
},
|
| 23 |
-
callbackUrl: {
|
| 24 |
-
name: `__Secure-next-auth.callback-url`,
|
| 25 |
-
options: {
|
| 26 |
-
sameSite: 'none',
|
| 27 |
-
path: '/',
|
| 28 |
-
secure: true,
|
| 29 |
-
},
|
| 30 |
-
},
|
| 31 |
-
csrfToken: {
|
| 32 |
-
name: `__Host-next-auth.csrf-token`,
|
| 33 |
-
options: {
|
| 34 |
-
httpOnly: true,
|
| 35 |
-
sameSite: 'none',
|
| 36 |
-
path: '/',
|
| 37 |
-
secure: true,
|
| 38 |
-
},
|
| 39 |
-
},
|
| 40 |
-
},
|
| 41 |
} satisfies NextAuthConfig;
|
|
|
|
| 10 |
// while this file is also used in non-Node.js environments
|
| 11 |
],
|
| 12 |
callbacks: {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
} satisfies NextAuthConfig;
|
middleware.ts
CHANGED
|
@@ -20,27 +20,15 @@ export async function middleware(request: NextRequest) {
|
|
| 20 |
const token = await getToken({
|
| 21 |
req: request,
|
| 22 |
secret: process.env.AUTH_SECRET,
|
| 23 |
-
secureCookie:
|
| 24 |
-
cookieName: '__Secure-next-auth.session-token',
|
| 25 |
});
|
| 26 |
|
| 27 |
if (!token) {
|
| 28 |
const redirectUrl = encodeURIComponent(request.url);
|
| 29 |
-
|
| 30 |
-
// Allow a few retries before redirecting to avoid loops
|
| 31 |
-
const retryCount = request.cookies.get('auth-retry')?.value || '0';
|
| 32 |
-
if (parseInt(retryCount) > 2) {
|
| 33 |
-
// Clear retry count and continue without auth
|
| 34 |
-
const response = NextResponse.next();
|
| 35 |
-
response.cookies.delete('auth-retry');
|
| 36 |
-
return response;
|
| 37 |
-
}
|
| 38 |
|
| 39 |
-
|
| 40 |
new URL(`/api/auth/guest?redirectUrl=${redirectUrl}`, request.url),
|
| 41 |
);
|
| 42 |
-
response.cookies.set('auth-retry', String(parseInt(retryCount) + 1));
|
| 43 |
-
return response;
|
| 44 |
}
|
| 45 |
|
| 46 |
const isGuest = guestRegex.test(token?.email ?? '');
|
|
|
|
| 20 |
const token = await getToken({
|
| 21 |
req: request,
|
| 22 |
secret: process.env.AUTH_SECRET,
|
| 23 |
+
secureCookie: !isDevelopmentEnvironment,
|
|
|
|
| 24 |
});
|
| 25 |
|
| 26 |
if (!token) {
|
| 27 |
const redirectUrl = encodeURIComponent(request.url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
return NextResponse.redirect(
|
| 30 |
new URL(`/api/auth/guest?redirectUrl=${redirectUrl}`, request.url),
|
| 31 |
);
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
const isGuest = guestRegex.test(token?.email ?? '');
|
next.config.ts
CHANGED
|
@@ -12,6 +12,39 @@ const nextConfig: NextConfig = {
|
|
| 12 |
},
|
| 13 |
],
|
| 14 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
};
|
| 16 |
|
| 17 |
export default nextConfig;
|
|
|
|
| 12 |
},
|
| 13 |
],
|
| 14 |
},
|
| 15 |
+
async headers() {
|
| 16 |
+
return [
|
| 17 |
+
{
|
| 18 |
+
source: '/:path*',
|
| 19 |
+
headers: [
|
| 20 |
+
{
|
| 21 |
+
key: 'Content-Security-Policy',
|
| 22 |
+
value: 'frame-ancestors https://huggingface.co https://*.hf.space https://seriousmountain-vercel-demo.hf.space;',
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
key: 'X-Frame-Options',
|
| 26 |
+
value: 'ALLOWALL',
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
key: 'Access-Control-Allow-Origin',
|
| 30 |
+
value: 'https://huggingface.co',
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
key: 'Access-Control-Allow-Credentials',
|
| 34 |
+
value: 'true',
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
key: 'Access-Control-Allow-Methods',
|
| 38 |
+
value: 'GET, POST, PUT, DELETE, OPTIONS',
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
key: 'Access-Control-Allow-Headers',
|
| 42 |
+
value: 'X-Requested-With, Content-Type, Authorization',
|
| 43 |
+
},
|
| 44 |
+
],
|
| 45 |
+
},
|
| 46 |
+
];
|
| 47 |
+
},
|
| 48 |
};
|
| 49 |
|
| 50 |
export default nextConfig;
|