rufus-treechecker 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,11 @@
2
2
  = rufus-treechecker CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-treechecker - 1.0.5 released 2011/05/10
6
+
7
+ - clone then add_rules issue fix
8
+
9
+
5
10
  == rufus-treechecker - 1.0.4 released 2010/12/22
6
11
 
7
12
  - require rufus/tree_checker and rufus-tree_checker
@@ -122,11 +122,12 @@ module Rufus
122
122
  #
123
123
  class TreeChecker
124
124
 
125
- VERSION = '1.0.4'
125
+ VERSION = '1.0.5'
126
126
 
127
127
  # pretty-prints the sexp tree of the given rubycode
128
128
  #
129
129
  def ptree(rubycode)
130
+
130
131
  puts stree(rubycode)
131
132
  end
132
133
 
@@ -134,6 +135,7 @@ module Rufus
134
135
  # (thanks ruby_parser).
135
136
  #
136
137
  def stree(rubycode)
138
+
137
139
  "#{rubycode.inspect}\n =>\n#{parse(rubycode).inspect}"
138
140
  end
139
141
 
@@ -149,6 +151,7 @@ module Rufus
149
151
  end
150
152
 
151
153
  def to_s
154
+
152
155
  s = "#{self.class} (#{self.object_id})\n"
153
156
  s << "root_set :\n"
154
157
  s << @root_set.to_s
@@ -176,9 +179,11 @@ module Rufus
176
179
  #
177
180
  def clone
178
181
 
179
- tc = TreeChecker.new
182
+ tc = TreeChecker.allocate
180
183
  tc.instance_variable_set(:@root_set, @root_set.clone)
181
184
  tc.instance_variable_set(:@set, @set.clone)
185
+ tc.instance_variable_set(:@current_set, tc.instance_variable_get(:@set))
186
+
182
187
  tc
183
188
  end
184
189
 
@@ -194,6 +199,7 @@ module Rufus
194
199
  # Freezes the treechecker instance "in depth"
195
200
  #
196
201
  def freeze
202
+
197
203
  super
198
204
  @root_set.freeze
199
205
  @set.freeze
@@ -203,6 +209,10 @@ module Rufus
203
209
 
204
210
  class RuleSet
205
211
 
212
+ # Mostly for easier specs
213
+ #
214
+ attr_accessor :excluded_symbols, :accepted_patterns, :excluded_patterns
215
+
206
216
  def initialize
207
217
 
208
218
  @excluded_symbols = {} # symbol => exclusion_message
@@ -211,10 +221,12 @@ module Rufus
211
221
  end
212
222
 
213
223
  def clone
224
+
214
225
  rs = RuleSet.new
215
- rs.instance_variable_set(:@excluded_symbols, @excluded_symbols.dup)
216
- rs.instance_variable_set(:@accepted_patterns, @accepted_patterns.dup)
217
- rs.instance_variable_set(:@excluded_patterns, @excluded_patterns.dup)
226
+ rs.excluded_symbols = @excluded_symbols.dup
227
+ rs.accepted_patterns = @accepted_patterns.dup
228
+ rs.excluded_patterns = @excluded_patterns.dup
229
+
218
230
  rs
219
231
  end
220
232
 
@@ -236,10 +248,9 @@ module Rufus
236
248
 
237
249
  def check(sexp)
238
250
 
239
- if sexp.is_a?(Symbol)
251
+ if sexp.is_a?(Symbol) and m = @excluded_symbols[sexp]
240
252
 
241
- m = @excluded_symbols[sexp]
242
- raise SecurityError.new(m) if m
253
+ raise SecurityError.new(m)
243
254
 
244
255
  elsif sexp.is_a?(Array)
245
256
 
@@ -292,6 +303,15 @@ module Rufus
292
303
  s
293
304
  end
294
305
 
306
+ # Mostly a spec method
307
+ #
308
+ def ==(oth)
309
+
310
+ @excluded_symbols == oth.instance_variable_get(:@excluded_symbols) &&
311
+ @accepted_patterns == oth.instance_variable_get(:@accepted_patterns) &&
312
+ @excluded_patterns == oth.instance_variable_get(:@excluded_patterns)
313
+ end
314
+
295
315
  protected
296
316
 
297
317
  def check_pattern(sexp, pat)
@@ -352,33 +372,40 @@ module Rufus
352
372
  end
353
373
 
354
374
  def exclude_symbol(*args)
375
+
355
376
  args, message = extract_message(args)
356
377
  args.each { |a| @current_set.exclude_symbol(a, message) }
357
378
  end
358
379
 
359
380
  def exclude_fcall(*args)
381
+
360
382
  do_exclude_pair(:fcall, args)
361
383
  end
362
384
 
363
385
  def exclude_vcall(*args)
386
+
364
387
  do_exclude_pair(:vcall, args)
365
388
  end
366
389
 
367
390
  def exclude_fvcall(*args)
391
+
368
392
  do_exclude_pair(:fcall, args)
369
393
  do_exclude_pair(:vcall, args)
370
394
  end
371
395
 
372
396
  def exclude_call_on(*args)
397
+
373
398
  do_exclude_pair(:call, args)
374
399
  end
375
400
 
