Skip to content

Commit 4919ef3

Browse files
Code cleanup
1 parent e2651ee commit 4919ef3

File tree

4 files changed

+14
-71
lines changed

4 files changed

+14
-71
lines changed

frontend/src/__tests__/utils/csrf-utils.test.ts

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { cookies } from 'next/headers';
1010
import { sign } from 'jsonwebtoken';
1111
import type { ReadonlyRequestCookies } from 'next/dist/server/web/spec-extension/adapters/request-cookies';
1212
import { BinaryLike, BinaryToTextEncoding } from 'node:crypto';
13+
import { getAccessTokenServer } from '@utils/amplify-utils';
1314

1415
class MockHmac {
1516
constructor() {}
@@ -35,6 +36,7 @@ jest.mock('node:crypto', () => ({
3536
createHmac: () => new MockHmac(),
3637
randomBytes: () => 'salt',
3738
}));
39+
jest.mock('@utils/amplify-utils');
3840

3941
const OLD_ENV = { ...process.env };
4042

@@ -80,11 +82,7 @@ describe('getCsrfFormValue', () => {
8082

8183
describe('getSessionId', () => {
8284
test('errors when access token not found', async () => {
83-
jest.mocked(cookies).mockReturnValue(
84-
mockDeep<ReadonlyRequestCookies>({
85-
getAll: () => [],
86-
})
87-
);
85+
jest.mocked(getAccessTokenServer).mockResolvedValue(undefined);
8886

8987
await expect(() => getSessionId()).rejects.toThrow(
9088
'Could not get access token'
@@ -99,33 +97,15 @@ describe('getSessionId', () => {
9997
'key'
10098
);
10199

102-
jest.mocked(cookies).mockReturnValue(
103-
mockDeep<ReadonlyRequestCookies>({
104-
getAll: () => [
105-
{
106-
name: 'Cognito.123.accessToken',
107-
value: mockEmptyJwt,
108-
},
109-
],
110-
})
111-
);
100+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockEmptyJwt);
112101

113102
await expect(() => getSessionId()).rejects.toThrow(
114103
'Could not get session ID'
115104
);
116105
});
117106

118107
test('returns session id', async () => {
119-
jest.mocked(cookies).mockReturnValue(
120-
mockDeep<ReadonlyRequestCookies>({
121-
getAll: () => [
122-
{
123-
name: 'Cognito.123.accessToken',
124-
value: mockJwt,
125-
},
126-
],
127-
})
128-
);
108+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt);
129109

130110
const sessionId = await getSessionId();
131111

@@ -164,16 +144,7 @@ describe('verifyCsrfTokenFull', () => {
164144
test('missing CSRF cookie', async () => {
165145
const formData = mockDeep<FormData>();
166146

167-
jest.mocked(cookies).mockReturnValue(
168-
mockDeep<ReadonlyRequestCookies>({
169-
getAll: () => [
170-
{
171-
name: 'Cognito.123.accessToken',
172-
value: mockJwt,
173-
},
174-
],
175-
})
176-
);
147+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt);
177148

178149
await expect(() => verifyCsrfTokenFull(formData)).rejects.toThrow(
179150
'missing CSRF cookie'
@@ -185,14 +156,9 @@ describe('verifyCsrfTokenFull', () => {
185156
get: () => null,
186157
});
187158

159+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt);
188160
jest.mocked(cookies).mockReturnValue(
189161
mockDeep<ReadonlyRequestCookies>({
190-
getAll: () => [
191-
{
192-
name: 'Cognito.123.accessToken',
193-
value: mockJwt,
194-
},
195-
],
196162
get: (_: string) => ({
197163
name: 'csrf_token',
198164
value: 'hmac.salt',
@@ -210,14 +176,9 @@ describe('verifyCsrfTokenFull', () => {
210176
get: () => 'hmac2.salt',
211177
});
212178

179+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt);
213180
jest.mocked(cookies).mockReturnValue(
214181
mockDeep<ReadonlyRequestCookies>({
215-
getAll: () => [
216-
{
217-
name: 'Cognito.123.accessToken',
218-
value: mockJwt,
219-
},
220-
],
221182
get: (_: string) => ({
222183
name: 'csrf_token',
223184
value: 'hmac.salt',
@@ -235,14 +196,9 @@ describe('verifyCsrfTokenFull', () => {
235196
get: () => 'hmac2.salt',
236197
});
237198

199+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt);
238200
jest.mocked(cookies).mockReturnValue(
239201
mockDeep<ReadonlyRequestCookies>({
240-
getAll: () => [
241-
{
242-
name: 'Cognito.123.accessToken',
243-
value: mockJwt,
244-
},
245-
],
246202
get: (_: string) => ({
247203
name: 'csrf_token',
248204
value: 'hmac2.salt',
@@ -260,14 +216,9 @@ describe('verifyCsrfTokenFull', () => {
260216
get: () => 'hmac.salt',
261217
});
262218

219+
jest.mocked(getAccessTokenServer).mockResolvedValue(mockJwt);
263220
jest.mocked(cookies).mockReturnValue(
264221
mockDeep<ReadonlyRequestCookies>({
265-
getAll: () => [
266-
{
267-
name: 'Cognito.123.accessToken',
268-
value: mockJwt,
269-
},
270-
],
271222
get: (_: string) => ({
272223
name: 'csrf_token',
273224
value: 'hmac.salt',

frontend/src/utils/csrf-utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import { jwtDecode } from 'jwt-decode';
44
import { createHmac, randomBytes } from 'node:crypto';
55
import { cookies } from 'next/headers';
66
import { getEnvironmentVariable } from './get-environment-variable';
7+
import { getAccessTokenServer } from './amplify-utils';
78

89
export const getCsrfFormValue = async () =>
910
cookies().get('csrf_token')?.value ?? 'no_token';
1011

1112
export const getSessionId = async () => {
12-
const accessToken = cookies()
13-
.getAll()
14-
.find(({ name }) => name.endsWith('accessToken'))?.value;
13+
const accessToken = await getAccessTokenServer();
1514

1615
if (!accessToken) {
1716
throw new Error('Could not get access token');

infrastructure/terraform/components/app/ssm_parameter_csrf_secret.tf

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ resource "aws_ssm_parameter" "csrf_secret" {
22
name = "/${local.csi}/csrf_secret"
33
description = "The Basic Auth password used for the amplify app. This parameter is sourced from Github Environment variables"
44

5-
type = "String"
6-
value = var.CSRF_SECRET != "unset" ? var.CSRF_SECRET : random_bytes.csrf_secret[0].hex
7-
}
8-
9-
resource "random_bytes" "csrf_secret" {
10-
count = var.CSRF_SECRET == "unset" ? 1 : 0
11-
12-
length = 16
5+
type = "SecureString"
6+
value = var.CSRF_SECRET
137
}

infrastructure/terraform/components/app/variables.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ variable "CSRF_SECRET" {
110110
# Github only does uppercase env vars
111111
type = string
112112
description = "Secure cryptographic key to be used for generating CSRF tokens - This is entended to be read from CI variables and not commited to any codebase"
113-
default = "unset"
114113
}
115114

116115
variable "branch_name" {

0 commit comments

Comments
 (0)