serge 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE +23 -0
- data/README.md +39 -0
- data/Rakefile +8 -0
- data/lib/serge/error.rb +3 -0
- data/lib/serge/fedi_signature.rb +32 -0
- data/lib/serge/inbox_delivery/http_legacy_signature.rb +113 -0
- data/lib/serge/inbox_delivery/http_message_signature.rb +85 -0
- data/lib/serge/inbox_delivery.rb +62 -0
- data/lib/serge/signature_verification_error.rb +3 -0
- data/lib/serge/version.rb +5 -0
- data/lib/serge.rb +19 -0
- metadata +107 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7d123b881b2747c9be15783a3acc8f79af45aacf0e9823708c504bdd83877c04
|
|
4
|
+
data.tar.gz: aef408116087ad1eb5427f777766c4ac44880dece234772416455784641ffdc6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 22b4f55ebbb5b38a9eef5fe699ff17421e9bfa56eb208a6dfc0a9f698364c2f17e48a1a68619f11168cdc1c0a7d27ab2bf1fd32da003898b94e62b6a92c7c625
|
|
7
|
+
data.tar.gz: 5a497cf2a756d26ac3d1165d77eaeab1c6385e323daa5751446f909626706c2e48bf41fbb6415f94162f15f9de72d8128a581031cea9ecbefc7729666c0b6c92
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Copyright (c) 2026 Tom Brown
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Serge
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/serge`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/serge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/serge/blob/main/CODE_OF_CONDUCT.md).
|
|
36
|
+
|
|
37
|
+
## Code of Conduct
|
|
38
|
+
|
|
39
|
+
Everyone interacting in the Serge project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/serge/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lib/serge/error.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module Serge
|
|
2
|
+
class FediSignature
|
|
3
|
+
ALGORITHM = 'rsa-sha256'
|
|
4
|
+
|
|
5
|
+
def self.signing_string(header_names)
|
|
6
|
+
header_names.map { |name| "#{name}: #{yield(name)}" }.join("\n")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.sign(signing_string, private_key)
|
|
10
|
+
keypair = OpenSSL::PKey::RSA.new(private_key)
|
|
11
|
+
Base64.strict_encode64(keypair.sign(OpenSSL::Digest::SHA256.new, signing_string))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.build_header(key_id, signature, header_names)
|
|
15
|
+
"keyId=\"#{key_id}\",signature=\"#{signature}\",algorithm=\"#{ALGORITHM}\",headers=\"#{header_names.join(' ')}\""
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.parse_header(signature_header)
|
|
19
|
+
return nil if signature_header.blank?
|
|
20
|
+
|
|
21
|
+
signature_hash = signature_header.split(',').map do |pair|
|
|
22
|
+
pair.split('=', 2).map do |value|
|
|
23
|
+
value.strip.gsub(/\A"/, '').gsub(/"\z/, '') # "foo" -> foo
|
|
24
|
+
end
|
|
25
|
+
end.to_h
|
|
26
|
+
|
|
27
|
+
return nil if signature_hash['signature'].nil? || signature_hash['headers'].nil? || signature_hash['keyId'].nil?
|
|
28
|
+
|
|
29
|
+
signature_hash
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
module Serge
|
|
2
|
+
class InboxDelivery
|
|
3
|
+
class HttpLegacySignature
|
|
4
|
+
def initialize(request)
|
|
5
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
6
|
+
@request = request
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def created_time
|
|
10
|
+
Time.httpdate(@request.headers['Date']).utc if @request.headers['Date'].present?
|
|
11
|
+
rescue ArgumentError => e
|
|
12
|
+
raise Serge::SignatureVerificationError, "Invalid Date header: #{e.message}"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def expires_time
|
|
16
|
+
# just take default
|
|
17
|
+
nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def missing_signature_parameters
|
|
21
|
+
nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def key_id
|
|
25
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
26
|
+
signature_header&.dig('keyId')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def verify_signature_strength!
|
|
30
|
+
signed_headers = signature_header['headers']
|
|
31
|
+
raise Serge::SignatureVerificationError, 'Date header must be signed' unless signed_headers.split.include?('date')
|
|
32
|
+
raise Serge::SignatureVerificationError, 'Digest header must be signed' unless signed_headers.split.include?('digest')
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def verify_body_digest!
|
|
36
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
37
|
+
raise Serge::SignatureVerificationError, 'Digest header missing' unless @request.headers.key?('Digest')
|
|
38
|
+
digests = @request.headers['Digest'].split(',').map { |digest| digest.split('=', 2) }.map { |key, value| [key.downcase, value] }
|
|
39
|
+
sha256 = digests.assoc('sha-256')
|
|
40
|
+
if sha256.nil?
|
|
41
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} Error. offered algorithms: #{digests.map(&:first).join(', ')}"
|
|
42
|
+
raise Serge::SignatureVerificationError, "Only SHA-256 is supported in Digest header."
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
return if body_digest == sha256[1]
|
|
46
|
+
|
|
47
|
+
digest_size = begin
|
|
48
|
+
Base64.strict_decode64(sha256[1].strip).length
|
|
49
|
+
rescue ArgumentError
|
|
50
|
+
raise Serge::SignatureVerificationError, "Invalid Digest value. The provided Digest value is not a valid base64 string. Given digest: #{sha256[1]}"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
if digest_size != 32
|
|
54
|
+
raise Serge::SignatureVerificationError, "Invalid Digest value. The provided Digest value is not a SHA-256 digest. Given digest: #{sha256[1]}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
raise Serge::SignatureVerificationError, "Invalid Digest value. Computed SHA-256 digest: #{body_digest}; given: #{sha256[1]}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def verify(signature, comparison_string, public_key)
|
|
61
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
62
|
+
|
|
63
|
+
key = OpenSSL::PKey::RSA.new(public_key)
|
|
64
|
+
result = key.verify(OpenSSL::Digest::SHA256.new, signature, comparison_string)
|
|
65
|
+
if !result
|
|
66
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} Error verifying signature"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
result
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def verified?(public_key)
|
|
73
|
+
headers = signature_header['headers']
|
|
74
|
+
signature = Base64.decode64(signature_header['signature'])
|
|
75
|
+
|
|
76
|
+
comparison_string = FediSignature.signing_string(headers.split) do |header|
|
|
77
|
+
'(request-target)' == header ? "post #{@request.path}" : @request.headers[capitalized(header)]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
return true if verify(signature, comparison_string, public_key)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def body_digest
|
|
86
|
+
@body_digest ||= Digest::SHA256.base64digest(request_body)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def request_body
|
|
90
|
+
@request_body ||= @request.raw_post
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def capitalized(text)
|
|
94
|
+
text.gsub(/(?:^|-)([a-z])/) { |m| m.upcase }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def signature_header
|
|
98
|
+
return @signature_header if defined?(@signature_header)
|
|
99
|
+
|
|
100
|
+
@signature_header = FediSignature.parse_header(@request.headers['Signature'])
|
|
101
|
+
if @signature_header.nil?
|
|
102
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} Error. malformed Signature header from #{@request.remote_ip}: #{@request.headers['Signature'].inspect}"
|
|
103
|
+
else
|
|
104
|
+
header_values = "algorithm: #{@signature_header['algorithm']}, headers: #{@signature_header['headers']}"
|
|
105
|
+
header_values << ", expires: #{@signature_header['expires']}" if @signature_header['expires'].present?
|
|
106
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} #{header_values}"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
@signature_header
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
require "linzer/rack"
|
|
2
|
+
|
|
3
|
+
module Serge
|
|
4
|
+
class InboxDelivery
|
|
5
|
+
class HttpMessageSignature
|
|
6
|
+
REQUIRED_PARAMETERS = %w(keyid created).freeze
|
|
7
|
+
|
|
8
|
+
def initialize(request)
|
|
9
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
10
|
+
@request = request
|
|
11
|
+
@linzer_signature = Linzer::Signature.build({
|
|
12
|
+
'signature-input' => @request.headers['signature-input'],
|
|
13
|
+
'signature' => @request.headers['signature'],
|
|
14
|
+
})
|
|
15
|
+
rescue Linzer::Error => e
|
|
16
|
+
raise Serge::SignatureVerificationError, e.message
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def created_time
|
|
20
|
+
Time.at(@linzer_signature.parameters['created'].to_i).utc
|
|
21
|
+
rescue ArgumentError => e
|
|
22
|
+
raise Serge::SignatureVerificationError, "Invalid Date header: #{e.message}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def expires_time
|
|
26
|
+
Time.at(@linzer_signature.parameters['expires'].to_i).utc if @linzer_signature.parameters['expires'].present?
|
|
27
|
+
rescue ArgumentError => e
|
|
28
|
+
raise Serge::SignatureVerificationError, "Invalid Date header: #{e.message}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def key_id
|
|
32
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
33
|
+
@linzer_signature.parameters['keyid']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def missing_signature_parameters
|
|
37
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
38
|
+
REQUIRED_PARAMETERS if REQUIRED_PARAMETERS.any? { |p| @linzer_signature.parameters[p].blank? }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def signed_headers
|
|
42
|
+
@signed_headers ||= @linzer_signature.components.reject { |c| c.start_with?('@') }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def verify_signature_strength!
|
|
46
|
+
raise Serge::SignatureVerificationError, 'created parameter must be signed' if @linzer_signature.parameters['created'].blank?
|
|
47
|
+
raise Serge::SignatureVerificationError, 'the @method and @target-uri derived components must be signed' unless @linzer_signature.components.include?('@method') && @linzer_signature.components.include?('@target-uri')
|
|
48
|
+
raise Serge::SignatureVerificationError, 'the Content-Digest header must be signed when doing a POST request' if @request.post? && !signed_headers.include?('content-digest')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def verify_body_digest!
|
|
52
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
53
|
+
return unless signed_headers.include?('content-digest')
|
|
54
|
+
raise Serge::SignatureVerificationError, 'Content-Digest header missing' unless @request.headers.key?('content-digest')
|
|
55
|
+
|
|
56
|
+
digests = Starry.parse_dictionary(@request.headers['content-digest'])
|
|
57
|
+
raise Serge::SignatureVerificationError, "Mastodon only supports SHA-256 in Content-Digest header. Offered algorithms: #{digests.keys.join(', ')}" unless digests.key?('sha-256')
|
|
58
|
+
|
|
59
|
+
received_digest = Base64.strict_encode64(digests['sha-256'].value)
|
|
60
|
+
return if body_digest == received_digest
|
|
61
|
+
|
|
62
|
+
raise Serge::SignatureVerificationError, "Invalid Digest value. Computed SHA-256 digest: #{body_digest}; given: #{received_digest}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def verified?(public_key)
|
|
66
|
+
InboxDelivery.logger.info "#{self.class}##{__method__}"
|
|
67
|
+
key = Linzer.new_rsa_v1_5_sha256_public_key(public_key)
|
|
68
|
+
|
|
69
|
+
Linzer.verify!(Rack::Request.new(@request.env), key:)
|
|
70
|
+
rescue Linzer::VerifyError
|
|
71
|
+
false
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def body_digest
|
|
77
|
+
@body_digest ||= Digest::SHA256.base64digest(request_body)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def request_body
|
|
81
|
+
@request_body ||= @request.raw_post
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require "logger"
|
|
2
|
+
require "serge/inbox_delivery/http_legacy_signature"
|
|
3
|
+
require "serge/inbox_delivery/http_message_signature"
|
|
4
|
+
|
|
5
|
+
module Serge
|
|
6
|
+
class InboxDelivery
|
|
7
|
+
EXPIRATION_WINDOW_LIMIT = 12.hours
|
|
8
|
+
CLOCK_SKEW_MARGIN = 1.hour
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
attr_writer :logger
|
|
12
|
+
|
|
13
|
+
def logger
|
|
14
|
+
@logger ||= Logger.new(IO::NULL)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
attr_reader :signature
|
|
19
|
+
|
|
20
|
+
delegate :key_id, to: :signature
|
|
21
|
+
|
|
22
|
+
def initialize(request)
|
|
23
|
+
if request.headers['signature-input'].present?
|
|
24
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} signature-input: #{request.headers['signature-input']}"
|
|
25
|
+
@signature = HttpMessageSignature.new(request)
|
|
26
|
+
else
|
|
27
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} signature-input not present"
|
|
28
|
+
@signature = HttpLegacySignature.new(request)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def verified?(public_key)
|
|
33
|
+
missing_signature_parameters = @signature.missing_signature_parameters
|
|
34
|
+
raise Serge::SignatureVerificationError, "Incompatible request signature." if missing_signature_parameters
|
|
35
|
+
raise Serge::SignatureVerificationError, 'Signed request date outside acceptable time window' unless matches_time_window?
|
|
36
|
+
|
|
37
|
+
@signature.verify_signature_strength!
|
|
38
|
+
@signature.verify_body_digest!
|
|
39
|
+
@signature.verified?(public_key)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def matches_time_window?
|
|
45
|
+
created_time = @signature.created_time
|
|
46
|
+
expires_time = @signature.expires_time
|
|
47
|
+
|
|
48
|
+
if created_time.nil?
|
|
49
|
+
InboxDelivery.logger.info "#{self.class}##{__method__} created_time is nil"
|
|
50
|
+
return false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
expires_time ||= created_time + 5.minutes
|
|
54
|
+
expires_time = [expires_time, created_time + EXPIRATION_WINDOW_LIMIT].min
|
|
55
|
+
|
|
56
|
+
return false if created_time.present? && created_time > Time.now.utc + CLOCK_SKEW_MARGIN
|
|
57
|
+
return false if expires_time.present? && Time.now.utc > expires_time + CLOCK_SKEW_MARGIN
|
|
58
|
+
|
|
59
|
+
true
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/serge.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "serge/version"
|
|
4
|
+
require "active_support"
|
|
5
|
+
require "active_support/core_ext/integer/time"
|
|
6
|
+
require "active_support/core_ext/object/blank"
|
|
7
|
+
require "active_support/core_ext/module/delegation"
|
|
8
|
+
require "base64"
|
|
9
|
+
require "digest"
|
|
10
|
+
require "openssl"
|
|
11
|
+
require "time"
|
|
12
|
+
|
|
13
|
+
module Serge
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
require 'serge/error.rb'
|
|
17
|
+
require 'serge/signature_verification_error.rb'
|
|
18
|
+
require 'serge/fedi_signature.rb'
|
|
19
|
+
require 'serge/inbox_delivery.rb'
|
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: serge
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tom Brown
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-09-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '7.1'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '9'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '7.1'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '9'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: linzer
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.8'
|
|
40
|
+
type: :runtime
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0.8'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: rack
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '3.2'
|
|
54
|
+
type: :runtime
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '3.2'
|
|
61
|
+
description: Accommodates both http signature formats in use on the fediverse for
|
|
62
|
+
inbox deliveries
|
|
63
|
+
email:
|
|
64
|
+
- herestomwiththeweather@gmail.com
|
|
65
|
+
executables: []
|
|
66
|
+
extensions: []
|
|
67
|
+
extra_rdoc_files: []
|
|
68
|
+
files:
|
|
69
|
+
- ".rspec"
|
|
70
|
+
- CHANGELOG.md
|
|
71
|
+
- CODE_OF_CONDUCT.md
|
|
72
|
+
- LICENSE
|
|
73
|
+
- README.md
|
|
74
|
+
- Rakefile
|
|
75
|
+
- lib/serge.rb
|
|
76
|
+
- lib/serge/error.rb
|
|
77
|
+
- lib/serge/fedi_signature.rb
|
|
78
|
+
- lib/serge/inbox_delivery.rb
|
|
79
|
+
- lib/serge/inbox_delivery/http_legacy_signature.rb
|
|
80
|
+
- lib/serge/inbox_delivery/http_message_signature.rb
|
|
81
|
+
- lib/serge/signature_verification_error.rb
|
|
82
|
+
- lib/serge/version.rb
|
|
83
|
+
homepage: https://github.com/herestomwiththeweather/serge
|
|
84
|
+
licenses:
|
|
85
|
+
- MIT
|
|
86
|
+
metadata:
|
|
87
|
+
homepage_uri: https://github.com/herestomwiththeweather/serge
|
|
88
|
+
post_install_message:
|
|
89
|
+
rdoc_options: []
|
|
90
|
+
require_paths:
|
|
91
|
+
- lib
|
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 3.0.0
|
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
requirements: []
|
|
103
|
+
rubygems_version: 3.5.22
|
|
104
|
+
signing_key:
|
|
105
|
+
specification_version: 4
|
|
106
|
+
summary: Ruby HTTP Signature for Fedi Inbox Deliveries
|
|
107
|
+
test_files: []
|