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_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_integer(value, key = 'unknown', min = 0, max = 1_000_000) ⇒ Integer?
Validates integer configuration values with range checking.
-
.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_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 |
# 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 # Content validation is handled by the actual minification libraries # They will properly parse and validate the content true end |
.validate_file_path(path) ⇒ Boolean
Validates file paths for security issues
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/jekyll-minifier.rb', line 199 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.}. Using default.") nil end 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_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 |