sqlitegraph 2.0.7

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! Malicious payload tests for JSON input validation.
//!
//! This test module verifies that JSON input validation properly rejects
//! malicious payloads that could cause DoS, memory exhaustion, or stack overflow.

use sqlitegraph::backend::native::v2::storage::{
    JsonLimits, JsonValidationError, parse_and_validate_json, parse_and_validate_json_str,
};

/// Test: Malicious payload that exceeds size limit
#[test]
fn test_malicious_json_payload_size() {
    let limits = JsonLimits::default(); // 10MB limit
    // Create 11MB payload
    // Actually create a real large payload
    let large_payload: String = "a".repeat(11 * 1024 * 1024);
    let json_str = format!(r#"{{"data": "{}"}}"#, large_payload);

    let result = parse_and_validate_json_str(&json_str, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::SizeTooLarge { .. })),
        "Expected SizeTooLarge error, got: {:?}",
        result
    );
}

/// Test: Malicious payload with excessive nesting depth
#[test]
fn test_malicious_json_payload_depth() {
    let limits = JsonLimits::default(); // 128 depth limit
    // Create 200 levels of nesting
    // Note: serde_json has its own recursion limit (default 128)
    // so deeply nested JSON may fail during parsing before our depth check
    let mut json_str = String::from("null");
    for _ in 0..200 {
        json_str = format!("[{}]", json_str);
    }

    let result = parse_and_validate_json_str(&json_str, &limits);
    // Either our depth validation catches it, or serde_json's recursion limit does
    // Both are valid protections against deeply nested payloads
    assert!(
        matches!(
            result,
            Err(JsonValidationError::DepthTooLarge { .. })
                | Err(JsonValidationError::ParseError(_))
        ),
        "Expected DepthTooLarge or ParseError (recursion limit), got: {:?}",
        result
    );
}

/// Test: Payload with both large size AND deep nesting
#[test]
fn test_malicious_combined_size_and_depth() {
    let limits = JsonLimits {
        max_size: 1000,
        max_depth: 10,
    };

    // Create deep nesting (will exceed depth limit)
    let mut json_str = String::from("null");
    for _ in 0..20 {
        json_str = format!("[{}]", json_str);
    }

    let result = parse_and_validate_json_str(&json_str, &limits);
    // Should fail on depth check (after size check passes)
    assert!(
        matches!(result, Err(JsonValidationError::DepthTooLarge { .. })),
        "Expected DepthTooLarge error, got: {:?}",
        result
    );
}

/// Test: Payload exactly at size limit boundary
#[test]
fn test_payload_at_size_boundary() {
    // Create exactly 88 bytes of JSON (actual length of the string below)
    let payload = r#"{"a":"12345678901234567890123456789012345678901234567890123456789012345678901234567890"}"#;
    let actual_len = payload.len();
    assert_eq!(
        actual_len, 88,
        "Payload length is {} not 88 as expected",
        actual_len
    );

    let limits = JsonLimits {
        max_size: 88,
        max_depth: 128,
    };

    let result = parse_and_validate_json_str(payload, &limits);
    // Should pass - exactly at limit
    assert!(
        result.is_ok(),
        "Expected success at size boundary, got: {:?}",
        result
    );
}

/// Test: Payload just over size limit boundary
#[test]
fn test_payload_just_over_size_boundary() {
    // Create 89 bytes of JSON (actual length + 1)
    let payload = r#"{"a":"123456789012345678901234567890123456789012345678901234567890123456789012345678901"}"#;
    let actual_len = payload.len();
    assert_eq!(
        actual_len, 89,
        "Payload length is {} not 89 as expected",
        actual_len
    );

    let limits = JsonLimits {
        max_size: 88,
        max_depth: 128,
    };

    let result = parse_and_validate_json_str(payload, &limits);
    // Should fail - just over limit
    assert!(
        matches!(
            result,
            Err(JsonValidationError::SizeTooLarge {
                actual: 89,
                max: 88
            })
        ),
        "Expected SizeTooLarge error, got: {:?}",
        result
    );
}

