|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Vmpooler |
| 4 | + class API |
| 5 | + # Input validation helpers to enhance security |
| 6 | + module InputValidator |
| 7 | + # Maximum lengths to prevent abuse |
| 8 | + MAX_HOSTNAME_LENGTH = 253 |
| 9 | + MAX_TAG_KEY_LENGTH = 50 |
| 10 | + MAX_TAG_VALUE_LENGTH = 255 |
| 11 | + MAX_REASON_LENGTH = 500 |
| 12 | + MAX_POOL_NAME_LENGTH = 100 |
| 13 | + MAX_TOKEN_LENGTH = 64 |
| 14 | + |
| 15 | + # Valid patterns |
| 16 | + HOSTNAME_PATTERN = /\A[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)* \z/ix.freeze |
| 17 | + POOL_NAME_PATTERN = /\A[a-zA-Z0-9_-]+\z/.freeze |
| 18 | + TAG_KEY_PATTERN = /\A[a-zA-Z0-9_\-.]+\z/.freeze |
| 19 | + TOKEN_PATTERN = /\A[a-zA-Z0-9\-_]+\z/.freeze |
| 20 | + INTEGER_PATTERN = /\A\d+\z/.freeze |
| 21 | + |
| 22 | + class ValidationError < StandardError; end |
| 23 | + |
| 24 | + # Validate hostname format and length |
| 25 | + def validate_hostname(hostname) |
| 26 | + return error_response('Hostname is required') if hostname.nil? || hostname.empty? |
| 27 | + return error_response('Hostname too long') if hostname.length > MAX_HOSTNAME_LENGTH |
| 28 | + return error_response('Invalid hostname format') unless hostname.match?(HOSTNAME_PATTERN) |
| 29 | + |
| 30 | + true |
| 31 | + end |
| 32 | + |
| 33 | + # Validate pool/template name |
| 34 | + def validate_pool_name(pool_name) |
| 35 | + return error_response('Pool name is required') if pool_name.nil? || pool_name.empty? |
| 36 | + return error_response('Pool name too long') if pool_name.length > MAX_POOL_NAME_LENGTH |
| 37 | + return error_response('Invalid pool name format') unless pool_name.match?(POOL_NAME_PATTERN) |
| 38 | + |
| 39 | + true |
| 40 | + end |
| 41 | + |
| 42 | + # Validate tag key and value |
| 43 | + def validate_tag(key, value) |
| 44 | + return error_response('Tag key is required') if key.nil? || key.empty? |
| 45 | + return error_response('Tag key too long') if key.length > MAX_TAG_KEY_LENGTH |
| 46 | + return error_response('Invalid tag key format') unless key.match?(TAG_KEY_PATTERN) |
| 47 | + |
| 48 | + if value |
| 49 | + return error_response('Tag value too long') if value.length > MAX_TAG_VALUE_LENGTH |
| 50 | + |
| 51 | + # Sanitize value to prevent injection attacks |
| 52 | + sanitized_value = value.gsub(/[^\w\s\-.@:\/]/, '') |
| 53 | + return error_response('Tag value contains invalid characters') if sanitized_value != value |
| 54 | + end |
| 55 | + |
| 56 | + true |
| 57 | + end |
| 58 | + |
| 59 | + # Validate token format |
| 60 | + def validate_token_format(token) |
| 61 | + return error_response('Token is required') if token.nil? || token.empty? |
| 62 | + return error_response('Token too long') if token.length > MAX_TOKEN_LENGTH |
| 63 | + return error_response('Invalid token format') unless token.match?(TOKEN_PATTERN) |
| 64 | + |
| 65 | + true |
| 66 | + end |
| 67 | + |
| 68 | + # Validate integer parameter |
| 69 | + def validate_integer(value, name = 'value', min: nil, max: nil) |
| 70 | + return error_response("#{name} is required") if value.nil? |
| 71 | + |
| 72 | + value_str = value.to_s |
| 73 | + return error_response("#{name} must be a valid integer") unless value_str.match?(INTEGER_PATTERN) |
| 74 | + |
| 75 | + int_value = value.to_i |
| 76 | + return error_response("#{name} must be at least #{min}") if min && int_value < min |
| 77 | + return error_response("#{name} must be at most #{max}") if max && int_value > max |
| 78 | + |
| 79 | + int_value |
| 80 | + end |
| 81 | + |
| 82 | + # Validate VM request count |
| 83 | + def validate_vm_count(count) |
| 84 | + validated = validate_integer(count, 'VM count', min: 1, max: 100) |
| 85 | + return validated if validated.is_a?(Hash) # error response |
| 86 | + |
| 87 | + validated |
| 88 | + end |
| 89 | + |
| 90 | + # Validate disk size |
| 91 | + def validate_disk_size(size) |
| 92 | + validated = validate_integer(size, 'Disk size', min: 1, max: 2048) |
| 93 | + return validated if validated.is_a?(Hash) # error response |
| 94 | + |
| 95 | + validated |
| 96 | + end |
| 97 | + |
| 98 | + # Validate lifetime (TTL) in hours |
| 99 | + def validate_lifetime(lifetime) |
| 100 | + validated = validate_integer(lifetime, 'Lifetime', min: 1, max: 168) # max 1 week |
| 101 | + return validated if validated.is_a?(Hash) # error response |
| 102 | + |
| 103 | + validated |
| 104 | + end |
| 105 | + |
| 106 | + # Validate reason text |
| 107 | + def validate_reason(reason) |
| 108 | + return true if reason.nil? || reason.empty? |
| 109 | + return error_response('Reason too long') if reason.length > MAX_REASON_LENGTH |
| 110 | + |
| 111 | + # Sanitize to prevent XSS/injection |
| 112 | + sanitized = reason.gsub(/[<>"']/, '') |
| 113 | + return error_response('Reason contains invalid characters') if sanitized != reason |
| 114 | + |
| 115 | + true |
| 116 | + end |
| 117 | + |
| 118 | + # Sanitize JSON body to prevent injection |
| 119 | + def sanitize_json_body(body) |
| 120 | + return {} if body.nil? || body.empty? |
| 121 | + |
| 122 | + begin |
| 123 | + parsed = JSON.parse(body) |
| 124 | + return error_response('Request body must be a JSON object') unless parsed.is_a?(Hash) |
| 125 | + |
| 126 | + # Limit depth and size to prevent DoS |
| 127 | + return error_response('Request body too complex') if json_depth(parsed) > 5 |
| 128 | + return error_response('Request body too large') if body.length > 10_240 # 10KB max |
| 129 | + |
| 130 | + parsed |
| 131 | + rescue JSON::ParserError => e |
| 132 | + error_response("Invalid JSON: #{e.message}") |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + # Check if validation result is an error |
| 137 | + def validation_error?(result) |
| 138 | + result.is_a?(Hash) && result['ok'] == false |
| 139 | + end |
| 140 | + |
| 141 | + private |
| 142 | + |
| 143 | + def error_response(message) |
| 144 | + { 'ok' => false, 'error' => message } |
| 145 | + end |
| 146 | + |
| 147 | + def json_depth(obj, depth = 0) |
| 148 | + return depth unless obj.is_a?(Hash) || obj.is_a?(Array) |
| 149 | + return depth + 1 if obj.empty? |
| 150 | + |
| 151 | + if obj.is_a?(Hash) |
| 152 | + depth + 1 + obj.values.map { |v| json_depth(v, 0) }.max |
| 153 | + else |
| 154 | + depth + 1 + obj.map { |v| json_depth(v, 0) }.max |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments