Skip to content

Commit 0b7ced7

Browse files
authored
feat(wabe): throw error if cookie session or csrfToken protection enabled without jwt secret (#267)
* feat(wabe): throw an error if no jwt secret is provided with cookie session * feat: more tests * fix: test helper * feat: error before * fix: ci * fix: test
1 parent 231ca01 commit 0b7ced7

File tree

7 files changed

+140
-2
lines changed

7 files changed

+140
-2
lines changed

packages/wabe-mongodb/utils/testHelper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export const setupTests = async (
2828
},
2929
},
3030
port,
31+
security: {
32+
disableCSRFProtection: true,
33+
},
3134
schema: {
3235
classes: [
3336
...additionalClasses,

packages/wabe-postgres-launcher/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export const startPostgres = async (): Promise<void> => {
6464

6565
console.info('PostgreSQL started')
6666
} catch (error: any) {
67+
console.error('An error occurred:', error)
68+
6769
if (error.message.includes('there a typo in the url or port')) {
6870
console.error('You need to run Docker on your machine')
6971
process.exit(1)
@@ -87,7 +89,5 @@ export const startPostgres = async (): Promise<void> => {
8789
} catch (cleanupError) {
8890
console.error('Error during cleanup:', cleanupError)
8991
}
90-
91-
console.error('An error occurred:', error)
9292
}
9393
}

packages/wabe-postgres/src/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ describe('Postgres adapter', () => {
440440
}),
441441
},
442442
port,
443+
security: {
444+
disableCSRFProtection: true,
445+
},
443446
schema: {
444447
classes: [
445448
{
@@ -466,6 +469,9 @@ describe('Postgres adapter', () => {
466469
databaseName: databaseId,
467470
}),
468471
},
472+
security: {
473+
disableCSRFProtection: true,
474+
},
469475
port: port2,
470476
schema: {
471477
classes: [

packages/wabe-postgres/utils/testHelper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export const setupTests = async (
3030
cookieSession: true,
3131
},
3232
},
33+
security: {
34+
disableCSRFProtection: true,
35+
},
3336
port,
3437
schema: {
3538
classes: [

packages/wabe/src/graphql/GraphQLSchema.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const createWabe = async (schema: SchemaInterface<DevWabeTypes>) => {
2828
// @ts-expect-error
2929
adapter: await getDatabaseAdapter(databaseId),
3030
},
31+
security: {
32+
disableCSRFProtection: true,
33+
},
3134
})
3235

3336
await wabe.start()

packages/wabe/src/server/index.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,85 @@ import { getDatabaseAdapter } from '../utils/testHelper'
1010
import { RoleEnum } from 'generated/wabe'
1111

1212
describe('Server', () => {
13+
it('should throw error if no jwt secret provided but cookie session choosen', async () => {
14+
const databaseId = uuid()
15+
16+
const port = await getPort()
17+
const wabe = new Wabe({
18+
isProduction: false,
19+
rootKey:
20+
'eIUbb9abFa8PJGRfRwgiGSCU0fGnLErph2QYjigDRjLsbyNA3fZJ8Npd0FJNzxAc',
21+
database: {
22+
// @ts-expect-error
23+
adapter: await getDatabaseAdapter(databaseId),
24+
},
25+
port,
26+
authentication: {
27+
// @ts-expect-error
28+
session: {
29+
cookieSession: true,
30+
},
31+
},
32+
routes: [
33+
{
34+
handler: (ctx) => ctx.res.send('Hello World!'),
35+
path: '/hello',
36+
method: 'GET',
37+
},
38+
],
39+
schema: {
40+
classes: [
41+
{
42+
name: 'Collection1',
43+
fields: { name: { type: 'String' } },
44+
},
45+
],
46+
},
47+
})
48+
49+
expect(wabe.start()).rejects.toThrow(
50+
'Authentication with cookie needs jwt secret',
51+
)
52+
})
53+
54+
it('should throw error if no jwt secret provided but csrf protection is enabled', async () => {
55+
const databaseId = uuid()
56+
57+
const port = await getPort()
58+
const wabe = new Wabe({
59+
isProduction: false,
60+
rootKey:
61+
'eIUbb9abFa8PJGRfRwgiGSCU0fGnLErph2QYjigDRjLsbyNA3fZJ8Npd0FJNzxAc',
62+
database: {
63+
// @ts-expect-error
64+
adapter: await getDatabaseAdapter(databaseId),
65+
},
66+
port,
67+
security: {
68+
disableCSRFProtection: false,
69+
},
70+
routes: [
71+
{
72+
handler: (ctx) => ctx.res.send('Hello World!'),
73+
path: '/hello',
74+
method: 'GET',
75+
},
76+
],
77+
schema: {
78+
classes: [
79+
{
80+
name: 'Collection1',
81+
fields: { name: { type: 'String' } },
82+
},
83+
],
84+
},
85+
})
86+
87+
expect(wabe.start()).rejects.toThrow(
88+
'Authentication with cookie needs jwt secret',
89+
)
90+
})
91+
1392
it('should mask graphql errors message', async () => {
1493
spyOn(console, 'error').mockReturnValue()
1594
const databaseId = uuid()
@@ -25,6 +104,7 @@ describe('Server', () => {
25104
},
26105
security: {
27106
hideSensitiveErrorMessage: true,
107+
disableCSRFProtection: true,
28108
},
29109
port,
30110
schema: {
@@ -68,6 +148,9 @@ describe('Server', () => {
68148
// @ts-expect-error
69149
adapter: await getDatabaseAdapter(databaseId),
70150
},
151+
security: {
152+
disableCSRFProtection: true,
153+
},
71154
port,
72155
routes: [
73156
{
@@ -105,6 +188,9 @@ describe('Server', () => {
105188
// @ts-expect-error
106189
adapter: await getDatabaseAdapter(databaseId),
107190
},
191+
security: {
192+
disableCSRFProtection: true,
193+
},
108194
port,
109195
schema: {
110196
classes: [
@@ -158,6 +244,9 @@ describe('Server', () => {
158244
// @ts-expect-error
159245
adapter: await getDatabaseAdapter(databaseId),
160246
},
247+
security: {
248+
disableCSRFProtection: true,
249+
},
161250
port,
162251
schema: {
163252
classes: [
@@ -191,6 +280,9 @@ describe('Server', () => {
191280
adapter: await getDatabaseAdapter(databaseId),
192281
},
193282
port,
283+
security: {
284+
disableCSRFProtection: true,
285+
},
194286
schema: {
195287
classes: [
196288
{
@@ -223,6 +315,9 @@ describe('Server', () => {
223315
// @ts-expect-error
224316
adapter: await getDatabaseAdapter(databaseId),
225317
},
318+
security: {
319+
disableCSRFProtection: true,
320+
},
226321
port,
227322
hooks: [
228323
{
@@ -244,6 +339,9 @@ describe('Server', () => {
244339
// @ts-expect-error
245340
adapter: await getDatabaseAdapter(databaseId),
246341
},
342+
security: {
343+
disableCSRFProtection: true,
344+
},
247345
port,
248346
hooks: [],
249347
}),
@@ -260,6 +358,9 @@ describe('Server', () => {
260358
adapter: await getDatabaseAdapter(databaseId),
261359
},
262360
port,
361+
security: {
362+
disableCSRFProtection: true,
363+
},
263364
hooks: [
264365
{
265366
operationType: OperationType.BeforeCreate,
@@ -284,6 +385,9 @@ describe('Server', () => {
284385
adapter: await getDatabaseAdapter(databaseId),
285386
},
286387
port,
388+
security: {
389+
disableCSRFProtection: true,
390+
},
287391
})
288392

289393
await wabe.start()
@@ -311,6 +415,9 @@ describe('Server', () => {
311415
adapter: await getDatabaseAdapter(databaseId),
312416
},
313417
port,
418+
security: {
419+
disableCSRFProtection: true,
420+
},
314421
schema: {
315422
classes: [
316423
{
@@ -350,6 +457,9 @@ describe('Server', () => {
350457
adapter: await getDatabaseAdapter(databaseId),
351458
},
352459
port,
460+
security: {
461+
disableCSRFProtection: true,
462+
},
353463
schema: {
354464
classes: [
355465
{
@@ -396,6 +506,9 @@ describe('Server', () => {
396506
roles: ['Client'],
397507
},
398508
port,
509+
security: {
510+
disableCSRFProtection: true,
511+
},
399512
})
400513

401514
await wabeMain.start()
@@ -424,6 +537,9 @@ describe('Server', () => {
424537
}
425538
},
426539
},
540+
security: {
541+
disableCSRFProtection: true,
542+
},
427543
schema: {
428544
classes: [
429545
{

packages/wabe/src/server/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ export class Wabe<T extends WabeTypes> {
218218
}
219219

220220
async start() {
221+
if (
222+
!this.config.authentication?.session?.jwtSecret &&
223+
(this.config.authentication?.session?.cookieSession ||
224+
!this.config.security?.disableCSRFProtection)
225+
)
226+
throw new Error('Authentication with cookie needs jwt secret')
227+
221228
const wabeSchema = new Schema(this.config)
222229

223230
this.config.schema = wabeSchema.schema

0 commit comments

Comments
 (0)