|
| 1 | +//! Validation rule checking whether a GraphQL operation contains introspection (`__schema` or |
| 2 | +//! `__type` fields). |
| 3 | +
|
| 4 | +use crate::{ |
| 5 | + ast::Field, |
| 6 | + parser::Spanning, |
| 7 | + validation::{ValidatorContext, Visitor}, |
| 8 | + value::ScalarValue, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Validation rule checking whether a GraphQL operation contains introspection (`__schema` or |
| 12 | +/// `__type` fields). |
| 13 | +pub struct DisableIntrospection; |
| 14 | + |
| 15 | +/// Produces a new [`DisableIntrospection`] validation rule. |
| 16 | +#[inline] |
| 17 | +#[must_use] |
| 18 | +pub fn factory() -> DisableIntrospection { |
| 19 | + DisableIntrospection |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a, S> Visitor<'a, S> for DisableIntrospection |
| 23 | +where |
| 24 | + S: ScalarValue, |
| 25 | +{ |
| 26 | + fn enter_field( |
| 27 | + &mut self, |
| 28 | + context: &mut ValidatorContext<'a, S>, |
| 29 | + field: &'a Spanning<Field<S>>, |
| 30 | + ) { |
| 31 | + let field_name = field.item.name.item; |
| 32 | + if matches!(field_name, "__schema" | "__type") { |
| 33 | + context.report_error(&error_message(field_name), &[field.item.name.span.start]); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn error_message(field_name: &str) -> String { |
| 39 | + format!("GraphQL introspection is not allowed, but the operation contained `{field_name}`") |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::{error_message, factory}; |
| 45 | + |
| 46 | + use crate::{ |
| 47 | + parser::SourcePosition, |
| 48 | + validation::{expect_fails_rule, expect_passes_rule, RuleError}, |
| 49 | + value::DefaultScalarValue, |
| 50 | + }; |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn allows_regular_fields() { |
| 54 | + // language=GraphQL |
| 55 | + expect_passes_rule::<_, _, DefaultScalarValue>( |
| 56 | + factory, |
| 57 | + r#" |
| 58 | + query { |
| 59 | + user { |
| 60 | + name |
| 61 | + ... on User { |
| 62 | + email |
| 63 | + } |
| 64 | + alias: email |
| 65 | + ... { |
| 66 | + typeless |
| 67 | + } |
| 68 | + friends { |
| 69 | + name |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + "#, |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn allows_typename_field() { |
| 79 | + // language=GraphQL |
| 80 | + expect_passes_rule::<_, _, DefaultScalarValue>( |
| 81 | + factory, |
| 82 | + r#" |
| 83 | + query { |
| 84 | + __typename |
| 85 | + user { |
| 86 | + __typename |
| 87 | + ... on User { |
| 88 | + __typename |
| 89 | + } |
| 90 | + ... { |
| 91 | + __typename |
| 92 | + } |
| 93 | + friends { |
| 94 | + __typename |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + "#, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn forbids_query_schema() { |
| 104 | + // language=GraphQL |
| 105 | + expect_fails_rule::<_, _, DefaultScalarValue>( |
| 106 | + factory, |
| 107 | + r#" |
| 108 | + query { |
| 109 | + __schema { |
| 110 | + queryType { |
| 111 | + name |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + "#, |
| 116 | + &[RuleError::new( |
| 117 | + &error_message("__schema"), |
| 118 | + &[SourcePosition::new(37, 2, 16)], |
| 119 | + )], |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn forbids_query_type() { |
| 125 | + // language=GraphQL |
| 126 | + expect_fails_rule::<_, _, DefaultScalarValue>( |
| 127 | + factory, |
| 128 | + r#" |
| 129 | + query { |
| 130 | + __type( |
| 131 | + name: "Query" |
| 132 | + ) { |
| 133 | + name |
| 134 | + } |
| 135 | + } |
| 136 | + "#, |
| 137 | + &[RuleError::new( |
| 138 | + &error_message("__type"), |
| 139 | + &[SourcePosition::new(37, 2, 16)], |
| 140 | + )], |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn forbids_field_type() { |
| 146 | + // language=GraphQL |
| 147 | + expect_fails_rule::<_, _, DefaultScalarValue>( |
| 148 | + factory, |
| 149 | + r#" |
| 150 | + query { |
| 151 | + user { |
| 152 | + name |
| 153 | + ... on User { |
| 154 | + email |
| 155 | + } |
| 156 | + alias: email |
| 157 | + ... { |
| 158 | + typeless |
| 159 | + } |
| 160 | + friends { |
| 161 | + name |
| 162 | + } |
| 163 | + __type |
| 164 | + } |
| 165 | + } |
| 166 | + "#, |
| 167 | + &[RuleError::new( |
| 168 | + &error_message("__type"), |
| 169 | + &[SourcePosition::new(370, 14, 20)], |
| 170 | + )], |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn forbids_field_schema() { |
| 176 | + // language=GraphQL |
| 177 | + expect_fails_rule::<_, _, DefaultScalarValue>( |
| 178 | + factory, |
| 179 | + r#" |
| 180 | + query { |
| 181 | + user { |
| 182 | + name |
| 183 | + ... on User { |
| 184 | + email |
| 185 | + } |
| 186 | + alias: email |
| 187 | + ... { |
| 188 | + typeless |
| 189 | + } |
| 190 | + friends { |
| 191 | + name |
| 192 | + } |
| 193 | + __schema |
| 194 | + } |
| 195 | + } |
| 196 | + "#, |
| 197 | + &[RuleError::new( |
| 198 | + &error_message("__schema"), |
| 199 | + &[SourcePosition::new(370, 14, 20)], |
| 200 | + )], |
| 201 | + ); |
| 202 | + } |
| 203 | +} |
0 commit comments