cratefield-module-guardrails 0.1.4

Cratefield module: the policy engine every automated value-moving action must pass - hard denies, allowlists, required simulation, micro-USD caps, a kill switch and audit, default deny and fail closed
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! The allowlists: what a subject may touch at all. Default deny — an
//! allowlist that names nothing allows nothing.
//!
//! A policy has a **global** layer and a **per-subject** layer, each an
//! [`Allowlist`]; the effective allowlist for a subject is the union of the
//! two, so a venture can name its common contracts once and per-actor
//! extras on top. Caps are different: they live per subject only, and they
//! are required — a subject with no caps is a subject that moves nothing.
//!
//! EVM addresses may be inserted in any case (checksummed or not); the
//! builders normalise to lowercase, and comparisons on EVM are
//! case-insensitive. Solana addresses are matched exactly.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use crate::action::{Action, Chain, Request};
use crate::audit::{DenyReason, NotAllowlisted};
use crate::caps::Caps;
use crate::deny::{
    SEL_TRANSFER, SEL_TRANSFER_FROM, SYS_TRANSFER, TOK_TRANSFER, TOK_TRANSFER_CHECKED,
    system_discriminant, tok_discriminant, word_addr,
};

/// One layer of the allowlist: the chains, contracts, programs, destinations
/// and spenders named at this layer.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Allowlist {
    /// Chains this layer may act on.
    pub chains: BTreeSet<Chain>,
    /// EVM contracts this layer may call, each with its explicit selector
    /// set. A selector outside the set is refused even though the contract
    /// is known.
    pub evm_contracts: BTreeMap<String, BTreeSet<u32>>,
    /// Solana programs this layer may invoke.
    pub solana_programs: BTreeSet<String>,
    /// Destination addresses this layer may move funds to.
    pub destinations: BTreeSet<String>,
    /// Spenders this layer may approve or permit.
    pub spenders: BTreeSet<String>,
}

impl Allowlist {
    /// Allows a chain.
    #[must_use]
    pub fn chain(mut self, chain: Chain) -> Self {
        self.chains.insert(chain);
        self
    }

    /// Allows a contract for the given selectors; the address is normalised
    /// to lowercase.
    #[must_use]
    pub fn contract(mut self, address: &str, selectors: &[u32]) -> Self {
        self.evm_contracts
            .entry(address.to_ascii_lowercase())
            .or_default()
            .extend(selectors.iter().copied());
        self
    }

    /// Allows a Solana program.
    #[must_use]
    pub fn program(mut self, program_id: &str) -> Self {
        self.solana_programs.insert(program_id.to_owned());
        self
    }

    /// Allows a destination address.
    #[must_use]
    pub fn destination(mut self, address: &str) -> Self {
        self.destinations.insert(address.to_owned());
        self
    }

    /// Allows a spender.
    #[must_use]
    pub fn spender(mut self, address: &str) -> Self {
        self.spenders.insert(address.to_owned());
        self
    }

    /// Whether `address` names a destination: exact match always, and
    /// case-insensitive on EVM, where a checksummed spelling is never a
    /// different address.
    #[must_use]
    pub fn names_destination(&self, address: &str, evm: bool) -> bool {
        self.destinations.contains(address)
            || (evm
                && self
                    .destinations
                    .iter()
                    .any(|a| a.eq_ignore_ascii_case(address)))
    }

    /// Whether `address` names a spender; EVM comparisons are
    /// case-insensitive.
    #[must_use]
    pub fn names_spender(&self, address: &str) -> bool {
        self.spenders.contains(address)
            || self
                .spenders
                .iter()
                .any(|a| a.eq_ignore_ascii_case(address))
    }
}

/// One subject's layer of the policy: an [`Allowlist`], the subject's caps,
/// and whether the hygiene scan may revoke automatically.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubjectPolicy {
    /// What this subject may touch, on top of the global layer.
    #[serde(flatten)]
    pub allowlist: Allowlist,
    /// The spend caps. `None` denies everything: caps are required.
    #[serde(default)]
    pub caps: Option<Caps>,
    /// Whether the hygiene scan may send its revoke transactions through
    /// `check` automatically.
    #[serde(default)]
    pub auto_revoke: bool,
}

impl SubjectPolicy {
    /// An empty policy: allows nothing.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Allows a chain.
    #[must_use]
    pub fn chain(mut self, chain: Chain) -> Self {
        self.allowlist = self.allowlist.chain(chain);
        self
    }

    /// Allows a contract for the given selectors.
    #[must_use]
    pub fn contract(mut self, address: &str, selectors: &[u32]) -> Self {
        self.allowlist = self.allowlist.contract(address, selectors);
        self
    }

    /// Allows a Solana program.
    #[must_use]
    pub fn program(mut self, program_id: &str) -> Self {
        self.allowlist = self.allowlist.program(program_id);
        self
    }

