@@ -3,7 +3,7 @@ import { DIRECTIVES } from '@graphql-codegen/typescript-mongodb';
3
3
import express from 'express' ;
4
4
import passport from 'passport' ;
5
5
import session from 'express-session' ;
6
- import cors , { CorsOptions } from 'cors ' ;
6
+ import helmet from 'helmet ' ;
7
7
import MongoStore , { MongoUrlOptions } from 'connect-mongo' ;
8
8
import gqlSchema from '../common/schema.graphql' ;
9
9
import { resolvers } from './resolvers' ;
@@ -15,11 +15,10 @@ import { UnsubscribeHandler } from './mail/handlers';
15
15
import { UserDbInterface } from './generated/graphql' ;
16
16
import { pullCalendar } from './events' ;
17
17
18
- const { SESSION_SECRET , PORT , CALENDARID , NODE_ENV , PROD_ORIGIN } = process . env ;
18
+ const { SESSION_SECRET , PORT , CALENDARID , NODE_ENV } = process . env ;
19
19
if ( ! SESSION_SECRET ) throw new Error ( `SESSION_SECRET not set` ) ;
20
20
if ( ! PORT ) throw new Error ( `PORT not set` ) ;
21
21
if ( ! CALENDARID ) logger . info ( 'CALENDARID not set; skipping ical integration' ) ;
22
- if ( ! PROD_ORIGIN ) throw new Error ( `PROD_ORIGIN not set` ) ;
23
22
const IS_PROD = NODE_ENV === 'production' ;
24
23
logger . info ( `Node env: ${ NODE_ENV } ` ) ;
25
24
@@ -38,8 +37,7 @@ export const schema = makeExecutableSchema({
38
37
const dbClient = new DB ( ) ;
39
38
const models = await dbClient . collections ;
40
39
41
- // Email unsubscribe link
42
- app . use ( '/api/unsubscribe' , UnsubscribeHandler ( models ) ) ;
40
+ app . use ( helmet ( ) ) ; // sets good security defaults, see https://helmetjs.github.io/
43
41
44
42
// Register auth functions
45
43
app . use (
@@ -48,6 +46,7 @@ export const schema = makeExecutableSchema({
48
46
store : new ( MongoStore ( session ) ) ( ( {
49
47
clientPromise : dbClient . client ,
50
48
} as unknown ) as MongoUrlOptions ) ,
49
+ cookie : { secure : true } ,
51
50
} )
52
51
) ;
53
52
app . use ( passport . initialize ( ) ) ;
@@ -66,6 +65,9 @@ export const schema = makeExecutableSchema({
66
65
} ) ( req , res , next )
67
66
) ;
68
67
68
+ // Email unsubscribe link
69
+ app . use ( '/api/unsubscribe' , UnsubscribeHandler ( models ) ) ;
70
+
69
71
// Pull events callback
70
72
app . use ( '/api/manage/events/pull' , async ( req , res ) => {
71
73
const calendar = await pullCalendar ( CALENDARID ) ;
@@ -82,43 +84,25 @@ export const schema = makeExecutableSchema({
82
84
// give friendly error message to frontend, hide internal server details
83
85
return new Error ( error . message ) ;
84
86
} ,
85
- introspection : true , // OFF by default in prod, needs to be set true to remove compile errors
87
+ introspection : false , // OFF by default in prod for security reasons
86
88
// playground: NODE_ENV !== 'production', // by DEFAULT, enabled when not in prod + disabled in prod
87
89
schema,
88
90
} ) ;
89
91
90
- const allowedOrigin = IS_PROD ? PROD_ORIGIN || '' : '' ;
91
- logger . info ( `Allowed origins: ${ allowedOrigin } ` ) ;
92
-
93
- const corsOptions : CorsOptions = {
94
- origin ( requestOrigin , cb ) {
95
- if ( requestOrigin === null || requestOrigin === undefined ) {
96
- logger . error ( 'Request origin missing, not allowed by CORS' ) ;
97
- return ;
98
- }
99
- const allowed = ! IS_PROD || requestOrigin . endsWith ( allowedOrigin ) ;
100
-
101
- logger . info ( requestOrigin , allowed ) ;
102
- if ( ! allowed ) {
103
- logger . error ( 'Not allowed by CORS' ) ;
104
- return ;
105
- }
106
- cb ( null , allowed ) ;
107
- } ,
108
- } ;
109
-
110
- server . applyMiddleware ( { app, cors : corsOptions } ) ;
92
+ server . applyMiddleware ( { app } ) ;
111
93
112
- if ( IS_PROD ) {
94
+ if ( NODE_ENV !== 'development' ) {
113
95
// Serve front-end asset files in prod.
114
96
app . use ( express . static ( 'dist/server/app' ) ) ;
115
97
// MUST BE LAST AS THIS WILL REROUTE ALL REMAINING TRAFFIC TO THE FRONTEND!
116
- app . use ( ( req , res ) => res . sendFile ( 'index.html' , { root : 'dist/server/app' } ) ) ;
98
+ app . use ( ( req , res ) => {
99
+ res . sendFile ( 'index.html' , { root : 'dist/server/app' } ) ;
100
+ } ) ;
117
101
}
118
102
119
103
app . listen (
120
104
{ port : PORT } ,
121
- ( ) => void logger . info ( `Server ready at http://localhost:8080 ${ server . graphqlPath } ` )
105
+ ( ) => void logger . info ( `Server ready at http://localhost:${ PORT } ${ server . graphqlPath } ` )
122
106
) ;
123
107
} ) ( ) ;
124
108
0 commit comments