Skip to content

The open alternative to Salesforce, designed for AI.

3.9 KB
Raw Blame
import {
  extractSessionCookie,
  hasClearingCookie,
  normalizeSessionCookieForSnapshot,
  postMetadataOperationWithHeaders,
  signInWithCookieCapture,
} from 'test/integration/graphql/suites/auth/user-sessions/utils/sign-in-with-cookie-capture.util';
import {
  currentUserIdentityQueryFactory,
  signOutQueryFactory,
} from 'test/integration/graphql/suites/auth/user-sessions/utils/user-session-operations.util';

import {
  ALLOWED_ORIGIN,
  DISALLOWED_ORIGIN,
} from 'test/integration/graphql/suites/auth/user-sessions/constants/session-origins.constants';
import { IS_SECURE_DEPLOYMENT } from 'test/integration/graphql/suites/auth/user-sessions/constants/is-secure-deployment.constant';

import { USER_SESSION_COOKIE_NAME } from 'src/engine/core-modules/user-session/constants/user-session-cookie-name.constant';
import { USER_SESSION_SECURE_COOKIE_NAME } from 'src/engine/core-modules/user-session/constants/user-session-secure-cookie-name.constant';

// Needs an https SERVER_URL (env-only, read by isSecureDeployment()): run via `nx run twenty-server:test:integration:secure`.
describe('session cookie on a production-like secure deployment (integration)', () => {
  let sessionToken: string;

  beforeAll(() => {
    if (!IS_SECURE_DEPLOYMENT) {
      throw new Error(
        'This suite requires an https SERVER_URL; run it via nx run twenty-server:test:integration:secure',
      );
    }
  });

  it('should deliver the production cookie: __Host- name, Secure, SameSite=Lax', async () => {
    const response = await signInWithCookieCapture({
      originHeader: ALLOWED_ORIGIN,
    });

    expect(
      extractSessionCookie(response, USER_SESSION_COOKIE_NAME),
    ).toBeUndefined();

    const secureSessionCookie = extractSessionCookie(
      response,
      USER_SESSION_SECURE_COOKIE_NAME,
    );

    expect(secureSessionCookie).toBeDefined();

    if (!secureSessionCookie) {
      throw new Error('Expected the secure session cookie');
    }

    sessionToken = secureSessionCookie.sessionToken;

    // Not a snapshot: this dedicated run's snapshots would be obsolete in the plain-http run.
    expect(
      normalizeSessionCookieForSnapshot(secureSessionCookie.rawCookie),
    ).toBe(
      '__Host-twenty-session=sess_<redacted>; Path=/; Expires=<redacted>; HttpOnly; Secure; SameSite=Lax',
    );
  });

  it('should authenticate a request presenting the __Host- cookie', async () => {
    const response = await postMetadataOperationWithHeaders(
      currentUserIdentityQueryFactory(),
      {
        originHeader: ALLOWED_ORIGIN,
        cookieHeader: `${USER_SESSION_SECURE_COOKIE_NAME}=${sessionToken}`,
      },
    );

    expect(response.body.errors).toBeUndefined();
    expect(response.body.data.currentUser.email).toBe('[email protected]');
  });

  it('should ignore the plain cookie name, refusing a downgrade', async () => {
    const response = await postMetadataOperationWithHeaders(
      currentUserIdentityQueryFactory(),
      {
        originHeader: ALLOWED_ORIGIN,
        cookieHeader: `${USER_SESSION_COOKIE_NAME}=${sessionToken}`,
      },
    );

    expect(response.body.errors).toBeDefined();
  });

  it('should enforce the CSRF origin gate on __Host- cookie requests', async () => {
    const response = await postMetadataOperationWithHeaders(
      currentUserIdentityQueryFactory(),
      {
        originHeader: DISALLOWED_ORIGIN,
        cookieHeader: `${USER_SESSION_SECURE_COOKIE_NAME}=${sessionToken}`,
      },
      403,
    );

    expect(response.body.error).toBe('CSRF_ORIGIN_MISMATCH');
  });

  it('should clear the __Host- cookie on sign-out', async () => {
    const response = await postMetadataOperationWithHeaders(
      signOutQueryFactory(),
      {
        originHeader: ALLOWED_ORIGIN,
        cookieHeader: `${USER_SESSION_SECURE_COOKIE_NAME}=${sessionToken}`,
      },
    );

    expect(response.body.data.signOut).toBe(true);
    expect(hasClearingCookie(response, USER_SESSION_SECURE_COOKIE_NAME)).toBe(
      true,
    );
  });
});