From 46d2bf02c98902da7b344753372d48cfe0e5ebb3 Mon Sep 17 00:00:00 2001
From: Maxwell <145994855+ping-maxwell@users.noreply.github.com>
Date: Tue, 7 Jul 2026 10:08:47 +1000
Subject: [PATCH] fix: get-session should have `no-cache` cache control headers
(#10222)
---
.changeset/sixty-animals-brake.md | 5 ++
.../src/api/routes/session-api.test.ts | 73 +++++++++++++++++++
.../better-auth/src/api/routes/session.ts | 11 ++-
3 files changed, 88 insertions(+), 1 deletion(-)
create mode 100644 .changeset/sixty-animals-brake.md
diff --git a/.changeset/sixty-animals-brake.md b/.changeset/sixty-animals-brake.md
new file mode 100644
index 0000000000..3e03125cbf
--- /dev/null
+++ b/.changeset/sixty-animals-brake.md
@@ -0,0 +1,5 @@
+---
+"better-auth": patch
+---
+
+fix: add no-store cache-control headers to get-session route
diff --git a/packages/better-auth/src/api/routes/session-api.test.ts b/packages/better-auth/src/api/routes/session-api.test.ts
index 4f75fa6c50..393a9152ed 100644
--- a/packages/better-auth/src/api/routes/session-api.test.ts
+++ b/packages/better-auth/src/api/routes/session-api.test.ts
@@ -2433,3 +2433,76 @@ describe("forced strict session validation", async () => {
});
});
});
+
+describe("get-session cache headers", async () => {
+ /**
+ * @see https://github.com/better-auth/better-auth/issues/10217
+ */
+ it("sets Cache-Control: no-store on authenticated GET /get-session responses", async () => {
+ const { auth, client, testUser, cookieSetter } = await getTestInstance();
+
+ const headers = new Headers();
+ await client.signIn.email(
+ { email: testUser.email, password: testUser.password },
+ { onSuccess: cookieSetter(headers) },
+ );
+
+ const res = await auth.handler(
+ new Request("http://localhost:3000/api/auth/get-session", {
+ headers: { cookie: headers.get("cookie") || "" },
+ }),
+ );
+
+ expect(res.status).toBe(200);
+ const body = (await res.json()) as { user?: { id: string } } | null;
+ expect(body?.user?.id).toBeTruthy();
+ expect(res.headers.get("cache-control")).toContain("no-store");
+ });
+
+ /**
+ * @see https://github.com/better-auth/better-auth/issues/10217
+ */
+ it("sets Cache-Control: no-store on unauthenticated GET /get-session responses", async () => {
+ const { auth } = await getTestInstance();
+
+ const res = await auth.handler(
+ new Request("http://localhost:3000/api/auth/get-session"),
+ );
+
+ expect(res.status).toBe(200);
+ expect(await res.json()).toBeNull();
+ expect(res.headers.get("cache-control")).toContain("no-store");
+ });
+
+ /**
+ * @see https://github.com/better-auth/better-auth/pull/10222
+ */
+ it("does not set Cache-Control: no-store on session-gated endpoints", async () => {
+ const sessionGatedPlugin = {
+ id: "session-gated-cache-test",
+ endpoints: {
+ sessionGatedCheck: createAuthEndpoint(
+ "/session-gated-cache-check",
+ {
+ method: "GET",
+ use: [freshSessionMiddleware],
+ },
+ async () => ({ status: true }),
+ ),
+ },
+ };
+ const { auth, signInWithTestUser } = await getTestInstance({
+ plugins: [sessionGatedPlugin],
+ });
+ const { headers } = await signInWithTestUser();
+
+ const res = await auth.handler(
+ new Request("http://localhost:3000/api/auth/session-gated-cache-check", {
+ headers: { cookie: headers.get("cookie") || "" },
+ }),
+ );
+
+ expect(res.status).toBe(200);
+ expect(res.headers.get("cache-control")).toBeNull();
+ });
+});
diff --git a/packages/better-auth/src/api/routes/session.ts b/packages/better-auth/src/api/routes/session.ts
index e3c057bd52..1134250ac7 100644
--- a/packages/better-auth/src/api/routes/session.ts
+++ b/packages/better-auth/src/api/routes/session.ts
@@ -72,6 +72,9 @@ export const getSession =