Skip to content

An AI-powered search engine with a generative UI

fix: preserve equals signs in cookie values (#1095)

Tianlang (Jackie) Chene674d3b52159
diff --git a/lib/utils/__tests__/cookies.test.ts b/lib/utils/__tests__/cookies.test.ts
new file mode 100644
index 00000000..fa97694d
--- /dev/null
+++ b/lib/utils/__tests__/cookies.test.ts
@@ -0,0 +1,15 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { getCookie } from '../cookies'
+
+describe('getCookie', () => {
+ afterEach(() => vi.unstubAllGlobals())
+
+ it('preserves equals signs in a cookie value', () => {
+ vi.stubGlobal('document', { cookie: 'token=abc==; searchMode=quick' })
+
+ expect(getCookie('token')).toBe('abc==')
+ expect(getCookie('searchMode')).toBe('quick')
+ expect(getCookie('missing')).toBeNull()
+ })
+})
diff --git a/lib/utils/cookies.ts b/lib/utils/cookies.ts
index a758e7a2..dbaf654c 100644
--- a/lib/utils/cookies.ts
+++ b/lib/utils/cookies.ts
@@ -15,9 +15,10 @@ export function getCookie(name: string): string | null {
const cookies = document.cookie.split(';')
for (const cookie of cookies) {
- const [cookieName, cookieValue] = cookie.trim().split('=')
- if (cookieName === name) {
- return cookieValue
+ const entry = cookie.trim()
+ const separator = entry.indexOf('=')
+ if (separator !== -1 && entry.slice(0, separator) === name) {
+ return entry.slice(separator + 1)
}
}
return null