|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "base" |
| 4 | + |
| 5 | +module Hooks |
| 6 | + module Plugins |
| 7 | + module RequestValidator |
| 8 | + # Generic shared secret validator for webhooks |
| 9 | + # |
| 10 | + # This validator provides simple shared secret authentication for webhook requests. |
| 11 | + # It compares a secret value sent in a configurable HTTP header against the expected |
| 12 | + # secret value. This is a common (though less secure than HMAC) authentication pattern |
| 13 | + # used by various webhook providers. |
| 14 | + # |
| 15 | + # @example Basic configuration |
| 16 | + # request_validator: |
| 17 | + # type: shared_secret |
| 18 | + # secret_env_key: WEBHOOK_SECRET |
| 19 | + # header: Authorization |
| 20 | + # |
| 21 | + # @example Custom header configuration |
| 22 | + # request_validator: |
| 23 | + # type: shared_secret |
| 24 | + # secret_env_key: SOME_OTHER_WEBHOOK_SECRET |
| 25 | + # header: X-API-Key |
| 26 | + # |
| 27 | + # @note This validator performs direct string comparison of the shared secret. |
| 28 | + # While simpler than HMAC, it provides less security since the secret is |
| 29 | + # transmitted directly in the request header. |
| 30 | + class SharedSecret < Base |
| 31 | + # Default configuration values for shared secret validation |
| 32 | + # |
| 33 | + # @return [Hash<Symbol, String>] Default configuration settings |
| 34 | + DEFAULT_CONFIG = { |
| 35 | + header: "Authorization" |
| 36 | + }.freeze |
| 37 | + |
| 38 | + # Validate shared secret from webhook requests |
| 39 | + # |
| 40 | + # Performs secure comparison of the shared secret value from the configured |
| 41 | + # header against the expected secret. Uses secure comparison to prevent |
| 42 | + # timing attacks. |
| 43 | + # |
| 44 | + # @param payload [String] Raw request body (unused but required by interface) |
| 45 | + # @param headers [Hash<String, String>] HTTP headers from the request |
| 46 | + # @param secret [String] Expected secret value for comparison |
| 47 | + # @param config [Hash] Endpoint configuration containing validator settings |
| 48 | + # @option config [Hash] :request_validator Validator-specific configuration |
| 49 | + # @option config [String] :header ('Authorization') Header containing the secret |
| 50 | + # @return [Boolean] true if secret is valid, false otherwise |
| 51 | + # @raise [StandardError] Rescued internally, returns false on any error |
| 52 | + # @note This method is designed to be safe and will never raise exceptions |
| 53 | + # @note Uses Rack::Utils.secure_compare to prevent timing attacks |
| 54 | + # @example Basic validation |
| 55 | + # SharedSecret.valid?( |
| 56 | + # payload: request_body, |
| 57 | + # headers: request.headers, |
| 58 | + # secret: ENV['WEBHOOK_SECRET'], |
| 59 | + # config: { request_validator: { header: 'Authorization' } } |
| 60 | + # ) |
| 61 | + def self.valid?(payload:, headers:, secret:, config:) |
| 62 | + return false if secret.nil? || secret.empty? |
| 63 | + |
| 64 | + validator_config = build_config(config) |
| 65 | + |
| 66 | + # Security: Check raw headers BEFORE normalization to detect tampering |
| 67 | + return false unless headers.respond_to?(:each) |
| 68 | + |
| 69 | + secret_header = validator_config[:header] |
| 70 | + |
| 71 | + # Find the secret header with case-insensitive matching but preserve original value |
| 72 | + raw_secret = nil |
| 73 | + headers.each do |key, value| |
| 74 | + if key.to_s.downcase == secret_header.downcase |
| 75 | + raw_secret = value.to_s |
| 76 | + break |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + return false if raw_secret.nil? || raw_secret.empty? |
| 81 | + |
| 82 | + stripped_secret = raw_secret.strip |
| 83 | + |
| 84 | + # Security: Reject secrets with leading/trailing whitespace |
| 85 | + return false if raw_secret != stripped_secret |
| 86 | + |
| 87 | + # Security: Reject secrets containing null bytes or other control characters |
| 88 | + return false if raw_secret.match?(/[\u0000-\u001f\u007f-\u009f]/) |
| 89 | + |
| 90 | + # Use secure comparison to prevent timing attacks |
| 91 | + Rack::Utils.secure_compare(secret, stripped_secret) |
| 92 | + rescue StandardError => _e |
| 93 | + # Log error in production - for now just return false |
| 94 | + false |
| 95 | + end |
| 96 | + |
| 97 | + private |
| 98 | + |
| 99 | + # Build final configuration by merging defaults with provided config |
| 100 | + # |
| 101 | + # Combines default configuration values with user-provided settings, |
| 102 | + # ensuring all required configuration keys are present with sensible defaults. |
| 103 | + # |
| 104 | + # @param config [Hash] Raw endpoint configuration |
| 105 | + # @return [Hash<Symbol, Object>] Merged configuration with defaults applied |
| 106 | + # @note Missing configuration values are filled with DEFAULT_CONFIG values |
| 107 | + # @api private |
| 108 | + def self.build_config(config) |
| 109 | + validator_config = config.dig(:request_validator) || {} |
| 110 | + |
| 111 | + DEFAULT_CONFIG.merge({ |
| 112 | + header: validator_config[:header] || DEFAULT_CONFIG[:header] |
| 113 | + }) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments