openlatch-client 0.1.6

The open-source security layer for AI agents — client forwarder
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
use chrono::{DateTime, Utc};
use serde::Serialize;

use super::CloudEvent;

#[derive(Debug, Clone, Serialize)]
pub struct TamperEvent {
    pub ocsf: OcsfHeader,
    pub tamper: TamperPayload,
}

#[derive(Debug, Clone, Serialize)]
pub struct OcsfHeader {
    pub class_uid: u32,
    pub activity_id: u32,
    pub type_uid: u32,
    pub severity_id: u32,
    pub time: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize)]
pub struct TamperPayload {
    pub event_id: String,
    pub entry_id: String,
    pub agent_type: String,
    pub settings_path_hash: String,
    pub hook_event: String,
    pub detection_method: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub field_deltas: Vec<FieldDelta>,
    pub heal: HealOutcome,
    /// Set on the follow-up `tamper_healed` event to the detection event's
    /// `event_id` so the platform can stitch the two halves together.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub related_event_id: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct FieldDelta {
    /// Dotted path of the changed field (e.g. `"hooks[0].url"`, `"timeout"`).
    /// Paths only — values are never serialised so the wire message cannot
    /// leak URLs, tokens, or other entry contents.
    pub field: String,
    /// One of `"modified" | "added" | "removed"`.
    pub change: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct HealOutcome {
    pub outcome: String,
    pub attempt: u32,
    pub circuit: String,
}

/// Which half of the tamper event pair is being emitted. The OCSF `type_uid`
/// stays `200402` for both — differentiation is purely at the CloudEvent
/// `type` attribute so the platform can route them separately.
#[derive(Debug, Clone, Copy)]
pub enum TamperCloudEventKind {
    Detected,
    Healed,
}

impl TamperCloudEventKind {
    fn type_string(self) -> &'static str {
        match self {
            Self::Detected => "ai.openlatch.security.tamper_detected",
            Self::Healed => "ai.openlatch.security.tamper_healed",
        }
    }
}

impl TamperEvent {
    pub fn new(
        entry_id: String,
        agent_type: String,
        settings_path_hash: String,
        hook_event: String,
        detection_method: String,
    ) -> Self {
        let severity = if detection_method == "legacy_marker_upgrade" {
            1
        } else {
            3
        };

        Self {
            ocsf: OcsfHeader {
                class_uid: 2004,
                activity_id: 2,
                type_uid: 200402,
                severity_id: severity,
                time: Utc::now(),
            },
            tamper: TamperPayload {
                event_id: uuid::Uuid::now_v7().to_string(),
                entry_id,
                agent_type,
                settings_path_hash,
                hook_event,
                detection_method,
                field_deltas: Vec::new(),
                heal: HealOutcome {
                    outcome: "pending".into(),
                    attempt: 0,
                    circuit: "closed".into(),
                },
                related_event_id: None,
            },
        }
    }

    pub fn with_heal(mut self, outcome: &str, attempt: u32, circuit: &str) -> Self {
        self.tamper.heal = HealOutcome {
            outcome: outcome.into(),
            attempt,
            circuit: circuit.into(),
        };
        self
    }

    pub fn with_field_deltas(mut self, deltas: Vec<FieldDelta>) -> Self {
        self.tamper.field_deltas = deltas;
        self
    }

    /// Build the follow-up heal-outcome event from a detection event. Keeps
    /// `entry_id`, `agent_type`, `settings_path_hash`, `hook_event`, and
    /// `detection_method` aligned; assigns a fresh `event_id`; links back to
    /// the detection event via `related_event_id`.
    pub fn new_healed(original: &TamperEvent, outcome: &str, attempt: u32, circuit: &str) -> Self {
        Self {
            ocsf: OcsfHeader {
                class_uid: original.ocsf.class_uid,
                activity_id: original.ocsf.activity_id,
                type_uid: original.ocsf.type_uid,
                severity_id: original.ocsf.severity_id,
                time: Utc::now(),
            },
            tamper: TamperPayload {
                event_id: uuid::Uuid::now_v7().to_string(),
                entry_id: original.tamper.entry_id.clone(),
                agent_type: original.tamper.agent_type.clone(),
                settings_path_hash: original.tamper.settings_path_hash.clone(),
                hook_event: original.tamper.hook_event.clone(),
                detection_method: original.tamper.detection_method.clone(),
                field_deltas: original.tamper.field_deltas.clone(),
                heal: HealOutcome {
                    outcome: outcome.into(),
                    attempt,
                    circuit: circuit.into(),
                },
                related_event_id: Some(original.tamper.event_id.clone()),
            },
        }
    }

