libmagic-rs 0.9.0

A pure-Rust implementation of libmagic for file type identification
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
// Copyright (c) 2025-2026 the libmagic-rs contributors
// SPDX-License-Identifier: Apache-2.0

//! Integration tests for JSON output functionality
//!
//! These tests verify that the CLI correctly integrates the JSON output formatter
//! and produces the expected JSON structure when the --json flag is used.

use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Test that the CLI produces valid JSON output when --json flag is used
#[test]
fn test_cli_json_output_format() {
    // Create a temporary test file
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let test_file = temp_dir.path().join("test.bin");
    fs::write(&test_file, b"Hello, World!").expect("Failed to write test file");

    // Create a basic magic file
    let magic_file = temp_dir.path().join("test.magic");
    fs::write(&magic_file, "0 byte 72 Hello file\n").expect("Failed to write magic file");

    // Run the CLI with --json flag
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "rmagic",
            "--",
            test_file.to_str().unwrap(),
            "--json",
            "--magic-file",
            magic_file.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    // Check that the command succeeded
    if !output.status.success() {
        eprintln!(
            "Command failed with stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Command failed with exit code: {:?}", output.status.code());
    }

    let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 in stdout");

    // Parse the JSON output
    let json_value: serde_json::Value =
        serde_json::from_str(&stdout).expect("Failed to parse JSON output");

    // Verify the JSON structure matches the expected format
    assert!(json_value.is_object(), "Output should be a JSON object");

    let json_obj = json_value.as_object().unwrap();
    assert!(
        json_obj.contains_key("matches"),
        "JSON should contain 'matches' field"
    );

    let matches = json_obj["matches"]
        .as_array()
        .expect("'matches' should be an array");

    if !matches.is_empty() {
        let first_match = &matches[0];
        assert!(first_match.is_object(), "Match should be a JSON object");

        let match_obj = first_match.as_object().unwrap();

        // Verify required fields are present
        assert!(
            match_obj.contains_key("text"),
            "Match should contain 'text' field"
        );
        assert!(
            match_obj.contains_key("offset"),
            "Match should contain 'offset' field"
        );
        assert!(
            match_obj.contains_key("value"),
            "Match should contain 'value' field"
        );
        assert!(
            match_obj.contains_key("tags"),
            "Match should contain 'tags' field"
        );
        assert!(
            match_obj.contains_key("score"),
            "Match should contain 'score' field"
        );

        // Verify field types
        assert!(match_obj["text"].is_string(), "'text' should be a string");
        assert!(
            match_obj["offset"].is_number(),
            "'offset' should be a number"
        );
        assert!(match_obj["value"].is_string(), "'value' should be a string");
        assert!(match_obj["tags"].is_array(), "'tags' should be an array");
        assert!(match_obj["score"].is_number(), "'score' should be a number");
    }
}

/// Test that the CLI produces empty matches array when no rules match
#[test]
fn test_cli_json_output_no_matches() {
    // Create a temporary test file
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let test_file = temp_dir.path().join("test.bin");
    fs::write(&test_file, b"Random binary data").expect("Failed to write test file");

    // Create a magic file that won't match
    let magic_file = temp_dir.path().join("test.magic");
    fs::write(&magic_file, "0 byte 255 No match file\n").expect("Failed to write magic file");

    // Run the CLI with --json flag
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "rmagic",
            "--",
            test_file.to_str().unwrap(),
            "--json",
            "--magic-file",
            magic_file.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    // Check that the command succeeded
    if !output.status.success() {
        eprintln!(
            "Command failed with stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Command failed with exit code: {:?}", output.status.code());
    }

    let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 in stdout");

    // Parse the JSON output
    let json_value: serde_json::Value =
        serde_json::from_str(&stdout).expect("Failed to parse JSON output");

    // Verify the JSON structure
    assert!(json_value.is_object(), "Output should be a JSON object");

    let json_obj = json_value.as_object().unwrap();
    assert!(
        json_obj.contains_key("matches"),
        "JSON should contain 'matches' field"
    );

    let matches = json_obj["matches"]
        .as_array()
        .expect("'matches' should be an array");

    // When no rules match, we should get an empty matches array or a single "data" match
    // depending on the implementation
    if !matches.is_empty() {
        // If there's a match, it should be the fallback "data" match
        assert_eq!(matches.len(), 1, "Should have exactly one fallback match");
        let match_obj = matches[0].as_object().unwrap();
        // The fallback match might be "data" or similar generic description
        assert!(match_obj["text"].is_string(), "'text' should be a string");
    }
}