    /// Allows a destination address.
    #[must_use]
    pub fn destination(mut self, address: &str) -> Self {
        self.allowlist = self.allowlist.destination(address);
        self
    }

    /// Allows a spender.
    #[must_use]
    pub fn spender(mut self, address: &str) -> Self {
        self.allowlist = self.allowlist.spender(address);
        self
    }

    /// Sets the caps.
    #[must_use]
    pub fn caps(mut self, caps: Caps) -> Self {
        self.caps = Some(caps);
        self
    }

    /// Lets the hygiene scan revoke automatically.
    #[must_use]
    pub fn auto_revoke(mut self, yes: bool) -> Self {
        self.auto_revoke = yes;
        self
    }
}

/// The whole policy: a global layer plus one [`SubjectPolicy`] per subject.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Policy {
    /// What any subject may touch.
    #[serde(flatten)]
    pub allowlist: Allowlist,
    /// The per-subject layers, keyed by subject id.
    pub subjects: BTreeMap<String, SubjectPolicy>,
}

impl Policy {
    /// An empty policy: allows nothing, denies everything.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a subject's layer.
    #[must_use]
    pub fn subject(mut self, id: &str, policy: SubjectPolicy) -> Self {
        self.subjects.insert(id.to_owned(), policy);
        self
    }

    /// Allows a chain globally.
    #[must_use]
    pub fn chain(mut self, chain: Chain) -> Self {
        self.allowlist = self.allowlist.chain(chain);
        self
    }

    /// Allows a contract globally for the given selectors.
    #[must_use]
    pub fn contract(mut self, address: &str, selectors: &[u32]) -> Self {
        self.allowlist = self.allowlist.contract(address, selectors);
        self
    }

    /// Allows a Solana program globally.
    #[must_use]
    pub fn program(mut self, program_id: &str) -> Self {
        self.allowlist = self.allowlist.program(program_id);
        self
    }

    /// Allows a destination globally.
    #[must_use]
    pub fn destination(mut self, address: &str) -> Self {
        self.allowlist = self.allowlist.destination(address);
        self
    }

    /// Allows a spender globally.
    #[must_use]
    pub fn spender(mut self, address: &str) -> Self {
        self.allowlist = self.allowlist.spender(address);
        self
    }

    fn subject_of(&self, subject: &str) -> Option<&SubjectPolicy> {
        self.subjects.get(subject)
    }

    /// Whether either the subject's layer or the global layer answers yes.
    fn allowed(&self, subject: &str, f: impl Fn(&Allowlist) -> bool) -> bool {
        self.subject_of(subject).is_some_and(|s| f(&s.allowlist)) || f(&self.allowlist)
    }

    /// Whether `chain` is on the effective allowlist.
    #[must_use]
    pub fn chain_allowed(&self, subject: &str, chain: &Chain) -> bool {
        self.allowed(subject, |l| l.chains.contains(chain))
    }

    /// Whether `contract` is known to the effective allowlist, for any
    /// selector.
    #[must_use]
    pub fn contract_known(&self, subject: &str, contract: &str) -> bool {
        let lower = contract.to_ascii_lowercase();
        self.allowed(subject, |l| l.evm_contracts.contains_key(&lower))
    }

    /// Whether the effective allowlist lets `subject` call `selector` on
    /// `contract`.
    #[must_use]
    pub fn selector_allowed(&self, subject: &str, contract: &str, selector: u32) -> bool {
        let lower = contract.to_ascii_lowercase();
        self.allowed(subject, |l| {
            l.evm_contracts
                .get(&lower)
                .is_some_and(|sels| sels.contains(&selector))
        })
    }

    /// Whether `program` is on the effective allowlist.
    #[must_use]
    pub fn program_allowed(&self, subject: &str, program: &str) -> bool {
        self.allowed(subject, |l| l.solana_programs.contains(program))
    }

    /// Whether `address` is an allowed destination. EVM comparisons are
    /// case-insensitive, Solana exact.
    #[must_use]
    pub fn destination_allowed(&self, subject: &str, address: &str, evm: bool) -> bool {
        self.allowed(subject, |l| l.names_destination(address, evm))
    }

    /// Whether `spender` is an allowed spender. EVM comparisons are
    /// case-insensitive.
    #[must_use]
    pub fn spender_allowed(&self, subject: &str, spender: &str) -> bool {
        self.allowed(subject, |l| l.names_spender(spender))
    }

    /// The subject's caps, which must exist for anything to move.
    #[must_use]
    pub fn caps_for(&self, subject: &str) -> Option<&Caps> {
        self.subject_of(subject).and_then(|s| s.caps.as_ref())
    }

