Skip to content

Commit

Permalink
fix(adapter-nextjs): wrong naming and impl. of isSSLOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Dec 27, 2024
1 parent 24fba6f commit 43e6b22
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isNonSSLOrigin,
isSSLOrigin,
} from '../../../src/auth/utils';

jest.mock('../../../src/auth/utils');
Expand All @@ -31,7 +31,7 @@ const mockCreateSignUpEndpoint = jest.mocked(createSignUpEndpoint);
const mockCreateUrlSearchParamsForSignInSignUp = jest.mocked(
createUrlSearchParamsForSignInSignUp,
);
const mockIsNonSSLOrigin = jest.mocked(isNonSSLOrigin);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignInSignUpRequest', () => {
const mockCustomState = 'mockCustomState';
Expand All @@ -44,7 +44,7 @@ describe('handleSignInSignUpRequest', () => {
const mockToCodeChallenge = jest.fn(() => 'mockCodeChallenge');

beforeAll(() => {
mockIsNonSSLOrigin.mockReturnValue(true);
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
Expand All @@ -56,7 +56,7 @@ describe('handleSignInSignUpRequest', () => {
mockCreateSignUpEndpoint.mockClear();
mockCreateUrlSearchParamsForSignInSignUp.mockClear();
mockToCodeChallenge.mockClear();
mockIsNonSSLOrigin.mockClear();
mockIsSSLOrigin.mockClear();
});

test.each(['signIn' as const, 'signUp' as const])(
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('handleSignInSignUpRequest', () => {
mockCreateSignInFlowProofCookiesResult,
mockCreateAuthFlowProofCookiesSetOptionsResult,
);
expect(isNonSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(isSSLOrigin).toHaveBeenCalledWith(mockOrigin);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isNonSSLOrigin,
isSSLOrigin,
} from '../../../src/auth/utils';
import { createMockNextApiResponse } from '../testUtils';

Expand All @@ -35,7 +35,7 @@ const mockCreateSignUpEndpoint = jest.mocked(createSignUpEndpoint);
const mockCreateUrlSearchParamsForSignInSignUp = jest.mocked(
createUrlSearchParamsForSignInSignUp,
);
const mockIsNonSSLOrigin = jest.mocked(isNonSSLOrigin);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignInSignUpRequest', () => {
const mockCustomState = 'mockCustomState';
Expand All @@ -57,7 +57,7 @@ describe('handleSignInSignUpRequest', () => {
} = createMockNextApiResponse();

beforeAll(() => {
mockIsNonSSLOrigin.mockReturnValue(true);
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
Expand All @@ -69,7 +69,7 @@ describe('handleSignInSignUpRequest', () => {
mockCreateSignUpEndpoint.mockClear();
mockCreateUrlSearchParamsForSignInSignUp.mockClear();
mockToCodeChallenge.mockClear();
mockIsNonSSLOrigin.mockClear();
mockIsSSLOrigin.mockClear();

mockResponseAppendHeader.mockClear();
mockResponseEnd.mockClear();
Expand Down Expand Up @@ -189,7 +189,7 @@ describe('handleSignInSignUpRequest', () => {
mockCreateSignInFlowProofCookiesResult,
mockCreateAuthFlowProofCookiesSetOptionsResult,
);
expect(isNonSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(isSSLOrigin).toHaveBeenCalledWith(mockOrigin);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isNonSSLOrigin,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../../../src/auth/utils';

Expand All @@ -24,11 +24,11 @@ const mockCreateSignOutFlowProofCookies = jest.mocked(
createSignOutFlowProofCookies,
);
const mockResolveRedirectSignOutUrl = jest.mocked(resolveRedirectSignOutUrl);
const mockIsNonSSLOrigin = jest.mocked(isNonSSLOrigin);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignOutRequest', () => {
beforeAll(() => {
mockIsNonSSLOrigin.mockReturnValue(true);
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
Expand All @@ -37,7 +37,7 @@ describe('handleSignOutRequest', () => {
mockCreateLogoutEndpoint.mockClear();
mockCreateSignOutFlowProofCookies.mockClear();
mockResolveRedirectSignOutUrl.mockClear();
mockIsNonSSLOrigin.mockClear();
mockIsSSLOrigin.mockClear();
});

it('returns a 302 response with the correct headers and cookies', async () => {
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('handleSignOutRequest', () => {
expect.any(URLSearchParams),
);
expect(mockCreateSignOutFlowProofCookies).toHaveBeenCalled();
expect(mockIsNonSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(mockIsSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(mockCreateAuthFlowProofCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isNonSSLOrigin,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../../../src/auth/utils';
import { createMockNextApiResponse } from '../testUtils';
Expand All @@ -27,7 +27,7 @@ const mockCreateSignOutFlowProofCookies = jest.mocked(
createSignOutFlowProofCookies,
);
const mockResolveRedirectSignOutUrl = jest.mocked(resolveRedirectSignOutUrl);
const mockIsNonSSLOrigin = jest.mocked(isNonSSLOrigin);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignOutRequest', () => {
const {
Expand All @@ -40,7 +40,7 @@ describe('handleSignOutRequest', () => {
} = createMockNextApiResponse();

beforeAll(() => {
mockIsNonSSLOrigin.mockReturnValue(true);
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('handleSignOutRequest', () => {
expect.any(URLSearchParams),
);
expect(mockCreateSignOutFlowProofCookies).toHaveBeenCalled();
expect(mockIsNonSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(mockIsSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(mockCreateAuthFlowProofCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
isNonSSLOrigin,
isValidOrigin,
} from '../../../src/auth/utils/isValidOrigin';
import { isSSLOrigin, isValidOrigin } from '../../../src/auth/utils/origin';

describe('isValidOrigin', () => {
test.each([
Expand Down Expand Up @@ -52,10 +49,11 @@ describe('isValidOrigin', () => {

describe('isNonSSLLocalhostOrigin', () => {
test.each([
['http://localhost', true],
['http://localhost:3000', true],
['https://some-app.com', false],
['https://some-app.com', true],
['http://localhost', false],
['http://localhost:3000', false],
['https:// some-app.com', false],
])('check origin is non-SSL localhost %s as %s', (origin, expected) => {
expect(isNonSSLOrigin(origin)).toBe(expected);
expect(isSSLOrigin(origin)).toBe(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isNonSSLOrigin,
isSSLOrigin,
} from '../utils';

import { HandleSignInSignUpRequest } from './types';
Expand Down Expand Up @@ -45,7 +45,7 @@ export const handleSignInSignUpRequest: HandleSignInSignUpRequest = ({
headers,
createSignInFlowProofCookies({ state, pkce: codeVerifier.value }),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isNonSSLOrigin(origin),
secure: isSSLOrigin(origin),
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isNonSSLOrigin,
isSSLOrigin,
} from '../utils';

import { HandleSignInSignUpRequestForPagesRouter } from './types';
Expand Down Expand Up @@ -39,7 +39,7 @@ export const handleSignInSignUpRequestForPagesRouter: HandleSignInSignUpRequestF
response,
createSignInFlowProofCookies({ state, pkce: codeVerifier.value }),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isNonSSLOrigin(origin),
secure: isSSLOrigin(origin),
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isNonSSLOrigin,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../utils';

Expand All @@ -32,7 +32,7 @@ export const handleSignOutRequest: HandleSignOutRequest = ({
headers,
createSignOutFlowProofCookies(),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isNonSSLOrigin(origin),
secure: isSSLOrigin(origin),
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isNonSSLOrigin,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../utils';

Expand All @@ -23,7 +23,7 @@ export const handleSignOutRequestForPagesRouter: HandleSignOutRequestForPagesRou
response,
createSignOutFlowProofCookies(),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isNonSSLOrigin(origin),
secure: isSSLOrigin(origin),
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-nextjs/src/auth/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export {
hasActiveUserSessionWithPagesRouter,
} from './hasActiveUserSession';
export { isSupportedAuthApiRoutePath } from './isSupportedAuthApiRoutePath';
export { isValidOrigin, isNonSSLOrigin } from './isValidOrigin';
export { isValidOrigin, isSSLOrigin } from './origin';
export { resolveCodeAndStateFromUrl } from './resolveCodeAndStateFromUrl';
export { resolveIdentityProviderFromUrl } from './resolveIdentityProviderFromUrl';
export {
Expand Down
12 changes: 0 additions & 12 deletions packages/adapter-nextjs/src/auth/utils/isValidOrigin.ts

This file was deleted.

27 changes: 27 additions & 0 deletions packages/adapter-nextjs/src/auth/utils/origin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// a regular expression that validates the origin string to be any valid origin, and allowing local development localhost
const originRegex =
/^(http:\/\/localhost(:\d{1,5})?)|(https?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)*(:\d{1,5})?)$/;

export const isValidOrigin = (origin: string): boolean => {
try {
const url = new URL(origin);

return (
(url.protocol === 'http:' || url.protocol === 'https:') &&
originRegex.test(origin)
);
} catch {
return false;
}
};

export const isSSLOrigin = (origin: string): boolean => {
if (isValidOrigin(origin)) {
return origin.startsWith('https://');
}

return false;
};

0 comments on commit 43e6b22

Please sign in to comment.