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.
@@ -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, :check_box,
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
- encoded_data = Base64.strict_encode64 Marshal.dump(@signed_attributes)
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
- hash = {}
67
- array = []
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
- add_signed_fields hash
89
+ fields = []
90
+ key = nested_attributes_association?(record_name) ? "#{record_name}_attributes" : record_name
91
+ add_signed_fields key => fields
76
92
 
77
- content = super
78
- array.uniq!
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[:html][:method]
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
- data, signature = @params['form_signature'].split('--', 2)
21
- hmac = SignedForm::HMAC.new secret_key: SignedForm.secret_key
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
 
@@ -1,4 +1,4 @@
1
- require "openssl"
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 secret_key.nil? || secret_key.empty?
11
- if defined?(::Rails) and ::Rails.application.respond_to?(:secrets)
12
- self.secret_key = ::Rails.application.secrets.secret_key_base
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 secret_key.nil? || secret_key.empty?
17
- raise Errors::NoSecretKey, "Please consult the README for instructions on creating a secret key"
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("C*")
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
@@ -1,6 +1,6 @@
1
1
  module SignedForm
2
2
  MAJOR = 0
3
- MINOR = 5
3
+ MINOR = 6
4
4
  PATCH = 0
5
5
  PRE = nil
6
6
 
data/lib/signed_form.rb CHANGED
@@ -1,15 +1,19 @@
1
- require "action_view"
2
- require "action_controller"
3
-
4
- require "signed_form/version"
5
- require "signed_form/errors"
6
- require "signed_form/form_builder"
7
- require "signed_form/hmac"
8
- require "signed_form/digest_stores"
9
- require "signed_form/digestor"
10
- require "signed_form/action_view/form_helper"
11
- require "signed_form/gate_keeper"
12
- require "signed_form/action_controller/permit_signed_params"
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", "~> 2.13"
24
- spec.add_development_dependency "activemodel", ">= 3.1"
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", ">= 3.1"
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 = '>= 1.9'
29
+ spec.required_ruby_version = '>= 3.3'
31
30
  end
@@ -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