oatf 0.4.0

Rust SDK for the Open Agent Threat Format (OATF)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! YAML → [`Document`] deserialization.
//!
//! Two-step process: YAML → `serde_json::Value` → `Document`. Pre-parse rejects
//! YAML anchors/aliases/merge keys, multi-document streams, and unknown top-level keys.

use crate::error::{ParseError, ParseErrorKind};
use crate::types::Document;

/// Parse a YAML string into an unvalidated Document.
///
/// Performs YAML deserialization and type mapping only.
/// Does NOT validate document conformance or apply normalization.
pub fn parse(input: &str) -> Result<Document, ParseError> {
    if input.trim().is_empty() {
        return Err(ParseError {
            kind: ParseErrorKind::Syntax,
            message: "empty input".to_string(),
            path: None,
            line: None,
            column: None,
        });
    }

    const MAX_INPUT_SIZE: usize = 10 * 1024 * 1024; // 10 MB
    if input.len() > MAX_INPUT_SIZE {
        return Err(ParseError {
            kind: ParseErrorKind::Syntax,
            message: format!(
                "input size {} bytes exceeds maximum of {} bytes",
                input.len(),
                MAX_INPUT_SIZE
            ),
            path: None,
            line: None,
            column: None,
        });
    }

    // Check for YAML anchors, aliases, and merge keys (V-020)
    // We do a pre-scan of the raw text for anchor/alias markers
    check_yaml_anchors_aliases(input)?;

    // Check for multi-document YAML (multiple --- markers)
    check_multi_document(input)?;

    // Deserialize using serde-saphyr via serde_json Value as intermediate
    // First parse YAML to serde_json::Value, then convert to Document
    let value: serde_json::Value = serde_saphyr::from_str(input).map_err(|e| {
        let msg = e.to_string();
        // Try to extract location info from the error message
        ParseError {
            kind: classify_saphyr_error(&msg),
            message: msg,
            path: None,
            line: None,
            column: None,
        }
    })?;

    // Ensure root is a mapping/object
    if !value.is_object() {
        return Err(ParseError {
            kind: ParseErrorKind::TypeMismatch,
            message: "document root must be a YAML mapping".to_string(),
            path: None,
            line: None,
            column: None,
        });
    }

    // Validate no unknown top-level keys (only oatf, $schema, attack allowed)
    let oatf_is_first_key = if let Some(obj) = value.as_object() {
        for key in obj.keys() {
            match key.as_str() {
                "oatf" | "$schema" | "attack" => {}
                other => {
                    return Err(ParseError {
                        kind: ParseErrorKind::TypeMismatch,
                        message: format!("unknown top-level field: {}", other),
                        path: Some(other.to_string()),
                        line: None,
                        column: None,
                    });
                }
            }
        }
        obj.keys().next().map(|k| k == "oatf").unwrap_or(false)
    } else {
        false
    };

    // Convert serde_json::Value to Document
    let mut doc: Document = serde_json::from_value(value).map_err(|e| {
        let msg = e.to_string();
        ParseError {
            kind: classify_json_error(&msg),
            message: msg,
            path: None,
            line: None,
            column: None,
        }
    })?;

    doc.oatf_is_first_key = oatf_is_first_key;

    // Validate extension fields (only x-* prefixed keys allowed)
    validate_extension_keys(&doc)?;

    Ok(doc)
}

/// Validate that all extension (flatten) fields start with "x-".
fn validate_extension_keys(doc: &Document) -> Result<(), ParseError> {
    check_extensions(&doc.attack.extensions, "attack")?;
    check_extensions(&doc.attack.execution.extensions, "attack.execution")?;

    if let Some(actors) = &doc.attack.execution.actors {
        for (i, actor) in actors.iter().enumerate() {
            check_extensions(
                &actor.extensions,
                &format!("attack.execution.actors[{}]", i),
            )?;
            for (j, phase) in actor.phases.iter().enumerate() {
                check_extensions(
                    &phase.extensions,
                    &format!("attack.execution.actors[{}].phases[{}]", i, j),
                )?;
            }
        }
    }

    if let Some(phases) = &doc.attack.execution.phases {
        for (j, phase) in phases.iter().enumerate() {
            check_extensions(
                &phase.extensions,
                &format!("attack.execution.phases[{}]", j),
            )?;
        }
    }

    if let Some(indicators) = &doc.attack.indicators {
        for (i, ind) in indicators.iter().enumerate() {
            check_extensions(&ind.extensions, &format!("attack.indicators[{}]", i))?;
        }
    }

    Ok(())
}