    /// Wrap the tamper event in a CloudEvents v1.0.2 structured-mode envelope
    /// so it can travel through the existing cloud forwarding pipeline. The
    /// worker will stamp the `agentid` extension just before POST — we only
    /// populate the passthrough fields here.
    pub fn to_cloud_event(
        &self,
        agent_id: &str,
        client_version: &str,
        os: &str,
        kind: TamperCloudEventKind,
    ) -> CloudEvent {
        let payload = serde_json::json!({
            "ocsf": self.ocsf,
            "tamper": self.tamper,
        });

        let envelope = serde_json::json!({
            "specversion": "1.0",
            "id": format!("evt_{}", self.tamper.event_id),
            "source": "openlatch-client",
            "type": kind.type_string(),
            "time": self.ocsf.time.to_rfc3339(),
            "datacontenttype": "application/json",
            "subject": self.tamper.entry_id,
            "data": payload,
            "clientversion": client_version,
            "os": os,
        });

        CloudEvent {
            envelope,
            agent_id: agent_id.to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_event() -> TamperEvent {
        TamperEvent::new(
            "entry-id-123".into(),
            "claude-code".into(),
            "sha256:abc123".into(),
            "PreToolUse".into(),
            "hmac_mismatch".into(),
        )
    }

    #[test]
    fn ocsf_class_uid_is_2004() {
        let e = make_event();
        assert_eq!(e.ocsf.class_uid, 2004);
        assert_eq!(e.ocsf.activity_id, 2);
        assert_eq!(e.ocsf.type_uid, 200402);
    }

    #[test]
    fn severity_is_info_for_legacy_upgrade() {
        let e = TamperEvent::new(
            "id".into(),
            "claude-code".into(),
            "sha256:x".into(),
            "PreToolUse".into(),
            "legacy_marker_upgrade".into(),
        );
        assert_eq!(e.ocsf.severity_id, 1);
    }

    #[test]
    fn severity_is_medium_for_hmac_mismatch() {
        let e = make_event();
        assert_eq!(e.ocsf.severity_id, 3);
    }

    #[test]
    fn no_settings_values_in_serialized_event() {
        let e = make_event();
        let json = serde_json::to_string(&e).unwrap();
        assert!(!json.contains("http://attacker"));
        assert!(!json.contains("Bearer "));
        assert!(!json.contains("OPENLATCH_TOKEN"));
    }

    #[test]
    fn no_settings_path_literal_in_serialized_event() {
        let e = TamperEvent::new(
            "id".into(),
            "claude-code".into(),
            "sha256:abc123def456".into(),
            "PreToolUse".into(),
            "hmac_mismatch".into(),
        );
        let json = serde_json::to_string(&e).unwrap();
        assert!(!json.contains(".claude"));
        assert!(!json.contains("settings.json"));
        assert!(json.contains("sha256:abc123def456"));
    }

    #[test]
    fn with_heal_sets_outcome() {
        let e = make_event().with_heal("succeeded", 2, "closed");
        assert_eq!(e.tamper.heal.outcome, "succeeded");
        assert_eq!(e.tamper.heal.attempt, 2);
    }

    #[test]
    fn event_id_is_uuid_v7() {
        let e = make_event();
        assert!(!e.tamper.event_id.is_empty());
    }

    #[test]
    fn related_event_id_absent_on_detection_event() {
        let e = make_event();
        let json = serde_json::to_string(&e).unwrap();
        assert!(
            !json.contains("related_event_id"),
            "detection events must not serialise related_event_id"
        );
    }

    #[test]
    fn new_healed_links_to_original() {
        let original = make_event();
        let healed = TamperEvent::new_healed(&original, "succeeded", 1, "closed");
        assert_eq!(
            healed.tamper.related_event_id.as_deref(),
            Some(original.tamper.event_id.as_str())
        );
        assert_ne!(healed.tamper.event_id, original.tamper.event_id);
        assert_eq!(healed.tamper.entry_id, original.tamper.entry_id);
        assert_eq!(healed.tamper.heal.outcome, "succeeded");
    }

    #[test]
    fn new_healed_preserves_detection_metadata() {
        let original = make_event();
        let healed = TamperEvent::new_healed(&original, "failed", 3, "open");
        assert_eq!(
            healed.tamper.detection_method,
            original.tamper.detection_method
        );
        assert_eq!(healed.tamper.agent_type, original.tamper.agent_type);
        assert_eq!(healed.tamper.hook_event, original.tamper.hook_event);
        assert_eq!(
            healed.tamper.settings_path_hash,
            original.tamper.settings_path_hash
        );
    }

    #[test]
    fn with_field_deltas_populates_vec() {
        let deltas = vec![
            FieldDelta {
                field: "hooks[0].url".into(),
                change: "modified".into(),
            },
            FieldDelta {
                field: "timeout".into(),
                change: "removed".into(),
            },
        ];
        let e = make_event().with_field_deltas(deltas);
        assert_eq!(e.tamper.field_deltas.len(), 2);
        assert_eq!(e.tamper.field_deltas[0].field, "hooks[0].url");
        assert_eq!(e.tamper.field_deltas[0].change, "modified");

        let json = serde_json::to_string(&e).unwrap();
        assert!(json.contains("hooks[0].url"));
        assert!(json.contains("modified"));
    }

    #[test]
    fn field_deltas_omitted_when_empty() {
        let e = make_event();
        let json = serde_json::to_string(&e).unwrap();
        assert!(
            !json.contains("field_deltas"),
            "empty field_deltas must be skipped in serialisation"
        );
    }

    #[test]
    fn to_cloud_event_detected_has_correct_type() {
        let e = make_event();
        let ce = e.to_cloud_event("agt_xyz", "0.2.0", "linux", TamperCloudEventKind::Detected);

        assert_eq!(ce.agent_id, "agt_xyz");
        assert_eq!(
            ce.envelope["type"].as_str(),
            Some("ai.openlatch.security.tamper_detected")
        );
        assert_eq!(ce.envelope["specversion"].as_str(), Some("1.0"));
        assert_eq!(ce.envelope["source"].as_str(), Some("openlatch-client"));
        assert_eq!(ce.envelope["subject"].as_str(), Some("entry-id-123"));
        assert_eq!(ce.envelope["clientversion"].as_str(), Some("0.2.0"));
        assert_eq!(ce.envelope["os"].as_str(), Some("linux"));
        assert_eq!(
            ce.envelope["datacontenttype"].as_str(),
            Some("application/json")
        );
    }

    #[test]
    fn to_cloud_event_healed_has_correct_type() {
        let e = TamperEvent::new_healed(&make_event(), "succeeded", 1, "closed");
        let ce = e.to_cloud_event("agt_xyz", "0.2.0", "macos", TamperCloudEventKind::Healed);
        assert_eq!(
            ce.envelope["type"].as_str(),
            Some("ai.openlatch.security.tamper_healed")
        );
    }

    #[test]
    fn to_cloud_event_id_has_evt_prefix() {
        let e = make_event();
        let ce = e.to_cloud_event("agt_xyz", "0.2.0", "linux", TamperCloudEventKind::Detected);
        let id = ce.envelope["id"].as_str().unwrap();
        assert!(
            id.starts_with("evt_"),
            "CloudEvent id must have evt_ prefix: {id}"
        );
        assert!(id.contains(&e.tamper.event_id));
    }

    #[test]
    fn to_cloud_event_payload_contains_ocsf_and_tamper() {
        let e = make_event();
        let ce = e.to_cloud_event("agt_xyz", "0.2.0", "linux", TamperCloudEventKind::Detected);
        let data = &ce.envelope["data"];
        assert_eq!(data["ocsf"]["class_uid"].as_u64(), Some(2004));
        assert_eq!(
            data["tamper"]["detection_method"].as_str(),
            Some("hmac_mismatch")
        );
        assert_eq!(data["tamper"]["entry_id"].as_str(), Some("entry-id-123"));
    }

    #[test]
    fn to_cloud_event_never_stamps_agentid_in_envelope() {
        // The worker stamps `agentid` onto the envelope immediately before
        // POST. If we stamped it here too, the worker's insert would be a
        // no-op instead of the expected overwrite — and more importantly, the
        // tamper CloudEvent would diverge from how hook CloudEvents are shaped
        // on their way into the channel.
        let e = make_event();
        let ce = e.to_cloud_event("agt_xyz", "0.2.0", "linux", TamperCloudEventKind::Detected);
        assert!(
            ce.envelope.get("agentid").is_none(),
            "to_cloud_event must leave agentid stamping to the worker"
        );
    }
}