    /// Whether the subject's hygiene scan revokes automatically.
    #[must_use]
    pub fn auto_revokes(&self, subject: &str) -> bool {
        self.subject_of(subject).is_some_and(|s| s.auto_revoke)
    }
}

/// The allowlist half of the engine: everything that is refused because the
/// policy does not name it. Runs after the hard denies, which is why it may
/// assume the request's shape is not itself dangerous.
pub fn allowlist_denies(req: &Request, policy: &Policy) -> Vec<DenyReason> {
    let subject = &req.subject;
    let mut out = Vec::new();
    if !policy.chain_allowed(subject, &req.chain()) {
        out.push(DenyReason::NotAllowlisted(NotAllowlisted::Chain));
        return out;
    }
    match &req.action {
        Action::EvmTx {
            to,
            data,
            authorizations,
            ..
        } => {
            match to {
                None => out.push(DenyReason::NotAllowlisted(NotAllowlisted::Contract(
                    "<contract creation>".to_owned(),
                ))),
                Some(target) if data.is_empty() => {
                    if !policy.destination_allowed(subject, target, true) {
                        out.push(DenyReason::NotAllowlisted(NotAllowlisted::Destination(
                            target.clone(),
                        )));
                    }
                }
                Some(target) => {
                    let Some(sel) = selector_of(data) else {
                        // Shorter than a selector and not empty: malformed.
                        out.push(DenyReason::NotAllowlisted(NotAllowlisted::Selector(
                            "<malformed calldata>".to_owned(),
                        )));
                        return out;
                    };
                    if !policy.contract_known(subject, target) {
                        out.push(DenyReason::NotAllowlisted(NotAllowlisted::Contract(
                            target.clone(),
                        )));
                    } else if !policy.selector_allowed(subject, target, sel) {
                        out.push(DenyReason::NotAllowlisted(NotAllowlisted::Selector(
                            format!("0x{sel:08x}"),
                        )));
                    }
                    // A transfer that hides inside an allowlisted call still
                    // lands somewhere: its recipient is a destination.
                    // Calldata: selector (4 bytes), then words — so
                    // `transfer(to, amount)` carries `to` at byte 4, and
                    // `transferFrom(from, to, amount)` carries it at 36.
                    let recipient_at: Option<usize> = match sel {
                        SEL_TRANSFER => Some(4),
                        SEL_TRANSFER_FROM => Some(36),
                        _ => None,
                    };
                    if let Some(at) = recipient_at
                        && let Some(addr) = word_addr(data, at)
                        && !policy.destination_allowed(subject, &addr, true)
                    {
                        out.push(DenyReason::NotAllowlisted(NotAllowlisted::Destination(
                            addr,
                        )));
                    }
                }
            }
            for a in authorizations {
                if !policy.contract_known(subject, &a.address) {
                    out.push(DenyReason::NotAllowlisted(NotAllowlisted::Delegate(
                        a.address.clone(),
                    )));
                }
            }
        }
        Action::EvmTypedData {
            verifying_contract, ..
        } => {
            if !policy.contract_known(subject, verifying_contract) {
                out.push(DenyReason::NotAllowlisted(NotAllowlisted::Contract(
                    verifying_contract.clone(),
                )));
            }
        }
        Action::SolanaTx { instructions, .. } => {
            for ins in instructions {
                if !policy.program_allowed(subject, &ins.program_id) {
                    out.push(DenyReason::NotAllowlisted(NotAllowlisted::Program(
                        ins.program_id.clone(),
                    )));
                    continue;
                }
                if let Some(dest) = solana_destination(ins)
                    && !policy.destination_allowed(subject, &dest, false)
                {
                    out.push(DenyReason::NotAllowlisted(NotAllowlisted::Destination(
                        dest,
                    )));
                }
            }
        }
    }
    out
}

fn selector_of(data: &[u8]) -> Option<u32> {
    data.first_chunk::<4>().map(|s| u32::from_be_bytes(*s))
}

/// The destination account a Solana instruction moves funds to, if it is a
/// move-funds instruction the policy knows: System `Transfer` (to is
/// account 1), SPL `Transfer` (account 1) and `TransferChecked` (account 2).
#[must_use]
pub fn solana_destination(ins: &crate::action::SolanaInstruction) -> Option<String> {
    if ins.program_id == crate::deny::SYSTEM_PROGRAM {
        if system_discriminant(ins) == SYS_TRANSFER {
            return ins.accounts.get(1).cloned();
        }
        return None;
    }
    if ins.program_id == crate::deny::SPL_TOKEN_PROGRAM
        || ins.program_id == crate::deny::SPL_TOKEN_2022_PROGRAM
    {
        return match tok_discriminant(ins) {
            d if d == TOK_TRANSFER => ins.accounts.get(1).cloned(),
            d if d == TOK_TRANSFER_CHECKED => ins.accounts.get(2).cloned(),
            _ => None,
        };
    }
    None
}