diff --git a/src/entities/sdk-setup.test.ts b/src/entities/sdk-setup.test.ts index d25226e7..68e782e9 100644 --- a/src/entities/sdk-setup.test.ts +++ b/src/entities/sdk-setup.test.ts @@ -1,4 +1,5 @@ import { loginRead } from '@pulp-ui/sdk/core/login'; +import { rolesList } from '@pulp-ui/sdk/core/roles'; import { statusRead } from '@pulp-ui/sdk/core/status'; import { tasksDelete, tasksRead } from '@pulp-ui/sdk/core/tasks'; import { HttpResponse, http } from 'msw'; @@ -243,6 +244,19 @@ describe('the sdk and the session', () => { return { arrived, release }; } + it('asks who is signed in nothing for a refused read of the roles list, and leaves the refusal to its caller', async () => { + setSignedIn(true); + refuse('*/pulp/api/v3/roles/', 403, 'Forbidden here.'); + const logins = recordHits('*/pulp/api/v3/login/'); + + const error = await rolesList({ fields: ['name'] }).catch((e) => e); + + expect(requestErrorStatus(error)).toBe(403); + expect(isSessionEnd(error)).toBe(false); + expect(logins).toHaveLength(0); + expect(getSnapshot().ended).toBe(false); + }); + it('ignores the 401 of a call sent before the session was resumed', async () => { setSignedIn(true); const held = holdAnswer('*/pulp/api/v3/status/', 401); diff --git a/src/session/request-policy.test.ts b/src/session/request-policy.test.ts index 33af2561..2d1d42b6 100644 --- a/src/session/request-policy.test.ts +++ b/src/session/request-policy.test.ts @@ -320,6 +320,53 @@ describe('handleFailure', () => { expect(check).not.toHaveBeenCalled(); }); + it.each([ + '/pulp/api/v3/roles/?fields=name%2Cpermissions&limit=1000', + '/pulp/api/v3/roles/', + 'roles/?limit=1', + ])( + 'asks nothing for a 403 of a read of the roles list (%s), which is the answer to what the account may read', + async (url) => { + setSignedIn(true); + const check = vi.fn(); + const error = answer(403); + + await handleFailure( + 403, + url, + error, + check, + prepareRequest(url, 'GET', new Headers()), + ); + + expect(check).not.toHaveBeenCalled(); + expect(isMarkedSessionEnd(error)).toBe(false); + expect(getSnapshot().ended).toBe(false); + }, + ); + + it.each([ + ['a read of one role', 'GET', '/pulp/api/v3/roles/0001/'], + ['the roles of a user', 'GET', '/pulp/api/v3/users/1/roles/'], + ['the roles of a group', 'GET', '/pulp/api/v3/groups/1/roles/'], + ['a role being made', 'POST', '/pulp/api/v3/roles/'], + ['a role being removed', 'DELETE', '/pulp/api/v3/roles/'], + ['a list that only ends alike', 'GET', '/pulp/api/v3/things/roles/'], + ])('still asks about the session for a 403 of %s', async (_, method, url) => { + setSignedIn(true); + const check = vi.fn(() => Promise.resolve({ username: 'zoe' })); + + await handleFailure( + 403, + url, + answer(403), + check, + prepareRequest(url, method, new Headers()), + ); + + expect(check).toHaveBeenCalledTimes(1); + }); + it('shares one check between refusals that arrive together, whoever asks', async () => { setSignedIn(true); let finish: (value: unknown) => void = () => undefined; diff --git a/src/session/request-policy.ts b/src/session/request-policy.ts index 5cd720fd..fef9a86f 100644 --- a/src/session/request-policy.ts +++ b/src/session/request-policy.ts @@ -74,6 +74,21 @@ export function isLoginUrl(url: string): boolean { return path.slice(prefix?.length ?? 0).startsWith('login/'); } +// Whether the URL is the list of roles. Whoever may not read it is refused, +// and that refusal is the answer the caller asked for (what the account may +// read), not a sign that the session ended: a screen reads this list to learn +// what an account may do, and a limited account is refused it by the server's +// access rules. Only the list itself counts, not one role and not the roles +// of a user or group, which are asked for other reasons. +export function isRolesListUrl(url: string): boolean { + const path = collapseDoubleSlash(url); + const prefix = [config.API_BASE_PATH, SCHEMA_API_ROOT].find((candidate) => + path.startsWith(candidate), + ); + + return (path.slice(prefix?.length ?? 0).split('?')[0] ?? '') === 'roles/'; +} + // The methods the server checks the CSRF token of: those that change state. const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']); @@ -166,6 +181,9 @@ const isForOlderSession = (context: IRequestContext | undefined) => // who-is-signed-in request, made by `checkSession`, tells them apart: a 2xx // answer means the refusal is real and the error reaches the caller // untouched; a 401 or 403 answer means the session ended. +// The one read that is no occasion for that request is the roles list: its +// refusal is an answer, and any later request of an ended session is refused +// again and tells. export async function handleFailure( status: number | undefined, url: string, @@ -179,7 +197,11 @@ export async function handleFailure( if (status === 401) { reportUnauthorized('request'); - } else if (status === 403 && isSignedIn()) { + } else if ( + status === 403 && + isSignedIn() && + !(context?.method === 'GET' && isRolesListUrl(url)) + ) { if ((await sessionEnded(checkSession)) && !isForOlderSession(context)) { markSessionEnd(error); reportUnauthorized('request');