/// Test that JSON output is valid and well-formed
#[test]
fn test_cli_json_output_validity() {
    // Create a temporary test file with known content
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let test_file = temp_dir.path().join("test.txt");
    fs::write(&test_file, "#!/bin/bash\necho 'Hello World'\n").expect("Failed to write test file");

    // Create a magic file that should match
    let magic_file = temp_dir.path().join("test.magic");
    fs::write(&magic_file, "0 byte 35 Bash script\n").expect("Failed to write magic file");

    // Run the CLI with --json flag
    let output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "rmagic",
            "--",
            test_file.to_str().unwrap(),
            "--json",
            "--magic-file",
            magic_file.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    // Check that the command succeeded
    if !output.status.success() {
        eprintln!(
            "Command failed with stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Command failed with exit code: {:?}", output.status.code());
    }

    let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 in stdout");

    // Verify that the output is valid JSON
    let json_value: serde_json::Value =
        serde_json::from_str(&stdout).expect("Failed to parse JSON output");

    // Verify the JSON can be serialized back to string (round-trip test)
    let serialized =
        serde_json::to_string(&json_value).expect("Failed to serialize JSON back to string");

    // Verify the serialized JSON can be parsed again
    let _reparsed: serde_json::Value =
        serde_json::from_str(&serialized).expect("Failed to reparse serialized JSON");

    // Verify the output contains the expected structure
    assert!(json_value.is_object(), "Root should be an object");
    let root_obj = json_value.as_object().unwrap();
    assert!(
        root_obj.contains_key("matches"),
        "Should contain matches array"
    );

    let matches = root_obj["matches"]
        .as_array()
        .expect("matches should be an array");

    // If there are matches, verify their structure
    for match_item in matches {
        assert!(match_item.is_object(), "Each match should be an object");
        let match_obj = match_item.as_object().unwrap();

        // Verify all required fields are present and have correct types
        assert!(match_obj.contains_key("text") && match_obj["text"].is_string());
        assert!(match_obj.contains_key("offset") && match_obj["offset"].is_number());
        assert!(match_obj.contains_key("value") && match_obj["value"].is_string());
        assert!(match_obj.contains_key("tags") && match_obj["tags"].is_array());
        assert!(match_obj.contains_key("score") && match_obj["score"].is_number());

        // Verify score is in valid range (0-100)
        let score = match_obj["score"]
            .as_u64()
            .expect("score should be a number");
        assert!(score <= 100, "Score should be <= 100, got {}", score);
    }
}

