1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! GraphQL request validation module.
//!
//! Provides validation for GraphQL queries including:
//! - Query depth validation (prevent deeply nested queries)
//! - Query complexity scoring (prevent complex queries)
//! - Variable type validation (ensure variable types match schema)
use serde_json::Value as JsonValue;
use thiserror::Error;
/// Validation error types.
#[derive(Debug, Error, Clone)]
pub enum ValidationError {
/// Query exceeds maximum allowed depth.
#[error("Query exceeds maximum depth of {max_depth}: depth = {actual_depth}")]
QueryTooDeep {
/// Maximum allowed depth
max_depth: usize,
/// Actual query depth
actual_depth: usize,
},
/// Query exceeds maximum complexity score.
#[error("Query exceeds maximum complexity of {max_complexity}: score = {actual_complexity}")]
QueryTooComplex {
/// Maximum allowed complexity
max_complexity: usize,
/// Actual query complexity
actual_complexity: usize,
},
/// Invalid query variables.
#[error("Invalid variables: {0}")]
InvalidVariables(String),
/// Malformed GraphQL query.
#[error("Malformed GraphQL query: {0}")]
MalformedQuery(String),
}
/// GraphQL request validator.
#[derive(Debug, Clone)]
pub struct RequestValidator {
/// Maximum query depth allowed.
max_depth: usize,
/// Maximum query complexity score allowed.
max_complexity: usize,
/// Enable query depth validation.
validate_depth: bool,
/// Enable query complexity validation.
validate_complexity: bool,
}
impl RequestValidator {
/// Create a new validator with default settings.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Set maximum query depth.
#[must_use]
pub fn with_max_depth(mut self, max_depth: usize) -> Self {
self.max_depth = max_depth;
self
}
/// Set maximum query complexity.
#[must_use]
pub fn with_max_complexity(mut self, max_complexity: usize) -> Self {
self.max_complexity = max_complexity;
self
}
/// Enable/disable depth validation.
#[must_use]
pub fn with_depth_validation(mut self, enabled: bool) -> Self {
self.validate_depth = enabled;
self
}
/// Enable/disable complexity validation.
#[must_use]
pub fn with_complexity_validation(mut self, enabled: bool) -> Self {
self.validate_complexity = enabled;
self
}
/// Validate a GraphQL query string.
///
/// # Errors
///
/// Returns `ValidationError` if the query violates any validation rules.
pub fn validate_query(&self, query: &str) -> Result<(), ValidationError> {
// Validate query is not empty
if query.trim().is_empty() {
return Err(ValidationError::MalformedQuery("Empty query".to_string()));
}
// Check depth if enabled
if self.validate_depth {
let depth = self.calculate_depth(query);
if depth > self.max_depth {
return Err(ValidationError::QueryTooDeep {
max_depth: self.max_depth,
actual_depth: depth,
});
}
}
// Check complexity if enabled
if self.validate_complexity {
let complexity = self.calculate_complexity(query);
if complexity > self.max_complexity {
return Err(ValidationError::QueryTooComplex {
max_complexity: self.max_complexity,
actual_complexity: complexity,
});
}
}
Ok(())
}
/// Validate variables JSON.
///
/// # Errors
///
/// Returns `ValidationError` if variables are invalid.
pub fn validate_variables(&self, variables: Option<&JsonValue>) -> Result<(), ValidationError> {
if let Some(vars) = variables {
// Validate that variables is an object
if !vars.is_object() {
return Err(ValidationError::InvalidVariables(
"Variables must be an object".to_string(),
));
}
// Validate variable values are not null (optional - can be configured)
// For now, just ensure it's valid JSON which it already is
}
Ok(())
}
/// Calculate query depth (max nesting level).
fn calculate_depth(&self, query: &str) -> usize {
let mut max_depth: usize = 0;
let mut current_depth: usize = 0;
let mut in_string = false;
let mut escape_next = false;
for ch in query.chars() {
if escape_next {
escape_next = false;
continue;
}
if ch == '\\' && in_string {
escape_next = true;
continue;
}
if ch == '"' {
in_string = !in_string;
continue;
}
if in_string {
continue;
}
match ch {
'{' => {
current_depth += 1;
max_depth = max_depth.max(current_depth);
},
'}' => {
current_depth = current_depth.saturating_sub(1);
},
_ => {},
}
}
max_depth
}
/// Calculate query complexity score (heuristic).
fn calculate_complexity(&self, query: &str) -> usize {
let mut complexity = 0;
let mut in_string = false;
let mut escape_next = false;
for ch in query.chars() {
if escape_next {
escape_next = false;
continue;
}
if ch == '\\' && in_string {
escape_next = true;
continue;
}
if ch == '"' {
in_string = !in_string;
continue;
}
if in_string {
continue;
}
match ch {
'{' => complexity += 1,
'[' => complexity += 2, // Array selections cost more
'(' => complexity += 1, // Arguments
_ => {},
}
}
complexity
}
}
impl Default for RequestValidator {
fn default() -> Self {
Self {
max_depth: 10,
max_complexity: 100,
validate_depth: true,
validate_complexity: true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_query_validation() {
let validator = RequestValidator::new();
assert!(validator.validate_query("").is_err());
assert!(validator.validate_query(" ").is_err());
}
#[test]
fn test_query_depth_validation() {
let validator = RequestValidator::new().with_max_depth(3);
// Shallow query should pass
let shallow = "{ user { id } }";
assert!(validator.validate_query(shallow).is_ok());
// Deep query should fail
let deep = "{ user { profile { settings { theme } } } }";
assert!(validator.validate_query(deep).is_err());
}
#[test]
fn test_query_complexity_validation() {
let validator = RequestValidator::new().with_max_complexity(5);
// Simple query should pass
let simple = "{ user { id name } }";
assert!(validator.validate_query(simple).is_ok());
// Complex query should fail (many nested fields and array selections)
let complex = "{ user [ id name email [ tags [ name ] ] profile { bio avatar [ url size ] settings { theme notifications } } ] }";
assert!(validator.validate_query(complex).is_err());
}
#[test]
fn test_variables_validation() {
let validator = RequestValidator::new();
// Valid variables object
let valid = serde_json::json!({"id": "123", "name": "John"});
assert!(validator.validate_variables(Some(&valid)).is_ok());
// No variables
assert!(validator.validate_variables(None).is_ok());
// Invalid: variables is not an object
let invalid = serde_json::json!([1, 2, 3]);
assert!(validator.validate_variables(Some(&invalid)).is_err());
}
#[test]
fn test_depth_calculation_with_strings() {
let validator = RequestValidator::new();
// Query with string containing braces should not affect depth
let query = r#"{ user { description: "Has { and }" } }"#;
let depth = validator.calculate_depth(query);
assert_eq!(depth, 2);
}
#[test]
fn test_disable_validation() {
let validator = RequestValidator::new()
.with_depth_validation(false)
.with_complexity_validation(false)
.with_max_depth(1)
.with_max_complexity(1);
// Even very deep query should pass when validation is disabled
let deep = "{ a { b { c { d { e { f } } } } } }";
assert!(validator.validate_query(deep).is_ok());
}
}