2017-07-15 04:41:49 +10:00
# frozen_string_literal: true
# Implemented according to HTTP signatures (Draft 6)
# <https://tools.ietf.org/html/draft-cavage-http-signatures-06>
module SignatureVerification
extend ActiveSupport :: Concern
2019-07-09 11:27:35 +10:00
include DomainControlHelper
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
EXPIRATION_WINDOW_LIMIT = 12 . hours
CLOCK_SKEW_MARGIN = 1 . hour
class SignatureVerificationError < StandardError ; end
2022-09-22 06:45:57 +10:00
def require_account_signature!
2023-01-19 02:47:56 +11:00
render json : signature_verification_failure_reason , status : signature_verification_failure_code unless signed_request_account
2019-07-12 04:11:09 +10:00
end
2022-09-22 06:45:57 +10:00
def require_actor_signature!
2023-01-19 02:47:56 +11:00
render json : signature_verification_failure_reason , status : signature_verification_failure_code unless signed_request_actor
2022-09-22 06:45:57 +10:00
end
2017-07-15 04:41:49 +10:00
def signed_request?
request . headers [ 'Signature' ] . present?
end
2017-10-04 08:21:19 +11:00
def signature_verification_failure_reason
2019-07-12 04:11:09 +10:00
@signature_verification_failure_reason
end
def signature_verification_failure_code
@signature_verification_failure_code || 401
2017-10-04 08:21:19 +11:00
end
2019-08-19 02:03:56 +10:00
def signature_key_id
signature_params [ 'keyId' ]
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
rescue SignatureVerificationError
nil
2019-08-19 02:03:56 +10:00
end
2017-07-15 04:41:49 +10:00
def signed_request_account
2022-09-22 06:45:57 +10:00
signed_request_actor . is_a? ( Account ) ? signed_request_actor : nil
end
def signed_request_actor
return @signed_request_actor if defined? ( @signed_request_actor )
2017-07-15 04:41:49 +10:00
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
raise SignatureVerificationError , 'Request not signed' unless signed_request?
raise SignatureVerificationError , 'Incompatible request signature. keyId and signature are required' if missing_required_signature_parameters?
raise SignatureVerificationError , 'Unsupported signature algorithm (only rsa-sha256 and hs2019 are supported)' unless %w( rsa-sha256 hs2019 ) . include? ( signature_algorithm )
raise SignatureVerificationError , 'Signed request date outside acceptable time window' unless matches_time_window?
2017-07-15 04:41:49 +10:00
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
verify_signature_strength!
2020-11-02 09:38:31 +11:00
verify_body_digest!
2017-07-15 04:41:49 +10:00
2022-09-22 06:45:57 +10:00
actor = actor_from_key_id ( signature_params [ 'keyId' ] )
2017-07-15 04:41:49 +10:00
2022-09-22 06:45:57 +10:00
raise SignatureVerificationError , " Public key not found for key #{ signature_params [ 'keyId' ] } " if actor . nil?
2017-07-15 04:41:49 +10:00
signature = Base64 . decode64 ( signature_params [ 'signature' ] )
2024-01-03 22:29:26 +11:00
compare_signed_string = build_signed_string ( include_query_string : true )
2017-07-15 04:41:49 +10:00
2022-09-22 06:45:57 +10:00
return actor unless verify_signature ( actor , signature , compare_signed_string ) . nil?
2017-09-29 01:50:14 +10:00
2024-01-03 22:29:26 +11:00
# Compatibility quirk with older Mastodon versions
compare_signed_string = build_signed_string ( include_query_string : false )
return actor unless verify_signature ( actor , signature , compare_signed_string ) . nil?
2024-04-03 02:47:40 +11:00
actor = stoplight_wrapper . run { actor_refresh_key! ( actor ) }
2019-01-08 07:45:13 +11:00
2023-01-19 02:47:56 +11:00
raise SignatureVerificationError , " Could not refresh public key #{ signature_params [ 'keyId' ] } " if actor . nil?
2019-01-08 07:45:13 +11:00
2024-01-03 22:29:26 +11:00
compare_signed_string = build_signed_string ( include_query_string : true )
return actor unless verify_signature ( actor , signature , compare_signed_string ) . nil?
# Compatibility quirk with older Mastodon versions
compare_signed_string = build_signed_string ( include_query_string : false )
2022-09-22 06:45:57 +10:00
return actor unless verify_signature ( actor , signature , compare_signed_string ) . nil?
2019-01-08 07:45:13 +11:00
2023-01-19 02:47:56 +11:00
fail_with! " Verification failed for #{ actor . to_log_human_identifier } #{ actor . uri } using rsa-sha256 (RSASSA-PKCS1-v1_5 with SHA-256) " , signed_string : compare_signed_string , signature : signature_params [ 'signature' ]
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
rescue SignatureVerificationError = > e
2022-09-21 07:30:26 +10:00
fail_with! e . message
rescue HTTP :: Error , OpenSSL :: SSL :: SSLError = > e
fail_with! " Failed to fetch remote data: #{ e . message } "
2022-09-21 22:48:35 +10:00
rescue Mastodon :: UnexpectedResponseError
2022-09-21 07:30:26 +10:00
fail_with! 'Failed to fetch remote data (got unexpected reply from server)'
rescue Stoplight :: Error :: RedLight
fail_with! 'Fetching attempt skipped because of recent connection failure'
2017-07-15 04:41:49 +10:00
end
2017-08-10 07:54:14 +10:00
def request_body
@request_body || = request . raw_post
end
2017-07-15 04:41:49 +10:00
private
2023-01-19 02:47:56 +11:00
def fail_with! ( message , ** options )
2023-09-06 20:17:22 +10:00
Rails . logger . debug { " Signature verification failed: #{ message } " }
2023-08-29 18:29:07 +10:00
2023-01-19 02:47:56 +11:00
@signature_verification_failure_reason = { error : message } . merge ( options )
2022-09-22 06:45:57 +10:00
@signed_request_actor = nil
2022-09-21 07:30:26 +10:00
end
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
def signature_params
2024-02-08 00:35:37 +11:00
@signature_params || = SignatureParser . parse ( request . headers [ 'Signature' ] )
rescue SignatureParser :: ParsingError
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
raise SignatureVerificationError , 'Error parsing signature parameters'
end
def signature_algorithm
signature_params . fetch ( 'algorithm' , 'hs2019' )
end
def signed_headers
2023-03-16 12:34:00 +11:00
signature_params . fetch ( 'headers' , signature_algorithm == 'hs2019' ? '(created)' : 'date' ) . downcase . split
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
end
def verify_signature_strength!
raise SignatureVerificationError , 'Mastodon requires the Date header or (created) pseudo-header to be signed' unless signed_headers . include? ( 'date' ) || signed_headers . include? ( '(created)' )
raise SignatureVerificationError , 'Mastodon requires the Digest header or (request-target) pseudo-header to be signed' unless signed_headers . include? ( Request :: REQUEST_TARGET ) || signed_headers . include? ( 'digest' )
2020-11-02 09:38:31 +11:00
raise SignatureVerificationError , 'Mastodon requires the Host header to be signed when doing a GET request' if request . get? && ! signed_headers . include? ( 'host' )
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
raise SignatureVerificationError , 'Mastodon requires the Digest header to be signed when doing a POST request' if request . post? && ! signed_headers . include? ( 'digest' )
end
2020-11-02 09:38:31 +11:00
def verify_body_digest!
return unless signed_headers . include? ( 'digest' )
2021-02-27 03:40:27 +11:00
raise SignatureVerificationError , 'Digest header missing' unless request . headers . key? ( 'Digest' )
2020-11-02 09:38:31 +11:00
digests = request . headers [ 'Digest' ] . split ( ',' ) . map { | digest | digest . split ( '=' , 2 ) } . map { | key , value | [ key . downcase , value ] }
sha256 = digests . assoc ( 'sha-256' )
raise SignatureVerificationError , " Mastodon only supports SHA-256 in Digest header. Offered algorithms: #{ digests . map ( & :first ) . join ( ', ' ) } " if sha256 . nil?
2022-09-21 07:30:26 +10:00
return if body_digest == sha256 [ 1 ]
digest_size = begin
Base64 . strict_decode64 ( sha256 [ 1 ] . strip ) . length
rescue ArgumentError
raise SignatureVerificationError , " Invalid Digest value. The provided Digest value is not a valid base64 string. Given digest: #{ sha256 [ 1 ] } "
end
raise SignatureVerificationError , " Invalid Digest value. The provided Digest value is not a SHA-256 digest. Given digest: #{ sha256 [ 1 ] } " if digest_size != 32
2023-02-20 16:58:28 +11:00
2022-09-21 07:30:26 +10:00
raise SignatureVerificationError , " Invalid Digest value. Computed SHA-256 digest: #{ body_digest } ; given: #{ sha256 [ 1 ] } "
2020-11-02 09:38:31 +11:00
end
2022-09-22 06:45:57 +10:00
def verify_signature ( actor , signature , compare_signed_string )
if actor . keypair . public_key . verify ( OpenSSL :: Digest . new ( 'SHA256' ) , signature , compare_signed_string )
@signed_request_actor = actor
@signed_request_actor
2019-01-08 07:45:13 +11:00
end
rescue OpenSSL :: PKey :: RSAError
nil
end
2024-01-03 22:29:26 +11:00
def build_signed_string ( include_query_string : true )
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
signed_headers . map do | signed_header |
2023-05-04 13:51:18 +10:00
case signed_header
when Request :: REQUEST_TARGET
2024-01-03 22:29:26 +11:00
if include_query_string
" #{ Request :: REQUEST_TARGET } : #{ request . method . downcase } #{ request . original_fullpath } "
else
# Current versions of Mastodon incorrectly omit the query string from the (request-target) pseudo-header.
# Therefore, temporarily support such incorrect signatures for compatibility.
# TODO: remove eventually some time after release of the fixed version
" #{ Request :: REQUEST_TARGET } : #{ request . method . downcase } #{ request . path } "
end
2023-05-04 13:51:18 +10:00
when '(created)'
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
raise SignatureVerificationError , 'Invalid pseudo-header (created) for rsa-sha256' unless signature_algorithm == 'hs2019'
raise SignatureVerificationError , 'Pseudo-header (created) used but corresponding argument missing' if signature_params [ 'created' ] . blank?
" (created): #{ signature_params [ 'created' ] } "
2023-05-04 13:51:18 +10:00
when '(expires)'
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
raise SignatureVerificationError , 'Invalid pseudo-header (expires) for rsa-sha256' unless signature_algorithm == 'hs2019'
raise SignatureVerificationError , 'Pseudo-header (expires) used but corresponding argument missing' if signature_params [ 'expires' ] . blank?
" (expires): #{ signature_params [ 'expires' ] } "
2017-07-15 04:41:49 +10:00
else
" #{ signed_header } : #{ request . headers [ to_header_name ( signed_header ) ] } "
end
end . join ( " \n " )
end
2018-10-12 09:15:55 +11:00
def matches_time_window?
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
created_time = nil
expires_time = nil
2018-10-12 09:15:55 +11:00
begin
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
if signature_algorithm == 'hs2019' && signature_params [ 'created' ] . present?
created_time = Time . at ( signature_params [ 'created' ] . to_i ) . utc
elsif request . headers [ 'Date' ] . present?
created_time = Time . httpdate ( request . headers [ 'Date' ] ) . utc
end
expires_time = Time . at ( signature_params [ 'expires' ] . to_i ) . utc if signature_params [ 'expires' ] . present?
2023-01-19 02:47:56 +11:00
rescue ArgumentError = > e
raise SignatureVerificationError , " Invalid Date header: #{ e . message } "
2018-10-12 09:15:55 +11:00
end
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
expires_time || = created_time + 5 . minutes unless created_time . nil?
expires_time = [ expires_time , created_time + EXPIRATION_WINDOW_LIMIT ] . min unless created_time . nil?
return false if created_time . present? && created_time > Time . now . utc + CLOCK_SKEW_MARGIN
return false if expires_time . present? && Time . now . utc > expires_time + CLOCK_SKEW_MARGIN
true
2018-10-12 09:15:55 +11:00
end
2017-08-10 07:54:14 +10:00
def body_digest
2020-11-02 09:38:31 +11:00
@body_digest || = Digest :: SHA256 . base64digest ( request_body )
2017-08-10 07:54:14 +10:00
end
2017-07-15 04:41:49 +10:00
def to_header_name ( name )
2023-02-08 12:25:20 +11:00
name . split ( '-' ) . map ( & :capitalize ) . join ( '-' )
2017-07-15 04:41:49 +10:00
end
Add support for latest HTTP Signatures spec draft (#14556)
* Add support for latest HTTP Signatures spec draft
https://www.ietf.org/id/draft-ietf-httpbis-message-signatures-00.html
- add support for the “hs2019” signature algorithm (assumed to be equivalent
to RSA-SHA256, since we do not have a mechanism to specify the algorithm
within the key metadata yet)
- add support for (created) and (expires) pseudo-headers and related
signature parameters, when using the hs2019 signature algorithm
- adjust default “headers” parameter while being backwards-compatible with
previous implementation
- change the acceptable time window logic from 12 hours surrounding the “date”
header to accepting signatures created up to 1 hour in the future and
expiring up to 1 hour in the past (but only allowing expiration dates up to
12 hours after the creation date)
This doesn't conform with the current draft, as it doesn't permit accounting
for clock skew.
This, however, should be addressed in a next version of the draft:
https://github.com/httpwg/http-extensions/pull/1235
* Add additional signature requirements
* Rewrite signature params parsing using Parslet
* Make apparent which signature algorithm Mastodon on verification failure
Mastodon uses RSASSA-PKCS1-v1_5, which is not recommended for new applications,
and new implementers may thus unknowingly use RSASSA-PSS.
* Add workaround for PeerTube's invalid signature header
The previous parser allowed incorrect Signature headers, such as
those produced by old versions of the `http-signature` node.js package,
and seemingly used by PeerTube.
This commit adds a workaround for that.
* Fix `signature_key_id` raising an exception
Previously, parsing failures would result in `signature_key_id` being nil,
but the parser changes made that result in an exception.
This commit changes the `signature_key_id` method to return `nil` in case
of parsing failures.
* Move extra HTTP signature helper methods to private methods
* Relax (request-target) requirement to (request-target) || digest
This lets requests from Plume work without lowering security significantly.
2020-08-25 02:21:07 +10:00
def missing_required_signature_parameters?
signature_params [ 'keyId' ] . blank? || signature_params [ 'signature' ] . blank?
2017-08-10 07:54:14 +10:00
end
2022-09-22 06:45:57 +10:00
def actor_from_key_id ( key_id )
2019-07-12 04:11:09 +10:00
domain = key_id . start_with? ( 'acct:' ) ? key_id . split ( '@' ) . last : key_id
if domain_not_allowed? ( domain )
@signature_verification_failure_code = 403
return
end
2017-08-10 07:54:14 +10:00
if key_id . start_with? ( 'acct:' )
2024-04-03 02:47:40 +11:00
stoplight_wrapper . run { ResolveAccountService . new . call ( key_id . delete_prefix ( 'acct:' ) , suppress_errors : false ) }
2017-08-10 07:54:14 +10:00
elsif ! ActivityPub :: TagManager . instance . local_uri? ( key_id )
2022-09-22 06:45:57 +10:00
account = ActivityPub :: TagManager . instance . uri_to_actor ( key_id )
2024-04-03 02:47:40 +11:00
account || = stoplight_wrapper . run { ActivityPub :: FetchRemoteKeyService . new . call ( key_id , suppress_errors : false ) }
2017-08-22 06:57:34 +10:00
account
2017-08-10 07:54:14 +10:00
end
2022-09-21 07:30:26 +10:00
rescue Mastodon :: PrivateNetworkAddressError = > e
raise SignatureVerificationError , " Requests to private network addresses are disallowed (tried to query #{ e . host } ) "
2022-09-22 06:45:57 +10:00
rescue Mastodon :: HostValidationError , ActivityPub :: FetchRemoteActorService :: Error , ActivityPub :: FetchRemoteKeyService :: Error , Webfinger :: Error = > e
2022-09-21 07:30:26 +10:00
raise SignatureVerificationError , e . message
2017-07-15 04:41:49 +10:00
end
2019-01-08 07:45:13 +11:00
2024-04-03 02:47:40 +11:00
def stoplight_wrapper
Stoplight ( " source: #{ request . remote_ip } " )
2019-05-23 23:22:39 +10:00
. with_threshold ( 1 )
. with_cool_off_time ( 5 . minutes . seconds )
2019-08-19 02:03:56 +10:00
. with_error_handler { | error , handle | error . is_a? ( HTTP :: Error ) || error . is_a? ( OpenSSL :: SSL :: SSLError ) ? handle . call ( error ) : raise ( error ) }
2019-05-23 23:22:39 +10:00
end
2022-09-22 06:45:57 +10:00
def actor_refresh_key! ( actor )
return if actor . local? || ! actor . activitypub?
return actor . refresh! if actor . respond_to? ( :refresh! ) && actor . possibly_stale?
ActivityPub :: FetchRemoteActorService . new . call ( actor . uri , only_key : true , suppress_errors : false )
2022-09-21 07:30:26 +10:00
rescue Mastodon :: PrivateNetworkAddressError = > e
raise SignatureVerificationError , " Requests to private network addresses are disallowed (tried to query #{ e . host } ) "
2022-09-22 06:45:57 +10:00
rescue Mastodon :: HostValidationError , ActivityPub :: FetchRemoteActorService :: Error , Webfinger :: Error = > e
2022-09-21 07:30:26 +10:00
raise SignatureVerificationError , e . message
2019-01-08 07:45:13 +11:00
end
2017-07-15 04:41:49 +10:00
end