Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security] get queries did not run through validation #424

Merged
merged 5 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### VNEXT
* **Security Fix** GET queries did not run through validation ([@DxCx](https://github.com/DxCx)) on [#424](https://github.com/apollographql/graphql-server/pull/424)

### v0.8.0
* Persist `window.location.hash` on URL updates [#386](https://github.com/apollographql/graphql-server/issues/386)
Expand Down
14 changes: 0 additions & 14 deletions packages/graphql-server-core/src/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,6 @@ describe('runQuery', () => {
});
});

it('does not run validation if the query is a document', () => {
// this would not pass validation, because $base ought to be Int!, not String
// what effecively happens is string concatentation, but it's returned as Int
const query = parse(`query TestVar($base: String){ testArgumentValue(base: $base) }`);
const expected = { testArgumentValue: 15 };
return runQuery({
schema,
query: query,
variables: { base: 1 },
}).then((res) => {
return expect(res.data).to.deep.equal(expected);
});
});

it('correctly passes in the rootValue', () => {
const query = `{ testRootValue }`;
const expected = { testRootValue: 'it also works' };
Expand Down
26 changes: 12 additions & 14 deletions packages/graphql-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,31 @@ function doRunQuery(options: QueryOptions): Promise<ExecutionResult> {
logFunction({action: LogAction.request, step: LogStep.status, key: 'operationName', data: options.operationName});

// if query is already an AST, don't parse or validate
// XXX: This refers the operations-store flow.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by this comment exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wanted to disable getting to runQuery with document parsed to eliminate this flow at all.
but i've seen the module-operation-store depends on that "feature".

if (typeof options.query === 'string') {
try {
// TODO: time this with log function
logFunction({action: LogAction.parse, step: LogStep.start});
documentAST = parse(options.query as string);
logFunction({action: LogAction.parse, step: LogStep.end});
} catch (syntaxError) {
logFunction({action: LogAction.parse, step: LogStep.end});
return Promise.resolve({ errors: format([syntaxError]) });
}

// TODO: time this with log function

let rules = specifiedRules;
if (options.validationRules) {
rules = rules.concat(options.validationRules);
}
logFunction({action: LogAction.validation, step: LogStep.start});
const validationErrors = validate(options.schema, documentAST, rules);
logFunction({action: LogAction.validation, step: LogStep.end});
if (validationErrors.length) {
return Promise.resolve({ errors: format(validationErrors) });
}
} else {
documentAST = options.query as DocumentNode;
}

let rules = specifiedRules;
if (options.validationRules) {
rules = rules.concat(options.validationRules);
}
logFunction({action: LogAction.validation, step: LogStep.start});
const validationErrors = validate(options.schema, documentAST, rules);
logFunction({action: LogAction.validation, step: LogStep.end});
if (validationErrors.length) {
return Promise.resolve({ errors: format(validationErrors) });
}

try {
logFunction({action: LogAction.execute, step: LogStep.start});
return execute(
Expand Down
25 changes: 22 additions & 3 deletions packages/graphql-server-express/src/apolloServerHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,33 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
const response = await request(app)
.post(urlString())
.send({
query: '{test(who: 123)}',
query: '{notExists}',
});

expect(response.status).to.equal(400);
expect(JSON.parse(response.text)).to.deep.equal({
errors: [ {
message: 'Argument \"who\" has invalid value 123.\nExpected type \"String\", found 123.',
locations: [ { line: 1, column: 12 } ],
message: 'Cannot query field \"notExists\" on type \"QueryRoot\".',
locations: [ { line: 1, column: 2 } ],
} ]
});
});

it('handles type validation (GET)', async () => {
const app = express();

app.use(urlString(), graphqlExpress({
schema: TestSchema
}));

const response = await request(app)
.get(urlString({ query: '{notExists}' }))

expect(response.status).to.equal(400);
expect(JSON.parse(response.text)).to.deep.equal({
errors: [ {
message: 'Cannot query field \"notExists\" on type \"QueryRoot\".',
locations: [ { line: 1, column: 2 } ],
} ]
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/graphql-server-restify/src/restifyApollo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { graphiqlRestify, graphqlRestify } from './restifyApollo';
import testSuite, { schema, CreateAppOptions } from 'graphql-server-integration-testsuite';
import { expect } from 'chai';
import { GraphQLOptions } from 'graphql-server-core';
import 'mocha';

function createApp(options: CreateAppOptions = {}) {
const server = restify.createServer({
Expand Down
5 changes: 4 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ require('../packages/graphql-server-express/dist/connectApollo.test');
require('../packages/graphql-server-hapi/dist/hapiApollo.test');
(NODE_MAJOR_VERSION >= 6) && require('../packages/graphql-server-micro/dist/microApollo.test');
(NODE_MAJOR_VERSION >= 7) && require('../packages/graphql-server-koa/dist/koaApollo.test');
require('../packages/graphql-server-restify/dist/restifyApollo.test');
require('../packages/graphql-server-lambda/dist/lambdaApollo.test');
require('../packages/graphql-server-express/dist/apolloServerHttp.test');

// XXX: Running restify last as it breaks http.
// for more info: https://github.com/restify/node-restify/issues/700
require('../packages/graphql-server-restify/dist/restifyApollo.test');