-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
thibaudlabat
committed
Jul 28, 2022
1 parent
9493c08
commit 7c3633d
Showing
14 changed files
with
236 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core'; | ||
const express = require('express'); | ||
const http = require('http'); | ||
import {gql} from 'apollo-server'; | ||
import {Armor} from '../src'; | ||
|
||
const typeDefs = gql` | ||
type Book { | ||
title: String | ||
author: String | ||
} | ||
type Nested { | ||
child : Nested | ||
text : String | ||
} | ||
type Query { | ||
books: [Book] | ||
} | ||
type Mutation { | ||
addBook(title: String, author: String): Book | ||
} | ||
`; | ||
|
||
|
||
const books = [ | ||
{ | ||
title: 'The Awakening', | ||
author: 'Kate Chopin', | ||
}, | ||
{ | ||
title: 'City of Glass', | ||
author: 'Paul Auster', | ||
}, | ||
]; | ||
|
||
|
||
const resolvers = { | ||
Query: { | ||
books: () => books, | ||
}, | ||
Mutation: { | ||
addBook: (title: String, author: String) => { | ||
return {title: 'title_test', author: 'author_test'}; | ||
}, | ||
}, | ||
}; | ||
|
||
const app = express(); | ||
const httpServer = http.createServer(app); | ||
|
||
|
||
const armor = new Armor({ | ||
|
||
}); | ||
|
||
const server = armor.apolloServer({ | ||
typeDefs, | ||
resolvers, | ||
cache: 'bounded', | ||
// eslint-disable-next-line new-cap | ||
plugins: [ApolloServerPluginDrainHttpServer({httpServer})], | ||
}); | ||
|
||
(async () => { | ||
await server.start(); | ||
server.applyMiddleware({ | ||
app, | ||
path: '/', | ||
}); | ||
|
||
await new Promise<void>((resolve) => { | ||
const x = httpServer | ||
.listen({port: 4000}, resolve); | ||
}); | ||
|
||
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,34 +4,40 @@ | |
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.json", | ||
"watch": "tsc -p tsconfig.json -w", | ||
"start": "NODE_PATH=dist node dist/index.js", | ||
"test": "echo 'NotImplementedError'", | ||
"lint": "npx eslint src/**/*.ts", | ||
"lint-fix": "npx eslint --fix src/**/*.ts" | ||
"build": "tsc -p tsconfig.json", | ||
"watch": "tsc -p tsconfig.json -w", | ||
"start": "NODE_PATH=dist node dist/index.js", | ||
"test": "echo 'NotImplementedError'", | ||
"lint": "npx eslint src/**/*.ts", | ||
"lint-fix": "npx eslint --fix src/**/*.ts", | ||
"example": "nodemon examples/server.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]:Escape-Technologies/graphql-armor.git" | ||
"type": "git", | ||
"url": "git+ssh://[email protected]:Escape-Technologies/graphql-armor.git" | ||
}, | ||
"author": "Escape Technologies SAS", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Escape-Technologies/graphql-armor/issues" | ||
"url": "https://github.com/Escape-Technologies/graphql-armor/issues" | ||
}, | ||
"homepage": "https://github.com/Escape-Technologies/graphql-armor", | ||
"dependencies": { | ||
"apollo-server": "^3.10.0", | ||
"apollo-server-express": "^3.10.0", | ||
"express": "^4.18.1", | ||
"http": "^0.0.1-security" | ||
}, | ||
"devDependencies": { | ||
"@escape.tech/mookme": "^2.1.1", | ||
"@commitlint/cli": "^17.0.0", | ||
"@commitlint/config-angular": "^17.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.25.0", | ||
"@typescript-eslint/parser": "^5.25.0", | ||
"eslint": ">=8.15.0", | ||
"eslint-config-google": "^0.14.0", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.6.4" | ||
"@commitlint/cli": "^17.0.0", | ||
"@commitlint/config-angular": "^17.0.0", | ||
"@escape.tech/mookme": "^2.1.1", | ||
"@typescript-eslint/eslint-plugin": "^5.25.0", | ||
"@typescript-eslint/parser": "^5.25.0", | ||
"eslint": ">=8.15.0", | ||
"eslint-config-google": "^0.14.0", | ||
"nodemon": "^2.0.19", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.6.4" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {PluginDefinition, ValidationRule} from './types'; | ||
import {Armor} from './index'; | ||
|
||
export class ArmorPlugin { | ||
private armor: Armor; | ||
|
||
constructor(armor: Armor) { | ||
this.armor = armor; | ||
} | ||
|
||
getApolloPlugins(): PluginDefinition[] { | ||
return []; | ||
} | ||
|
||
getValidationRules(): ValidationRule[] { | ||
return []; | ||
} | ||
|
||
apolloPatchConfig(config) { | ||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
console.log('Hello World'); | ||
import {ApolloServer} from 'apollo-server-express'; | ||
import {Config} from 'apollo-server-core/src/types'; | ||
import {ExpressContext} from 'apollo-server-express/src/ApolloServer'; | ||
import {CharacterLimitPlugin, DisableIntrospectionPlugin} from './plugins'; | ||
import {ArmorPlugin} from './ArmorPlugin'; | ||
import {PluginDefinition, ValidationRule} from './types'; | ||
|
||
export type ArmorConfig = any; | ||
|
||
export class Armor { | ||
private armorPlugins: ArmorPlugin[] = []; | ||
|
||
constructor(config: ArmorConfig) { | ||
// here we add new plugins (to be moved somewhere else) | ||
this.armorPlugins.push(new DisableIntrospectionPlugin(this)); | ||
this.armorPlugins.push(new CharacterLimitPlugin(this)); | ||
} | ||
|
||
|
||
public apolloServer<ContextFunctionParams = ExpressContext>(config: Config<ContextFunctionParams>) { | ||
config.plugins ||= []; | ||
config.validationRules ||= []; | ||
|
||
let apolloPlugins: PluginDefinition[] = []; | ||
let validationRules: ValidationRule[] = []; | ||
|
||
for (const plugin of this.armorPlugins) { | ||
config = plugin.apolloPatchConfig(config); | ||
apolloPlugins=apolloPlugins.concat(plugin.getApolloPlugins()); | ||
validationRules=validationRules.concat(plugin.getValidationRules()); | ||
} | ||
|
||
// We prepend our plugins/rules | ||
// So that we can protect the following user-defined plugins from attacks | ||
config.plugins = apolloPlugins.concat(config.plugins as PluginDefinition[]); | ||
config.validationRules = validationRules.concat(config.validationRules as ValidationRule[]); | ||
|
||
|
||
return new ApolloServer<ContextFunctionParams>(config); | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {ArmorPlugin} from './ArmorPlugin'; | ||
import {PluginDefinition} from './types'; | ||
|
||
export class DisableIntrospectionPlugin extends ArmorPlugin { | ||
apolloPatchConfig(config) { | ||
return {...config, introspection: false}; | ||
} | ||
} | ||
|
||
export class CharacterLimitPlugin extends ArmorPlugin { | ||
getApolloPlugins(): PluginDefinition[] { | ||
const characterLimitPlugin = { | ||
async requestDidStart(context) { | ||
if (context.request.query.length > 3000) { | ||
throw new Error('Query too large.'); | ||
} | ||
}, | ||
}; | ||
|
||
return [characterLimitPlugin]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {ApolloServerPlugin} from 'apollo-server-plugin-base'; | ||
|
||
export type PluginDefinition = ApolloServerPlugin | (() => ApolloServerPlugin); // apollo-server-core/src/types.ts | ||
export type ValidationRule = any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters