signed_form 0.5.0 → 0.6.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 +4 -4
- data/.github/workflows/tests.yml +56 -0
- data/.gitignore +0 -1
- data/Changes.md +60 -34
- data/Gemfile +10 -5
- data/README.md +81 -79
- data/Rakefile +4 -0
- data/lib/signed_form/action_view/form_helper.rb +44 -17
- data/lib/signed_form/digestor.rb +1 -1
- data/lib/signed_form/form_builder.rb +45 -18
- data/lib/signed_form/gate_keeper.rb +5 -9
- data/lib/signed_form/hmac.rb +16 -8
- data/lib/signed_form/signed_data.rb +51 -0
- data/lib/signed_form/version.rb +1 -1
- data/lib/signed_form.rb +20 -12
- data/signed_form.gemspec +6 -7
- data/spec/digestor_spec.rb +0 -7
- data/spec/form_builder_spec.rb +244 -26
- data/spec/gate_keeper_spec.rb +119 -0
- data/spec/hmac_spec.rb +61 -10
- data/spec/permit_signed_params_spec.rb +10 -8
- data/spec/signed_data_spec.rb +66 -0
- data/spec/signed_form_contract_spec.rb +229 -0
- data/spec/spec_helper.rb +38 -26
- metadata +25 -36
- data/.travis.yml +0 -20
|
@@ -3,9 +3,10 @@ module SignedForm
|
|
|
3
3
|
FIELDS_TO_SIGN = [{:select => :multiple_select?}, {:collection_select => :multiple_select?},
|
|
4
4
|
{:grouped_collection_select => :multiple_select?},
|
|
5
5
|
:time_zone_select, :collection_radio_buttons, {:collection_check_boxes => []},
|
|
6
|
+
{:collection_checkboxes => []}, :weekday_select,
|
|
6
7
|
:date_select, :datetime_select, :time_select,
|
|
7
8
|
:text_field, :password_field, :hidden_field,
|
|
8
|
-
:file_field, :text_area, :
|
|
9
|
+
:file_field, :text_area, :textarea,
|
|
9
10
|
:radio_button, :color_field,
|
|
10
11
|
:telephone_field, :phone_field, :date_field,
|
|
11
12
|
:time_field, :datetime_field, :datetime_local_field,
|
|
@@ -31,12 +32,34 @@ module SignedForm
|
|
|
31
32
|
end
|
|
32
33
|
end
|
|
33
34
|
|
|
35
|
+
def check_box(field, options = {}, *values, &block)
|
|
36
|
+
html_options = options.stringify_keys
|
|
37
|
+
unless html_options['disabled']
|
|
38
|
+
if html_options['multiple']
|
|
39
|
+
add_signed_fields field => []
|
|
40
|
+
else
|
|
41
|
+
add_signed_fields field
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
super
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
alias_method :checkbox, :check_box if ::ActionView::Helpers::FormBuilder.method_defined?(:checkbox)
|
|
48
|
+
|
|
34
49
|
BUILDERS = Hash.new do |h,k|
|
|
35
50
|
h[k] = Class.new(k) do
|
|
36
51
|
include FormBuilder
|
|
37
52
|
end
|
|
38
53
|
end
|
|
39
54
|
|
|
55
|
+
def self.signed_builder(builder)
|
|
56
|
+
return builder if builder.ancestors.include?(FormBuilder)
|
|
57
|
+
unless builder <= ::ActionView::Helpers::FormBuilder
|
|
58
|
+
raise "Form signing not supported on builders that don't subclass ActionView::Helpers::FormBuilder or include SignedForm::FormBuilder"
|
|
59
|
+
end
|
|
60
|
+
BUILDERS[builder]
|
|
61
|
+
end
|
|
62
|
+
|
|
40
63
|
def initialize(*)
|
|
41
64
|
super
|
|
42
65
|
if options[:signed_attributes_context]
|
|
@@ -51,11 +74,7 @@ module SignedForm
|
|
|
51
74
|
def form_signature_tag
|
|
52
75
|
@signed_attributes.each { |k,v| v.uniq! if v.is_a?(Array) }
|
|
53
76
|
recursive_merge_identical_hashes! @signed_attributes
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
hmac = SignedForm::HMAC.new(secret_key: SignedForm.secret_key)
|
|
57
|
-
signature = hmac.create(encoded_data)
|
|
58
|
-
token = "#{encoded_data}--#{signature}"
|
|
77
|
+
token = SignedForm.tokenize(@signed_attributes)
|
|
59
78
|
%(<input type="hidden" name="form_signature" value="#{token}" />\n).html_safe
|
|
60
79
|
end
|
|
61
80
|
|
|
@@ -63,19 +82,19 @@ module SignedForm
|
|
|
63
82
|
#
|
|
64
83
|
# @see http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for
|
|
65
84
|
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if nested_attributes_association?(record_name)
|
|
70
|
-
hash["#{record_name}_attributes"] = fields_options[:signed_attributes_context] = array
|
|
71
|
-
else
|
|
72
|
-
hash[record_name] = fields_options[:signed_attributes_context] = array
|
|
85
|
+
if record_object.is_a?(Hash) && record_object.extractable_options?
|
|
86
|
+
fields_options, record_object = record_object, nil
|
|
73
87
|
end
|
|
74
88
|
|
|
75
|
-
|
|
89
|
+
fields = []
|
|
90
|
+
key = nested_attributes_association?(record_name) ? "#{record_name}_attributes" : record_name
|
|
91
|
+
add_signed_fields key => fields
|
|
76
92
|
|
|
77
|
-
|
|
78
|
-
|
|
93
|
+
builder = fields_options[:builder] || options[:builder] || self.class
|
|
94
|
+
fields_options = fields_options.merge(:builder => FormBuilder.signed_builder(builder),
|
|
95
|
+
:signed_attributes_context => fields)
|
|
96
|
+
content = super(record_name, record_object, fields_options, &block)
|
|
97
|
+
fields.uniq!
|
|
79
98
|
content
|
|
80
99
|
end
|
|
81
100
|
|
|
@@ -93,12 +112,20 @@ module SignedForm
|
|
|
93
112
|
|
|
94
113
|
private
|
|
95
114
|
|
|
115
|
+
def determine_html_method
|
|
116
|
+
if object.respond_to?(:persisted?) && object.persisted?
|
|
117
|
+
:patch
|
|
118
|
+
else
|
|
119
|
+
:post
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
96
123
|
def prepare_signed_attributes_hash
|
|
97
124
|
@signed_attributes[:_options_] = {}
|
|
98
125
|
|
|
99
126
|
if options[:sign_destination]
|
|
100
|
-
@signed_attributes[:_options_][:method] = options
|
|
101
|
-
@signed_attributes[:_options_][:url] = options[:url]
|
|
127
|
+
@signed_attributes[:_options_][:method] = options.dig(:html, :method) || options[:method] || determine_html_method
|
|
128
|
+
@signed_attributes[:_options_][:url] = options[:signed_form_url] || options[:url]
|
|
102
129
|
end
|
|
103
130
|
|
|
104
131
|
if options[:digest]
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SignedForm
|
|
2
4
|
class GateKeeper
|
|
3
5
|
attr_reader :allowed_attributes
|
|
@@ -17,21 +19,15 @@ module SignedForm
|
|
|
17
19
|
end
|
|
18
20
|
|
|
19
21
|
def extract_and_verify_form_signature
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
signature ||= ''
|
|
24
|
-
|
|
25
|
-
raise Errors::InvalidSignature, "Form signature is not valid" unless hmac.verify signature, data
|
|
26
|
-
|
|
27
|
-
@allowed_attributes = Marshal.load Base64.strict_decode64(data)
|
|
22
|
+
signed_data = SignedForm::SignedData.new(secret_key: SignedForm.secret_key)
|
|
23
|
+
@allowed_attributes = signed_data.verify(@params['form_signature'])
|
|
28
24
|
@options = allowed_attributes.delete(:_options_)
|
|
29
25
|
end
|
|
30
26
|
|
|
31
27
|
def verify_destination
|
|
32
28
|
return unless options[:method] && options[:url]
|
|
33
29
|
raise Errors::InvalidURL if options[:method].to_s.casecmp(@request.request_method) != 0
|
|
34
|
-
url = @controller.url_for(options[:url])
|
|
30
|
+
url = @controller.url_for(options[:url]).split('#').first
|
|
35
31
|
raise Errors::InvalidURL if url != @request.fullpath && url != @request.url
|
|
36
32
|
end
|
|
37
33
|
|
data/lib/signed_form/hmac.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'openssl'
|
|
2
2
|
|
|
3
3
|
module SignedForm
|
|
4
4
|
class HMAC
|
|
@@ -7,14 +7,18 @@ module SignedForm
|
|
|
7
7
|
def initialize(options = {})
|
|
8
8
|
self.secret_key = options[:secret_key]
|
|
9
9
|
|
|
10
|
-
if
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
if missing_secret_key? && defined?(::Rails) && ::Rails.respond_to?(:application)
|
|
11
|
+
application = ::Rails.application
|
|
12
|
+
if application && application.respond_to?(:secret_key_base)
|
|
13
|
+
self.secret_key = application.secret_key_base
|
|
14
|
+
end
|
|
15
|
+
if missing_secret_key? && application && application.respond_to?(:secrets)
|
|
16
|
+
self.secret_key = application.secrets.secret_key_base
|
|
13
17
|
end
|
|
14
18
|
end
|
|
15
19
|
|
|
16
|
-
if
|
|
17
|
-
raise Errors::NoSecretKey,
|
|
20
|
+
if missing_secret_key?
|
|
21
|
+
raise Errors::NoSecretKey, 'Please consult the README for instructions on creating a secret key'
|
|
18
22
|
end
|
|
19
23
|
end
|
|
20
24
|
|
|
@@ -28,14 +32,18 @@ module SignedForm
|
|
|
28
32
|
|
|
29
33
|
private
|
|
30
34
|
|
|
35
|
+
def missing_secret_key?
|
|
36
|
+
secret_key.nil? || secret_key.empty?
|
|
37
|
+
end
|
|
38
|
+
|
|
31
39
|
# After the Rack implementation
|
|
32
40
|
def secure_compare(a, b)
|
|
33
41
|
return false unless a.bytesize == b.bytesize
|
|
34
42
|
|
|
35
|
-
l = a.unpack(
|
|
43
|
+
l = a.unpack('C*')
|
|
36
44
|
|
|
37
45
|
r, i = 0, -1
|
|
38
|
-
b.each_byte { |v| r |= v ^ l[i+=1] }
|
|
46
|
+
b.each_byte { |v| r |= v ^ l[i += 1] }
|
|
39
47
|
r == 0
|
|
40
48
|
end
|
|
41
49
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'signed_form/errors'
|
|
5
|
+
require 'signed_form/hmac'
|
|
6
|
+
|
|
7
|
+
module SignedForm
|
|
8
|
+
# The authenticated-data boundary used by signed forms.
|
|
9
|
+
#
|
|
10
|
+
# This intentionally preserves SignedForm's original wire format so forms
|
|
11
|
+
# rendered by older releases remain valid while signing is separated from
|
|
12
|
+
# Rails-specific form and controller concerns.
|
|
13
|
+
class SignedData
|
|
14
|
+
SEPARATOR = '--'.freeze
|
|
15
|
+
INVALID_SIGNATURE = 'Form signature is not valid'.freeze
|
|
16
|
+
|
|
17
|
+
def initialize(secret_key:)
|
|
18
|
+
@hmac = HMAC.new(secret_key: secret_key)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def sign(value)
|
|
22
|
+
data = Base64.strict_encode64(Marshal.dump(value))
|
|
23
|
+
signature = @hmac.create(data)
|
|
24
|
+
|
|
25
|
+
"#{data}#{SEPARATOR}#{signature}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def verify(token)
|
|
29
|
+
data, signature = split(token)
|
|
30
|
+
|
|
31
|
+
invalid_signature! unless @hmac.verify(signature, data)
|
|
32
|
+
|
|
33
|
+
Marshal.load(Base64.strict_decode64(data))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def split(token)
|
|
39
|
+
invalid_signature! unless token.is_a?(String)
|
|
40
|
+
|
|
41
|
+
data, signature = token.split(SEPARATOR, 2)
|
|
42
|
+
invalid_signature! if data.nil? || data.empty? || signature.nil? || signature.empty?
|
|
43
|
+
|
|
44
|
+
[data, signature]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def invalid_signature!
|
|
48
|
+
raise Errors::InvalidSignature, INVALID_SIGNATURE
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/signed_form/version.rb
CHANGED
data/lib/signed_form.rb
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
|
|
7
|
-
require
|
|
8
|
-
require
|
|
9
|
-
require
|
|
10
|
-
require
|
|
11
|
-
require
|
|
12
|
-
require
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'action_view'
|
|
5
|
+
require 'action_controller'
|
|
6
|
+
|
|
7
|
+
require 'signed_form/version'
|
|
8
|
+
require 'signed_form/errors'
|
|
9
|
+
require 'signed_form/form_builder'
|
|
10
|
+
require 'signed_form/hmac'
|
|
11
|
+
require 'signed_form/signed_data'
|
|
12
|
+
require 'signed_form/digest_stores'
|
|
13
|
+
require 'signed_form/digestor'
|
|
14
|
+
require 'signed_form/action_view/form_helper'
|
|
15
|
+
require 'signed_form/gate_keeper'
|
|
16
|
+
require 'signed_form/action_controller/permit_signed_params'
|
|
13
17
|
|
|
14
18
|
module SignedForm
|
|
15
19
|
DEFAULT_OPTIONS = {
|
|
@@ -35,5 +39,9 @@ module SignedForm
|
|
|
35
39
|
def config
|
|
36
40
|
yield self
|
|
37
41
|
end
|
|
42
|
+
|
|
43
|
+
def tokenize(attributes = {})
|
|
44
|
+
SignedData.new(secret_key: secret_key).sign(attributes)
|
|
45
|
+
end
|
|
38
46
|
end
|
|
39
47
|
end
|
data/signed_form.gemspec
CHANGED
|
@@ -18,14 +18,13 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
21
|
spec.add_development_dependency "rake"
|
|
23
|
-
spec.add_development_dependency "rspec", "~>
|
|
24
|
-
spec.add_development_dependency "activemodel", ">=
|
|
25
|
-
spec.add_development_dependency "coveralls"
|
|
26
|
-
spec.add_development_dependency "byebug"
|
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.13"
|
|
23
|
+
spec.add_development_dependency "activemodel", ">= 4.2"
|
|
27
24
|
|
|
28
|
-
spec.add_dependency "actionpack", ">=
|
|
25
|
+
spec.add_dependency "actionpack", ">= 4.2"
|
|
26
|
+
spec.add_dependency "base64"
|
|
27
|
+
spec.add_dependency "psych", ">= 2.0"
|
|
29
28
|
|
|
30
|
-
spec.required_ruby_version = '>=
|
|
29
|
+
spec.required_ruby_version = '>= 3.3'
|
|
31
30
|
end
|
data/spec/digestor_spec.rb
CHANGED
|
@@ -57,13 +57,6 @@ describe SignedForm::Digestor do
|
|
|
57
57
|
digestor.to_s.should == digestor2.to_s
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
it "should marshal and taint the digest" do
|
|
61
|
-
digestor = SignedForm::Digestor.new(template)
|
|
62
|
-
data = Marshal.dump digestor
|
|
63
|
-
digestor = Marshal.load data
|
|
64
|
-
digestor.to_s.should be_tainted
|
|
65
|
-
end
|
|
66
|
-
|
|
67
60
|
it "should reset the digest if a template is added" do
|
|
68
61
|
digestor = SignedForm::Digestor.new(template)
|
|
69
62
|
first_digest = digestor.to_s
|