@@ -2,7 +2,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22import {
33 type OrySessionTokens ,
44 openOrySession ,
5+ orySessionCookieDeleteOptions ,
56 orySessionCookieOptions ,
7+ resolveSessionCookieDomain ,
68 sealOrySession ,
79} from '@/core/server/auth/ory/session-cookie'
810
@@ -79,3 +81,56 @@ describe('e2b_session cookie', () => {
7981 expect ( orySessionCookieOptions ( ) . secure ) . toBe ( false )
8082 } )
8183} )
84+
85+ describe ( 'e2b_session cookie domain' , ( ) => {
86+ beforeEach ( ( ) => {
87+ vi . stubEnv ( 'NEXT_PUBLIC_E2B_DOMAIN' , 'e2b-staging.dev' )
88+ } )
89+
90+ afterEach ( ( ) => {
91+ vi . unstubAllEnvs ( )
92+ } )
93+
94+ it ( 'scopes a subdomain host to the parent domain' , ( ) => {
95+ expect ( resolveSessionCookieDomain ( 'dashboard.e2b-staging.dev' ) ) . toBe (
96+ '.e2b-staging.dev'
97+ )
98+ } )
99+
100+ it ( 'scopes the apex host to the parent domain' , ( ) => {
101+ expect ( resolveSessionCookieDomain ( 'e2b-staging.dev' ) ) . toBe (
102+ '.e2b-staging.dev'
103+ )
104+ } )
105+
106+ it ( 'ignores the port when matching' , ( ) => {
107+ expect ( resolveSessionCookieDomain ( 'e2b-staging.dev:3000' ) ) . toBe (
108+ '.e2b-staging.dev'
109+ )
110+ } )
111+
112+ it ( 'returns no domain for unrelated hosts (localhost, previews)' , ( ) => {
113+ expect ( resolveSessionCookieDomain ( 'localhost' ) ) . toBeUndefined ( )
114+ expect ( resolveSessionCookieDomain ( 'preview.vercel.app' ) ) . toBeUndefined ( )
115+ // A suffix that is not a domain boundary must not match.
116+ expect ( resolveSessionCookieDomain ( 'evil-e2b-staging.dev' ) ) . toBeUndefined ( )
117+ } )
118+
119+ it ( 'returns no domain when the env is unset' , ( ) => {
120+ vi . stubEnv ( 'NEXT_PUBLIC_E2B_DOMAIN' , '' )
121+ expect (
122+ resolveSessionCookieDomain ( 'dashboard.e2b-staging.dev' )
123+ ) . toBeUndefined ( )
124+ } )
125+
126+ it ( 'flows the resolved domain into set and delete options' , ( ) => {
127+ expect ( orySessionCookieOptions ( 'app.e2b-staging.dev' ) . domain ) . toBe (
128+ '.e2b-staging.dev'
129+ )
130+ expect ( orySessionCookieDeleteOptions ( 'app.e2b-staging.dev' ) ) . toEqual ( {
131+ name : 'e2b_session' ,
132+ path : '/' ,
133+ domain : '.e2b-staging.dev' ,
134+ } )
135+ } )
136+ } )
0 commit comments