fn check_extensions(
    extensions: &indexmap::IndexMap<String, serde_json::Value>,
    path: &str,
) -> Result<(), ParseError> {
    for key in extensions.keys() {
        if !key.starts_with("x-") {
            return Err(ParseError {
                kind: ParseErrorKind::TypeMismatch,
                message: format!(
                    "unknown field '{}' at {} (non-extension fields must not use reserved names; extension fields must start with 'x-')",
                    key, path
                ),
                path: Some(format!("{}.{}", path, key)),
                line: None,
                column: None,
            });
        }
    }
    Ok(())
}

/// Check for YAML anchors (&), aliases (*), and merge keys (<<).
/// Tracks block scalar state to skip content inside `|` and `>` blocks.
fn check_yaml_anchors_aliases(input: &str) -> Result<(), ParseError> {
    let lines: Vec<&str> = input.lines().collect();
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();

        // Skip comments and empty lines
        if trimmed.is_empty() || trimmed.starts_with('#') {
            i += 1;
            continue;
        }

        // Check if this line introduces a block scalar (value ends with |, >, |-, |+, >-, >+)
        if line_introduces_block_scalar(trimmed) {
            i = skip_block_scalar(&lines, i);
            continue;
        }

        // Strip inline comments before scanning for forbidden YAML features.
        // This avoids false positives on comment text like `# <<:` or `# &anchor`.
        let no_comment = strip_trailing_comment(trimmed);
        let in_content = strip_yaml_string_literals(no_comment);

        // Check for merge keys
        if in_content.contains("<<:") || in_content.contains("<< :") {
            return Err(ParseError {
                kind: ParseErrorKind::Syntax,
                message: "YAML merge keys (<<) are not allowed in OATF documents".to_string(),
                path: None,
                line: Some(i + 1),
                column: None,
            });
        }

        // Mask plain (unquoted) scalar values so that & and * inside them
        // are not mistaken for YAML anchors/aliases.
        let scannable = mask_plain_scalar_values(&in_content);

        // Check for anchors: & at start of value position
        if let Some(pos) = find_yaml_anchor(&scannable) {
            return Err(ParseError {
                kind: ParseErrorKind::Syntax,
                message: "YAML anchors (&) are not allowed in OATF documents".to_string(),
                path: None,
                line: Some(i + 1),
                column: Some(pos + 1),
            });
        }

        // Check for aliases: * at start of value position
        if let Some(pos) = find_yaml_alias(&scannable) {
            return Err(ParseError {
                kind: ParseErrorKind::Syntax,
                message: "YAML aliases (*) are not allowed in OATF documents".to_string(),
                path: None,
                line: Some(i + 1),
                column: Some(pos + 1),
            });
        }

        // Check for custom YAML tags (! at value position)
        if let Some(pos) = find_yaml_tag(&in_content) {
            return Err(ParseError {
                kind: ParseErrorKind::Syntax,
                message: "custom YAML tags are not allowed in OATF documents".to_string(),
                path: None,
                line: Some(i + 1),
                column: Some(pos + 1),
            });
        }

        i += 1;
    }
    Ok(())
}

/// Mask plain (unquoted) scalar value portions of a YAML line.
///
/// In a mapping line like `key: some &text`, the `&text` is part of a plain
/// scalar value, not a YAML anchor. This function replaces the plain scalar
/// portion after a colon with spaces so that anchor/alias detection only
/// fires on structural YAML positions.
///
/// Leaves intact: anchors/aliases that appear right after `: ` or `- `
/// (e.g., `key: &anchor value` or `key: *alias`), flow indicators (`[`, `{`),
/// and quoted strings (already stripped by the caller).
fn mask_plain_scalar_values(line: &str) -> String {
    let mut result = line.to_string();

    // Find the mapping value start (after colon)
    if let Some(colon_pos) = find_colon_in_yaml(line) {
        let after_colon = &line[colon_pos + 1..];
        let value_start = after_colon.find(|c: char| c != ' ' && c != '\t');
        if let Some(offset) = value_start {
            let abs_pos = colon_pos + 1 + offset;
            let first_char = line.as_bytes()[abs_pos];
            // If the value starts with &, *, [, {, or is empty — keep scanning
            // (these are structural YAML positions where anchors/aliases are valid)
            if first_char != b'&' && first_char != b'*' && first_char != b'[' && first_char != b'{'
            {
                // Plain scalar value — mask it to prevent false positives
                let mask = " ".repeat(line.len() - abs_pos);
                result = format!("{}{}", &line[..abs_pos], mask);
            }
        }
    } else if line.trim_start().starts_with("- ") {
        // Sequence entry: `- value`
        let trimmed = line.trim_start();
        let prefix_len = line.len() - trimmed.len();
        let after_dash = &trimmed[2..]; // skip "- "
        let value_start = after_dash.find(|c: char| c != ' ' && c != '\t');
        if let Some(offset) = value_start {
            let abs_pos = prefix_len + 2 + offset;
            let first_char = line.as_bytes()[abs_pos];
            if first_char != b'&' && first_char != b'*' && first_char != b'[' && first_char != b'{'
            {
                let mask = " ".repeat(line.len() - abs_pos);
                result = format!("{}{}", &line[..abs_pos], mask);
            }
        }
    }

    result
}

