safemode 2.0.3 → 2.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2feca54490235c1a1dc543fbdb04ac9d3f81495b8f1126cc4d7b411c71fd963
4
- data.tar.gz: 8a2279cf395544183a5947822bf68a1b7cb190caf093544eae667ca79d8423d3
3
+ metadata.gz: 8fcb0610067cef4810bfc86b88ad0c317b4e5f8cdfd58ad1ab151f988187c0ef
4
+ data.tar.gz: 36fdc5e97f61fe5b79fd84bc2bf1f57fa69f842036acf24c8ec5bf9d4d66c41a
5
5
  SHA512:
6
- metadata.gz: b965556669e1de0b4b966cd7d69a8a55397965a6fbad4b559e91736ed400ccaea3e2f4c50f2dcdef739aa8fb4e2891f927c31a42b471b7b3a2b10020eb981b7c
7
- data.tar.gz: a0ea7e3948e167e017e27f1e56a1d3a3ad6d6701368aebcd04a0599ebd89d6aab1fd6328ba2624983852e55bb5c4e654480a90a8249239968caea8766d1eb568
6
+ metadata.gz: a3069b0cd27363a042a7bd7d5e8e0a543153f5af6ed5f00fa24929ac177bf58369a2b9cbab16489589b3be81eef44c9c60e458753d3eb393c0e4ab923a45a91b
7
+ data.tar.gz: ca169d4812f4f2068666382063903f0f425f45454e2148b6d50d3358c81294f0b94596d0a36fe27300028ceaf133e36924d4142d1d348a04dd071677c63a9725
data/README.markdown CHANGED
@@ -49,6 +49,13 @@ end
49
49
  This will allow your template users to access the name method on your User
50
50
  objects.
51
51
 
52
+ Safemode supports logical compound assignments such as `user.name ||= value`,
53
+ `user&.name &&= value`, and `values[0] ||= value`. Their getters and conditional
54
+ setters are mediated by Jail, so both methods must be allowed when an assignment
55
+ is required. Compound assignments that implicitly invoke another operator, such
56
+ as `user.count += 1`, raise `Safemode::SecurityError`. Compound assignments to
57
+ local variables remain supported and their method calls are mediated by Jail.
58
+
52
59
  Class methods can be whitelisted by calling `allow_class_method :foo` from
53
60
  within the Jail. Note that access to raw constants is not permitted, so the
54
61
  class is only accessible when returned by a method or passed into a template.
@@ -90,9 +90,10 @@ module Safemode
90
90
  :colon2,
91
91
  # unnecessarily advanced?
92
92
  :argscat, :argspush, :splat, :kwsplat,
93
- # NOTE: op_asgn*/safe_op_asgn* do not insert .to_jail on the
94
- # receiver. Setters are called directly on the real object, bypassing Jail.
95
- :op_asgn, :op_asgn1, :op_asgn2, :op_asgn_and, :op_asgn_or,
93
+ # local variable logical assignments
94
+ :op_asgn_and, :op_asgn_or,
95
+ # object logical assignments; handlers below reject other operators
96
+ :op_asgn, :op_asgn1, :op_asgn2,
96
97
  :safe_op_asgn, :safe_op_asgn2,
97
98
  # pattern matching (Ruby 3.0+)
98
99
  :in, :array_pat, :hash_pat, :find_pat, :kwrest,
@@ -155,6 +156,52 @@ module Safemode
155
156
  raise Safemode::SecurityError.new(type, info)
156
157
  end
157
158
 
