gqlprobe 1.0.0

Fast GraphQL security scanner - schema-aware introspection, injection, DoS, CSRF, auth bypass, SARIF
Documentation
//! [`AttackModule`] trait and built-in module registry.

use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use secfinding::Finding;

use crate::context::ModuleRun;
use crate::modules::{
    alias_abuse, auth_bypass, batching, csrf, directive_abuse, field_suggestion, graphql_injection,
    idor_probe, information_disclosure, introspection, permissions, query_depth, rate_limit,
    subscription_abuse,
};

/// Detection module interface - all built-in checks implement this trait.
#[async_trait]
pub trait AttackModule: Send + Sync {
    /// Stable module identifier (e.g. `GQL-001`).
    fn id(&self) -> &'static str;
    /// Human-readable module name.
    fn name(&self) -> &'static str;
    /// Run detection and return findings.
    async fn detect(&self, run: &ModuleRun<'_>) -> Result<Vec<Finding>>;
}

macro_rules! module_impl {
    ($ty:ty, $id:expr, $name:expr, $func:path) => {
        #[async_trait]
        impl AttackModule for $ty {
            fn id(&self) -> &'static str {
                $id
            }
            fn name(&self) -> &'static str {
                $name
            }
            async fn detect(&self, run: &ModuleRun<'_>) -> Result<Vec<Finding>> {
                $func(run.client, run.ctx).await
            }
        }
    };
}

/// GQL-001 Introspection.
pub struct IntrospectionModule;
module_impl!(
    IntrospectionModule,
    "GQL-001",
    "Introspection Enabled",
    introspection::test_with_context
);

/// GQL-002 Query depth DoS.
pub struct QueryDepthModule;
module_impl!(
    QueryDepthModule,
    "GQL-002",
    "Query Depth DoS",
    query_depth::test_with_context
);

/// GQL-003 Batching abuse.
pub struct BatchingModule;
module_impl!(
    BatchingModule,
    "GQL-003",
    "Batching Abuse",
    batching::test_with_context
);

/// GQL-004 Field suggestion enumeration.
pub struct FieldSuggestionModule;
module_impl!(
    FieldSuggestionModule,
    "GQL-004",
    "Field Suggestion Enumeration",
    field_suggestion::test_with_context
);

/// GQL-005 Directive abuse.
pub struct DirectiveAbuseModule;
module_impl!(
    DirectiveAbuseModule,
    "GQL-005",
    "Directive Abuse",
    directive_abuse::test_with_context
);

/// GQL-006 GraphQL injection.
pub struct GraphqlInjectionModule;
module_impl!(
    GraphqlInjectionModule,
    "GQL-006",
    "GraphQL Injection",
    graphql_injection::test_with_context
);

/// GQL-007 CSRF.
pub struct CsrfModule;
module_impl!(CsrfModule, "GQL-007", "CSRF", csrf::test_with_context);

/// GQL-008 Subscription abuse.
pub struct SubscriptionAbuseModule;
module_impl!(
    SubscriptionAbuseModule,
    "GQL-008",
    "Subscription Abuse",
    subscription_abuse::test_with_context
);

/// GQL-009 Information disclosure.
pub struct InformationDisclosureModule;
module_impl!(
    InformationDisclosureModule,
    "GQL-009",
    "Information Disclosure",
    information_disclosure::test_with_context
);

/// GQL-010 Authentication bypass.
pub struct AuthBypassModule;
module_impl!(
    AuthBypassModule,
    "GQL-010",
    "Authentication Bypass",
    auth_bypass::test_with_context
);

/// GQL-011 Alias abuse (standalone).
pub struct AliasAbuseModule;
module_impl!(
    AliasAbuseModule,
    "GQL-011",
    "Alias Abuse",
    alias_abuse::test_with_context
);

/// GQL-012 Schema permission audit.
pub struct PermissionsModule;
module_impl!(
    PermissionsModule,
    "GQL-012",
    "Permission Audit",
    permissions::test_with_context
);

/// GQL-013 IDOR / BOLA probe.
pub struct IdorProbeModule;
module_impl!(
    IdorProbeModule,
    "GQL-013",
    "IDOR / BOLA Probe",
    idor_probe::test_with_context
);

/// GQL-014 Rate-limit burst probe.
pub struct RateLimitModule;
module_impl!(
    RateLimitModule,
    "GQL-014",
    "Rate Limit Probe",
    rate_limit::test_with_context
);

/// Default module registry (all built-in modules).
pub fn default_registry() -> Vec<Arc<dyn AttackModule>> {
    vec![
        Arc::new(IntrospectionModule),
        Arc::new(QueryDepthModule),
        Arc::new(BatchingModule),
        Arc::new(FieldSuggestionModule),
        Arc::new(DirectiveAbuseModule),
        Arc::new(GraphqlInjectionModule),
        Arc::new(CsrfModule),
        Arc::new(SubscriptionAbuseModule),
        Arc::new(InformationDisclosureModule),
        Arc::new(AuthBypassModule),
        Arc::new(AliasAbuseModule),
        Arc::new(PermissionsModule),
        Arc::new(IdorProbeModule),
        Arc::new(RateLimitModule),
    ]
}

/// Look up modules by ID.
pub fn registry_for_ids(ids: &[&str]) -> Vec<Arc<dyn AttackModule>> {
    default_registry()
        .into_iter()
        .filter(|m| ids.iter().any(|id| *id == m.id()))
        .collect()
}