/// Test: Deeply nested object structure
#[test]
fn test_deeply_nested_objects() {
    let limits = JsonLimits {
        max_size: 10000,
        max_depth: 5,
    };

    // Create 6 levels of nested objects (exceeds limit of 5)
    let json_str = r#"{"a":{"b":{"c":{"d":{"e":{"f":null}}}}}}"#;

    let result = parse_and_validate_json_str(json_str, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::DepthTooLarge { .. })),
        "Expected DepthTooLarge error for 6 levels, got: {:?}",
        result
    );
}

/// Test: Wide array (many elements, shallow depth)
#[test]
fn test_wide_array_shallow_depth() {
    let limits = JsonLimits {
        max_size: 10000,
        max_depth: 128,
    };

    // Create array with 1000 elements (but shallow depth)
    let elements: Vec<&str> = (0..1000).map(|_| "null").collect();
    let json_str = format!("[{}]", elements.join(","));

    let result = parse_and_validate_json_str(&json_str, &limits);
    // Should pass - depth is only 2 (root array -> elements)
    assert!(result.is_ok(), "Wide array should pass, got: {:?}", result);
}

/// Test: Malformed JSON that passes size check
#[test]
fn test_malformed_json_with_valid_size() {
    let limits = JsonLimits::default();
    let invalid_json = r#"{"unclosed": ["array"#;

    let result = parse_and_validate_json_str(invalid_json, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::ParseError(_))),
        "Expected ParseError for malformed JSON, got: {:?}",
        result
    );
}

/// Test: Unicode payload that could cause issues
#[test]
fn test_unicode_payload() {
    let limits = JsonLimits {
        max_size: 1000,
        max_depth: 10,
    };

    // Valid JSON with unicode
    let unicode_json = r#"{"emoji": "😀🎉", "chinese": "你好", "arabic": "مرحبا"}"#;

    let result = parse_and_validate_json_str(unicode_json, &limits);
    assert!(
        result.is_ok(),
        "Unicode JSON should be valid, got: {:?}",
        result
    );
}

/// Test: Empty payload edge case
#[test]
fn test_empty_payload() {
    let limits = JsonLimits::default();
    let result = parse_and_validate_json(b"", &limits);
    assert!(
        matches!(result, Err(JsonValidationError::ParseError(_))),
        "Empty payload should fail to parse"
    );
}

/// Test: Single element edge cases
#[test]
fn test_single_element_edge_cases() {
    let limits = JsonLimits::default();

    // Test single values
    assert!(parse_and_validate_json(b"null", &limits).is_ok());
    assert!(parse_and_validate_json(b"true", &limits).is_ok());
    assert!(parse_and_validate_json(b"false", &limits).is_ok());
    assert!(parse_and_validate_json(b"42", &limits).is_ok());
    assert!(parse_and_validate_json(br#""hello""#, &limits).is_ok());
}

/// Test: Special characters that could be used for injection
#[test]
fn test_special_characters() {
    let limits = JsonLimits::default();

    // JSON with escaped special characters
    let escaped_json = r#"{"path": "C:\\Users\\test", "quote": "He said \"hello\"", "newline": "line1\nline2", "tab": "col1\tcol2"}"#;

    let result = parse_and_validate_json_str(escaped_json, &limits);
    assert!(result.is_ok(), "Escaped characters should be valid");
}

/// Test: Payload with many keys (potential hash DoS)
#[test]
fn test_many_keys_object() {
    let limits = JsonLimits {
        max_size: 50000,
        max_depth: 10,
    };

    // Create object with many keys
    let mut json_str = String::from("{");
    for i in 0..1000 {
        if i > 0 {
            json_str.push(',');
        }
        json_str.push_str(&format!(r#""key{}": "value{}""#, i, i));
    }
    json_str.push('}');

    let result = parse_and_validate_json_str(&json_str, &limits);
    // Should pass if size is within limits
    assert!(result.is_ok(), "Many keys object should pass size check");
}

/// Test: Alternating arrays and objects
#[test]
fn test_alternating_nesting() {
    let limits = JsonLimits {
        max_size: 10000,
        max_depth: 3, // Only allow 3 levels
    };

    // Create alternating nested structure: [{}, {}] - 4 levels deep
    let json_str = r#"[{"a": [{"b": null}]}, {"c": [{"d": null}]}]"#;

    let result = parse_and_validate_json_str(json_str, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::DepthTooLarge { .. })),
        "Expected DepthTooLarge for alternating 4-level structure"
    );
}

/// Test: Very long string value
#[test]
fn test_very_long_string_value() {
    let limits = JsonLimits {
        max_size: 500,
        max_depth: 10,
    };

    // Create a single long string that exceeds size limit
    // Need more than 500 - 12 = 488 characters
    let long_string = "x".repeat(490); // 490 chars plus JSON overhead = 502 bytes
    let json_str = format!(r#"{{"data": "{}"}}"#, long_string);

    let result = parse_and_validate_json_str(&json_str, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::SizeTooLarge { .. })),
        "Long string should exceed size limit"
    );
}

/// Test: Valid complex JSON within limits
#[test]
fn test_valid_complex_json_within_limits() {
    let limits = JsonLimits::default();

    // Complex but valid JSON
    let complex_json = r#"{
        "users": [
            {
                "id": 1,
                "name": "Alice",
                "roles": ["admin", "user"],
                "metadata": {"created": "2024-01-01", "active": true}
            },
            {
                "id": 2,
                "name": "Bob",
                "roles": ["user"],
                "metadata": {"created": "2024-01-02", "active": false}
            }
        ],
        "settings": {
            "timeout": 30,
            "retries": 3,
            "features": {"feature_a": true, "feature_b": false}
        }
    }"#;

    let result = parse_and_validate_json_str(complex_json, &limits);
    assert!(
        result.is_ok(),
        "Complex valid JSON should parse successfully"
    );

    // Verify structure is preserved
    let parsed = result.unwrap();
    assert_eq!(parsed["users"][0]["name"], "Alice");
    assert_eq!(parsed["users"][1]["roles"][0], "user");
    assert_eq!(parsed["settings"]["timeout"], 30);
}

