Skip to content

Commit

Permalink
Restore 3rd party session cookies for localhost (for Chromium & FF) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF authored Dec 19, 2024
1 parent 4dd9a0f commit 4f17e7c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/authentication/session.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class SessionInterceptor implements NestInterceptor {
}

private getTokenFromCookie(req: IRequest | undefined): string | null {
return req?.cookies?.[this.config.sessionCookie.name] || null;
return req?.cookies?.[this.config.sessionCookie(req).name] || null;
}

getImpersonateeFromContext(
Expand Down
4 changes: 3 additions & 1 deletion src/components/authentication/session.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export class SessionResolver {
const userFromSession = session.anonymous ? undefined : session.userId;

if (browser) {
const { name, expires, ...options } = this.config.sessionCookie;
const { name, expires, ...options } = this.config.sessionCookie(
context.request!,
);
if (!context.response) {
throw new ServerException(
'Cannot use cookie session without a response object',
Expand Down
17 changes: 10 additions & 7 deletions src/core/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ProgressReportStatus } from '../../components/progress-report/dto/progr
import type { TransitionName as ProgressReportTransitionName } from '../../components/progress-report/workflow/transitions';
import { DefaultTimezoneWrapper } from '../email/templates/formatted-date-time';
import { FrontendUrlWrapper } from '../email/templates/frontend-url';
import type { CookieOptions, CorsOptions } from '../http';
import type { CookieOptions, CorsOptions, IRequest } from '../http';
import { LogLevel } from '../logger/logger.interface';
import { EnvironmentService } from './environment.service';
import { determineRootUser } from './root-user.config';
Expand Down Expand Up @@ -252,10 +252,9 @@ export const makeConfig = (env: EnvironmentService) =>
} satisfies CorsOptions;
})();

sessionCookie = ((): Merge<
CookieOptions,
{ name: string; expires?: DurationLike }
> => {
sessionCookie = (
req: IRequest,
): Merge<CookieOptions, { name: string; expires?: DurationLike }> => {
const name = env.string('SESSION_COOKIE_NAME').optional('cordsession');

let domain = env.string('SESSION_COOKIE_DOMAIN').optional();
Expand All @@ -265,6 +264,10 @@ export const makeConfig = (env: EnvironmentService) =>
domain = '.' + domain;
}

const userAgent = req.headers['user-agent'];
const isSafari =
userAgent && /^((?!chrome|android).)*safari/i.test(userAgent);

return {
name,
domain,
Expand All @@ -274,14 +277,14 @@ export const makeConfig = (env: EnvironmentService) =>
httpOnly: true,
// All paths, not just the current one
path: '/',
...(!isDev && {
...(!(isSafari && isDev) && {
// Require HTTPS (required for SameSite)
secure: true,
// Allow 3rd party (other domains)
sameSite: 'none',
}),
};
})();
};

xray = {
daemonAddress: this.jest
Expand Down

0 comments on commit 4f17e7c

Please sign in to comment.