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
use crate::executable::Operation;
use crate::executable::Selection;
use crate::executable::SelectionSet;
use crate::parser::SourceSpan;
use crate::validation::Valid;
use crate::ExecutableDocument;

const MAX_LISTS_DEPTH: u32 = 3;

#[derive(Debug)]
pub struct DeeplyNestedIntrospectionListError {
    pub location: Option<SourceSpan>,
}

pub fn check_introspection_max_depth(
    document: &Valid<ExecutableDocument>,
    operation: &Operation,
) -> Result<(), DeeplyNestedIntrospectionListError> {
    let initial_depth = 0;
    check_selection_set(document, initial_depth, &operation.selection_set)
}

fn check_selection_set(
    document: &Valid<ExecutableDocument>,
    depth_so_far: u32,
    selection_set: &SelectionSet,
) -> Result<(), DeeplyNestedIntrospectionListError> {
    for selection in &selection_set.selections {
        match selection {
            Selection::InlineFragment(inline) => {
                check_selection_set(document, depth_so_far, &inline.selection_set)?
            }
            Selection::FragmentSpread(spread) => {
                // Validation ensures that `Valid<ExecutableDocument>` does not contain fragment cycles
                if let Some(def) = document.fragments.get(&spread.fragment_name) {
                    check_selection_set(document, depth_so_far, &def.selection_set)?
                }
            }
            Selection::Field(field) => {
                let mut depth = depth_so_far;
                if matches!(
                    field.name.as_str(),
                    "fields" | "interfaces" | "possibleTypes" | "inputFields"
                ) {
                    depth += 1;
                    if depth >= MAX_LISTS_DEPTH {
                        return Err(DeeplyNestedIntrospectionListError {
                            location: field.name.location(),
                        });
                    }
                }
                check_selection_set(document, depth, &field.selection_set)?
            }
        }
    }
    Ok(())
}