hedl-core 2.0.0

Core parser and data model for HEDL (Hierarchical Entity Data Language)
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Block string parsing for multi-line string literals.
//!
//! This module handles the parsing of block strings (multi-line strings delimited by `"""`).
//! Block strings are used in HEDL for long text values that span multiple lines.
//!
//! # Format
//!
//! ```text
//! key: """
//!   Line 1
//!   Line 2
//!   Line 3
//! """
//! ```
//!
//! # Rules
//!
//! - Opening `"""` must be followed by a newline (single-line block strings are not allowed)
//! - Closing `"""` must be on its own line (only whitespace/comments allowed after)
//! - Content between delimiters is preserved as-is, including leading/trailing whitespace
//! - Size limits are enforced to prevent memory exhaustion
//!
//! # Security
//!
//! This module implements robust overflow protection to prevent memory exhaustion attacks:
//!
//! - **Fail-Fast Overflow Detection**: All size calculations use `checked_add` to detect
//!   integer overflow at the earliest point, ensuring malicious inputs cannot bypass security limits.
//! - **Per-Line Validation**: Each line's contribution to the total size is validated before
//!   accumulation, preventing individual oversized lines from saturating size counters.
//! - **Cumulative Size Tracking**: Total block string size is tracked across all lines and
//!   validated against configurable limits (`max_block_string_size`).
//! - **No Silent Failures**: Integer overflows are never silently capped; they always result
//!   in explicit security errors with clear diagnostic messages.
//!
//! These protections ensure that block string parsing cannot be exploited for DoS attacks
//! through memory exhaustion, even with carefully crafted inputs near `usize::MAX`.

use crate::error::{HedlError, HedlResult};
use crate::lex::is_valid_key_token;
use crate::limits::Limits;

/// Result of trying to start a block string.
#[derive(Debug)]
pub(crate) enum BlockStringResult {
    /// Not a block string - parse as normal line.
    NotBlockString,
    /// Multi-line block string started - need to accumulate lines.
    MultiLineStarted(BlockStringState),
}

/// State for parsing multi-line block strings.
#[derive(Debug)]
pub(crate) struct BlockStringState {
    /// The key for the block string value.
    pub key: String,
    /// Accumulated content lines.
    pub content: Vec<String>,
    /// Starting line number (for error messages).
    pub start_line: usize,
    /// Indent level of the key.
    pub indent: usize,
    /// Total accumulated size in bytes.
    pub total_size: usize,
}

impl BlockStringState {
    /// Process a line while accumulating a block string.
    ///
    /// Returns `Some(String)` if the block string is complete (closing `"""` found),
    /// otherwise returns `None` and continues accumulation.
    pub fn process_line(
        &mut self,
        line: &str,
        line_num: usize,
        limits: &Limits,
    ) -> HedlResult<Option<String>> {
        // Check if this line contains the closing """
        if let Some(end_pos) = line.find("\"\"\"") {
            // Found closing - extract content before """
            let before_close = &line[..end_pos];

            // Check size limit before adding
            let new_size = self
                .total_size
                .checked_add(before_close.len())
                .ok_or_else(|| HedlError::security("block string size overflow", line_num))?;
            if new_size > limits.max_block_string_size {
                return Err(HedlError::security(
                    format!(
                        "block string size {} exceeds limit {}",
                        new_size, limits.max_block_string_size
                    ),
                    line_num,
                ));
            }
            self.total_size = new_size;
            self.content.push(before_close.to_string());

            // Validate nothing meaningful after closing """
            let after_close = line[end_pos + 3..].trim();
            if !after_close.is_empty() && !after_close.starts_with('#') {
                return Err(HedlError::syntax(
                    "unexpected content after closing \"\"\"",
                    line_num,
                ));
            }

            // Complete the block string
            let full_content = self.content.join("\n");
            Ok(Some(full_content))
        } else {
            // Check size limit before accumulating
            // Add 1 for the newline that will be added when joining
            let line_contribution = line
                .len()
                .checked_add(1)
                .ok_or_else(|| HedlError::security("line length overflow", line_num))?;
            let new_size = self
                .total_size
                .checked_add(line_contribution)
                .ok_or_else(|| HedlError::security("block string size overflow", line_num))?;
            if new_size > limits.max_block_string_size {
                return Err(HedlError::security(
                    format!(
                        "block string size {} exceeds limit {}",
                        new_size, limits.max_block_string_size
                    ),
                    line_num,
                ));
            }
            self.total_size = new_size;
            self.content.push(line.to_string());
            Ok(None)
        }
    }
}