159
+ LOGICAL_ASSIGNMENT_OPERATORS = [:"||", :"&&"].freeze
160
+
161
+ def process_op_asgn(exp)
162
+ return raise_security_error(:op_asgn, super(exp)) unless LOGICAL_ASSIGNMENT_OPERATORS.include?(exp[4])
163
+
164
+ _, receiver, rhs, name, operator = exp
165
+ process_logical_attribute_assignment(receiver, name, operator, rhs)
166
+ end
167
+
168
+ def process_safe_op_asgn(exp)
169
+ return raise_security_error(:safe_op_asgn, super(exp)) unless LOGICAL_ASSIGNMENT_OPERATORS.include?(exp[4])
170
+
171
+ _, receiver, rhs, name, operator = exp
172
+ process_logical_attribute_assignment(receiver, name, operator, rhs, true)
173
+ end
174
+
175
+ def process_op_asgn2(exp)
176
+ return raise_security_error(:op_asgn2, super(exp)) unless LOGICAL_ASSIGNMENT_OPERATORS.include?(exp[3])
177
+
178
+ _, receiver, name, operator, rhs = exp
179
+ process_logical_attribute_assignment(receiver, name, operator, rhs)
180
+ end
181
+
182
+ def process_safe_op_asgn2(exp)
183
+ return raise_security_error(:safe_op_asgn2, super(exp)) unless LOGICAL_ASSIGNMENT_OPERATORS.include?(exp[3])
184
+
185
+ _, receiver, name, operator, rhs = exp
186
+ process_logical_attribute_assignment(receiver, name, operator, rhs, true)
187
+ end
188
+
189
+ def process_op_asgn1(exp)
190
+ return raise_security_error(:op_asgn1, super(exp)) unless LOGICAL_ASSIGNMENT_OPERATORS.include?(exp[3])
191
+
192
+ _, receiver, index, operator, rhs = exp
193
+ receiver = jail(process_call_receiver(receiver))
194
+ "#{receiver}[#{process index}] #{operator}= #{process rhs}"
195
+ end
196
+
197
+ def process_logical_attribute_assignment(receiver, name, operator, rhs, safe_call = false)
198
+ receiver = jail(process_call_receiver(receiver), safe_call: safe_call)
199
+ name = name.to_s.sub(/=$/, "")
200
+ dot = safe_call ? "&." : "."
201
+
202
+ "#{receiver}#{dot}#{name} #{operator}= #{process rhs}"
203
+ end
204
+
158
205
  # split up Ruby2Ruby#process_call monster method so we can hook into it
159
206
  # in a more readable manner
160
207
 
data/safemode.gemspec CHANGED
@@ -4,7 +4,7 @@ require 'date'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "safemode".freeze
7
- s.version = "2.0.3"
7
+ s.version = "2.0.4"
8
8
  s.date = Date.today
9
9
 
10
10
  s.summary = "A library for safe evaluation of Ruby code based on Prism and Ruby2Ruby"
@@ -5,6 +5,128 @@ require File.join(File.dirname(__FILE__), 'test_helper')
5
5
  class TestSafemodeEval < Test::Unit::TestCase
6
6
  include TestHelper
7
7
 
