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

[graphql/rpc] limit query payload size #14715

Merged
merged 3 commits into from
Nov 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ type ServiceConfig {
Maximum time in milliseconds that will be spent to serve one request.
"""
requestTimeoutMs: BigInt!
"""
Maximum length of a query payload string.
"""
maxQueryPayloadSize: Int!
}

type Stake {
Expand Down
13 changes: 13 additions & 0 deletions crates/sui-graphql-rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::functional_group::FunctionalGroup;
// TODO: calculate proper cost limits
const MAX_QUERY_DEPTH: u32 = 20;
const MAX_QUERY_NODES: u32 = 200;
const MAX_QUERY_PAYLOAD_SIZE: u32 = 5_000;
const MAX_DB_QUERY_COST: u64 = 20_000; // Max DB query cost (normally f64) truncated
const MAX_QUERY_VARIABLES: u32 = 50;
const MAX_QUERY_FRAGMENTS: u32 = 50;
Expand Down Expand Up @@ -52,6 +53,8 @@ pub struct Limits {
#[serde(default)]
pub(crate) max_query_nodes: u32,
#[serde(default)]
pub(crate) max_query_payload_size: u32,
#[serde(default)]
pub(crate) max_db_query_cost: u64,
#[serde(default)]
pub(crate) max_query_variables: u32,
Expand Down Expand Up @@ -171,6 +174,11 @@ impl ServiceConfig {
async fn request_timeout_ms(&self) -> BigInt {
BigInt::from(self.limits.request_timeout_ms)
}

/// Maximum length of a query payload string.
async fn max_query_payload_size(&self) -> u32 {
self.limits.max_query_payload_size
}
}

impl Default for ConnectionConfig {
Expand All @@ -190,6 +198,7 @@ impl Default for Limits {
Self {
max_query_depth: MAX_QUERY_DEPTH,
max_query_nodes: MAX_QUERY_NODES,
max_query_payload_size: MAX_QUERY_PAYLOAD_SIZE,
max_db_query_cost: MAX_DB_QUERY_COST,
max_query_variables: MAX_QUERY_VARIABLES,
max_query_fragments: MAX_QUERY_FRAGMENTS,
Expand Down Expand Up @@ -285,6 +294,7 @@ mod tests {
r#" [limits]
max-query-depth = 100
max-query-nodes = 300
max-query-payload-size = 2000
max-db-query-cost = 50
max-query-variables = 45
max-query-fragments = 32
Expand All @@ -297,6 +307,7 @@ mod tests {
limits: Limits {
max_query_depth: 100,
max_query_nodes: 300,
max_query_payload_size: 2000,
max_db_query_cost: 50,
max_query_variables: 45,
max_query_fragments: 32,
Expand Down Expand Up @@ -354,6 +365,7 @@ mod tests {
[limits]
max-query-depth = 42
max-query-nodes = 320
max-query-payload-size = 200
max-db-query-cost = 20
max-query-variables = 34
max-query-fragments = 31
Expand All @@ -369,6 +381,7 @@ mod tests {
limits: Limits {
max_query_depth: 42,
max_query_nodes: 320,
max_query_payload_size: 200,
max_db_query_cost: 20,
max_query_variables: 34,
max_query_fragments: 31,
Expand Down
1 change: 1 addition & 0 deletions crates/sui-graphql-rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::context_data::db_data_provider::DbValidationError;
pub(crate) mod code {
pub const BAD_REQUEST: &str = "BAD_REQUEST";
pub const BAD_USER_INPUT: &str = "BAD_USER_INPUT";
pub const GRAPHQL_VALIDATION_FAILED: &str = "GRAPHQL_VALIDATION_FAILED";
pub const INTERNAL_SERVER_ERROR: &str = "INTERNAL_SERVER_ERROR";
}

Expand Down
16 changes: 15 additions & 1 deletion crates/sui-graphql-rpc/src/extensions/query_limits_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

use crate::config::Limits;
use crate::config::ServiceConfig;
use crate::error::code;
use crate::error::code::INTERNAL_SERVER_ERROR;
use crate::error::graphql_error;
use crate::error::graphql_error_at_pos;
use crate::metrics::RequestMetrics;
use async_graphql::extensions::NextParseQuery;
Expand Down Expand Up @@ -42,6 +44,7 @@ struct ValidationRes {
depth: u32,
num_variables: u32,
num_fragments: u32,
query_payload: u32,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -103,6 +106,7 @@ impl Extension for QueryLimitsChecker {
"depth": validation_result.depth,
"variables": validation_result.num_variables,
"fragments": validation_result.num_fragments,
"query_payload": validation_result.query_payload,
}),
)
} else {
Expand All @@ -121,12 +125,21 @@ impl Extension for QueryLimitsChecker {
) -> ServerResult<ExecutableDocument> {
// TODO: limit number of variables, fragments, etc
// TODO: limit/ban directives for now
// TODO: limit overall query size

let cfg = ctx
.data::<ServiceConfig>()
.expect("No service config provided in schema data");

if query.len() > cfg.limits.max_query_payload_size as usize {
return Err(graphql_error(
code::GRAPHQL_VALIDATION_FAILED,
format!(
"Query payload is too large. The maximum allowed is {} bytes",
cfg.limits.max_query_payload_size
),
));
}

if variables.len() > cfg.limits.max_query_variables as usize {
return Err(ServerError::new(
format!(
Expand Down Expand Up @@ -173,6 +186,7 @@ impl Extension for QueryLimitsChecker {
*self.validation_result.lock().await = Some(ValidationRes {
num_nodes: running_costs.num_nodes,
depth: running_costs.depth,
query_payload: query.len() as u32,
num_variables: variables.len() as u32,
num_fragments: doc.fragments.len() as u32,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,10 @@ type ServiceConfig {
Maximum time in milliseconds that will be spent to serve one request.
"""
requestTimeoutMs: BigInt!
"""
Maximum length of a query payload string.
"""
maxQueryPayloadSize: Int!
}

type Stake {
Expand Down