/// Test: Numeric edge cases
#[test]
fn test_numeric_edge_cases() {
    let limits = JsonLimits::default();

    // Very large number
    assert!(parse_and_validate_json_str(r#"{"big": 999999999999999999}"#, &limits).is_ok());

    // Negative number
    assert!(parse_and_validate_json_str(r#"{"neg": -42}"#, &limits).is_ok());

    // Float
    assert!(parse_and_validate_json_str(r#"{"float": 3.14159}"#, &limits).is_ok());

    // Scientific notation
    assert!(parse_and_validate_json_str(r#"{"sci": 1.5e10}"#, &limits).is_ok());
}

/// Test: Whitespace handling
#[test]
fn test_whitespace_handling() {
    let limits = JsonLimits::default();

    // JSON with lots of whitespace (still valid)
    let whitespace_json = r#"{
        "key1" : "value1",
        "key2" : "value2",
        "nested" : {
            "deep" : "value"
        }
    }"#;

    let result = parse_and_validate_json_str(whitespace_json, &limits);
    assert!(result.is_ok(), "Whitespace should be handled correctly");
}

/// Test: Zero max_size limit (rejects everything)
#[test]
fn test_zero_max_size_rejects_all() {
    let limits = JsonLimits {
        max_size: 0,
        max_depth: 128,
    };

    // Even empty object exceeds size 0
    let result = parse_and_validate_json_str(r#"{}"#, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::SizeTooLarge { .. })),
        "Zero max_size should reject even empty object"
    );
}

/// Test: Zero max_depth limit (rejects all nested structures)
#[test]
fn test_zero_max_depth_rejects_nested() {
    let limits = JsonLimits {
        max_size: 10000,
        max_depth: 0,
    };

    // Root level primitive values should pass (depth 0)
    assert!(parse_and_validate_json_str(r#"null"#, &limits).is_ok());
    assert!(parse_and_validate_json_str(r#"42"#, &limits).is_ok());
    assert!(parse_and_validate_json_str(r#""hello""#, &limits).is_ok());

    // Any nesting should fail (use non-empty to trigger depth calculation)
    let result = parse_and_validate_json_str(r#"[null]"#, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::DepthTooLarge { .. })),
        "Array with element should exceed max_depth=0"
    );

    let result = parse_and_validate_json_str(r#"{"a": null}"#, &limits);
    assert!(
        matches!(result, Err(JsonValidationError::DepthTooLarge { .. })),
        "Object with field should exceed max_depth=0"
    );
}