Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions apps/frontend/src/components/layout/layout.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { FetchWrapperComponent } from '@gitroom/helpers/utils/custom.fetch';
import { deleteDialog } from '@gitroom/react/helpers/delete.dialog';
import { useReturnUrl } from '@gitroom/frontend/app/(app)/auth/return.url.component';
import { useVariables } from '@gitroom/react/helpers/variable.context';
import { shouldPreserveOAuthConsentUnauthorized } from './oauth-consent-unauthorized';
import {
shouldHandleGlobalLogout,
shouldPreserveOAuthConsentUnauthorized,
} from './oauth-consent-unauthorized';
export default function LayoutContext(params: { children: ReactNode }) {
if (params?.children) {
// eslint-disable-next-line react/no-children-prop
Expand Down Expand Up @@ -43,6 +46,15 @@ function LayoutContextInner(params: { children: ReactNode }) {
response?.headers?.get('Impersonate');
const logout =
response?.headers?.get('logout') || response?.headers?.get('Logout');
if (
shouldPreserveOAuthConsentUnauthorized(
url,
options.method,
response.status
)
) {
Comment on lines +49 to +55

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using options.method directly can cause a runtime TypeError: Cannot read properties of undefined (reading 'method') if options is not provided (e.g., when a fetch request is initiated without an options object). Since options is potentially optional or undefined, it is safer to use optional chaining (options?.method).

Suggested change
if (
shouldPreserveOAuthConsentUnauthorized(
url,
options.method,
response.status
)
) {
if (
shouldPreserveOAuthConsentUnauthorized(
url,
options?.method,
response.status
)
) {

return true;
}
if (headerAuth) {
setCookie('auth', headerAuth, 365);
}
Expand All @@ -52,7 +64,15 @@ function LayoutContextInner(params: { children: ReactNode }) {
if (impersonate) {
setCookie('impersonate', impersonate, 365);
}
if (logout && !isSecured) {
if (
shouldHandleGlobalLogout(
url,
options.method,
response.status,
Boolean(logout)
) &&
!isSecured
) {
Comment on lines +67 to +75

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using options.method directly can cause a runtime TypeError if options is undefined. Use optional chaining (options?.method) to safely handle cases where options is omitted.

Suggested change
if (
shouldHandleGlobalLogout(
url,
options.method,
response.status,
Boolean(logout)
) &&
!isSecured
) {
if (
shouldHandleGlobalLogout(
url,
options?.method,
response.status,
Boolean(logout)
) &&
!isSecured
) {

setCookie('auth', '', -10);
setCookie('showorg', '', -10);
setCookie('impersonate', '', -10);
Expand Down Expand Up @@ -82,11 +102,12 @@ function LayoutContextInner(params: { children: ReactNode }) {
}

if (
(response.status === 401 || response?.headers?.get('logout')) &&
!shouldPreserveOAuthConsentUnauthorized(
response.status === 401 ||
shouldHandleGlobalLogout(
url,
options.method,
response.status,
Boolean(response?.headers?.get('logout'))
Boolean(logout)
)
) {
Comment on lines 104 to 112

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using options.method directly can cause a runtime TypeError if options is undefined. Use optional chaining (options?.method) to safely handle cases where options is omitted.

Suggested change
if (
(response.status === 401 || response?.headers?.get('logout')) &&
!shouldPreserveOAuthConsentUnauthorized(
response.status === 401 ||
shouldHandleGlobalLogout(
url,
options.method,
response.status,
Boolean(response?.headers?.get('logout'))
Boolean(logout)
)
) {
if (
response.status === 401 ||
shouldHandleGlobalLogout(
url,
options?.method,
response.status,
Boolean(logout)
)
) {

if (!isSecured) {
Expand Down
21 changes: 18 additions & 3 deletions apps/frontend/src/components/layout/oauth-consent-unauthorized.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
export function shouldPreserveOAuthConsentUnauthorized(
url: string,
method: string | undefined,
status: number
) {
if (status !== 401 || method?.toUpperCase() !== 'POST') return false;
try {
return (
new URL(url, 'https://oauth.invalid').pathname === '/oauth/authorize'
);
} catch {
return false;
}
}

export function shouldHandleGlobalLogout(
url: string,
method: string | undefined,
status: number,
hasLogoutHeader: boolean
) {
return (
!hasLogoutHeader &&
status === 401 &&
url.split('?', 1)[0] === '/oauth/authorize'
hasLogoutHeader &&
!shouldPreserveOAuthConsentUnauthorized(url, method, status)
);
}
29 changes: 22 additions & 7 deletions tests/bootstrap-oauth-consent-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {
authorizationActionResult,
CONSENT_SESSION_ERROR,
} from '../apps/frontend/src/app/(app)/oauth/authorize/authorization-action-result';
import { shouldPreserveOAuthConsentUnauthorized } from '../apps/frontend/src/components/layout/oauth-consent-unauthorized';
import {
shouldHandleGlobalLogout,
shouldPreserveOAuthConsentUnauthorized,
} from '../apps/frontend/src/components/layout/oauth-consent-unauthorized';

describe('First-party OAuth consent error UI', () => {
it('keeps superseded tab A on a visible safe error instead of redirecting', () => {
Expand All @@ -23,20 +26,32 @@ describe('First-party OAuth consent error UI', () => {

it('keeps a failed consent POST on the OAuth page instead of sending it to launches', () => {
expect(
shouldPreserveOAuthConsentUnauthorized('/oauth/authorize', 401, false)
shouldPreserveOAuthConsentUnauthorized('/oauth/authorize', 'POST', 401)
).toBe(true);
expect(
shouldPreserveOAuthConsentUnauthorized(
'/oauth/authorize?state=test',
401,
false
'https://beta-post.crove.com/oauth/authorize?state=test',
'POST',
401
)
).toBe(true);
expect(
shouldPreserveOAuthConsentUnauthorized('/user/profile', 401, false)
shouldPreserveOAuthConsentUnauthorized('/oauth/authorize', 'GET', 401)
).toBe(false);
expect(
shouldPreserveOAuthConsentUnauthorized('/user/profile', 'POST', 401)
).toBe(false);
expect(
shouldPreserveOAuthConsentUnauthorized('/oauth/authorize', 'POST', 500)
).toBe(false);
expect(
shouldPreserveOAuthConsentUnauthorized('/oauth/authorize', 401, true)
shouldHandleGlobalLogout('/oauth/authorize', 'POST', 401, true)
).toBe(false);
expect(shouldHandleGlobalLogout('/oauth/authorize', 'GET', 401, true)).toBe(
true
);
expect(shouldHandleGlobalLogout('/user/profile', 'POST', 401, true)).toBe(
true
);
});
});
Loading