/// Try to start a block string if the line contains `key: """`.
///
/// # Arguments
///
/// * `content` - The line content (after indentation is stripped)
/// * `line_num` - Current line number for error reporting
///
/// # Returns
///
/// - `BlockStringResult::MultiLineStarted` if a block string was started
/// - `BlockStringResult::NotBlockString` if this is not a block string
///
/// # Errors
///
/// Returns an error if:
/// - The key is invalid
/// - Content appears on the same line as opening `"""`
pub(crate) fn try_start_block_string(
    content: &str,
    indent: usize,
    line_num: usize,
) -> HedlResult<BlockStringResult> {
    // Look for key: """
    let Some(colon_pos) = content.find(':') else {
        return Ok(BlockStringResult::NotBlockString);
    };

    let key = content[..colon_pos].trim();
    let after_colon = &content[colon_pos + 1..];

    // Need space after colon
    if !after_colon.is_empty() && !after_colon.starts_with(' ') {
        return Ok(BlockStringResult::NotBlockString);
    }

    let value_str = after_colon.trim();

    // Check for """ start
    if !value_str.starts_with("\"\"\"") {
        return Ok(BlockStringResult::NotBlockString);
    }

    // Validate key
    if !is_valid_key_token(key) {
        return Err(HedlError::syntax(
            format!("invalid key: '{}'", key),
            line_num,
        ));
    }

    let after_open = &value_str[3..]; // Skip opening """

    // Per SPEC Section 8.2: After opening """, there MUST be a newline.
    // Single-line block strings like """content""" are NOT valid HEDL.
    let after_open_trimmed = after_open.trim_start();
    if after_open_trimmed.is_empty() || after_open_trimmed.starts_with('#') {
        // OK: nothing on this line after """ (will start multi-line)
        // Preserve whatever is after """ on this line (usually empty or whitespace/comment)
        Ok(BlockStringResult::MultiLineStarted(BlockStringState {
            key: key.to_string(),
            content: vec![after_open.to_string()],
            start_line: line_num,
            indent,
            total_size: after_open.len(),
        }))
    } else {
        // ERROR: content on same line as opening """
        Err(HedlError::syntax(
            "block string must have newline after opening \"\"\" (single-line block strings are not allowed)",
            line_num,
        ))
    }
}

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

    #[test]
    fn test_not_block_string_no_colon() {
        let result = try_start_block_string("just text", 0, 1).unwrap();
        assert!(matches!(result, BlockStringResult::NotBlockString));
    }

    #[test]
    fn test_not_block_string_no_triple_quote() {
        let result = try_start_block_string("key: value", 0, 1).unwrap();
        assert!(matches!(result, BlockStringResult::NotBlockString));
    }

    #[test]
    fn test_not_block_string_double_quote() {
        let result = try_start_block_string("key: \"value\"", 0, 1).unwrap();
        assert!(matches!(result, BlockStringResult::NotBlockString));
    }

    #[test]
    fn test_not_block_string_no_space_after_colon() {
        let result = try_start_block_string("key:\"\"\"", 0, 1).unwrap();
        assert!(matches!(result, BlockStringResult::NotBlockString));
    }

    #[test]
    fn test_valid_block_string_start() {
        let result = try_start_block_string("description: \"\"\"", 0, 1).unwrap();
        match result {
            BlockStringResult::MultiLineStarted(state) => {
                assert_eq!(state.key, "description");
                assert_eq!(state.start_line, 1);
                assert_eq!(state.indent, 0);
                assert_eq!(state.content.len(), 1);
            }
            BlockStringResult::NotBlockString => panic!("Expected block string to start"),
        }
    }

    #[test]
    fn test_valid_block_string_with_comment() {
        let result = try_start_block_string("key: \"\"\" # comment", 0, 1).unwrap();
        match result {
            BlockStringResult::MultiLineStarted(state) => {
                assert_eq!(state.key, "key");
            }
            BlockStringResult::NotBlockString => panic!("Expected block string to start"),
        }
    }

    #[test]
    fn test_invalid_key() {
        let result = try_start_block_string("123invalid: \"\"\"", 0, 1);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("invalid key"));
    }

    #[test]
    fn test_content_after_opening_quotes() {
        let result = try_start_block_string("key: \"\"\"content", 0, 1);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("must have newline after opening"));
    }

    #[test]
    fn test_process_line_accumulation() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 0,
        };
        let limits = Limits::default();

        let result = state.process_line("  Line 1", 2, &limits).unwrap();
        assert!(result.is_none()); // Still accumulating
        assert_eq!(state.content.len(), 2);
        assert_eq!(state.content[1], "  Line 1");
    }

    #[test]
    fn test_process_line_closing() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string(), "Line 1".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 7,
        };
        let limits = Limits::default();

        let result = state.process_line("\"\"\"", 3, &limits).unwrap();
        assert!(result.is_some());
        let content = result.unwrap();
        assert_eq!(content, "\nLine 1\n");
    }

    #[test]
    fn test_process_line_closing_with_content_before() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string(), "Line 1".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 7,
        };
        let limits = Limits::default();

        let result = state.process_line("Line 2\"\"\"", 3, &limits).unwrap();
        assert!(result.is_some());
        let content = result.unwrap();
        assert_eq!(content, "\nLine 1\nLine 2");
    }

    #[test]
    fn test_process_line_closing_with_comment() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string(), "Line 1".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 7,
        };
        let limits = Limits::default();

        let result = state.process_line("\"\"\" # comment", 3, &limits).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn test_process_line_closing_with_invalid_content_after() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 1,
        };
        let limits = Limits::default();

        let result = state.process_line("\"\"\" invalid", 3, &limits);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("unexpected content after closing"));
    }

    #[test]
    fn test_size_limit_exceeded_during_accumulation() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 0,
        };
        let limits = Limits {
            max_block_string_size: 10, // Very small limit
            ..Default::default()
        };

        let result = state.process_line(
            "This is a very long line that exceeds the limit",
            2,
            &limits,
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("exceeds limit"));
    }

    #[test]
    fn test_size_limit_exceeded_at_closing() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 5,
        };
        let limits = Limits {
            max_block_string_size: 10,
            ..Default::default()
        };

        let result = state.process_line("Long content before closing\"\"\"", 2, &limits);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("exceeds limit"));
    }

    #[test]
    fn test_preserve_empty_lines() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string(), "Line 1".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 7,
        };
        let limits = Limits::default();

        state.process_line("", 3, &limits).unwrap();
        state.process_line("Line 3", 4, &limits).unwrap();
        let result = state.process_line("\"\"\"", 5, &limits).unwrap();

        let content = result.unwrap();
        assert_eq!(content, "\nLine 1\n\nLine 3\n");
    }

    #[test]
    fn test_preserve_indentation() {
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 1,
        };
        let limits = Limits::default();

        state.process_line("  indented", 2, &limits).unwrap();
        state.process_line("    more indented", 3, &limits).unwrap();
        let result = state.process_line("\"\"\"", 4, &limits).unwrap();

        let content = result.unwrap();
        assert_eq!(content, "\n  indented\n    more indented\n");
    }

    // Security tests for overflow detection

    #[test]
    fn test_block_string_line_overflow() {
        // Test that a line with length usize::MAX causes overflow when adding 1
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 0,
        };
        let limits = Limits {
            max_block_string_size: usize::MAX,
            ..Default::default()
        };

        // Create a string that would overflow when adding 1
        // We can't actually allocate usize::MAX - 1 bytes in tests,
        // so we test the logic by verifying the check exists
        // by using a smaller case that would still overflow at usize::MAX

        // Instead, test near-overflow with a very large size
        state.total_size = usize::MAX - 10;
        let result = state.process_line("x".repeat(100).as_str(), 1, &limits);

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("overflow"),
            "Expected overflow error, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_block_string_cumulative_overflow() {
        // Test cumulative overflow detection
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: usize::MAX - 10,
        };
        let limits = Limits {
            max_block_string_size: usize::MAX,
            ..Default::default()
        };

        let result = state.process_line("x".repeat(100).as_str(), 1, &limits);

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("overflow"),
            "Expected overflow error, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_block_string_normal_operation_unaffected() {
        // Verify normal operation still works with the checked_add change
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 0,
        };
        let limits = Limits {
            max_block_string_size: 1000,
            ..Default::default()
        };

        // Normal lines should work fine
        assert!(state.process_line("normal line", 1, &limits).is_ok());
        assert!(state.process_line("another line", 2, &limits).is_ok());
        assert!(state.process_line("third line", 3, &limits).is_ok());

        // Complete the block string
        let result = state.process_line("\"\"\"", 4, &limits);
        assert!(result.is_ok());
        assert!(result.unwrap().is_some());
    }

    #[test]
    fn test_block_string_overflow_at_exactly_max() {
        // Test overflow when line.len() == usize::MAX
        // We can't create such a string, but we can test that
        // a line that would overflow when adding 1 is caught
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 0,
        };
        let limits = Limits {
            max_block_string_size: usize::MAX,
            ..Default::default()
        };

        // Set total_size close to max to trigger overflow in checked_add
        state.total_size = usize::MAX - 5;

        let result = state.process_line("123456", 1, &limits);

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("overflow"),
            "Expected overflow error, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_block_string_no_overflow_just_under_limit() {
        // Test that we can get close to the limit without overflow
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: 90,
        };
        let limits = Limits {
            max_block_string_size: 100,
            ..Default::default()
        };

        // Add a line that brings us to exactly the limit
        // line.len() = 8, +1 for newline = 9, total = 99 (under limit)
        let result = state.process_line("12345678", 1, &limits);
        assert!(result.is_ok());
        assert_eq!(state.total_size, 99);
    }

    #[test]
    fn test_block_string_overflow_security_message() {
        // Verify that overflow errors are marked as security errors
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: usize::MAX - 1,
        };
        let limits = Limits {
            max_block_string_size: usize::MAX,
            ..Default::default()
        };

        let result = state.process_line("test", 42, &limits);

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_msg = err.to_string();

        // Should be a security error with overflow in the message
        assert!(
            err_msg.contains("overflow"),
            "Expected overflow in error message"
        );

        // Verify it's the line length overflow that triggers first
        // (since line.len() + 1 would overflow before total_size check)
        assert!(
            err_msg.contains("line length overflow")
                || err_msg.contains("block string size overflow")
        );
    }

    #[test]
    fn test_block_string_closing_overflow() {
        // Test overflow detection in the closing path
        let mut state = BlockStringState {
            key: "test".to_string(),
            content: vec!["".to_string()],
            start_line: 1,
            indent: 0,
            total_size: usize::MAX - 5,
        };
        let limits = Limits {
            max_block_string_size: usize::MAX,
            ..Default::default()
        };

        // Try to close with content that would overflow
        let result = state.process_line("long content before\"\"\"", 2, &limits);

        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("overflow"),
            "Expected overflow error, got: {}",
            err_msg
        );
    }
}