Skip to main content

ruso_runtime/runtime/
spec.rs

1//! Declarative description of what a program sends: its probes and the check's
2//! metadata. The compiler fills these in; the executor reads them.
3
4use crate::contract::{HttpMethod, ObjectBody};
5
6/// One HTTP probe, as declared by an `http` block.
7///
8/// Every `Option` field means "not stated in the script" and falls back to the
9/// executor's configuration rather than to a hardcoded default here. At most
10/// one of the body fields is set — the grammar allows only one body form per
11/// probe.
12#[derive(Debug, Clone, PartialEq)]
13pub struct HttpRequestSpec {
14    /// Request method. Defaults to `GET`.
15    pub method: HttpMethod,
16    /// Request path, resolved against the scan target's base URL. May contain
17    /// `{{ var }}` placeholders.
18    pub path: String,
19    /// Per-probe timeout as a duration literal (`"5s"`); `None` uses the
20    /// executor's timeout.
21    pub timeout: Option<String>,
22    /// Whether to follow redirects; `None` uses the executor's setting.
23    pub follow_redirect: Option<bool>,
24    /// Whether to verify the TLS certificate; `None` uses the executor's
25    /// setting. Set to `false` only for checks that must reach a host with a
26    /// broken chain.
27    pub verify_ssl: Option<bool>,
28    /// Proxy URL for this probe; `None` uses the executor's proxy, if any.
29    pub proxy: Option<String>,
30    /// `User-Agent` override for this probe.
31    pub user_agent: Option<String>,
32    /// Extra request headers, in source order.
33    pub headers: Vec<(String, String)>,
34    /// Request cookies, in source order.
35    pub cookies: Vec<(String, String)>,
36    /// Query-string parameters appended to `path`, in source order.
37    pub queries: Vec<(String, String)>,
38    /// A `data { … }` body, sent form-urlencoded.
39    pub data_body: Option<ObjectBody>,
40    /// A `json { … }` body, sent as `application/json`.
41    pub json_body: Option<ObjectBody>,
42    /// A raw string body, sent verbatim.
43    pub raw_body: Option<String>,
44    /// A hex-encoded byte body, for non-UTF-8 payloads.
45    pub body_bytes: Option<String>,
46    /// A `multipart { … }` body, sent as `multipart/form-data`.
47    pub multipart_body: Option<ObjectBody>,
48}
49
50impl Default for HttpRequestSpec {
51    fn default() -> Self {
52        Self {
53            method: HttpMethod::Get,
54            path: "/".into(),
55            timeout: None,
56            follow_redirect: None,
57            verify_ssl: None,
58            proxy: None,
59            user_agent: None,
60            headers: Vec::new(),
61            cookies: Vec::new(),
62            queries: Vec::new(),
63            data_body: None,
64            json_body: None,
65            raw_body: None,
66            body_bytes: None,
67            multipart_body: None,
68        }
69    }
70}
71
72/// Generic socket options (shared by dns/tcp/udp probes).
73#[derive(Debug, Clone, PartialEq)]
74pub struct SocketProbeSpec {
75    /// Target host. Often `{{scan_host}}` so it follows the CLI `--target`.
76    pub host: String,
77    /// Target port. Required at runtime for `tcp`/`udp`; for `dns`, its
78    /// presence selects wire mode over the OS resolver.
79    pub port: Option<u16>,
80    /// Optional payload bytes to send on connect.
81    pub payload: Option<Vec<u8>>,
82    /// TLS handshake before application data (TCP only).
83    pub tls: bool,
84    /// Keep connection open across multiple `send` on the same probe name.
85    pub session: bool,
86    /// Maximum bytes to read per exchange (default 65536).
87    pub read_max: u32,
88    /// After first read, keep reading until idle for this many ms (0 = single read).
89    pub read_idle_ms: u32,
90}
91
92impl Default for SocketProbeSpec {
93    fn default() -> Self {
94        Self {
95            host: String::new(),
96            port: None,
97            payload: None,
98            tls: false,
99            session: false,
100            read_max: 65_536,
101            read_idle_ms: 0,
102        }
103    }
104}
105
106/// A probe definition, tagged by transport.
107#[derive(Debug, Clone, PartialEq)]
108pub enum ProbeKind {
109    /// HTTP/HTTPS request.
110    Http(HttpRequestSpec),
111    /// DNS probe — OS resolver or wire mode (see [`SocketProbeSpec`]).
112    Dns(SocketProbeSpec),
113    /// Raw TCP (optionally TLS).
114    Tcp(SocketProbeSpec),
115    /// Raw UDP.
116    Udp(SocketProbeSpec),
117}
118
119/// The `metadata { … }` block of a check — describes the finding it emits.
120///
121/// Only `name` is required to emit a finding; the rest enriches the report and
122/// is needed to publish to the registry.
123#[derive(Debug, Clone, Default)]
124pub struct CheckMetadata {
125    /// Short finding title (the registry slug derives from this).
126    pub name: Option<String>,
127    /// What the check does.
128    pub description: Option<String>,
129    /// The risk if the check is positive.
130    pub impact: Option<String>,
131    /// Finding severity (defaults to `Info` when unset).
132    pub severity: Option<crate::contract::Severity>,
133    /// Check author.
134    pub author: Option<String>,
135    /// Associated CVE identifiers.
136    pub cve: Vec<String>,
137    /// Associated CWE identifiers.
138    pub cwe: Vec<String>,
139    /// Reference URLs (advisories, docs).
140    pub references: Vec<String>,
141    /// CVSS vector strings (e.g. base + temporal).
142    pub cvss: Vec<String>,
143    /// CVSS numeric scores.
144    pub cvss_score: Vec<String>,
145    /// Single free-text remediation note. The language rejects more than one
146    /// `mitigation` line per script at compile time (unlike `cve`/`cwe`/
147    /// `references`/`tags`, which accumulate into lists).
148    pub mitigation: Option<String>,
149    /// Free-form discovery labels (many per check).
150    pub tags: Vec<String>,
151    /// SemVer string; required at publish time, optional for local use.
152    pub version: Option<String>,
153    /// Single curated category (e.g. `web`, `network`, `database`).
154    /// Distinct from `tags`: one-per-script, used for "scan everything
155    /// in this family" selection. The allowed set is enforced by the
156    /// registry at publish time, not here.
157    pub family: Option<String>,
158}
159
160/// The non-executable part of a compiled program: the probe table and the
161/// finding metadata. The instruction stream lives in [`BytecodeProgram`].
162///
163/// [`BytecodeProgram`]: crate::BytecodeProgram
164#[derive(Debug, Clone)]
165pub struct ProgramSpec {
166    /// Probes keyed by their script name (e.g. `home`, `redis`).
167    pub probes: std::collections::HashMap<String, ProbeKind>,
168    /// The check's finding metadata.
169    pub metadata: CheckMetadata,
170}
171
172impl SocketProbeSpec {
173    /// True when this spec uses the OS resolver (a `dns` probe with neither
174    /// `port` nor `payload`) rather than DNS wire mode.
175    pub fn is_dns_resolver_mode(&self) -> bool {
176        self.port.is_none() && self.payload.is_none()
177    }
178}