Skip to content

Commit a49583a

Browse files
committed
feat: removed NODE_ENV based playground enabling (security issue)
1 parent d9afedc commit a49583a

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"dependencies": {
3636
"@types/cors": "^2.8.3",
3737
"@types/express": "^4.0.39",
38-
"@types/graphql": "^0.11.7",
38+
"@types/graphql": "^0.11.8",
3939
"@types/zen-observable": "^0.5.3",
4040
"apollo-link": "^1.0.7",
4141
"apollo-server-express": "^1.3.2",
@@ -45,15 +45,15 @@
4545
"cors": "^2.8.4",
4646
"express": "^4.16.2",
4747
"graphql": "^0.12.0",
48-
"graphql-import": "^0.3.0",
49-
"graphql-playground-middleware-express": "1.4.9",
50-
"graphql-playground-middleware-lambda": "1.3.10",
48+
"graphql-import": "^0.4.1",
49+
"graphql-playground-middleware-express": "1.5.2",
50+
"graphql-playground-middleware-lambda": "1.4.0",
5151
"graphql-subscriptions": "^0.5.6",
52-
"graphql-tools": "^2.16.0",
53-
"subscriptions-transport-ws": "^0.9.4"
52+
"graphql-tools": "^2.18.0",
53+
"subscriptions-transport-ws": "^0.9.5"
5454
},
5555
"devDependencies": {
56-
"@types/aws-lambda": "0.0.26",
56+
"@types/aws-lambda": "0.0.27",
5757
"tslint": "5.9.1",
5858
"tslint-config-standard": "7.0.0",
5959
"typescript": "2.6.2"

src/index.ts

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { apolloUploadExpress, GraphQLUpload } from 'apollo-upload-server'
33
import * as bodyParser from 'body-parser-graphql'
44
import * as cors from 'cors'
55
import * as express from 'express'
6-
import { PathParams, RequestHandler, RequestHandlerParams } from 'express-serve-static-core'
6+
import {
7+
PathParams,
8+
RequestHandler,
9+
RequestHandlerParams,
10+
} from 'express-serve-static-core'
711
import * as fs from 'fs'
812
import { execute, GraphQLSchema, subscribe } from 'graphql'
913
import { importSchema } from 'graphql-import'
@@ -33,7 +37,10 @@ export class GraphQLServer {
3337
context: any
3438

3539
private middlewares: {
36-
[key: string]: { path?: PathParams; handlers: RequestHandler[] | RequestHandlerParams[] }[],
40+
[key: string]: {
41+
path?: PathParams
42+
handlers: RequestHandler[] | RequestHandlerParams[]
43+
}[]
3744
} = { use: [], get: [], post: [] }
3845

3946
constructor(props: Props) {
@@ -49,7 +56,9 @@ export class GraphQLServer {
4956

5057
// read from .graphql file if path provided
5158
if (typeDefs.endsWith('graphql')) {
52-
const schemaPath = path.isAbsolute(typeDefs) ? path.resolve(typeDefs) : path.resolve(typeDefs)
59+
const schemaPath = path.isAbsolute(typeDefs)
60+
? path.resolve(typeDefs)
61+
: path.resolve(typeDefs)
5362

5463
if (!fs.existsSync(schemaPath)) {
5564
throw new Error(`No schema found for path: ${schemaPath}`)
@@ -58,7 +67,9 @@ export class GraphQLServer {
5867
typeDefs = importSchema(schemaPath)
5968
}
6069

61-
const uploadMixin = typeDefs.includes('scalar Upload') ? { Upload: GraphQLUpload } : {}
70+
const uploadMixin = typeDefs.includes('scalar Upload')
71+
? { Upload: GraphQLUpload }
72+
: {}
6273
this.executableSchema = makeExecutableSchema({
6374
typeDefs,
6475
resolvers: {
@@ -89,11 +100,24 @@ export class GraphQLServer {
89100
return this
90101
}
91102

92-
start(options: Options, callback?: ((options: Options) => void)): Promise<void>
103+
start(
104+
options: Options,
105+
callback?: ((options: Options) => void),
106+
): Promise<void>
93107
start(callback?: ((options: Options) => void)): Promise<void>
94-
start(optionsOrCallback?: Options | ((options: Options) => void), callback?: ((options: Options) => void)): Promise<void> {
95-
const options = (optionsOrCallback && typeof optionsOrCallback === 'function') ? {} : optionsOrCallback
96-
const callbackFunc = callback ? callback : (optionsOrCallback && typeof optionsOrCallback === 'function') ? optionsOrCallback : () => null
108+
start(
109+
optionsOrCallback?: Options | ((options: Options) => void),
110+
callback?: ((options: Options) => void),
111+
): Promise<void> {
112+
const options =
113+
optionsOrCallback && typeof optionsOrCallback === 'function'
114+
? {}
115+
: optionsOrCallback
116+
const callbackFunc = callback
117+
? callback
118+
: optionsOrCallback && typeof optionsOrCallback === 'function'
119+
? optionsOrCallback
120+
: () => null
97121

98122
const app = this.express
99123

@@ -117,7 +141,11 @@ export class GraphQLServer {
117141
app.use(cors())
118142
}
119143

120-
app.post(this.options.endpoint, bodyParser.graphql(), apolloUploadExpress(this.options.uploads))
144+
app.post(
145+
this.options.endpoint,
146+
bodyParser.graphql(),
147+
apolloUploadExpress(this.options.uploads),
148+
)
121149

122150
if (this.options.uploads) {
123151
app.post(this.options.endpoint, apolloUploadExpress(this.options.uploads))
@@ -154,7 +182,10 @@ export class GraphQLServer {
154182
graphqlExpress(async request => {
155183
let context
156184
try {
157-
context = typeof this.context === 'function' ? await this.context({ request }) : this.context
185+
context =
186+
typeof this.context === 'function'
187+
? await this.context({ request })
188+
: this.context
158189
} catch (e) {
159190
console.error(e)
160191
throw e
@@ -178,12 +209,9 @@ export class GraphQLServer {
178209
)
179210

180211
if (this.options.playground) {
181-
const isDev = process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'development'
182-
const playgroundOptions = isDev
183-
? { useGraphQLConfig: true, env: process.env }
184-
: this.options.subscriptions
185-
? { endpoint: this.options.endpoint, subscriptionsEndpoint: this.options.subscriptions }
186-
: { endpoint: this.options.endpoint }
212+
const playgroundOptions = this.options.subscriptions
213+
? { endpoint: this.options.endpoint, subscriptionsEndpoint: this.options.subscriptions }
214+
: { endpoint: this.options.endpoint }
187215

188216
app.get(this.options.playground, expressPlayground(playgroundOptions))
189217
}
@@ -215,7 +243,9 @@ export class GraphQLServer {
215243
let context
216244
try {
217245
context =
218-
typeof this.context === 'function' ? await this.context({ connection }) : this.context
246+
typeof this.context === 'function'
247+
? await this.context({ connection })
248+
: this.context
219249
} catch (e) {
220250
console.error(e)
221251
throw e

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"outDir": "dist",
88
"sourceMap": true,
99
"lib": [
10+
"dom",
1011
"es2017",
1112
"esnext.asynciterable"
1213
]

0 commit comments

Comments
 (0)