8
+ class CompoundAssignmentProbe
9
+ attr_reader :calls
10
+
11
+ def initialize(status: nil)
12
+ @calls = []
13
+ @target = Target.new(@calls, status)
14
+ end
15
+
16
+ def record(value)
17
+ @calls << value
18
+ end
19
+
20
+ def receiver
21
+ @calls << :receiver
22
+ @target
23
+ end
24
+
25
+ def nil_receiver
26
+ @calls << :nil_receiver
27
+ nil
28
+ end
29
+
30
+ def index
31
+ @calls << :index
32
+ 0
33
+ end
34
+
35
+ def rhs
36
+ @calls << :rhs
37
+ 1
38
+ end
39
+
40
+ def operator_value
41
+ @calls << :operator_value
42
+ OperatorValue.new(@calls)
43
+ end
44
+
45
+ def operator_target
46
+ @calls << :operator_target
47
+ Target.new(@calls, OperatorValue.new(@calls))
48
+ end
49
+
50
+ def denied_receiver
51
+ @calls << :denied_receiver
52
+ DeniedTarget.new(@calls)
53
+ end
54
+
55
+ class Target
56
+ def initialize(calls, status)
57
+ @calls = calls
58
+ @status = status
59
+ @values = {}
60
+ end
61
+
62
+ def status
63
+ @calls << :getter
64
+ @status
65
+ end
66
+
67
+ def status=(value)
68
+ @calls << :setter
69
+ @status = value
70
+ end
71
+
72
+ def [](index)
73
+ @calls << :index_getter
74
+ @values[index]
75
+ end
76
+
77
+ def []=(index, value)
78
+ @calls << :index_setter
79
+ @values[index] = value
80
+ end
81
+
82
+ class Jail < Safemode::Jail
83
+ allow :status, :status=, :[], :[]=
84
+ end
85
+ end
86
+
87
+ class OperatorValue
88
+ def initialize(calls)
89
+ @calls = calls
90
+ end
91
+
92
+ def +(_value)
93
+ @calls << :operator
94
+ 2
95
+ end
96
+ end
97
+
98
+ class DeniedTarget
99
+ def initialize(calls)
100
+ @calls = calls
101
+ end
102
+
103
+ def status
104
+ @calls << :denied_getter
105
+ end
106
+
107
+ def status=(_value)
108
+ @calls << :denied_setter
109
+ end
110
+
111
+ def [](_index)
112
+ @calls << :denied_index_getter
113
+ end
114
+
115
+ def []=(_index, _value)
116
+ @calls << :denied_index_setter
117
+ end
118
+
119
+ class Jail < Safemode::Jail
120
+ allow :status=, :[]=
121
+ end
122
+ end
123
+
124
+ class Jail < Safemode::Jail
125
+ allow :record, :receiver, :nil_receiver, :index, :rhs, :operator_value,
126
+ :operator_target, :denied_receiver
127
+ end
128
+ end
129
+
8
130
  def setup
9
131
  @box = Safemode::Box.new
10
132
  @locals = { :article => Article.new }
@@ -178,21 +300,155 @@ class TestSafemodeEval < Test::Unit::TestCase
178
300
  assert_raise(Safemode::SecurityError) { @box.eval('@article&.status = "new_value"', @assigns) }
179
301
  end
180
302
 
181
- # op_asgn2 (@article.status ||= val) is NOT blocked at parse time.
182
- # NOTE: .to_jail is not inserted on the receiver, so the setter is called
183
- # directly on the real object, bypassing the Jail whitelist.
184
- def test_op_asgn2_bypasses_jail
303
+ def test_index_assignment_is_blocked
304
+ assert_raise(Safemode::SecurityError) { @box.eval('@article[0] = "new_value"', @assigns) }
305
+ end
306
+
307
+ def test_attribute_logical_compound_assignment_requires_an_allowed_setter
185
308
  article = Article.new
309
+ assert_raise(Safemode::NoMethodError) do
310
+ @box.eval('@article.status ||= "new_value"', { article: article })
311
+ end
186
312
  assert_nil article.status
187
- @box.eval('@article.status ||= "new_value"', { article: article })
188
- assert_equal "new_value", article.status
189
313
  end
190
314
 
191
- def test_safe_op_asgn2_bypasses_jail
315
+ def test_attribute_logical_compound_assignment_requires_an_allowed_getter
316
+ probe = CompoundAssignmentProbe.new
317
+
318
+ assert_raise(Safemode::NoMethodError) do
319
+ @box.eval('@probe.denied_receiver.status ||= "default"', { probe: probe })
320
+ end
321
+ assert_equal [:denied_receiver], probe.calls
322
+ end
323
+
324
+ def test_index_logical_compound_assignment_requires_an_allowed_getter
325
+ probe = CompoundAssignmentProbe.new
326
+
327
+ assert_raise(Safemode::NoMethodError) do
328
+ @box.eval('@probe.denied_receiver[0] ||= "default"', { probe: probe })
329
+ end
330
+ assert_equal [:denied_receiver], probe.calls
331
+ end
332
+
333
+ def test_attribute_logical_compound_assignment_uses_jail
334
+ probe = CompoundAssignmentProbe.new
335
+
336
+ assert_equal 'default', @box.eval('@probe.receiver.status ||= "default"', { probe: probe })
337
+ assert_equal [:receiver, :getter, :setter], probe.calls
338
+ end
339
+
340
+ def test_attribute_and_assignment_uses_jail
341
+ probe = CompoundAssignmentProbe.new(status: 'existing')
342
+
343
+ assert_equal 'replacement', @box.eval('@probe.receiver.status &&= "replacement"', { probe: probe })
344
+ assert_equal [:receiver, :getter, :setter], probe.calls
345
+ end
346
+
347
+ def test_attribute_logical_compound_assignment_short_circuits_setter
192
348
  article = Article.new
