-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhcl_validation.go
More file actions
51 lines (44 loc) · 1.61 KB
/
Copy pathhcl_validation.go
File metadata and controls
51 lines (44 loc) · 1.61 KB
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
// hcl_validation.go: Validation functions for HCL parser
//
// Copyright (c) 2025 AGILira - A. Giordano
// Series: an AGILira fragment
// SPDX-License-Identifier: MPL-2.0
package argus
import (
"fmt"
"strings"
"unicode"
"github.com/agilira/go-errors"
)
// validateHCLKey validates that an HCL key follows proper naming conventions.
// Keys must be non-empty and properly formatted according to HCL spec.
func validateHCLKey(key string, lineNum int) error {
if key == "" {
return errors.New(ErrCodeInvalidConfig,
fmt.Sprintf("invalid HCL key at line %d: key cannot be empty", lineNum))
}
// SECURITY FIX: Check for dangerous control characters including null bytes
for _, char := range key {
if char == '\x00' {
return errors.New(ErrCodeInvalidConfig,
fmt.Sprintf("invalid HCL key at line %d: null byte not allowed in keys", lineNum))
}
// Block other dangerous control characters (except tab, LF, CR)
if char < 32 && char != '\t' && char != '\n' && char != '\r' {
return errors.New(ErrCodeInvalidConfig,
fmt.Sprintf("invalid HCL key at line %d: control character not allowed in keys", lineNum))
}
// Block non-printable characters (like DEL 0x7F)
if !unicode.IsPrint(char) && char != '\t' {
return errors.New(ErrCodeInvalidConfig,
fmt.Sprintf("invalid HCL key at line %d: non-printable character not allowed in keys", lineNum))
}
}
// Check for whitespace in key (indicates potential parsing issue)
if strings.TrimSpace(key) != key {
return errors.New(ErrCodeInvalidConfig,
fmt.Sprintf("invalid HCL key at line %d: key contains unexpected whitespace",
lineNum))
}
return nil
}