ruso_runtime/contract.rs
1//! Shared types embedded in Ruso bytecode (constant pools and probe specs).
2//!
3//! `ruso-script` parses source into an AST that uses these types for matchers,
4//! bodies, and metadata so compiled output matches what `ruso-runtime` executes.
5
6/// Finding severity, in ascending order of urgency (`Info` is the catch-all
7/// used when a check declares no `severity`).
8#[derive(Debug, Clone, PartialEq)]
9pub enum Severity {
10 /// Low impact.
11 Low,
12 /// Moderate impact.
13 Medium,
14 /// High impact.
15 High,
16 /// Critical impact — typically remote code execution or full compromise.
17 Critical,
18 /// Informational; no direct security impact. The default when unset.
19 Info,
20}
21
22impl Severity {
23 /// The lowercase wire name of this severity (`"low"`, `"medium"`, …), as
24 /// it appears in reports and in the registry's metadata.
25 pub fn as_str(&self) -> &'static str {
26 match self {
27 Self::Low => "low",
28 Self::Medium => "medium",
29 Self::High => "high",
30 Self::Critical => "critical",
31 Self::Info => "info",
32 }
33 }
34}
35
36/// HTTP request method for an `http` probe.
37#[derive(Debug, Clone, PartialEq)]
38pub enum HttpMethod {
39 /// `GET`
40 Get,
41 /// `POST`
42 Post,
43 /// `PUT`
44 Put,
45 /// `PATCH`
46 Patch,
47 /// `DELETE`
48 Delete,
49 /// `HEAD`
50 Head,
51 /// `OPTIONS`
52 Options,
53}
54
55/// A structured request body (`data { … }` / `json { … }`): ordered key/value
56/// pairs. Order is preserved so serialization is deterministic.
57#[derive(Debug, Clone, PartialEq)]
58pub struct ObjectBody {
59 /// Key/value pairs in source order.
60 pub pairs: Vec<(String, BodyValue)>,
61}
62
63/// A value inside an [`ObjectBody`] or multipart body.
64#[derive(Debug, Clone, PartialEq)]
65pub enum BodyValue {
66 /// A literal string.
67 String(String),
68 /// A string containing `{{ var }}` placeholders to interpolate at runtime.
69 Interpolation(String),
70 /// A nested object.
71 Object(ObjectBody),
72 /// Hex-encoded raw bytes.
73 Bytes(String),
74 /// A multipart part.
75 Part(InlinePart),
76}
77
78/// One part of a multipart request body.
79#[derive(Debug, Clone, PartialEq)]
80pub struct InlinePart {
81 /// Optional `filename` for a file part.
82 pub filename: Option<String>,
83 /// The part's content.
84 pub body: InlinePartBody,
85}
86
87/// The content of an [`InlinePart`].
88#[derive(Debug, Clone, PartialEq)]
89pub enum InlinePartBody {
90 /// UTF-8 text.
91 Text(String),
92 /// Hex-encoded raw bytes.
93 Bytes(String),
94}
95
96/// A response field selector: which probe (`target`) and which part of its
97/// response (`kind`) a matcher or evidence rule reads.
98#[derive(Debug, Clone, PartialEq)]
99pub struct QualifiedField {
100 /// The probe name the field belongs to (e.g. `home`).
101 pub target: String,
102 /// Which part of the response to read.
103 pub kind: FieldKind,
104}
105
106/// Which part of a probe response a matcher reads.
107#[derive(Debug, Clone, PartialEq)]
108pub enum FieldKind {
109 /// HTTP status code.
110 Status,
111 /// HTTP response body.
112 Body,
113 /// A named HTTP response header.
114 Header(String),
115 /// HTTP round-trip time.
116 ResponseTime,
117 /// HTTP response body size in bytes.
118 ResponseSize,
119 /// Resolver answers (`dns` without `port` / `payload`).
120 Answer,
121 /// Raw probe bytes (tcp / udp / wire dns). Alias: `banner` in scripts.
122 Response,
123 /// The `banner` spelling of [`FieldKind::Response`], kept as its own
124 /// variant so a disassembly reads back the way the script was written.
125 Banner,
126}
127
128/// A single matcher: a response [field](QualifiedField) tested against a
129/// [predicate](MatchPredicate).
130#[derive(Debug, Clone, PartialEq)]
131pub struct QualifiedMatch {
132 /// The response field to read.
133 pub field: QualifiedField,
134 /// The condition the field must satisfy.
135 pub predicate: MatchPredicate,
136}
137
138/// The condition a [`QualifiedField`] is tested against.
139#[derive(Debug, Clone, PartialEq)]
140pub enum MatchPredicate {
141 /// Numeric/string/duration comparison (`==`, `!=`, `<`, …).
142 Compare {
143 /// The comparison operator.
144 op: CmpOp,
145 /// The right-hand value.
146 value: CmpValue,
147 },
148 /// Substring is present.
149 Contains(String),
150 /// Substring is absent.
151 NotContains(String),
152 /// Rust-syntax regular expression matches.
153 Regex(String),
154}
155
156/// Where an `extract` pulls a value from (HTTP only).
157#[derive(Debug, Clone, PartialEq)]
158pub enum ExtractSource {
159 /// From the response body, optionally via a capture-group regex.
160 Body {
161 /// Probe name.
162 target: String,
163 /// Optional regex; capture group 1 (or the whole match) is extracted.
164 regex: Option<String>,
165 },
166 /// From a named response header.
167 Header {
168 /// Probe name.
169 target: String,
170 /// Header name.
171 name: String,
172 },
173}
174
175/// A proof string attached to a finding. Every rule names an explicit source
176/// (`.body`, `.response`, or `.header "<name>"`); an optional `regex` extracts
177/// capture group 1 (or the whole match) from that source instead of taking it
178/// whole.
179#[derive(Debug, Clone, PartialEq)]
180pub enum EvidenceKind {
181 /// HTTP probe response body (`p.body` / `p.body regex '…'`).
182 Body {
183 /// Probe name.
184 target: String,
185 /// Optional regex; `None` takes the whole (truncated) body.
186 pattern: Option<String>,
187 },
188 /// Probe response payload — HTTP body, DNS answers, or socket data
189 /// (`p.response` / `p.response regex '…'`).
190 Response {
191 /// Probe name.
192 target: String,
193 /// Optional regex; `None` takes the whole (truncated) payload.
194 pattern: Option<String>,
195 },
196 /// A named HTTP response header value (`p.header "X" ` / `p.header "X" regex '…'`).
197 Header {
198 /// Probe name.
199 target: String,
200 /// Header name (case-insensitive).
201 name: String,
202 /// Optional regex; `None` takes the whole header value.
203 pattern: Option<String>,
204 },
205}
206
207/// A comparison operator used by [`MatchPredicate::Compare`].
208#[derive(Debug, Clone, Copy, PartialEq)]
209pub enum CmpOp {
210 /// `==`
211 Eq,
212 /// `!=`
213 Ne,
214 /// `<`
215 Lt,
216 /// `>`
217 Gt,
218 /// `<=`
219 Le,
220 /// `>=`
221 Ge,
222}
223
224/// The right-hand operand of a [`MatchPredicate::Compare`].
225#[derive(Debug, Clone, PartialEq)]
226pub enum CmpValue {
227 /// A numeric literal (e.g. a status code or size).
228 Number(u64),
229 /// A string literal.
230 String(String),
231 /// A duration literal (e.g. `500ms`), compared against `response_time`.
232 Duration(String),
233}