/// Test that the JSON output differs from text output
#[test]
fn test_cli_json_vs_text_output() {
    // Create a temporary test file
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let test_file = temp_dir.path().join("test.bin");
    fs::write(&test_file, b"Test content").expect("Failed to write test file");

    // Create a basic magic file
    let magic_file = temp_dir.path().join("test.magic");
    fs::write(&magic_file, "0 byte 84 Test file\n").expect("Failed to write magic file");

    // Run with JSON output
    let json_output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "rmagic",
            "--",
            test_file.to_str().unwrap(),
            "--json",
            "--magic-file",
            magic_file.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute JSON command");

    // Run with text output
    let text_output = Command::new("cargo")
        .args([
            "run",
            "--bin",
            "rmagic",
            "--",
            test_file.to_str().unwrap(),
            "--text",
            "--magic-file",
            magic_file.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute text command");

    // Both commands should succeed
    assert!(json_output.status.success(), "JSON command should succeed");
    assert!(text_output.status.success(), "Text command should succeed");

    let json_stdout = String::from_utf8(json_output.stdout).expect("Invalid UTF-8 in JSON stdout");
    let text_stdout = String::from_utf8(text_output.stdout).expect("Invalid UTF-8 in text stdout");

    // Outputs should be different
    assert_ne!(
        json_stdout, text_stdout,
        "JSON and text outputs should be different"
    );

    // JSON output should be parseable as JSON
    let _json_value: serde_json::Value =
        serde_json::from_str(&json_stdout).expect("JSON output should be valid JSON");

    // Text output should NOT be parseable as JSON
    assert!(
        serde_json::from_str::<serde_json::Value>(&text_stdout).is_err(),
        "Text output should not be valid JSON"
    );

    // Text output should contain the filename
    assert!(
        text_stdout.contains(test_file.file_name().unwrap().to_str().unwrap()),
        "Text output should contain filename"
    );
}

// =============================================================================
// 1B-H2 / 2A-M1: RuleMatch.type_kind must not leak into JSON output
// =============================================================================

/// Library-side `EvaluationResult` carries `RuleMatch.type_kind: TypeKind`
/// for runtime needs (width-masking, bit_width derivation). Serializing
/// the result directly via `serde_json::to_string` must NOT expose the
/// parser AST to JSON consumers. The documented JSON contract is
/// `output::json::JsonMatchResult` which omits this field; the library
/// type now matches that contract via `#[serde(skip)]` on the field.
///
/// Origin findings 1B-H2 (CWE-200 information exposure through serialized
/// AST shape) and 2A-M1 (security-lens variant).
#[test]
fn test_rule_match_type_kind_not_serialized_in_evaluation_result() {
    use libmagic_rs::parser::ast::{TypeKind, Value};
    use libmagic_rs::{EvaluationMetadata, EvaluationResult, evaluator::RuleMatch};

    // Construct an EvaluationResult containing a RuleMatch whose
    // `type_kind` is a fully-qualified TypeKind variant. Then serialize
    // it and assert the rendered JSON contains no `type_kind` key.
    let rule_match = RuleMatch::new(
        "ELF executable".to_string(),
        0,
        0,
        Value::Uint(0x7f),
        TypeKind::Byte { signed: false },
        0.9,
    );
    let metadata = EvaluationMetadata::default();
    let result = EvaluationResult::new(
        "ELF executable".to_string(),
        None,
        0.9,
        vec![rule_match],
        metadata,
    );

    let json = serde_json::to_string(&result).expect("must serialize");
    let parsed: serde_json::Value =
        serde_json::from_str(&json).expect("emitted JSON must round-trip through serde_json");

    // Walk the structure and assert `type_kind` is absent from every
    // RuleMatch entry under `matches[*]`. Substring checks would false-
    // fail if any user-visible string happened to contain `type_kind`,
    // and -- more importantly -- they cannot prove key absence; only
    // structural inspection can.
    let matches = parsed
        .get("matches")
        .and_then(|v| v.as_array())
        .expect("EvaluationResult JSON must have a `matches` array");
    assert!(
        !matches.is_empty(),
        "EvaluationResult JSON must include at least one rule match for this assertion to be meaningful"
    );
    for (i, m) in matches.iter().enumerate() {
        let obj = m.as_object().unwrap_or_else(|| {
            panic!("matches[{i}] must be a JSON object, got {m}");
        });
        assert!(
            !obj.contains_key("type_kind"),
            "matches[{i}] must not contain `type_kind` key \
             (1B-H2 / 2A-M1 regression: parser AST leaking into JSON output). \
             Got keys: {:?}",
            obj.keys().collect::<Vec<_>>()
        );
        assert!(
            obj.contains_key("value"),
            "matches[{i}] must still include `value` -- #[serde(skip)] should \
             only suppress type_kind, not the surrounding fields. Got keys: {:?}",
            obj.keys().collect::<Vec<_>>()
        );
        assert!(
            obj.contains_key("confidence"),
            "matches[{i}] must still include `confidence`. Got keys: {:?}",
            obj.keys().collect::<Vec<_>>()
        );
    }
}

/// Sanity check: the `type_kind` field is still accessible from Rust
/// code after `#[serde(skip)]`. The attribute affects serde only; field
/// access for runtime consumers (`format_magic_message`, `bit_width()`
/// derivation) is unchanged.
#[test]
fn test_rule_match_type_kind_still_accessible_in_rust() {
    use libmagic_rs::evaluator::RuleMatch;
    use libmagic_rs::parser::ast::{Endianness, TypeKind, Value};

    let m = RuleMatch::new(
        "test".to_string(),
        0,
        0,
        Value::Uint(0),
        TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        },
        1.0,
    );

    // Field access still works
    match m.type_kind {
        TypeKind::Long {
            endian: Endianness::Little,
            signed: false,
        } => {}
        ref other => panic!("type_kind field access broke: got {other:?}"),
    }

    // bit_width() derivation still works (relied on by format_magic_message)
    assert_eq!(m.type_kind.bit_width(), Some(32));
}