Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition for concurrent /auth/refresh calls #22433

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-apples-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

Check warning on line 1 in .changeset/hot-apples-greet.md

View workflow job for this annotation

GitHub Actions / Lint

File ignored by default.
'@directus/api': patch
---

Fixed a race condition that occurred when multiple browser tabs refresh at the same time, whereby a user could unintentionally lose the session
6 changes: 2 additions & 4 deletions api/src/services/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Accountability, SchemaOverview } from '@directus/types';
import jwt from 'jsonwebtoken';
import type { Knex } from 'knex';
import { clone, cloneDeep } from 'lodash-es';
import { nanoid } from 'nanoid';
import { performance } from 'perf_hooks';
import { getAuthProvider } from '../auth.js';
import { DEFAULT_AUTH_PROVIDER } from '../constants.js';
Expand Down Expand Up @@ -56,8 +57,6 @@ export class AuthenticationService {
session: boolean;
}>,
): Promise<LoginResult> {
const { nanoid } = await import('nanoid');

const STALL_TIME = env['LOGIN_STALL_TIME'] as number;
const timeStart = performance.now();

Expand Down Expand Up @@ -274,7 +273,6 @@ export class AuthenticationService {
}

async refresh(refreshToken: string, options?: Partial<{ session: boolean }>): Promise<LoginResult> {
const { nanoid } = await import('nanoid');
const STALL_TIME = env['LOGIN_STALL_TIME'] as number;
const timeStart = performance.now();

Expand Down Expand Up @@ -357,7 +355,7 @@ export class AuthenticationService {
});
}

const newRefreshToken = nanoid(64);
const newRefreshToken = options?.session ? refreshToken : nanoid(64);
const refreshTokenExpiration = new Date(Date.now() + getMilliseconds(env['REFRESH_TOKEN_TTL'], 0));

const tokenPayload: DirectusTokenPayload = {
Expand Down