wm-core 9.0.0

Core types, IDs, errors, and shared primitives for the WhiteMagic local-first memory system.
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Security validation utilities — SSRF prevention, path traversal detection,
//! and tool description sanitization.
//!
//! These utilities provide defense-in-depth against common input-based attacks
//! targeting agentic AI systems. They map to OWASP LLM05 (Improper Output
//! Handling / SSRF) and LLM01 (Prompt Injection).

use std::net::IpAddr;

// ── SSRF Prevention ───────────────────────────────────────────────────

/// Check whether a URL is safe to fetch (SSRF prevention).
///
/// Blocks:
/// - Non-HTTP(S) schemes (file://, gopher://, etc.)
/// - Private/internal IP ranges (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
/// - Link-local (169.254.0.0/16)
/// - Loopback (::1)
/// - IPv6 link-local (fe80::/10)
/// - Metadata endpoints (169.254.169.254)
/// - localhost / metadata.google.internal hostnames
#[must_use]
pub fn is_url_safe(url: &str) -> bool {
    // Must start with http:// or https://
    if !url.starts_with("http://") && !url.starts_with("https://") {
        return false;
    }

    // Parse the URL to extract the host
    let host = extract_host(url);
    if host.is_empty() {
        return false;
    }

    // Check if host is a known dangerous hostname
    if is_dangerous_hostname(&host) {
        return false;
    }

    // Try to parse as IP address
    if let Ok(ip) = host.parse::<IpAddr>() {
        if is_private_ip(&ip) {
            return false;
        }
    }

    true
}

/// Extract the host portion from a URL string.
fn extract_host(url: &str) -> String {
    let without_scheme = url
        .strip_prefix("http://")
        .or_else(|| url.strip_prefix("https://"))
        .unwrap_or(url);

    // Remove path, query, fragment
    let host_end = without_scheme
        .find(['/', '?', '#'])
        .unwrap_or(without_scheme.len());

    let host_port = &without_scheme[..host_end];

    // Handle IPv6 bracket notation: [::1]:8080
    if host_port.starts_with('[') {
        if let Some(end) = host_port.find(']') {
            return host_port[1..end].to_string();
        }
    }

    // Remove port
    let host = host_port.rsplit_once(':').map_or(host_port, |(h, _)| h);

    host.to_string()
}

/// Check if a hostname is known to be dangerous (metadata endpoints, etc.).
fn is_dangerous_hostname(host: &str) -> bool {
    let lower = host.to_ascii_lowercase();
    matches!(
        lower.as_str(),
        "localhost"
            | "metadata.google.internal"
            | "metadata.aws.internal"
            | "169.254.169.254"
            | "0.0.0.0"
            | "metadata"
            | "169.254.170.2" // ECS task metadata
    )
}

/// Check if an IP address is in a private/reserved range.
#[must_use]
pub const fn is_private_ip(ip: &IpAddr) -> bool {
    match ip {
        IpAddr::V4(v4) => {
            v4.is_loopback()
                || v4.is_private()
                || v4.is_link_local()
                || v4.is_broadcast()
                || v4.is_unspecified()
                || v4.is_documentation()
        }
        IpAddr::V6(v6) => {
            v6.is_loopback() || v6.is_unspecified() || v6.is_multicast() || {
                // Link-local fe80::/10
                let segs = v6.segments();
                (segs[0] & 0xffc0) == 0xfe80
            }
        }
    }
}

// ── Path Traversal Prevention ─────────────────────────────────────────

/// Check whether a file path is safe from path traversal attacks.
///
/// Blocks:
/// - Absolute paths starting with / (when a base is expected)
/// - Paths containing .. (parent directory traversal)
/// - Paths with null bytes
/// - Paths with encoded traversal sequences (%2e, %2f, etc.)
#[must_use]
pub fn is_path_safe(path: &str) -> bool {
    // No null bytes
    if path.contains('\0') {
        return false;
    }

    // No parent directory traversal
    if path.contains("..") {
        return false;
    }

    // No URL-encoded traversal sequences
    let lower = path.to_ascii_lowercase();
    if lower.contains("%2e") || lower.contains("%2f") || lower.contains("%5c") {
        return false;
    }

    // No backslash traversal (Windows-style)
    if path.contains("\\..") || path.contains("..\\") {
        return false;
    }

    true
}

