Skip to content

Commit 9ba0646

Browse files
committed
hmac improvements
1 parent dc156bf commit 9ba0646

File tree

1 file changed

+13
-3
lines changed
  • lib/hooks/plugins/request_validator

1 file changed

+13
-3
lines changed

lib/hooks/plugins/request_validator/hmac.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ def self.valid?(payload:, headers:, secret:, config:)
131131
def self.build_config(config)
132132
validator_config = config.dig(:request_validator) || {}
133133

134+
algorithm = validator_config[:algorithm] || DEFAULT_CONFIG[:algorithm]
135+
tolerance = validator_config[:timestamp_tolerance] || DEFAULT_CONFIG[:timestamp_tolerance]
136+
134137
DEFAULT_CONFIG.merge({
135138
header: validator_config[:header] || "X-Signature",
136139
timestamp_header: validator_config[:timestamp_header],
137-
timestamp_tolerance: validator_config[:timestamp_tolerance] || DEFAULT_CONFIG[:timestamp_tolerance],
138-
algorithm: validator_config[:algorithm] || DEFAULT_CONFIG[:algorithm],
140+
timestamp_tolerance: tolerance,
141+
algorithm: algorithm,
139142
format: validator_config[:format] || DEFAULT_CONFIG[:format],
140143
version_prefix: validator_config[:version_prefix] || DEFAULT_CONFIG[:version_prefix],
141144
payload_template: validator_config[:payload_template]
@@ -167,12 +170,19 @@ def self.normalize_headers(headers)
167170
# @note Tolerance is applied as absolute difference (past or future)
168171
# @api private
169172
def self.valid_timestamp?(headers, config)
170-
timestamp_header = config[:timestamp_header].downcase
173+
timestamp_header = config[:timestamp_header]
174+
return false unless timestamp_header
175+
176+
timestamp_header = timestamp_header.downcase
171177
timestamp_value = headers[timestamp_header]
172178

173179
return false unless timestamp_value
174180

175181
timestamp = timestamp_value.to_i
182+
183+
# Ensure timestamp is a valid integer
184+
return false unless timestamp.is_a?(Integer) && timestamp > 0
185+
176186
current_time = Time.now.to_i
177187
tolerance = config[:timestamp_tolerance]
178188

0 commit comments

Comments
 (0)