vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
//! Catalog entry for `detect_ssrf`.

use crate::ops::security_detection::detector_support::{attacks, DetectionError};

/// Embedded operation spec formerly stored in metadata/spec.toml.
pub const SPEC_TOML: &str = r#"schema_version = 1
id = "security_detection.detect_ssrf"
archetype = "rule-bytes-to-bool"
display_name = "Detect SSRF"
summary = "Returns true for URL payloads targeting loopback, metadata, or private hosts."
category = "C"

[intrinsic]
wgsl = "security_detection_detect_ssrf"

[signature]
inputs = ["Bytes"]
output = "Bool"

laws = []
equivalence_classes = ["metadata_ip", "localhost", "public_url", "t47_cap"]
workgroup_size = [64, 1, 1]
tags = ["security-detection", "ssrf", "owasp", "t47"]
fixtures_dir = "fixtures/"
"#;

/// Embedded reference vectors formerly stored in fixtures/reference-vectors.toml.
pub const REFERENCE_VECTORS_TOML: &str = r#"[[case]]
name = "positive_metadata"
input = "url=http://169.254.169.254/latest/meta-data/"
expected = true

[[case]]
name = "negative_public_url"
input = "url=https://example.com/status"
expected = false
"#;

/// WGSL lowering source for this detector.
pub mod lowering {
    /// Return the detector-specific WGSL source.
    #[must_use]
    pub const fn source() -> &'static str {
        r#"struct Params {
    input_len: u32,
    _pad0: u32,
    _pad1: u32,
    _pad2: u32,
}

@group(0) @binding(0) var<storage, read> input: array<u32>;
@group(0) @binding(1) var<storage, read_write> output: array<u32>;
@group(0) @binding(2) var<uniform> params: Params;

fn lower_ascii(byte: u32) -> u32 {
    if (byte >= 65u && byte <= 90u) {
        return byte + 32u;
    }
    return byte;
}

fn matches_at(index: u32, n0: u32, n1: u32, n2: u32, n3: u32, n4: u32, n5: u32, n6: u32, n7: u32, n8: u32, len: u32) -> bool {
    if (index + len > params.input_len) {
        return false;
    }
    let needle = array<u32, 9>(n0, n1, n2, n3, n4, n5, n6, n7, n8);
    for (var offset = 0u; offset < len; offset = offset + 1u) {
        if (lower_ascii(input[index + offset]) != needle[offset]) {
            return false;
        }
    }
    return true;
}

fn contains_pattern(lane: u32, n0: u32, n1: u32, n2: u32, n3: u32, n4: u32, n5: u32, n6: u32, n7: u32, n8: u32, len: u32) -> bool {
    for (var index = lane; index < params.input_len; index = index + 64u) {
        if (matches_at(index, n0, n1, n2, n3, n4, n5, n6, n7, n8, len)) {
            return true;
        }
    }
    return false;
}

var<workgroup> lane_flags: array<u32, 64>;

@compute @workgroup_size(64)
fn security_detection_detect_ssrf(@builtin(local_invocation_id) lid: vec3<u32>) {
    let lane = lid.x;
    var flags = 0u;
    if (contains_pattern(lane, 104u, 116u, 116u, 112u, 58u, 47u, 47u, 0u, 0u, 7u) ||
        contains_pattern(lane, 104u, 116u, 116u, 112u, 115u, 58u, 47u, 47u, 0u, 8u)) {
        flags = flags | 1u;
    }
    if (contains_pattern(lane, 108u, 111u, 99u, 97u, 108u, 104u, 111u, 115u, 116u, 9u) ||
        contains_pattern(lane, 49u, 50u, 55u, 46u, 48u, 46u, 48u, 46u, 49u, 9u) ||
        contains_pattern(lane, 48u, 46u, 48u, 46u, 48u, 46u, 48u, 0u, 0u, 7u) ||
        contains_pattern(lane, 49u, 48u, 46u, 0u, 0u, 0u, 0u, 0u, 0u, 3u)) {
        flags = flags | 2u;
    }
    lane_flags[lane] = flags;
    workgroupBarrier();
    if (lane == 0u) {
        var combined = 0u;
        for (var i = 0u; i < 64u; i = i + 1u) {
            combined = combined | lane_flags[i];
        }
        output[0] = select(0u, 1u, (combined & 3u) == 3u);
    }
}
"#
    }
}

/// Return true for URL payloads targeting loopback, metadata, or private hosts.
///
/// # Errors
///
/// Returns `Fix: ...` when input exceeds 64 MiB.
pub fn detect_ssrf(input: &[u8]) -> Result<bool, DetectionError> {
    attacks::detect_ssrf(input)
}

/// Compatibility surface for the previous generated implementation module.
pub mod implementation {
    pub use super::detect_ssrf;
    /// Compatibility module for callers that used the generated kernel path.
    pub mod kernel {
        pub use super::super::detect_ssrf;
    }

    /// Compatibility module for callers that used the generated lowering path.
    pub mod lowering {
        /// Compatibility module for callers that used `implementation::lowering::wgsl`.
        pub mod wgsl {
            pub use super::super::super::lowering::source;
        }
    }
}