376
401
  def exclude_call_to(*args)
402
+
377
403
  args, message = extract_message(args)
378
404
  args.each { |a| @current_set.exclude_pattern([ :call, :any, a], message) }
379
405
  end
380
406
 
381
407
  def exclude_fvccall(*args)
408
+
382
409
  exclude_fvcall(*args)
383
410
  exclude_call_to(*args)
384
411
  end
@@ -393,7 +420,9 @@ module Rufus
393
420
  # k = ::Kernel
394
421
  #
395
422
  def exclude_rebinding(*args)
423
+
396
424
  args, message = extract_message(args)
425
+
397
426
  args.each do |a|
398
427
  expand_class(a).each do |c|
399
428
  @current_set.exclude_pattern([ :lasgn, :any, c], message)
@@ -406,6 +435,7 @@ module Rufus
406
435
  # of classes
407
436
  #
408
437
  def exclude_access_to(*args)
438
+
409
439
  exclude_call_on *args
410
440
  exclude_rebinding *args
411
441
  end
@@ -507,7 +537,7 @@ module Rufus
507
537
 
508
538
  # check children
509
539
 
510
- sexp.each { |c| do_check c }
540
+ sexp.each { |c| do_check(c) }
511
541
  end
512
542
 
513
543
  # A simple parse (relies on ruby_parser currently)
data/spec/low_spec.rb CHANGED
@@ -89,11 +89,18 @@ describe Rufus::TreeChecker do
89
89
  [
90
90
 
91
91
  'def drink; "water"; end',
92
- 'class Toto; def drink; "water"; end; end'
92
+ 'class Toto; def drink; "water"; end; end',
93
+ %{
94
+ class Whatever
95
+ def eat
96
+ "food"
97
+ end
98
+ end
99
+ }
93
100
 
94
101
  ].each do |code|
95
102
 
96
- it "blocks '#{code}'" do
103
+ it "blocks #{code.inspect}" do
97
104
  lambda { tc.check(code) }.should raise_error(Rufus::SecurityError)
98
105
  end
99
106
  end
data/spec/misc_spec.rb CHANGED
@@ -38,6 +38,30 @@ describe Rufus::TreeChecker do
38
38
 
39
39
  tc1.set.object_id.should_not == tc0.set.object_id
40
40
  tc1.root_set.object_id.should_not == tc0.root_set.object_id
41
+
42
+ tc1.set.should == tc0.set
43
+ tc1.root_set.should == tc0.root_set
44
+ end
45
+
46
+ it "sets @current_set correclty when cloning" do
47
+
48
+ tc0 = Rufus::TreeChecker.new
49
+
50
+ tc1 = tc0.clone
51
+
52
+ tc1.add_rules do
53
+ exclude_def
54
+ end
55
+
56
+ class << tc0
57
+ attr_reader :set, :root_set
58
+ end
59
+ class << tc1
60
+ attr_reader :set, :root_set
61
+ end
62
+
63
+ tc0.set.excluded_symbols.keys.should_not include(:defn)
64
+ tc1.set.excluded_symbols.keys.should include(:defn)
41
65
  end
42
66
  end
43
67
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-treechecker
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 4
9
- version: 1.0.4
4
+ prerelease:
5
+ version: 1.0.5
10
6
  platform: ruby
11
7
  authors:
12
8
  - John Mettraux
@@ -14,20 +10,17 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-12-22 00:00:00 +09:00
13
+ date: 2011-05-10 00:00:00 +09:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
17
  name: ruby_parser
22
18
  prerelease: false
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
24
21
  requirements:
25
22
  - - ">="
26
23
  - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 0
30
- - 5
31
24
  version: 2.0.5
32
25
  type: :runtime
33
26
  version_requirements: *id001
@@ -35,11 +28,10 @@ dependencies:
35
28
  name: rake
36
29
  prerelease: false
37
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
38
32
  requirements:
39
33
  - - ">="
40
34
  - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
35
  version: "0"
44
36
  type: :development
45
37
  version_requirements: *id002
@@ -47,12 +39,10 @@ dependencies:
47
39
  name: rspec
48
40
  prerelease: false
49
41
  requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
50
43
  requirements:
51
44
  - - ">="
52
45
  - !ruby/object:Gem::Version
53
- segments:
54
- - 2
55
- - 0
56
46
  version: "2.0"
57
47
  type: :development
58
48
  version_requirements: *id003
@@ -92,23 +82,21 @@ rdoc_options: []
92
82
  require_paths:
93
83
  - lib
94
84
  required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
95
86
  requirements:
96
87
  - - ">="
97
88
  - !ruby/object:Gem::Version
98
- segments:
99
- - 0
100
89
  version: "0"
101
90
  required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
102
92
  requirements:
103
93
  - - ">="
104
94
  - !ruby/object:Gem::Version
105
- segments:
106
- - 0
107
95
  version: "0"
108
96
  requirements: []
109
97
 
110
98
  rubyforge_project: rufus
111
- rubygems_version: 1.3.6
99
+ rubygems_version: 1.6.2
112
100
  signing_key:
113
101
  specification_version: 3
114
102
  summary: tests strings of Ruby code for unauthorized patterns (exit, eval, ...)