193
- assert_nil article.status
194
- @box.eval('@article&.status ||= "new_value"', { article: article })
195
- assert_equal "new_value", article.status
349
+ article.status = 'existing'
350
+
351
+ assert_equal 'existing', @box.eval('@article.status ||= "default"', { article: article })
352
+ assert_equal 'existing', article.status
353
+ end
354
+
355
+ def test_safe_attribute_logical_compound_assignment_uses_jail
356
+ probe = CompoundAssignmentProbe.new
357
+
358
+ assert_equal 'default', @box.eval('@probe.receiver&.status ||= "default"', { probe: probe })
359
+ assert_equal [:receiver, :getter, :setter], probe.calls
360
+ end
361
+
362
+ def test_safe_attribute_logical_compound_assignment_short_circuits_nil_receiver
363
+ probe = CompoundAssignmentProbe.new
364
+
365
+ assert_nil @box.eval('@probe.nil_receiver&.status ||= @probe.rhs', { probe: probe })
366
+ assert_equal [:nil_receiver], probe.calls
367
+ end
368
+
369
+ def test_index_logical_compound_assignment_uses_jail
370
+ probe = CompoundAssignmentProbe.new
371
+
372
+ assert_equal 1, @box.eval('@probe.receiver[@probe.index] ||= @probe.rhs', { probe: probe })
373
+ assert_equal [:receiver, :index, :index_getter, :rhs, :index_setter], probe.calls
374
+ end
375
+
376
+ def test_object_logical_compound_assignment_rhs_uses_jail
377
+ probe = CompoundAssignmentProbe.new
378
+
379
+ assert_raise(Safemode::NoMethodError) do
380
+ @box.eval('@probe.receiver.status ||= @article.system', { probe: probe, article: Article.new })
381
+ end
382
+ assert_equal [:receiver, :getter], probe.calls
383
+ end
384
+
385
+ def test_attribute_operator_compound_assignment_is_blocked
386
+ assert_raise(Safemode::SecurityError) do
387
+ @box.eval('@article.status += 1', @assigns)
388
+ end
389
+ end
390
+
391
+ def test_safe_attribute_operator_compound_assignment_is_blocked
392
+ assert_raise(Safemode::SecurityError) do
393
+ @box.eval('@article&.status += 1', @assigns)
394
+ end
395
+ end
396
+
397
+ def test_compound_assignment_is_rejected_before_template_execution
398
+ [
399
+ '@probe.record(:earlier); @probe.operator_target.status += @probe.rhs',
400
+ '@probe.receiver[@probe.index] += @probe.rhs',
401
+ 'false && (@probe.receiver.status += @probe.rhs)',
402
+ '@probe.nil_receiver&.status += @probe.rhs',
403
+ ].each do |code|
404
+ probe = CompoundAssignmentProbe.new
405
+
406
+ assert_raise(Safemode::SecurityError, code) do
407
+ @box.eval(code, { probe: probe })
408
+ end
409
+ assert_empty(probe.calls, code)
410
+ end
411
+ end
412
+
413
+ def test_local_logical_compound_assignments_are_preserved
414
+ assert_equal 1, @box.eval('x = nil; x ||= 1; x')
415
+ assert_equal 1, @box.eval('x = false; x ||= 1; x')
416
+ assert_equal 2, @box.eval('x = 2; x ||= 1; x')
417
+ assert_equal 1, @box.eval('x = 2; x &&= 1; x')
418
+ assert_equal false, @box.eval('x = false; x &&= 1; x')
419
+ end
420
+
421
+ def test_local_logical_compound_assignments_short_circuit
422
+ probe = CompoundAssignmentProbe.new
423
+
424
+ assert_equal 1, @box.eval('x = nil; x ||= @probe.rhs; x', { probe: probe })
425
+ assert_equal [:rhs], probe.calls
426
+
427
+ probe = CompoundAssignmentProbe.new
428
+ assert_equal 2, @box.eval('x = 2; x ||= @probe.rhs; x', { probe: probe })
429
+ assert_equal false, @box.eval('x = false; x &&= @probe.rhs; x', { probe: probe })
430
+ assert_empty probe.calls
431
+ end
432
+
433
+ def test_evaluated_local_compound_assignment_rhs_uses_jail
434
+ assert_raise(Safemode::NoMethodError) do
435
+ @box.eval('x = nil; x ||= @article.system', @assigns)
436
+ end
437
+ end
438
+
439
+ def test_local_operator_compound_assignments_are_preserved
440
+ assert_equal 3, @box.eval('x = 2; x += 1; x')
441
+ assert_equal 8, @box.eval('x = 2; x **= 3; x')
442
+ assert_equal 4, @box.eval('x = 2; x <<= 1; x')
443
+ end
444
+
445
+ def test_local_operator_compound_assignment_uses_jail
446
+ probe = CompoundAssignmentProbe.new
447
+
448
+ assert_raise(Safemode::NoMethodError) do
449
+ @box.eval('x = @probe.operator_value; x += 1', { probe: probe })
450
+ end
451
+ assert_equal [:operator_value], probe.calls
196
452
  end
197
453
 
198
454
  def test_instance_variable_get_blocked_on_jail
@@ -73,9 +73,46 @@ class TestSafemodeParser < Test::Unit::TestCase
73
73
  end
74
74
  end
75
75
 
76
- def test_safe_op_asgn2_is_allowed
77
- assert_nothing_raised do
78
- jail('@article&.title ||= "new_value"')
76
+ def test_attribute_operator_compound_assignments_are_disabled
77
+ operators = %w[+ - * / % ** & | ^ << >>]
78
+
79
+ operators.each do |operator|
80
+ assert_raise(Safemode::SecurityError, "#{operator}= should be disabled") do
81
+ jail("@article.title #{operator}= 1")
82
+ end
83
+ end
84
+ end
85
+
86
+ def test_all_object_operator_compound_assignment_nodes_are_disabled
87
+ [
88
+ '@article.title += 1',
89
+ '@article&.title += 1',
90
+ '@article[0] += 1',
91
+ '@article.title += helper 1',
92
+ '@article&.title += helper 1',
93
+ '@article::title += 1',
94
+ '[1][0] += 1',
95
+ ].each do |code|
96
+ assert_raise(Safemode::SecurityError, "#{code} should be disabled") do
97
+ jail(code)
98
+ end
99
+ end
100
+ end
101
+
102
+ def test_object_logical_compound_assignments_are_allowed
103
+ [
104
+ '@article.title ||= 1',
105
+ '@article.title &&= 1',
106
+ '@article&.title ||= 1',
107
+ '@article&.title &&= 1',
108
+ '@article[0] ||= 1',
109
+ '@article[0] &&= 1',
110
+ '@article.title ||= helper 1',
111
+ '@article&.title ||= helper 1',
112
+ '[nil][0] ||= 1',
113
+ '({ key: nil })[:key] ||= 1',
114
+ ].each do |code|
115
+ assert_nothing_raised("#{code} should be allowed") { jail(code) }
79
116
  end
80
117
  end
81
118
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safemode
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -12,7 +12,7 @@ authors:
12
12
  - Dmitri Dolguikh
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2026-10-26 00:00:00.000000000 Z
15
+ date: 2026-11-19 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: ruby2ruby