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
data/spec/form_builder_spec.rb
CHANGED
|
@@ -2,9 +2,14 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
class User
|
|
4
4
|
extend ActiveModel::Naming
|
|
5
|
+
include ActiveModel::Conversion
|
|
5
6
|
|
|
6
7
|
attr_accessor :name, :options, :widgets_attributes
|
|
7
8
|
|
|
9
|
+
def persisted?
|
|
10
|
+
false
|
|
11
|
+
end
|
|
12
|
+
|
|
8
13
|
def to_key
|
|
9
14
|
[1]
|
|
10
15
|
end
|
|
@@ -36,7 +41,11 @@ class ControllerRenderer < AbstractController::Base
|
|
|
36
41
|
end
|
|
37
42
|
end
|
|
38
43
|
|
|
39
|
-
class MockBuilder < ActionView::Helpers::FormBuilder
|
|
44
|
+
class MockBuilder < ActionView::Helpers::FormBuilder
|
|
45
|
+
def name_input
|
|
46
|
+
text_field :name
|
|
47
|
+
end
|
|
48
|
+
end
|
|
40
49
|
|
|
41
50
|
describe SignedForm::FormBuilder do
|
|
42
51
|
include SignedFormViewHelper
|
|
@@ -59,6 +68,7 @@ describe SignedForm::FormBuilder do
|
|
|
59
68
|
'</form>'
|
|
60
69
|
|
|
61
70
|
content.should =~ Regexp.new(regex, Regexp::MULTILINE)
|
|
71
|
+
content.scan('name="form_signature"').size.should == 1
|
|
62
72
|
end
|
|
63
73
|
|
|
64
74
|
it "should not sign if no option is present" do
|
|
@@ -90,6 +100,53 @@ describe SignedForm::FormBuilder do
|
|
|
90
100
|
end
|
|
91
101
|
end
|
|
92
102
|
|
|
103
|
+
describe "form_with tag", if: ActionView::Helpers::FormHelper.method_defined?(:form_with) do
|
|
104
|
+
it "should build a form with one signature" do
|
|
105
|
+
content = form_with(model: User.new, url: '/users', signed: true) do |f|
|
|
106
|
+
f.text_field :name
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
data = get_data_from_form(content)
|
|
110
|
+
data['user'].should include(:name)
|
|
111
|
+
content.scan('name="form_signature"').size.should == 1
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it "should not sign if no option is present" do
|
|
115
|
+
content = form_with(model: User.new, url: '/users') do |f|
|
|
116
|
+
f.text_field :name
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
content.should_not include('form_signature')
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "should honor the global default and an explicit opt-out" do
|
|
123
|
+
SignedForm.options[:signed] = true
|
|
124
|
+
|
|
125
|
+
signed_content = form_with(model: User.new, url: '/users') { |f| f.text_field :name }
|
|
126
|
+
unsigned_content = form_with(model: User.new, url: '/users', signed: false) { |f| f.text_field :name }
|
|
127
|
+
|
|
128
|
+
signed_content.should include('form_signature')
|
|
129
|
+
unsigned_content.should_not include('form_signature')
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
it "signs the current path including its query when the action is omitted", if: rails_version_satisfies?('>= 7') do
|
|
133
|
+
allow(self).to receive(:request).and_return(double(fullpath: '/users?active=1'))
|
|
134
|
+
content = form_with(scope: :user, url: false, signed: true) { |f| f.text_field :name }
|
|
135
|
+
|
|
136
|
+
expect(content).not_to match(/<form[^>]* action=/)
|
|
137
|
+
expect(get_data_from_form(content)[:_options_][:url]).to eq('/users?active=1')
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it "should sign the resolved destination" do
|
|
141
|
+
content = form_with(model: User.new, url: '/users', signed: true, sign_destination: true) do |f|
|
|
142
|
+
f.text_field :name
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
data = get_data_from_form(content)
|
|
146
|
+
data[:_options_][:method].should == :post
|
|
147
|
+
data[:_options_][:url].should == '/users'
|
|
148
|
+
end
|
|
149
|
+
end
|
|
93
150
|
describe "third party builders" do
|
|
94
151
|
it "should build a signed form" do
|
|
95
152
|
content = form_for(User.new, signed: true, builder: MockBuilder) do |f|
|
|
@@ -100,16 +157,21 @@ describe SignedForm::FormBuilder do
|
|
|
100
157
|
end
|
|
101
158
|
|
|
102
159
|
it "should raise if a builder isn't supported" do
|
|
103
|
-
expect { form_for(User.new, signed: true, builder: Class.new) {} }.to raise_error
|
|
160
|
+
expect { form_for(User.new, signed: true, builder: Class.new) {} }.to raise_error(
|
|
161
|
+
RuntimeError,
|
|
162
|
+
"Form signing not supported on builders that don't subclass ActionView::Helpers::FormBuilder or include SignedForm::FormBuilder"
|
|
163
|
+
)
|
|
104
164
|
end
|
|
105
165
|
end
|
|
106
166
|
|
|
107
167
|
describe "sign_destination" do
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
168
|
+
def assert_method_and_url(content, method = :post, url = '/users/new')
|
|
169
|
+
data = get_data_from_form(content)
|
|
170
|
+
|
|
171
|
+
expect(data).to include(:_options_)
|
|
172
|
+
expect(data[:_options_]).to include(:method, :url)
|
|
173
|
+
expect(data[:_options_][:method]).to eql(method)
|
|
174
|
+
expect(data[:_options_][:url]).to eql(url)
|
|
113
175
|
end
|
|
114
176
|
|
|
115
177
|
it "should set a target" do
|
|
@@ -117,7 +179,7 @@ describe SignedForm::FormBuilder do
|
|
|
117
179
|
f.text_field :name
|
|
118
180
|
end
|
|
119
181
|
|
|
120
|
-
|
|
182
|
+
assert_method_and_url(content)
|
|
121
183
|
end
|
|
122
184
|
|
|
123
185
|
it "should set a target when the default options are enabled" do
|
|
@@ -127,7 +189,84 @@ describe SignedForm::FormBuilder do
|
|
|
127
189
|
f.text_field :name
|
|
128
190
|
end
|
|
129
191
|
|
|
130
|
-
|
|
192
|
+
assert_method_and_url(content)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
context "method" do
|
|
196
|
+
it "should read method from options -> html" do
|
|
197
|
+
SignedForm.options[:sign_destination] = true
|
|
198
|
+
|
|
199
|
+
content = form_for(User.new, signed: true, html: { method: :get }) do |f|
|
|
200
|
+
f.text_field :name
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
assert_method_and_url(content, :get)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
it "should read method from options" do
|
|
207
|
+
SignedForm.options[:sign_destination] = true
|
|
208
|
+
|
|
209
|
+
content = form_for(User.new, signed: true, method: :get) do |f|
|
|
210
|
+
f.text_field :name
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
assert_method_and_url(content, :get)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it "should infer method from object persisted state", if: rails_version_satisfies?('>= 7.0') do
|
|
217
|
+
SignedForm.options[:sign_destination] = true
|
|
218
|
+
|
|
219
|
+
user = User.new
|
|
220
|
+
allow(user).to receive(:persisted?).and_return(true)
|
|
221
|
+
|
|
222
|
+
content = form_for(user, signed: true) do |f|
|
|
223
|
+
f.text_field :name
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
assert_method_and_url(content, :patch, '/users/1')
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
context "url" do
|
|
231
|
+
it "should read url from options" do
|
|
232
|
+
SignedForm.options[:sign_destination] = true
|
|
233
|
+
|
|
234
|
+
content = form_for(User.new, signed: true, url: '/other/url') do |f|
|
|
235
|
+
f.text_field :name
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
assert_method_and_url(content, :post, '/other/url')
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
it "should work when model passed", if: rails_version_satisfies?('>= 7.0') do
|
|
242
|
+
SignedForm.options[:sign_destination] = true
|
|
243
|
+
|
|
244
|
+
content = form_for(User.new, signed: true) do |f|
|
|
245
|
+
f.text_field :name
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
assert_method_and_url(content, :post, '/users/new')
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
it "should read format from options", if: rails_version_satisfies?('>= 7.0') do
|
|
252
|
+
SignedForm.options[:sign_destination] = true
|
|
253
|
+
|
|
254
|
+
content = form_for(User.new, signed: true, format: 'bmp') do |f|
|
|
255
|
+
f.text_field :name
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
assert_method_and_url(content, :post, '/users/new.bmp')
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
it "should work when url hash is passed", if: rails_version_satisfies?('>= 7.0') do
|
|
262
|
+
SignedForm.options[:sign_destination] = true
|
|
263
|
+
|
|
264
|
+
content = form_for(User.new, signed: true, url: { action: "create" }) do |f|
|
|
265
|
+
f.text_field :name
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
assert_method_and_url(content, :post, '/some/path?action=create')
|
|
269
|
+
end
|
|
131
270
|
end
|
|
132
271
|
end
|
|
133
272
|
|
|
@@ -137,7 +276,7 @@ describe SignedForm::FormBuilder do
|
|
|
137
276
|
@data['user'].should include({:options=>[]})
|
|
138
277
|
end
|
|
139
278
|
|
|
140
|
-
it "should add to the allowed attributes when collection_check_boxes is used",
|
|
279
|
+
it "should add to the allowed attributes when collection_check_boxes is used", if: rails_version_satisfies?('> 4') do
|
|
141
280
|
content = form_for(User.new, signed: true) do |f|
|
|
142
281
|
f.collection_check_boxes :options, ['a', 'b'], :to_s, :to_s
|
|
143
282
|
end
|
|
@@ -145,6 +284,14 @@ describe SignedForm::FormBuilder do
|
|
|
145
284
|
@data = get_data_from_form(content)
|
|
146
285
|
end
|
|
147
286
|
|
|
287
|
+
it "should add to the allowed attributes when collection_checkboxes is used", if: rails_version_satisfies?('>= 8') do
|
|
288
|
+
content = form_for(User.new, signed: true) do |f|
|
|
289
|
+
f.collection_checkboxes :options, ['a', 'b'], :to_s, :to_s
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
@data = get_data_from_form(content)
|
|
293
|
+
end
|
|
294
|
+
|
|
148
295
|
it 'should pass a given block to the input helper method' do
|
|
149
296
|
content = form_for(User.new, signed: true) do |f|
|
|
150
297
|
f.collection_check_boxes :options, ['a'], :to_s, :to_s, {}, {} do |b|
|
|
@@ -158,18 +305,48 @@ describe SignedForm::FormBuilder do
|
|
|
158
305
|
end
|
|
159
306
|
|
|
160
307
|
describe "form inputs" do
|
|
161
|
-
fields
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
308
|
+
fields = ActionView::Helpers::FormBuilder.instance_methods - Object.instance_methods - %i(
|
|
309
|
+
button
|
|
310
|
+
collection_check_boxes
|
|
311
|
+
collection_checkboxes
|
|
312
|
+
collection_radio_buttons
|
|
313
|
+
collection_select
|
|
314
|
+
convert_to_model
|
|
315
|
+
date_select
|
|
316
|
+
datalist
|
|
317
|
+
datetime_select
|
|
318
|
+
emitted_hidden_id?
|
|
319
|
+
field_helpers
|
|
320
|
+
field_helpers=
|
|
321
|
+
field_helpers?
|
|
322
|
+
field_id
|
|
323
|
+
field_name
|
|
324
|
+
fields
|
|
325
|
+
fields_for
|
|
326
|
+
grouped_collection_select
|
|
327
|
+
id
|
|
328
|
+
index
|
|
329
|
+
label
|
|
330
|
+
model_name_from_record_or_class
|
|
331
|
+
multipart
|
|
332
|
+
multipart=
|
|
333
|
+
multipart?
|
|
334
|
+
object
|
|
335
|
+
object=
|
|
336
|
+
object_name
|
|
337
|
+
object_name=
|
|
338
|
+
options
|
|
339
|
+
options=
|
|
340
|
+
parent_builder
|
|
341
|
+
radio_button
|
|
342
|
+
search_field
|
|
343
|
+
select
|
|
344
|
+
submit
|
|
345
|
+
time_select
|
|
346
|
+
time_zone_select
|
|
347
|
+
to_model
|
|
348
|
+
to_partial_path
|
|
349
|
+
)
|
|
173
350
|
|
|
174
351
|
after do
|
|
175
352
|
@data['user'].size.should == 1
|
|
@@ -214,7 +391,7 @@ describe SignedForm::FormBuilder do
|
|
|
214
391
|
@data = get_data_from_form(content)
|
|
215
392
|
end
|
|
216
393
|
|
|
217
|
-
it "should add to the allowed attributes when collection_radio_buttons is used",
|
|
394
|
+
it "should add to the allowed attributes when collection_radio_buttons is used", if: rails_version_satisfies?('> 4') do
|
|
218
395
|
content = form_for(User.new, signed: true) do |f|
|
|
219
396
|
f.collection_radio_buttons :name, %w(a b), :to_s, :to_s
|
|
220
397
|
end
|
|
@@ -264,6 +441,14 @@ describe SignedForm::FormBuilder do
|
|
|
264
441
|
end
|
|
265
442
|
|
|
266
443
|
describe "disabled form inputs" do
|
|
444
|
+
it "skips a disabled multiple checkbox with explicit values" do
|
|
445
|
+
content = form_for(user, signed: true) do |f|
|
|
446
|
+
f.check_box :options, { multiple: true, disabled: true }, 'a', nil
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
get_data_from_form(content)['user'].should be_empty
|
|
450
|
+
end
|
|
451
|
+
|
|
267
452
|
it "should be explicitly signed" do
|
|
268
453
|
content = form_for(User.new, signed: true) do |f|
|
|
269
454
|
f.text_field :name, disabled: true
|
|
@@ -281,7 +466,19 @@ describe SignedForm::FormBuilder do
|
|
|
281
466
|
@data['user'].should include({:name => []})
|
|
282
467
|
end
|
|
283
468
|
|
|
284
|
-
it "
|
|
469
|
+
it "permits the selected values from repeated multiple checkboxes" do
|
|
470
|
+
content = form_for(user, signed: true) do |f|
|
|
471
|
+
f.check_box(:name, { multiple: true }, 'a', nil) +
|
|
472
|
+
f.check_box(:name, { multiple: true }, 'b', nil)
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
content.should include('name="user[name][]"')
|
|
476
|
+
@data = get_data_from_form(content)
|
|
477
|
+
submitted = ActionController::Parameters.new(name: %w(a b))
|
|
478
|
+
submitted.permit(*@data['user'])[:name].should == %w(a b)
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
it "should add a hash with an empty array when collection_check_boxes is used", if: rails_version_satisfies?('> 4') do
|
|
285
482
|
content = form_for(User.new, signed: true) do |f|
|
|
286
483
|
f.collection_check_boxes :name, ['a', 'b'], :to_s, :to_s
|
|
287
484
|
end
|
|
@@ -305,7 +502,7 @@ describe SignedForm::FormBuilder do
|
|
|
305
502
|
@data['user'].should_not include({:name => []})
|
|
306
503
|
end
|
|
307
504
|
|
|
308
|
-
it "shouldn't add a hash with an empty array when collection_radio_buttons is used",
|
|
505
|
+
it "shouldn't add a hash with an empty array when collection_radio_buttons is used", if: rails_version_satisfies?('> 4') do
|
|
309
506
|
content = form_for(User.new, signed: true) do |f|
|
|
310
507
|
f.collection_radio_buttons :name, ['a', 'b'], :to_s, :to_s
|
|
311
508
|
end
|
|
@@ -336,7 +533,7 @@ describe SignedForm::FormBuilder do
|
|
|
336
533
|
|
|
337
534
|
describe "fields_for" do
|
|
338
535
|
it "should nest attributes" do
|
|
339
|
-
user.
|
|
536
|
+
allow(user).to receive(:widgets).and_return([widget])
|
|
340
537
|
|
|
341
538
|
content = form_for(user, signed: true) do |f|
|
|
342
539
|
f.fields_for :widgets do |ff|
|
|
@@ -348,6 +545,27 @@ describe SignedForm::FormBuilder do
|
|
|
348
545
|
data['user'].should include("widgets_attributes" => [:name])
|
|
349
546
|
end
|
|
350
547
|
|
|
548
|
+
it "signs fields rendered by an explicit nested builder with a record" do
|
|
549
|
+
content = form_for(user, signed: true) do |f|
|
|
550
|
+
f.fields_for :widgets, [widget], builder: MockBuilder do |ff|
|
|
551
|
+
ff.name_input
|
|
552
|
+
end
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
get_data_from_form(content)['user'].should == [{ 'widgets_attributes' => [:name] }]
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
it "passes the signing context when nested options are the second argument" do
|
|
559
|
+
allow(user).to receive(:widgets).and_return([widget])
|
|
560
|
+
content = form_for(user, signed: true, builder: MockBuilder) do |f|
|
|
561
|
+
f.fields_for :widgets, builder: f.class do |ff|
|
|
562
|
+
ff.name_input
|
|
563
|
+
end
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
get_data_from_form(content)['user'].should == [{ 'widgets_attributes' => [:name] }]
|
|
567
|
+
end
|
|
568
|
+
|
|
351
569
|
it "should deeply nest attributes" do
|
|
352
570
|
content = form_for(:author, url: '/', signed: true) do |f|
|
|
353
571
|
f.fields_for :books do |ff|
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module SignedForm
|
|
6
|
+
RSpec.describe GateKeeper do
|
|
7
|
+
before { SignedForm.secret_key = 'hunter2' }
|
|
8
|
+
|
|
9
|
+
let(:allowed_attributes) { { 'user' => [:name] } }
|
|
10
|
+
let(:signed_options) { {} }
|
|
11
|
+
let(:token_attributes) do
|
|
12
|
+
allowed_attributes.merge(signed_options.empty? ? {} : { _options_: signed_options })
|
|
13
|
+
end
|
|
14
|
+
let(:token) { SignedForm.tokenize(token_attributes) }
|
|
15
|
+
let(:request_method) { 'POST' }
|
|
16
|
+
let(:request_fullpath) { '/posts/1/comments/2' }
|
|
17
|
+
let(:request_url) { "http://www.example.com#{request_fullpath}" }
|
|
18
|
+
let(:request) do
|
|
19
|
+
double(
|
|
20
|
+
'Request',
|
|
21
|
+
fullpath: request_fullpath,
|
|
22
|
+
url: request_url,
|
|
23
|
+
request_method: request_method
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
let(:controller) do
|
|
27
|
+
double('Controller', params: { 'form_signature' => token }, request: request).tap do |instance|
|
|
28
|
+
allow(instance).to receive(:url_for) { |value| value }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
subject(:gate_keeper) { described_class.new(controller) }
|
|
33
|
+
|
|
34
|
+
it 'returns the authenticated allowlist' do
|
|
35
|
+
expect(gate_keeper.allowed_attributes).to eq('user' => [:name])
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'removes signed transport options from the attribute allowlist' do
|
|
39
|
+
signed_options.merge!(method: :post, url: request_fullpath)
|
|
40
|
+
|
|
41
|
+
expect(gate_keeper.allowed_attributes).to eq('user' => [:name])
|
|
42
|
+
expect(gate_keeper.options).to eq(method: :post, url: request_fullpath)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'accepts a signed destination that matches the request fullpath' do
|
|
46
|
+
signed_options.merge!(method: :post, url: request_fullpath)
|
|
47
|
+
|
|
48
|
+
expect { gate_keeper }.not_to raise_error
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'accepts a signed absolute destination that matches request.url' do
|
|
52
|
+
signed_options.merge!(method: :post, url: request_url)
|
|
53
|
+
|
|
54
|
+
expect { gate_keeper }.not_to raise_error
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'compares the signed request method case-insensitively' do
|
|
58
|
+
signed_options.merge!(method: 'post', url: request_fullpath)
|
|
59
|
+
|
|
60
|
+
expect { gate_keeper }.not_to raise_error
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'ignores a fragment when verifying the signed destination' do
|
|
64
|
+
signed_options.merge!(method: :post, url: "#{request_url}#return-here")
|
|
65
|
+
|
|
66
|
+
expect { gate_keeper }.not_to raise_error
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'rejects a request sent to a different path' do
|
|
70
|
+
signed_options.merge!(method: :post, url: '/admin')
|
|
71
|
+
|
|
72
|
+
expect { gate_keeper }.to raise_error(SignedForm::Errors::InvalidURL)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it 'rejects a request whose query string differs from the signed destination' do
|
|
76
|
+
signed_options.merge!(method: :post, url: "#{request_fullpath}?admin=1")
|
|
77
|
+
|
|
78
|
+
expect { gate_keeper }.to raise_error(SignedForm::Errors::InvalidURL)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'rejects a request made with a different HTTP method' do
|
|
82
|
+
signed_options.merge!(method: :patch, url: request_fullpath)
|
|
83
|
+
|
|
84
|
+
expect { gate_keeper }.to raise_error(SignedForm::Errors::InvalidURL)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'rejects an allowlist changed after it was signed' do
|
|
88
|
+
data, signature = token.split('--', 2)
|
|
89
|
+
attributes = Marshal.load(Base64.strict_decode64(data))
|
|
90
|
+
attributes['user'] << :admin
|
|
91
|
+
tampered_data = Base64.strict_encode64(Marshal.dump(attributes))
|
|
92
|
+
allow(controller).to receive(:params).and_return(
|
|
93
|
+
'form_signature' => "#{tampered_data}--#{signature}"
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
expect { described_class.new(controller) }
|
|
97
|
+
.to raise_error(SignedForm::Errors::InvalidSignature)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it 'rejects a changed signature' do
|
|
101
|
+
data, signature = token.split('--', 2)
|
|
102
|
+
tampered_signature = signature.dup
|
|
103
|
+
tampered_signature[0] = signature[0] == '0' ? '1' : '0'
|
|
104
|
+
allow(controller).to receive(:params).and_return(
|
|
105
|
+
'form_signature' => "#{data}--#{tampered_signature}"
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
expect { described_class.new(controller) }
|
|
109
|
+
.to raise_error(SignedForm::Errors::InvalidSignature)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it 'rejects a malformed token instead of accepting an empty signature' do
|
|
113
|
+
allow(controller).to receive(:params).and_return('form_signature' => 'not-a-token')
|
|
114
|
+
|
|
115
|
+
expect { described_class.new(controller) }
|
|
116
|
+
.to raise_error(SignedForm::Errors::InvalidSignature)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
data/spec/hmac_spec.rb
CHANGED
|
@@ -1,24 +1,75 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe SignedForm::HMAC do
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
describe 'key selection' do
|
|
5
|
+
it 'raises when neither an explicit key nor a Rails key exists' do
|
|
6
|
+
allow(Rails).to receive(:application).and_return(nil)
|
|
7
|
+
|
|
8
|
+
expect { described_class.new }.to raise_error(SignedForm::Errors::NoSecretKey)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'uses Rails.application.secret_key_base when no key is supplied' do
|
|
12
|
+
application = double('Rails application', secret_key_base: 'rails-secret-key')
|
|
13
|
+
allow(Rails).to receive(:application).and_return(application)
|
|
14
|
+
|
|
15
|
+
expect(described_class.new.secret_key).to eq('rails-secret-key')
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'falls back to Rails.application.secret_key_base for an empty explicit key' do
|
|
19
|
+
application = double('Rails application', secret_key_base: 'rails-secret-key')
|
|
20
|
+
allow(Rails).to receive(:application).and_return(application)
|
|
21
|
+
|
|
22
|
+
expect(described_class.new(secret_key: '').secret_key).to eq('rails-secret-key')
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'prefers an explicit key without consulting the Rails application' do
|
|
26
|
+
expect(Rails).not_to receive(:application)
|
|
27
|
+
|
|
28
|
+
expect(described_class.new(secret_key: 'explicit-key').secret_key).to eq('explicit-key')
|
|
29
|
+
end
|
|
6
30
|
end
|
|
7
31
|
|
|
8
32
|
describe 'create' do
|
|
9
|
-
let(:hmac) {
|
|
33
|
+
let(:hmac) { described_class.new(secret_key: 'superdupersecret') }
|
|
34
|
+
|
|
35
|
+
it 'creates the stable SHA1 HMAC expected by existing signed forms' do
|
|
36
|
+
expect(hmac.create('my signed message')).to eq('93c1ecd4c10122cbf873ca6cf9eff08888565054')
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'returns a 40-character hexadecimal signature' do
|
|
40
|
+
expect(hmac.create('my signed message')).to match(/\A[0-9a-f]{40}\z/)
|
|
41
|
+
end
|
|
10
42
|
|
|
11
|
-
it '
|
|
12
|
-
|
|
13
|
-
|
|
43
|
+
it 'binds the signature to the secret key' do
|
|
44
|
+
other_hmac = described_class.new(secret_key: 'different-secret')
|
|
45
|
+
|
|
46
|
+
expect(other_hmac.create('my signed message')).not_to eq(hmac.create('my signed message'))
|
|
14
47
|
end
|
|
15
48
|
end
|
|
16
49
|
|
|
17
50
|
describe 'verify' do
|
|
18
|
-
let(:hmac) {
|
|
19
|
-
let(:
|
|
51
|
+
let(:hmac) { described_class.new(secret_key: 'superdupersecret') }
|
|
52
|
+
let(:data) { 'My super secret' }
|
|
53
|
+
let(:signature) { hmac.create(data) }
|
|
54
|
+
|
|
55
|
+
it 'accepts the exact signature for the exact data' do
|
|
56
|
+
expect(hmac.verify(signature, data)).to be(true)
|
|
57
|
+
end
|
|
20
58
|
|
|
21
|
-
|
|
22
|
-
|
|
59
|
+
it 'rejects the right signature for different data' do
|
|
60
|
+
expect(hmac.verify(signature, 'My bad secret')).to be(false)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'rejects a same-length signature with one changed byte' do
|
|
64
|
+
tampered_signature = signature.dup
|
|
65
|
+
tampered_signature[0] = signature[0] == '0' ? '1' : '0'
|
|
66
|
+
|
|
67
|
+
expect(hmac.verify(tampered_signature, data)).to be(false)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'rejects truncated and empty signatures without raising' do
|
|
71
|
+
expect(hmac.verify(signature[0...-1], data)).to be(false)
|
|
72
|
+
expect(hmac.verify('', data)).to be(false)
|
|
73
|
+
end
|
|
23
74
|
end
|
|
24
75
|
end
|
|
@@ -23,16 +23,18 @@ describe SignedForm::ActionController::PermitSignedParams do
|
|
|
23
23
|
"#{encoded_data}--#{signature}"
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
Object.send(:remove_const, :Rails) if defined?(Rails)
|
|
27
26
|
|
|
28
27
|
before do
|
|
28
|
+
hide_const("Rails")
|
|
29
29
|
SignedForm.secret_key = "abc123"
|
|
30
30
|
|
|
31
|
-
Controller.
|
|
32
|
-
|
|
31
|
+
allow_any_instance_of(Controller).to receive_messages(
|
|
32
|
+
request: double('request', method: 'POST', request_method: 'POST', fullpath: '/users', url: '/users', variant: nil),
|
|
33
|
+
params: ActionController::Parameters.new("user" => { name: "Erich Menge", occupation: 'developer' })
|
|
34
|
+
)
|
|
33
35
|
|
|
34
|
-
params.
|
|
35
|
-
params.
|
|
36
|
+
allow(params).to receive(:[]).and_call_original
|
|
37
|
+
allow(params).to receive(:[]).with('user').and_return(params)
|
|
36
38
|
end
|
|
37
39
|
|
|
38
40
|
it "should raise if signature isn't valid" do
|
|
@@ -41,7 +43,7 @@ describe SignedForm::ActionController::PermitSignedParams do
|
|
|
41
43
|
end
|
|
42
44
|
|
|
43
45
|
context "when the parameters are good" do
|
|
44
|
-
before { params.
|
|
46
|
+
before { expect(params).to receive(:permit).with(:name).and_return(params) }
|
|
45
47
|
|
|
46
48
|
it "should permit attributes that are allowed" do
|
|
47
49
|
params['form_signature'] = marshal_and_sign "user" => [:name]
|
|
@@ -51,7 +53,7 @@ describe SignedForm::ActionController::PermitSignedParams do
|
|
|
51
53
|
it "should verify current url matches targeted url" do
|
|
52
54
|
params['form_signature'] = marshal_and_sign("user" => [:name], :_options_ => { method: 'post', url: '/users' })
|
|
53
55
|
|
|
54
|
-
controller.request.
|
|
56
|
+
expect(controller.request).to receive(:fullpath).and_return '/users'
|
|
55
57
|
controller.permit_signed_form_data
|
|
56
58
|
end
|
|
57
59
|
end
|
|
@@ -67,7 +69,7 @@ describe SignedForm::ActionController::PermitSignedParams do
|
|
|
67
69
|
end
|
|
68
70
|
|
|
69
71
|
context "when the digest is bad" do
|
|
70
|
-
before { digestor.
|
|
72
|
+
before { allow(digestor).to receive(:to_s).and_return "bad" }
|
|
71
73
|
|
|
72
74
|
it "should not reject if inside grace period" do
|
|
73
75
|
params['form_signature'] = marshal_and_sign("user" => [:name], :_options_ => { digest: digestor, digest_expiration: Time.now + 20 })
|