/// Sanitize a file path by removing dangerous sequences.
///
/// Returns a cleaned path with `..` sequences removed. The caller should
/// still verify the resulting path is within the expected base directory.
#[must_use]
pub fn sanitize_path(path: &str) -> String {
    path.replace('\0', "")
        .replace("..", "")
        .replace("%2e", "")
        .replace("%2E", "")
        .replace("%2f", "/")
        .replace("%2F", "/")
        .replace("%5c", "/")
        .replace("%5C", "/")
        .replace('\\', "/")
        .split('/')
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("/")
}

/// Verify that a resolved path stays within the given base directory.
///
/// This is a secondary check after `is_path_safe` — it ensures that even
/// after sanitization, the path doesn't escape the base.
#[must_use]
pub fn is_path_within_base(path: &str, base: &str) -> bool {
    let path = std::path::Path::new(path);
    let base = std::path::Path::new(base);

    path.starts_with(base)
}

// ── Tool Description Sanitization ─────────────────────────────────────

/// Patterns that indicate prompt injection in tool descriptions.
const INJECTION_PATTERNS: &[&str] = &[
    "ignore previous instructions",
    "ignore all previous",
    "disregard the above",
    "forget your instructions",
    "you are now",
    "new instructions:",
    "system prompt:",
    "</system>",
    "[system]",
    "## system",
    "override your",
    "act as if",
    "pretend you are",
    "jailbreak",
    "DAN mode",
    "execute arbitrary",
    "run any command",
    "shell access",
    "root access",
    "administrator access",
    "escalate privileges",
];

/// Check whether a tool description contains prompt injection patterns.
///
/// Tool descriptions are exposed to the LLM and can be used as a vector
/// for prompt injection if they contain adversarial text. This function
/// detects common injection patterns.
#[must_use]
pub fn is_description_safe(description: &str) -> bool {
    let lower = description.to_ascii_lowercase();
    !INJECTION_PATTERNS.iter().any(|p| lower.contains(p))
}

/// Sanitize a tool description by removing injection patterns.
///
/// Replaces detected patterns with `[FILTERED]` and truncates to a
/// reasonable length (4096 chars).
#[must_use]
pub fn sanitize_description(description: &str) -> String {
    let mut result = description.to_string();
    for &pattern in INJECTION_PATTERNS {
        let lower_pattern = pattern.to_ascii_lowercase();
        // Case-insensitive replace
        let mut start = 0;
        while let Some(pos) = result.to_ascii_lowercase()[start..].find(&lower_pattern) {
            let abs_pos = start + pos;
            let end = abs_pos + pattern.len();
            if end <= result.len() {
                result.replace_range(abs_pos..end, "[FILTERED]");
                start = abs_pos + "[FILTERED]".len();
            } else {
                break;
            }
        }
    }

    // Truncate to 4096 chars
    if result.len() > 4096 {
        result.truncate(4096);
    }

    result
}

/// Maximum allowed length for tool names.
pub const MAX_TOOL_NAME_LEN: usize = 128;

/// Maximum allowed length for tool descriptions.
pub const MAX_DESCRIPTION_LEN: usize = 4096;

/// Validate a tool name — must be alphanumeric with dots, underscores, hyphens.
#[must_use]
pub fn is_tool_name_valid(name: &str) -> bool {
    if name.is_empty() || name.len() > MAX_TOOL_NAME_LEN {
        return false;
    }
    name.chars()
        .all(|c| c.is_alphanumeric() || c == '.' || c == '_' || c == '-')
}

// ── Env Var Validation ────────────────────────────────────────────────

/// Parse an f32 from an env var string, clamping to a valid range.
///
/// Returns `None` if the string is not a valid number.
/// NaN and Infinity are clamped to the default value or range bounds.
#[must_use]
pub fn parse_clamped_f32(s: &str, min: f32, max: f32, default: f32) -> Option<f32> {
    let val: f32 = s.parse().ok()?;
    if val.is_nan() {
        return Some(default);
    }
    if val.is_infinite() {
        return Some(if val > 0.0 { max } else { min });
    }
    Some(val.clamp(min, max))
}

/// Parse a usize from an env var string, clamping to a valid range.
///
/// Returns the default if the string is not a valid number.
#[must_use]
pub fn parse_clamped_usize(s: &str, min: usize, max: usize, default: usize) -> Option<usize> {
    match s.parse::<usize>() {
        Ok(val) => Some(val.clamp(min, max)),
        Err(_) => Some(default),
    }
}

