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

Validate blockHeight argument #1193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions packages/query/src/graphql/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Config} from '../configure';
import {PinoConfig} from '../utils/logger';
import {getYargsOption} from '../yargs';
import {plugins} from './plugins';
import {BlockHeightArgValidationPlugin} from './plugins/BlockHeightArgValidationPlugin';
import {PgSubscriptionPlugin} from './plugins/PgSubscriptionPlugin';
import {queryComplexityPlugin} from './plugins/QueryComplexityPlugin';
import {ProjectService} from './project.service';
Expand Down Expand Up @@ -108,6 +109,7 @@ export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
? ApolloServerPluginLandingPageGraphQLPlayground()
: ApolloServerPluginLandingPageDisabled(),
queryComplexityPlugin({schema, maxComplexity: argv['query-complexity']}),
BlockHeightArgValidationPlugin({dbSchema}),
];

const server = new ApolloServer({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import {UserInputError} from 'apollo-server-express';
import type {ApolloServerPlugin} from 'apollo-server-plugin-base';
import {DocumentNode, GraphQLError, visit} from 'graphql';
import {Pool} from 'pg';

function parseBlockHeightArgs(document: DocumentNode): string[] {
const values = [];
visit(document, {
Argument(node) {
if (node.name.value === 'blockHeight' && node.value.kind === 'StringValue') {
values.push(node.value.value);
}
},
});
return values;
}

async function fetchLastProcessedHeight(pgClient: Pool, schemaName: string): Promise<bigint | null> {
const result = await pgClient.query(`select value from "${schemaName}"._metadata WHERE key = 'lastProcessedHeight'`);
if (!result.rowCount) {
return null;
}
return BigInt(result.rows[0].value);
}

function validateBlockHeightArgs(args: string[], lastProcessedHeight: bigint): GraphQLError | null {
for (const arg of args) {
if (!arg.match(/^\d+$/)) {
return new UserInputError(`Invalid block height: ${arg}`);
}
const value = BigInt(arg);
if (value > lastProcessedHeight) {
return new UserInputError(`Block height ${arg} is larger than last processed height: ${lastProcessedHeight}`);
}
}
return null;
}

// Plugin to check that any provided blockHeight arg is <= lastProcessedHeight
export function BlockHeightArgValidationPlugin({dbSchema}: {dbSchema: string}): ApolloServerPlugin {
return {
// eslint-disable-next-line
requestDidStart: async () => {
return {
async responseForOperation({context, document}) {
const blockHeightArgs = parseBlockHeightArgs(document);
if (!blockHeightArgs) {
return null;
}
const lastProcessedHeight = await fetchLastProcessedHeight(context.pgClient, dbSchema);
if (!lastProcessedHeight) {
return null;
}
const error = validateBlockHeightArgs(blockHeightArgs, lastProcessedHeight);
if (!error) {
return null;
}
return {errors: [error]};
},
};
},
} as ApolloServerPlugin;
}