Skip to content

Commit

Permalink
feat(security): add cookie samesite & security settings (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin authored Oct 15, 2024
1 parent 2ff5f51 commit 1786194
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ MONGODB_DB_NAME=chat-ui
MONGODB_DIRECT_CONNECTION=false

COOKIE_NAME=hf-chat
COOKIE_SAMESITE=
COOKIE_SECURE=
TRUSTED_EMAIL_HEADER= # only set this if you understand the implications

HF_TOKEN=#hf_<token> from https://huggingface.co/settings/token
Expand Down
2 changes: 2 additions & 0 deletions chart/env/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ envVars:
APP_BASE: "/chat"
ALLOW_IFRAME: "false"
COMMUNITY_TOOLS: "true"
COOKIE_SAMESITE: "strict"
COOKIE_SECURE: "true"
ENABLE_ASSISTANTS: "true"
ENABLE_ASSISTANTS_RAG: "true"
EXPOSE_API: "true"
Expand Down
14 changes: 12 additions & 2 deletions src/lib/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,22 @@ export const OIDConfig = z

export const requiresUser = !!OIDConfig.CLIENT_ID && !!OIDConfig.CLIENT_SECRET;

const sameSite = z
.enum(["lax", "none", "strict"])
.default(dev || env.ALLOW_INSECURE_COOKIES === "true" ? "lax" : "none")
.parse(env.COOKIE_SAMESITE === "" ? undefined : env.COOKIE_SAMESITE);

const secure = z
.boolean()
.default(!(dev || env.ALLOW_INSECURE_COOKIES === "true"))
.parse(env.COOKIE_SECURE === "" ? undefined : env.COOKIE_SECURE === "true");

export function refreshSessionCookie(cookies: Cookies, sessionId: string) {
cookies.set(env.COOKIE_NAME, sessionId, {
path: "/",
// So that it works inside the space's iframe
sameSite: dev || env.ALLOW_INSECURE_COOKIES === "true" ? "lax" : "none",
secure: !dev && !(env.ALLOW_INSECURE_COOKIES === "true"),
sameSite,
secure,
httpOnly: true,
expires: addWeeks(new Date(), 2),
});
Expand Down

0 comments on commit 1786194

Please sign in to comment.