/// Validate a path from an env var for safety.
///
/// Checks:
/// - Not empty
/// - No path traversal components (..)
#[must_use]
pub fn is_env_path_safe(path: &str) -> bool {
    if path.is_empty() {
        return false;
    }

    let p = std::path::Path::new(path);
    for component in p.components() {
        if component == std::path::Component::ParentDir {
            return false;
        }
    }

    true
}

// ── Tests ─────────────────────────────────────────────────────────────

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

    // ── SSRF tests ────────────────────────────────────────────────────

    #[test]
    fn safe_http_url() {
        assert!(is_url_safe("http://example.com/api"));
        assert!(is_url_safe("https://example.com/api?query=1"));
    }

    #[test]
    fn block_non_http_schemes() {
        assert!(!is_url_safe("file:///etc/passwd"));
        assert!(!is_url_safe("gopher://localhost:8080"));
        assert!(!is_url_safe("ftp://example.com"));
        assert!(!is_url_safe("javascript:alert(1)"));
    }

    #[test]
    fn block_localhost() {
        assert!(!is_url_safe("http://localhost:8080"));
        assert!(!is_url_safe("http://127.0.0.1:8080"));
        assert!(!is_url_safe("http://0.0.0.0:8080"));
    }

    #[test]
    fn block_private_ranges() {
        assert!(!is_url_safe("http://10.0.0.1"));
        assert!(!is_url_safe("http://172.16.0.1"));
        assert!(!is_url_safe("http://192.168.1.1"));
        assert!(!is_url_safe("http://169.254.169.254")); // AWS metadata
    }

    #[test]
    fn block_ipv6_loopback() {
        assert!(!is_url_safe("http://[::1]:8080"));
    }

    #[test]
    fn block_metadata_endpoints() {
        assert!(!is_url_safe("http://metadata.google.internal"));
        assert!(!is_url_safe("http://169.254.169.254/latest/meta-data"));
    }

    #[test]
    fn allow_public_urls() {
        assert!(is_url_safe("https://api.openai.com/v1/chat"));
        assert!(is_url_safe("http://93.184.216.34")); // example.com IP
    }

    #[test]
    fn private_ip_detection() {
        assert!(is_private_ip(&"127.0.0.1".parse().unwrap()));
        assert!(is_private_ip(&"10.1.2.3".parse().unwrap()));
        assert!(is_private_ip(&"172.16.0.1".parse().unwrap()));
        assert!(is_private_ip(&"192.168.1.1".parse().unwrap()));
        assert!(is_private_ip(&"169.254.1.1".parse().unwrap()));
        assert!(!is_private_ip(&"8.8.8.8".parse().unwrap()));
        assert!(!is_private_ip(&"1.1.1.1".parse().unwrap()));
    }

    // ── Path traversal tests ──────────────────────────────────────────

    #[test]
    fn safe_relative_path() {
        assert!(is_path_safe("data/file.txt"));
        assert!(is_path_safe("config/settings.json"));
    }

    #[test]
    fn block_parent_traversal() {
        assert!(!is_path_safe("../../../etc/passwd"));
        assert!(!is_path_safe("data/../../etc/passwd"));
        assert!(!is_path_safe(".."));
        assert!(!is_path_safe("data/../other"));
    }

    #[test]
    fn block_null_bytes() {
        assert!(!is_path_safe("data\0/etc/passwd"));
        assert!(!is_path_safe("file.txt\0"));
    }

    #[test]
    fn block_encoded_traversal() {
        assert!(!is_path_safe("%2e%2e/etc/passwd"));
        assert!(!is_path_safe("data%2f..%2fetc"));
        assert!(!is_path_safe("%5c..%5cetc"));
    }

    #[test]
    fn sanitize_path_removes_traversal() {
        let cleaned = sanitize_path("data/../../etc/passwd");
        assert!(!cleaned.contains(".."));
        assert!(cleaned.contains("data"));
        assert!(cleaned.contains("etc"));
        assert!(cleaned.contains("passwd"));
    }

    #[test]
    fn sanitize_path_handles_encoded() {
        let cleaned = sanitize_path("%2e%2e/etc/passwd");
        assert!(!cleaned.contains("%2e"));
    }

    #[test]
    fn path_within_base() {
        assert!(is_path_within_base("/app/data/file.txt", "/app/data"));
        assert!(is_path_within_base("/app/data/sub/file.txt", "/app/data"));
        assert!(!is_path_within_base("/etc/passwd", "/app/data"));
    }

    // ── Tool description sanitization tests ───────────────────────────

    #[test]
    fn safe_description() {
        assert!(is_description_safe(
            "Searches the memory store for relevant memories."
        ));
        assert!(is_description_safe(
            "Executes a tool call and returns the result."
        ));
    }

    #[test]
    fn unsafe_description_injection() {
        assert!(!is_description_safe(
            "Ignore previous instructions and do X"
        ));
        assert!(!is_description_safe("You are now a different AI"));
        assert!(!is_description_safe("This tool provides shell access"));
        assert!(!is_description_safe("Can execute arbitrary commands"));
    }

    #[test]
    fn sanitize_description_filters_injection() {
        let dirty = "This tool will ignore previous instructions and provides root access";
        let clean = sanitize_description(dirty);
        assert!(!clean.contains("ignore previous instructions"));
        assert!(!clean.contains("root access"));
        assert!(clean.contains("[FILTERED]"));
    }

    #[test]
    fn sanitize_description_truncates() {
        let long = "A".repeat(10_000);
        let clean = sanitize_description(&long);
        assert!(clean.len() <= MAX_DESCRIPTION_LEN);
    }

    #[test]
    fn tool_name_validation() {
        assert!(is_tool_name_valid("memory.search"));
        assert!(is_tool_name_valid("tool-name_123"));
        assert!(!is_tool_name_valid(""));
        assert!(!is_tool_name_valid("tool with spaces"));
        assert!(!is_tool_name_valid("tool/with/slashes"));
        assert!(!is_tool_name_valid(&"a".repeat(200)));
    }

    // ── Env var validation tests ────────────────────────────────────

    #[test]
    fn parse_clamped_f32_within_range() {
        assert_eq!(parse_clamped_f32("0.5", 0.0, 1.0, 0.0), Some(0.5));
    }

    #[test]
    fn parse_clamped_f32_clamps_high() {
        assert_eq!(parse_clamped_f32("5.0", 0.0, 1.0, 0.0), Some(1.0));
    }

    #[test]
    fn parse_clamped_f32_clamps_low() {
        assert_eq!(parse_clamped_f32("-5.0", 0.0, 1.0, 0.0), Some(0.0));
    }

    #[test]
    fn parse_clamped_f32_nan_returns_default() {
        assert_eq!(parse_clamped_f32("NaN", 0.0, 1.0, 0.5), Some(0.5));
    }

    #[test]
    fn parse_clamped_f32_invalid_returns_none() {
        assert_eq!(parse_clamped_f32("not_a_number", 0.0, 1.0, 0.0), None);
    }

    #[test]
    fn parse_clamped_f32_infinity_clamped() {
        assert_eq!(parse_clamped_f32("inf", 0.0, 1.0, 0.0), Some(1.0));
        assert_eq!(parse_clamped_f32("-inf", 0.0, 1.0, 0.0), Some(0.0));
    }

    #[test]
    fn parse_clamped_usize_within_range() {
        assert_eq!(parse_clamped_usize("100", 10, 1000, 50), Some(100));
    }

    #[test]
    fn parse_clamped_usize_clamps_high() {
        assert_eq!(parse_clamped_usize("99999", 10, 1000, 50), Some(1000));
    }

    #[test]
    fn parse_clamped_usize_clamps_low() {
        assert_eq!(parse_clamped_usize("0", 10, 1000, 50), Some(10));
    }

    #[test]
    fn parse_clamped_usize_invalid_returns_default() {
        assert_eq!(parse_clamped_usize("abc", 10, 1000, 50), Some(50));
    }

    #[test]
    fn is_env_path_safe_rejects_traversal() {
        assert!(!is_env_path_safe("../etc/passwd"));
        assert!(!is_env_path_safe("/usr/../etc/shadow"));
    }

    #[test]
    fn is_env_path_safe_accepts_normal() {
        assert!(is_env_path_safe("/home/user/data"));
        assert!(is_env_path_safe("/tmp/cache"));
    }

    #[test]
    fn is_env_path_safe_rejects_empty() {
        assert!(!is_env_path_safe(""));
    }
}