Module: Jekyll::Minifier::ValidationHelpers
- Defined in:
- lib/jekyll-minifier.rb
Overview
ValidationHelpers module provides comprehensive input validation for Jekyll Minifier configurations and content processing
Constant Summary collapse
- MAX_SAFE_FILE_SIZE =
Maximum safe file size for processing (50MB)
50 * 1024 * 1024
- MAX_SAFE_STRING_LENGTH =
Maximum safe configuration value sizes
10_000- MAX_SAFE_ARRAY_SIZE =
1_000- MAX_SAFE_HASH_SIZE =
100
Class Method Summary collapse
-
.validate_array(value, key = 'unknown', max_size = MAX_SAFE_ARRAY_SIZE) ⇒ Array?
Validates array configuration values with size and content checks.
-
.validate_boolean(value, key = 'unknown') ⇒ Boolean?
Validates boolean configuration values.
-
.validate_css_content(content, file_path = 'unknown') ⇒ Boolean
Validates CSS content for basic syntax safety.
-
.validate_file_content(content, file_type = 'unknown', file_path = 'unknown') ⇒ Boolean
Validates file content size and encoding.
-
.validate_file_path(path) ⇒ Boolean
Validates file paths for security issues.
-
.validate_hash(value, key = 'unknown', max_size = MAX_SAFE_HASH_SIZE) ⇒ Hash?
Validates hash configuration values with size and content checks.
-
.validate_html_content(content, file_path = 'unknown') ⇒ Boolean
Validates HTML content for basic syntax safety.
-
.validate_integer(value, key = 'unknown', min = 0, max = 1_000_000) ⇒ Integer?
Validates integer configuration values with range checking.
-
.validate_js_content(content, file_path = 'unknown') ⇒ Boolean
Validates JavaScript content for basic syntax safety.
-
.validate_json_content(content, file_path = 'unknown') ⇒ Boolean
Validates JSON content for syntax safety.
-
.validate_string(value, key = 'unknown', max_length = MAX_SAFE_STRING_LENGTH) ⇒ String?
Validates string configuration values with length and safety checks.
Class Method Details
.validate_array(value, key = 'unknown', max_size = MAX_SAFE_ARRAY_SIZE) ⇒ Array?
Validates array configuration values with size and content checks
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 |
# File 'lib/jekyll-minifier.rb', line 95 def validate_array(value, key = 'unknown', max_size = MAX_SAFE_ARRAY_SIZE) return [] if value.nil? # Convert single values to arrays array_value = value.respond_to?(:to_a) ? value.to_a : [value] if array_value.size > max_size Jekyll.logger.warn("Jekyll Minifier:", "Array value for '#{key}' too large (#{array_value.size} > #{max_size}). Truncating.") array_value = array_value.take(max_size) end # Filter out invalid elements valid_elements = array_value.filter_map do |element| next nil if element.nil? if element.respond_to?(:to_s) str_element = element.to_s next nil if str_element.empty? || str_element.length > MAX_SAFE_STRING_LENGTH str_element else nil end end valid_elements end |
.validate_boolean(value, key = 'unknown') ⇒ Boolean?
Validates boolean configuration values
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jekyll-minifier.rb', line 25 def validate_boolean(value, key = 'unknown') return nil if value.nil? case value when true, false value when 'true', '1', 1 true when 'false', '0', 0 false else Jekyll.logger.warn("Jekyll Minifier:", "Invalid boolean value for '#{key}': #{value.inspect}. Using default.") nil end end |
.validate_css_content(content, file_path = 'unknown') ⇒ Boolean
Validates CSS content for basic syntax safety
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/jekyll-minifier.rb', line 210 def validate_css_content(content, file_path = 'unknown') # Check for extremely unbalanced braces (potential malformed CSS) open_braces = content.count('{') close_braces = content.count('}') if (open_braces - close_braces).abs > 100 Jekyll.logger.warn("Jekyll Minifier:", "CSS file appears malformed (unbalanced braces): #{file_path}") return false end true end |
.validate_file_content(content, file_type = 'unknown', file_path = 'unknown') ⇒ Boolean
Validates file content size and encoding
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 |
# File 'lib/jekyll-minifier.rb', line 175 def validate_file_content(content, file_type = 'unknown', file_path = 'unknown') return false if content.nil? return false unless content.respond_to?(:bytesize) # Check file size if content.bytesize > MAX_SAFE_FILE_SIZE Jekyll.logger.warn("Jekyll Minifier:", "File too large for safe processing: #{file_path} (#{content.bytesize} bytes > #{MAX_SAFE_FILE_SIZE})") return false end # Check encoding validity unless content.valid_encoding? Jekyll.logger.warn("Jekyll Minifier:", "Invalid encoding in file: #{file_path}. Skipping minification.") return false end # Basic content validation per file type case file_type when 'css' validate_css_content(content, file_path) when 'js', 'javascript' validate_js_content(content, file_path) when 'json' validate_json_content(content, file_path) when 'html', 'xml' validate_html_content(content, file_path) else true # Unknown types pass through end end |
.validate_file_path(path) ⇒ Boolean
Validates file paths for security issues
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/jekyll-minifier.rb', line 281 def validate_file_path(path) return false if path.nil? || path.empty? return false unless path.respond_to?(:to_s) path_str = path.to_s # Check for directory traversal attempts if path_str.include?('../') || path_str.include?('..\\') || path_str.include?('~/') Jekyll.logger.warn("Jekyll Minifier:", "Unsafe file path detected: #{path_str}") return false end # Check for null bytes if path_str.include?("\0") Jekyll.logger.warn("Jekyll Minifier:", "File path contains null byte: #{path_str}") return false end true end |
.validate_hash(value, key = 'unknown', max_size = MAX_SAFE_HASH_SIZE) ⇒ Hash?
Validates hash configuration values with size and content checks
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 |
# File 'lib/jekyll-minifier.rb', line 127 def validate_hash(value, key = 'unknown', max_size = MAX_SAFE_HASH_SIZE) return nil if value.nil? return nil unless value.respond_to?(:to_h) begin hash_value = value.to_h if hash_value.size > max_size Jekyll.logger.warn("Jekyll Minifier:", "Hash value for '#{key}' too large (#{hash_value.size} > #{max_size}). Using default.") return nil end # Validate hash keys and values validated_hash = {} hash_value.each do |k, v| # Convert keys to symbols for consistency key_sym = k.respond_to?(:to_sym) ? k.to_sym : nil next unless key_sym # Basic validation of values case v when String validated_value = validate_string(v, "#{key}[#{key_sym}]") validated_hash[key_sym] = validated_value if validated_value when Integer, Numeric validated_hash[key_sym] = v when true, false validated_hash[key_sym] = v when nil # Allow nil values validated_hash[key_sym] = nil else Jekyll.logger.warn("Jekyll Minifier:", "Unsupported value type for '#{key}[#{key_sym}]': #{v.class}. Skipping.") end end validated_hash rescue => e Jekyll.logger.warn("Jekyll Minifier:", "Failed to validate hash for '#{key}': #{e.message}. Using default.") nil end end |
.validate_html_content(content, file_path = 'unknown') ⇒ Boolean
Validates HTML content for basic syntax safety
263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/jekyll-minifier.rb', line 263 def validate_html_content(content, file_path = 'unknown') # Basic HTML tag balance check = content.scan(/<[^\/>]+>/).length = content.scan(/<\/[^>]+>/).length self_closing = content.scan(/<[^>]+\/>/).length # Allow for reasonable imbalance (HTML5 void elements, etc.) if ( - - self_closing).abs > 50 Jekyll.logger.warn("Jekyll Minifier:", "HTML file appears malformed (unbalanced tags): #{file_path}") return false end true end |
.validate_integer(value, key = 'unknown', min = 0, max = 1_000_000) ⇒ Integer?
Validates integer configuration values with range checking
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jekyll-minifier.rb', line 47 def validate_integer(value, key = 'unknown', min = 0, max = 1_000_000) return nil if value.nil? begin int_value = Integer(value) if int_value < min || int_value > max Jekyll.logger.warn("Jekyll Minifier:", "Integer value for '#{key}' out of range [#{min}-#{max}]: #{int_value}. Using default.") return nil end int_value rescue ArgumentError, TypeError Jekyll.logger.warn("Jekyll Minifier:", "Invalid integer value for '#{key}': #{value.inspect}. Using default.") nil end end |
.validate_js_content(content, file_path = 'unknown') ⇒ Boolean
Validates JavaScript content for basic syntax safety
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/jekyll-minifier.rb', line 227 def validate_js_content(content, file_path = 'unknown') # Check for extremely unbalanced braces and parentheses open_braces = content.count('{') close_braces = content.count('}') open_parens = content.count('(') close_parens = content.count(')') if (open_braces - close_braces).abs > 100 || (open_parens - close_parens).abs > 100 Jekyll.logger.warn("Jekyll Minifier:", "JavaScript file appears malformed (unbalanced braces/parens): #{file_path}") return false end true end |
.validate_json_content(content, file_path = 'unknown') ⇒ Boolean
Validates JSON content for syntax safety
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/jekyll-minifier.rb', line 246 def validate_json_content(content, file_path = 'unknown') # Basic JSON structure validation without full parsing trimmed = content.strip unless (trimmed.start_with?('{') && trimmed.end_with?('}')) || (trimmed.start_with?('[') && trimmed.end_with?(']')) Jekyll.logger.warn("Jekyll Minifier:", "JSON file doesn't appear to have valid structure: #{file_path}") return false end true end |
.validate_string(value, key = 'unknown', max_length = MAX_SAFE_STRING_LENGTH) ⇒ String?
Validates string configuration values with length and safety checks
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-minifier.rb', line 70 def validate_string(value, key = 'unknown', max_length = MAX_SAFE_STRING_LENGTH) return nil if value.nil? return nil unless value.respond_to?(:to_s) str_value = value.to_s if str_value.length > max_length Jekyll.logger.warn("Jekyll Minifier:", "String value for '#{key}' too long (#{str_value.length} > #{max_length}). Using default.") return nil end # Basic safety check for control characters if str_value.match?(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/) Jekyll.logger.warn("Jekyll Minifier:", "String value for '#{key}' contains unsafe control characters. Using default.") return nil end str_value end |