perimeterx-fastly-enforcer 2.2.2

PerimeterX Fastly Compute@Edge Rust Enforcer
Documentation
use crate::modules::pxutils;
use crate::px_debug;
use crate::pxconfig::PXConfig;
use fastly::Request;

pub fn should_filter_request(req: &Request, conf: &PXConfig) -> bool {
    // whitelisted extensions
    for ext in &conf.filter_by_extension {
        if req.get_method_str() == "GET"
            && req.get_path().trim().to_lowercase().ends_with(ext.as_str())
        {
            px_debug!("Skipping verification for whitelist ext: {}", ext);
            return true;
        }
    }
    // whitelisted User-Agent
    for ua in &conf.filter_by_user_agent {
        if let Some(header_value) = req.get_header_str_lossy("user-agent") {
            if header_value.as_ref() == ua.trim() {
                px_debug!("Skipping verification for filtered user agent: {}", ua);
                return true;
            }
        }
    }

    // whitelisted path
    for whitelist_prefix in &conf.filter_by_route {
        if req.get_path().trim().starts_with(whitelist_prefix) {
            px_debug!(
                "Skipping verification for filtered prefix: {}",
                whitelist_prefix
            );
            return true;
        }
    }

    // whitelisted IPs
    if let Some(client_ip) = pxutils::extract_ip_from_configured_headers(req, &conf.ip_headers)
        .or_else(|| req.get_client_ip_addr().map(|ip| ip.to_string()))
    {
        for ip in &conf.filter_by_ip {
            if pxutils::ip_matches_filter(client_ip.as_str(), ip.as_str()) {
                px_debug!("Skipping verification for filtered IP: {}", ip);
                return true;
            }
        }
    }

    // whitelisted HTTP methods
    for method in &conf.filter_by_http_method {
        if req.get_method_str().eq_ignore_ascii_case(method.trim()) {
            px_debug!("Skipping verification for filtered method: {}", method);
            return true;
        }
    }

    false
}