nsarrazin HF Staff commited on
Commit
ff0caae
·
unverified ·
1 Parent(s): a11801f

feat(security): add cookie samesite & security settings (#1517)

Browse files
Files changed (3) hide show
  1. .env +2 -0
  2. chart/env/prod.yaml +2 -0
  3. src/lib/server/auth.ts +12 -2
.env CHANGED
@@ -6,6 +6,8 @@ MONGODB_DB_NAME=chat-ui
6
  MONGODB_DIRECT_CONNECTION=false
7
 
8
  COOKIE_NAME=hf-chat
 
 
9
  TRUSTED_EMAIL_HEADER= # only set this if you understand the implications
10
 
11
  HF_TOKEN=#hf_<token> from https://huggingface.co/settings/token
 
6
  MONGODB_DIRECT_CONNECTION=false
7
 
8
  COOKIE_NAME=hf-chat
9
+ COOKIE_SAMESITE=
10
+ COOKIE_SECURE=
11
  TRUSTED_EMAIL_HEADER= # only set this if you understand the implications
12
 
13
  HF_TOKEN=#hf_<token> from https://huggingface.co/settings/token
chart/env/prod.yaml CHANGED
@@ -32,6 +32,8 @@ envVars:
32
  APP_BASE: "/chat"
33
  ALLOW_IFRAME: "false"
34
  COMMUNITY_TOOLS: "true"
 
 
35
  ENABLE_ASSISTANTS: "true"
36
  ENABLE_ASSISTANTS_RAG: "true"
37
  EXPOSE_API: "true"
 
32
  APP_BASE: "/chat"
33
  ALLOW_IFRAME: "false"
34
  COMMUNITY_TOOLS: "true"
35
+ COOKIE_SAMESITE: "strict"
36
+ COOKIE_SECURE: "true"
37
  ENABLE_ASSISTANTS: "true"
38
  ENABLE_ASSISTANTS_RAG: "true"
39
  EXPOSE_API: "true"
src/lib/server/auth.ts CHANGED
@@ -47,12 +47,22 @@ export const OIDConfig = z
47
 
48
  export const requiresUser = !!OIDConfig.CLIENT_ID && !!OIDConfig.CLIENT_SECRET;
49
 
 
 
 
 
 
 
 
 
 
 
50
  export function refreshSessionCookie(cookies: Cookies, sessionId: string) {
51
  cookies.set(env.COOKIE_NAME, sessionId, {
52
  path: "/",
53
  // So that it works inside the space's iframe
54
- sameSite: dev || env.ALLOW_INSECURE_COOKIES === "true" ? "lax" : "none",
55
- secure: !dev && !(env.ALLOW_INSECURE_COOKIES === "true"),
56
  httpOnly: true,
57
  expires: addWeeks(new Date(), 2),
58
  });
 
47
 
48
  export const requiresUser = !!OIDConfig.CLIENT_ID && !!OIDConfig.CLIENT_SECRET;
49
 
50
+ const sameSite = z
51
+ .enum(["lax", "none", "strict"])
52
+ .default(dev || env.ALLOW_INSECURE_COOKIES === "true" ? "lax" : "none")
53
+ .parse(env.COOKIE_SAMESITE === "" ? undefined : env.COOKIE_SAMESITE);
54
+
55
+ const secure = z
56
+ .boolean()
57
+ .default(!(dev || env.ALLOW_INSECURE_COOKIES === "true"))
58
+ .parse(env.COOKIE_SECURE === "" ? undefined : env.COOKIE_SECURE === "true");
59
+
60
  export function refreshSessionCookie(cookies: Cookies, sessionId: string) {
61
  cookies.set(env.COOKIE_NAME, sessionId, {
62
  path: "/",
63
  // So that it works inside the space's iframe
64
+ sameSite,
65
+ secure,
66
  httpOnly: true,
67
  expires: addWeeks(new Date(), 2),
68
  });