use anyhow::Result;
use crate::context::ScanContext;
use scanclient::reqwest::header::CONTENT_TYPE;
use scanclient::ScanClient;
use secfinding::{Finding, FindingKind, Severity};
use serde_json::json;
use tracing::debug;
pub const SUBSCRIPTION_BURST_COUNT: usize = 5;
pub async fn test_with_context(client: &ScanClient, ctx: &ScanContext) -> Result<Vec<Finding>> {
test(client, ctx.url()).await
}
pub async fn test(client: &ScanClient, url: &str) -> Result<Vec<Finding>> {
let mut findings = Vec::new();
debug!("Testing subscription abuse on {}", url);
match test_subscription_over_http(client, url).await? {
SubscriptionResult::Vulnerable(_detail) => {
findings.push(create_finding(
url,
"GraphQL Subscriptions Enabled",
"The GraphQL endpoint has subscriptions enabled. Subscriptions can be abused \
for denial of service through resource exhaustion or for unauthorized data streaming.",
Severity::Medium,
"Ensure subscriptions require authentication and implement rate limiting.",
)?);
}
SubscriptionResult::Limited(_) => {}
SubscriptionResult::Safe => {
debug!("Subscriptions not enabled or properly restricted");
}
SubscriptionResult::Error(e) => {
debug!("Subscription over HTTP test error: {}", e);
}
}
match test_subscription_rate_limit(client, url).await? {
SubscriptionResult::Vulnerable(detail) => {
findings.push(create_finding(
url,
"GraphQL Subscription Rate Limit Missing",
&format!(
"The GraphQL endpoint accepts multiple subscription requests without rate limiting. {detail}"
),
Severity::High,
"Implement rate limiting on subscription creation. Limit concurrent subscriptions per client.",
)?);
}
SubscriptionResult::Limited(detail) => {
findings.push(create_finding(
url,
"GraphQL Subscription Rate Limit Insufficient",
&format!(
"The GraphQL endpoint accepted some subscription requests before rate limiting kicked in. {detail}"
),
Severity::Medium,
"Implement stricter rate limiting on subscription creation. Limit concurrent subscriptions per client.",
)?);
}
SubscriptionResult::Safe => {}
SubscriptionResult::Error(e) => {
debug!("Subscription rate limit test error: {}", e);
}
}
debug!(
"Subscription abuse test complete: {} findings",
findings.len()
);
Ok(findings)
}
#[derive(Debug)]
pub enum SubscriptionResult {
Vulnerable(String),
Limited(String),
Safe,
Error(String),
}
pub async fn test_subscription_over_http(
client: &ScanClient,
url: &str,
) -> Result<SubscriptionResult> {
let query = r"
subscription TestSubscription {
__typename
}
";
let body = json!({ "query": query });
let request = client
.request(scanclient::reqwest::Method::POST, url)
.header(CONTENT_TYPE, "application/json")
.body(body.to_string());
match client.execute(request).await {
Ok(resp) => {
let body_text = resp.body_text().unwrap_or("").to_lowercase();
if resp.status().is_success() && !body_text.contains("error") {
return Ok(SubscriptionResult::Vulnerable(
"Subscription accepted over HTTP".to_string(),
));
}
if body_text.contains("subscription")
&& (body_text.contains("not supported") || body_text.contains("unsupported"))
{
return Ok(SubscriptionResult::Safe);
}
if resp.status().as_u16() == 426 {
return Ok(SubscriptionResult::Safe);
}
Ok(SubscriptionResult::Safe)
}
Err(e) => Ok(SubscriptionResult::Error(e.to_string())),
}
}
pub async fn test_subscription_rate_limit(
client: &ScanClient,
url: &str,
) -> Result<SubscriptionResult> {
let query = r"
subscription RateLimitTest {
__typename
}
";
let mut accepted = 0;
for _ in 0..SUBSCRIPTION_BURST_COUNT {
let body = json!({ "query": query });
let request = client
.request(scanclient::reqwest::Method::POST, url)
.header(CONTENT_TYPE, "application/json")
.body(body.to_string());
match client.execute(request).await {
Ok(resp) => {
if resp.status().is_success() {
let body_text = resp.body_text().unwrap_or_default().to_lowercase();
let is_rejected = body_text.contains("error")
|| body_text.contains("limit")
|| body_text.contains("throttled")
|| body_text.contains("unsupported");
if !is_rejected {
accepted += 1;
}
}
}
Err(_) => break,
}
}
if accepted >= SUBSCRIPTION_BURST_COUNT {
Ok(SubscriptionResult::Vulnerable(
"Multiple subscriptions accepted without rate limiting".to_string(),
))
} else if accepted > 0 {
Ok(SubscriptionResult::Limited(format!(
"Accepted {accepted} subscriptions before rate limit kicked in"
)))
} else {
Ok(SubscriptionResult::Safe)
}
}
pub fn create_finding(
url: &str,
title: &str,
detail: &str,
severity: Severity,
remediation: &str,
) -> Result<Finding> {
Ok(Finding::builder("gqlprobe", url, severity)
.title(title)
.detail(detail)
.kind(FindingKind::Vulnerability)
.tag("graphql")
.tag("subscription")
.tag("dos")
.cwe("CWE-400") .remediation(remediation)
.build()?)
}