| 1 | /* Minimal, security-focused TOML parser for gitswitch-c configuration |
| 2 | * Designed specifically for our configuration structure with extensive validation |
| 3 | */ |
| 4 | |
| 5 | #ifndef TOML_PARSER_H |
| 6 | #define TOML_PARSER_H |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include <stddef.h> |
| 10 | |
| 11 | /* Maximum limits for security */ |
| 12 | #define TOML_MAX_KEY_LEN 64 |
| 13 | #define TOML_MAX_VALUE_LEN 512 |
| 14 | #define TOML_MAX_SECTION_LEN 64 |
| 15 | #define TOML_MAX_SECTIONS 32 |
| 16 | #define TOML_MAX_KEYS_PER_SECTION 16 |
| 17 | #define TOML_MAX_FILE_SIZE (64 * 1024) /* 64KB max config file */ |
| 18 | |
| 19 | /* TOML value types */ |
| 20 | typedef enum { |
| 21 | TOML_TYPE_STRING, |
| 22 | TOML_TYPE_INTEGER, |
| 23 | TOML_TYPE_BOOLEAN, |
| 24 | TOML_TYPE_INVALID |
| 25 | } toml_value_type_t; |
| 26 | |
| 27 | /* TOML key-value pair */ |
| 28 | typedef struct { |
| 29 | char key[TOML_MAX_KEY_LEN]; |
| 30 | char value[TOML_MAX_VALUE_LEN]; |
| 31 | toml_value_type_t type; |
| 32 | bool is_set; |
| 33 | } toml_keyvalue_t; |
| 34 | |
| 35 | /* TOML section */ |
| 36 | typedef struct { |
| 37 | char name[TOML_MAX_SECTION_LEN]; |
| 38 | toml_keyvalue_t keys[TOML_MAX_KEYS_PER_SECTION]; |
| 39 | size_t key_count; |
| 40 | bool is_set; |
| 41 | } toml_section_t; |
| 42 | |
| 43 | /* TOML document structure */ |
| 44 | typedef struct { |
| 45 | toml_section_t sections[TOML_MAX_SECTIONS]; |
| 46 | size_t section_count; |
| 47 | char file_path[512]; |
| 48 | bool is_valid; |
| 49 | } toml_document_t; |
| 50 | |
| 51 | /* Parser state for security tracking */ |
| 52 | typedef struct { |
| 53 | const char *input; |
| 54 | size_t input_length; |
| 55 | size_t position; |
| 56 | size_t line_number; |
| 57 | size_t column_number; |
| 58 | bool has_error; |
| 59 | char error_message[256]; |
| 60 | } toml_parser_state_t; |
| 61 | |
| 62 | /* Function prototypes */ |
| 63 | |
| 64 | /** |
| 65 | * Initialize a TOML document structure |
| 66 | */ |
| 67 | void toml_init_document(toml_document_t *doc); |
| 68 | |
| 69 | /** |
| 70 | * Parse TOML from file with comprehensive security validation |
| 71 | * - Validates file size limits |
| 72 | * - Sanitizes all input |
| 73 | * - Checks for malicious patterns |
| 74 | * - Validates UTF-8 encoding |
| 75 | */ |
| 76 | int toml_parse_file(const char *file_path, toml_document_t *doc); |
| 77 | |
| 78 | /** |
| 79 | * Parse TOML from string buffer with security validation |
| 80 | */ |
| 81 | int toml_parse_string(const char *toml_string, size_t length, toml_document_t *doc); |
| 82 | |
| 83 | /** |
| 84 | * Get string value from TOML document with validation |
| 85 | * Returns validated and sanitized string value |
| 86 | */ |
| 87 | int toml_get_string(const toml_document_t *doc, const char *section, |
| 88 | const char *key, char *value, size_t value_size); |
| 89 | |
| 90 | /** |
| 91 | * Get integer value from TOML document with range validation |
| 92 | */ |
| 93 | int toml_get_integer(const toml_document_t *doc, const char *section, |
| 94 | const char *key, int *value); |
| 95 | |
| 96 | /** |
| 97 | * Get boolean value from TOML document |
| 98 | */ |
| 99 | int toml_get_boolean(const toml_document_t *doc, const char *section, |
| 100 | const char *key, bool *value); |
| 101 | |
| 102 | /** |
| 103 | * Set string value in TOML document with validation |
| 104 | */ |
| 105 | int toml_set_string(toml_document_t *doc, const char *section, |
| 106 | const char *key, const char *value); |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Set boolean value in TOML document |
| 111 | */ |
| 112 | int toml_set_boolean(toml_document_t *doc, const char *section, |
| 113 | const char *key, bool value); |
| 114 | |
| 115 | /** |
| 116 | * Write TOML document to file with atomic operations |
| 117 | * - Creates backup of existing file |
| 118 | * - Uses temporary file for atomic write |
| 119 | * - Validates written content |
| 120 | */ |
| 121 | int toml_write_file(const toml_document_t *doc, const char *file_path); |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Validate TOML document structure for our specific config schema |
| 126 | */ |
| 127 | int toml_validate_gitswitch_schema(const toml_document_t *doc); |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Get list of all sections in document |
| 132 | */ |
| 133 | int toml_get_sections(const toml_document_t *doc, char sections[][TOML_MAX_SECTION_LEN], |
| 134 | size_t max_sections, size_t *section_count); |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Security validation functions |
| 139 | */ |
| 140 | |
| 141 | /** |
| 142 | * Validate that input contains only safe characters |
| 143 | */ |
| 144 | bool toml_validate_safe_characters(const char *input, size_t length); |
| 145 | |
| 146 | /** |
| 147 | * Sanitize string value removing potentially dangerous content |
| 148 | */ |
| 149 | int toml_sanitize_string(const char *input, char *output, size_t output_size); |
| 150 | |
| 151 | /** |
| 152 | * Validate file path for security (no directory traversal, etc.) |
| 153 | */ |
| 154 | bool toml_validate_file_path(const char *path); |
| 155 | |
| 156 | /** |
| 157 | * Check for TOML injection patterns |
| 158 | */ |
| 159 | bool toml_check_injection_patterns(const char *input, size_t length); |
| 160 | |
| 161 | /** |
| 162 | * Cleanup and free TOML document resources |
| 163 | */ |
| 164 | void toml_cleanup_document(toml_document_t *doc); |
| 165 | |
| 166 | /** |
| 167 | * Get last parser error message |
| 168 | */ |
| 169 | const char *toml_get_error_message(const toml_parser_state_t *state); |
| 170 | |
| 171 | #endif /* TOML_PARSER_H */ |