-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add "shared secret" request_validator #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
007683a
Initial plan for issue
Copilot b9285cb
Initial plan for shared secret request validator implementation
Copilot d906144
Implement shared secret request validator with comprehensive tests
Copilot b3c1523
Update lib/hooks/plugins/request_validator/shared_secret.rb
GrantBirki dbb23c9
Update .bundle/config
GrantBirki 473809f
delete vendor bundle from copilot
GrantBirki 92a6dcf
add shared_secret
GrantBirki eb634bb
add okta acceptance test case using shared secrets
GrantBirki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rack/utils" | ||
| require_relative "base" | ||
|
|
||
| module Hooks | ||
| module Plugins | ||
| module RequestValidator | ||
| # Generic shared secret validator for webhooks | ||
| # | ||
| # This validator provides simple shared secret authentication for webhook requests. | ||
| # It compares a secret value sent in a configurable HTTP header against the expected | ||
| # secret value. This is a common (though less secure than HMAC) authentication pattern | ||
| # used by various webhook providers. | ||
| # | ||
| # @example Basic configuration | ||
| # request_validator: | ||
| # type: SharedSecret | ||
| # header: Authorization | ||
| # | ||
| # @example Custom header configuration | ||
| # request_validator: | ||
| # type: SharedSecret | ||
| # header: X-API-Key | ||
| # | ||
| # @note This validator performs direct string comparison of the shared secret. | ||
| # While simpler than HMAC, it provides less security since the secret is | ||
| # transmitted directly in the request header. | ||
| class SharedSecret < Base | ||
| # Default configuration values for shared secret validation | ||
| # | ||
| # @return [Hash<Symbol, String>] Default configuration settings | ||
| DEFAULT_CONFIG = { | ||
| header: "Authorization" | ||
| }.freeze | ||
|
|
||
| # Validate shared secret from webhook requests | ||
| # | ||
| # Performs secure comparison of the shared secret value from the configured | ||
| # header against the expected secret. Uses secure comparison to prevent | ||
| # timing attacks. | ||
| # | ||
| # @param payload [String] Raw request body (unused but required by interface) | ||
| # @param headers [Hash<String, String>] HTTP headers from the request | ||
| # @param secret [String] Expected secret value for comparison | ||
| # @param config [Hash] Endpoint configuration containing validator settings | ||
| # @option config [Hash] :request_validator Validator-specific configuration | ||
| # @option config [String] :header ('Authorization') Header containing the secret | ||
| # @return [Boolean] true if secret is valid, false otherwise | ||
| # @raise [StandardError] Rescued internally, returns false on any error | ||
| # @note This method is designed to be safe and will never raise exceptions | ||
| # @note Uses Rack::Utils.secure_compare to prevent timing attacks | ||
| # @example Basic validation | ||
| # SharedSecret.valid?( | ||
| # payload: request_body, | ||
| # headers: request.headers, | ||
| # secret: ENV['WEBHOOK_SECRET'], | ||
| # config: { request_validator: { header: 'Authorization' } } | ||
| # ) | ||
| def self.valid?(payload:, headers:, secret:, config:) | ||
| return false if secret.nil? || secret.empty? | ||
|
|
||
| validator_config = build_config(config) | ||
|
|
||
| # Security: Check raw headers BEFORE normalization to detect tampering | ||
| return false unless headers.respond_to?(:each) | ||
|
|
||
| secret_header = validator_config[:header] | ||
|
|
||
| # Find the secret header with case-insensitive matching but preserve original value | ||
| raw_secret = nil | ||
| headers.each do |key, value| | ||
| if key.to_s.downcase == secret_header.downcase | ||
| raw_secret = value.to_s | ||
| break | ||
| end | ||
| end | ||
|
|
||
| return false if raw_secret.nil? || raw_secret.empty? | ||
|
|
||
| # Security: Reject secrets with leading/trailing whitespace | ||
| return false if raw_secret != raw_secret.strip | ||
|
|
||
| # Security: Reject secrets containing null bytes or other control characters | ||
| return false if raw_secret.match?(/[\u0000-\u001f\u007f-\u009f]/) | ||
|
|
||
| # Use secure comparison to prevent timing attacks | ||
| Rack::Utils.secure_compare(secret, raw_secret.strip) | ||
GrantBirki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rescue StandardError => _e | ||
| # Log error in production - for now just return false | ||
| false | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Build final configuration by merging defaults with provided config | ||
| # | ||
| # Combines default configuration values with user-provided settings, | ||
| # ensuring all required configuration keys are present with sensible defaults. | ||
| # | ||
| # @param config [Hash] Raw endpoint configuration | ||
| # @return [Hash<Symbol, Object>] Merged configuration with defaults applied | ||
| # @note Missing configuration values are filled with DEFAULT_CONFIG values | ||
| # @api private | ||
| def self.build_config(config) | ||
| validator_config = config.dig(:request_validator) || {} | ||
|
|
||
| DEFAULT_CONFIG.merge({ | ||
| header: validator_config[:header] || DEFAULT_CONFIG[:header] | ||
| }) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.