/// Check if a trimmed YAML line's value ends with a block scalar indicator.
fn line_introduces_block_scalar(trimmed: &str) -> bool {
    // A block scalar is introduced when a mapping value (after `:`) or sequence entry (after `- `)
    // ends with |, >, |-, |+, >-, >+ (possibly followed by a comment).
    // Find the value part after the colon (for mappings)
    let value_part = if let Some(colon_pos) = find_colon_in_yaml(trimmed) {
        trimmed[colon_pos + 1..].trim()
    } else if let Some(rest) = trimmed.strip_prefix("- ") {
        rest.trim()
    } else {
        return false;
    };

    // Strip trailing comment
    let value_no_comment = strip_trailing_comment(value_part);
    let v = value_no_comment.trim();

    matches!(v, "|" | ">" | "|-" | "|+" | ">-" | ">+")
}

fn skip_double_quoted(bytes: &[u8], start: usize) -> usize {
    let mut i = start + 1;
    while i < bytes.len() {
        if bytes[i] == b'\\' {
            i += 2;
            continue;
        }
        if bytes[i] == b'"' {
            return i + 1;
        }
        i += 1;
    }
    i
}

fn skip_single_quoted(bytes: &[u8], start: usize) -> usize {
    let mut i = start + 1;
    while i < bytes.len() {
        if bytes[i] == b'\'' {
            i += 1;
            if i < bytes.len() && bytes[i] == b'\'' {
                i += 1;
            } else {
                break;
            }
        } else {
            i += 1;
        }
    }
    i
}

/// Find the position of the key-value colon in a YAML line, skipping quoted strings.
fn find_colon_in_yaml(line: &str) -> Option<usize> {
    let bytes = line.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'"' => i = skip_double_quoted(bytes, i),
            b'\'' => i = skip_single_quoted(bytes, i),
            b':' if i + 1 >= bytes.len() || bytes[i + 1] == b' ' || bytes[i + 1] == b'\t' => {
                return Some(i);
            }
            _ => i += 1,
        }
    }
    None
}

/// Strip trailing YAML comment (# ...) from a value, respecting quotes.
fn strip_trailing_comment(value: &str) -> &str {
    let bytes = value.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'"' => i = skip_double_quoted(bytes, i),
            b'\'' => i = skip_single_quoted(bytes, i),
            b' ' if i + 1 < bytes.len() && bytes[i + 1] == b'#' => {
                return &value[..i];
            }
            b'#' if i == 0 => {
                return "";
            }
            _ => i += 1,
        }
    }
    value
}

/// Skip all lines belonging to a block scalar starting at `start_idx`.
/// Returns the index of the first line after the block.
fn skip_block_scalar(lines: &[&str], start_idx: usize) -> usize {
    // The block scalar content indent is determined by the first non-empty line after the header.
    let mut i = start_idx + 1;

    // Find the content indent from the first non-empty content line
    let content_indent = loop {
        if i >= lines.len() {
            return i;
        }
        let line = lines[i];
        if line.trim().is_empty() {
            i += 1;
            continue;
        }
        // Count leading spaces
        let indent = line.len() - line.trim_start().len();
        break indent;
    };

    // The header line's indent level
    let header_indent = lines[start_idx].len() - lines[start_idx].trim_start().len();

    // Content must be indented more than the header
    if content_indent <= header_indent {
        return start_idx + 1;
    }

    // Skip all lines that are either empty or indented at content_indent or deeper
    while i < lines.len() {
        let line = lines[i];
        if line.trim().is_empty() {
            i += 1;
            continue;
        }
        let indent = line.len() - line.trim_start().len();
        if indent >= content_indent {
            i += 1;
        } else {
            break;
        }
    }
    i
}

/// Find YAML anchor (&name) in a line, returning position if found.
/// Requires `&` to be in value position (preceded by space, colon, dash, or at line start)
/// to avoid false positives on URLs and other content containing `&`.
fn find_yaml_anchor(line: &str) -> Option<usize> {
    let bytes = line.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'&' {
            // Check if followed by a valid YAML anchor character
            if i + 1 < bytes.len()
                && is_yaml_anchor_char(bytes[i + 1])
                && (i == 0 || bytes[i - 1] == b' ' || bytes[i - 1] == b':' || bytes[i - 1] == b'-')
            {
                return Some(i);
            }
        }
        i += 1;
    }
    None
}

/// Find YAML alias (*name) in a line, returning position if found.
fn find_yaml_alias(line: &str) -> Option<usize> {
    let bytes = line.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'*' {
            // Check if preceded by space or start of line, and followed by anchor char
            if i + 1 < bytes.len()
                && is_yaml_anchor_char(bytes[i + 1])
                && (i == 0 || bytes[i - 1] == b' ' || bytes[i - 1] == b':' || bytes[i - 1] == b'-')
            {
                return Some(i);
            }
        }
        i += 1;
    }
    None
}

/// Find custom YAML tag (`!name`) in a line, returning position if found.
/// Matches `!` in value position (preceded by space, colon, dash, or at line start)
/// followed by a non-space character.
fn find_yaml_tag(line: &str) -> Option<usize> {
    let bytes = line.as_bytes();
    for i in 0..bytes.len() {
        if bytes[i] == b'!' {
            // Must be in value position
            if i == 0 || bytes[i - 1] == b' ' || bytes[i - 1] == b':' || bytes[i - 1] == b'-' {
                // Must be followed by a non-space char (tag name)
                if i + 1 < bytes.len() && bytes[i + 1] != b' ' {
                    return Some(i);
                }
            }
        }
    }
    None
}

fn is_yaml_anchor_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}

/// Strip string literals from a YAML line for anchor/alias detection.
fn strip_yaml_string_literals(line: &str) -> String {
    let mut result = String::new();
    let mut chars = line.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '"' => {
                // Skip double-quoted string
                result.push(' ');
                loop {
                    match chars.next() {
                        Some('\\') => {
                            chars.next(); // skip escaped char
                        }
                        Some('"') | None => break,
                        _ => {}
                    }
                }
            }
            '\'' => {
                // Skip single-quoted string
                result.push(' ');
                loop {
                    match chars.next() {
                        Some('\'') => {
                            if chars.peek() == Some(&'\'') {
                                chars.next(); // escaped single quote
                            } else {
                                break;
                            }
                        }
                        None => break,
                        _ => {}
                    }
                }
            }
            _ => result.push(c),
        }
    }
    result
}

/// Check for multiple YAML documents (--- separator).
/// Only matches `---` at column 0 to avoid false positives inside block scalars.
fn check_multi_document(input: &str) -> Result<(), ParseError> {
    let mut doc_count = 0;
    for line in input.lines() {
        // Document markers must start at column 0 per YAML spec
        if line.starts_with("---") && line[3..].trim().is_empty() {
            doc_count += 1;
            if doc_count > 1 {
                return Err(ParseError {
                    kind: ParseErrorKind::Syntax,
                    message: "multi-document YAML is not supported".to_string(),
                    path: None,
                    line: None,
                    column: None,
                });
            }
        }
    }
    Ok(())
}

fn classify_saphyr_error(msg: &str) -> ParseErrorKind {
    let lower = msg.to_lowercase();
    if lower.contains("unknown") || lower.contains("variant") {
        ParseErrorKind::UnknownVariant
    } else if lower.contains("type") || lower.contains("invalid") || lower.contains("expected") {
        ParseErrorKind::TypeMismatch
    } else {
        ParseErrorKind::Syntax
    }
}

fn classify_json_error(msg: &str) -> ParseErrorKind {
    let lower = msg.to_lowercase();
    if lower.contains("unknown variant") || lower.contains("unknown field") {
        ParseErrorKind::UnknownVariant
    } else if lower.contains("missing field") || lower.contains("invalid type") {
        ParseErrorKind::TypeMismatch
    } else {
        ParseErrorKind::Syntax
    }
}