1
//! Security gate for the template render surface (Slice D).
2
//!
3
//! Templates are per-user nomiscript that must reach NO mutating and NO
4
//! secret/credential/auth native. Because Slice B keeps the per-user JWT
5
//! private key in the same per-user DB the eval channel reads, an escape here
6
//! would let a template exfiltrate the signing key — so these compile-failure
7
//! tests are a security gate, not mere coverage.
8
//!
9
//! The proof is structural and at compile time: `compile_template` validates
10
//! against `render_compiler_specs`, so a template naming any non-allowlisted
11
//! native fails to compile (the compiler doesn't know the native exists). Each
12
//! native asserted-excluded here IS present on the full eval surface
13
//! (`all_compiler_specs`), so the test proves exclusion, not absence.
14

            
15
use rpc::compile_template;
16
use rpc::natives::{RENDER_NATIVE_ALLOWLIST, all_compiler_specs, render_compiler_specs};
17

            
18
/// Every native that templates MUST NOT reach. Each is on the full eval surface
19
/// (verified by `dangerous_natives_are_on_the_full_surface`), so a
20
/// compile-failure proves the render whitelist excludes a real, callable
21
/// native — not a typo or a never-registered name.
22
const DANGEROUS_NATIVES: &[&str] = &[
23
    "get-config",          // returns config rows incl. the JWT private key
24
    "set-config",          // arbitrary per-user config write
25
    "list-config",         // enumerates every config row
26
    "merge-preview",       // client-form native, never linked into render
27
    "submit-form",         // runs `template-test`, which renders: no recursion
28
    "list-ssh-keys",       // auth surface
29
    "remove-ssh-key",      // auth revocation
30
    "delete-tag",          // mutator
31
    "delete-template",     // mutator
32
    "delete-script",       // mutator
33
    "run-script",          // mutator
34
    "create-transaction",  // mutator
35
    "update-transaction",  // mutator
36
    "delete-transaction",  // mutator
37
    "create-account",      // mutator
38
    "create-commodity",    // mutator
39
    "set-account-tag",     // mutator
40
    "set-transaction-tag", // mutator
41
    "set-split-tag",       // mutator
42
];
43

            
44
4
fn nomi_names(specs: &[nomiscript::HostFnSpec]) -> Vec<String> {
45
4
    specs
46
4
        .iter()
47
157
        .map(|s| s.nomi_name.to_ascii_lowercase())
48
        .collect()
49
4
}
50

            
51
#[test]
52
1
fn dangerous_natives_are_on_the_full_surface() {
53
1
    let full = nomi_names(&all_compiler_specs());
54
1
    for name in DANGEROUS_NATIVES {
55
19
        assert!(
56
19
            full.contains(&name.to_string()),
57
            "test premise broken: '{name}' is not on the full eval surface, \
58
             so excluding it from render proves nothing"
59
        );
60
    }
61
1
}
62

            
63
#[test]
64
1
fn render_specs_exclude_every_dangerous_native() {
65
1
    let render = nomi_names(&render_compiler_specs());
66
1
    for name in DANGEROUS_NATIVES {
67
19
        assert!(
68
19
            !render.contains(&name.to_string()),
69
            "render surface must not expose '{name}'"
70
        );
71
    }
72
1
}
73

            
74
#[test]
75
1
fn render_specs_are_exactly_the_allowlist() {
76
1
    let mut render = nomi_names(&render_compiler_specs());
77
1
    render.sort();
78
1
    let mut allow: Vec<String> = RENDER_NATIVE_ALLOWLIST
79
1
        .iter()
80
29
        .map(|s| s.to_string())
81
        .collect();
82
1
    allow.sort();
83
1
    assert_eq!(
84
1
        render, allow,
85
        "render specs must equal the allowlist — a drift means a native \
86
         leaked in or an expected one dropped out"
87
    );
88
1
}
89

            
90
#[test]
91
1
fn template_calling_get_config_fails_to_compile() {
92
1
    let err = compile_template("(get-config \"access_token_private_key\")")
93
1
        .expect_err("get-config must not compile in a template");
94
1
    assert!(format!("{err}").to_lowercase().contains("compile"));
95
1
}
96

            
97
#[test]
98
1
fn template_calling_create_transaction_fails_to_compile() {
99
1
    compile_template("(create-transaction \"x\")")
100
1
        .expect_err("create-transaction must not compile in a template");
101
1
}
102

            
103
#[test]
104
1
fn template_calling_set_config_fails_to_compile() {
105
1
    compile_template("(set-config \"k\" \"v\")")
106
1
        .expect_err("set-config must not compile in a template");
107
1
}
108

            
109
#[test]
110
1
fn template_submitting_the_ssh_key_form_fails_to_compile() {
111
1
    compile_template("(submit-form \"ssh-key-add\" '(:key \"ssh-ed25519 AAAA\"))")
112
1
        .expect_err("submit-form must not compile in a template, so ssh-key-add is unreachable");
113
1
}
114

            
115
#[test]
116
1
fn template_with_only_draft_and_reads_compiles() {
117
    // Pure-shape template: a draft note plus a read native. No DB is touched
118
    // (compile only), so this proves the allowlisted surface is usable.
119
1
    compile_template(
120
1
        "(set-draft-note \"Groceries\")\n\
121
1
         (set-draft-date \"2026-06-15\")\n\
122
1
         (draft-tag \"category\" \"food\")",
123
1
    )
124
1
    .expect("a template using only draft natives must compile");
125
1
}
126

            
127
#[test]
128
1
fn draft_natives_are_present_on_render_surface() {
129
1
    let render = nomi_names(&render_compiler_specs());
130
1
    for n in [
131
1
        "set-draft-note",
132
1
        "set-draft-date",
133
1
        "draft-split",
134
1
        "draft-tag",
135
1
    ] {
136
4
        assert!(render.contains(&n.to_string()), "render must expose '{n}'");
137
    }
138
1
}