veracode 1.0.0.alpha3 → 1.0.0.alpha4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/veracode CHANGED
@@ -16,12 +16,10 @@ require 'veracode'
16
16
  require 'veracode/version'
17
17
 
18
18
  $options = {
19
- :phase1 => false,
20
- :phase2 => false,
21
- :phase3 => true,
22
19
  :archive_source => true,
23
20
  :include_inherited => false,
24
21
  :jruby => false,
22
+ :environment => false,
25
23
  }
26
24
 
27
25
  subcommand = ARGV.shift
@@ -34,22 +32,13 @@ case subcommand
34
32
  $options[:verbose] = true
35
33
  end
36
34
 
37
- opts.on("-a", "--all", "Archive objects at all stages") do
38
- $options[:phase1] = true
39
- $options[:phase2] = true
40
- end
41
-
42
- opts.on("-f", "--file", "Disassemble .rb files") do
43
- $options[:disasm] = true
44
- end
45
-
46
35
  opts.on("-j", "--jruby", "Force JRuby mode") do
47
36
  $options[:jruby] = true
48
37
  end
49
38
 
50
- opts.on("--[no-]source", "[Don't] Include source code in archive") do |s|
51
- $options[:archive_source] = s
52
- end
39
+ # opts.on("--[no-]source", "[Don't] Include source code in archive") do |s|
40
+ # $options[:archive_source] = s
41
+ # end
53
42
 
54
43
  opts.on("-D", "--debug", "Enable debug output") do
55
44
  $DEBUG = true
@@ -73,6 +62,23 @@ case subcommand
73
62
  " #{opts.program_name} help"
74
63
  end.parse!
75
64
 
65
+ when "environment", "env"
66
+ $options[:environment] = true
67
+
68
+ OptionParser.new do |opts|
69
+ opts.banner = "Usage: veracode environment [options]"
70
+
71
+ opts.on("-v", "--verbose", "Run verbosely") do
72
+ $options[:verbose] = true
73
+ end
74
+
75
+ opts.on("-D", "--debug", "Enable debug output") do
76
+ $DEBUG = true
77
+ end
78
+
79
+ end.parse!
80
+ Veracode.prepare
81
+
76
82
  else
77
83
  $stderr.puts "#{subcommand.dump} is not a valid subcommand"
78
84
 
@@ -67,17 +67,20 @@ module Veracode
67
67
  schema = 'Veracode::' + File.read(schema_file).each_line.reject {|l| l =~ /^\s*#/}.join
68
68
  rescue Exception => e
69
69
  puts "Unable to retrieve schema information from 'db/schema.rb'. Are your migrations up to date?"
70
- log_error "Unable to archive 'db/schema.rb' (#{e.message})"
70
+ log_error "Unable to retrieve schema from 'db/schema.rb' (#{e.message})"
71
+ add_to_archive %Q|module "Veracode::Schema"\n|
72
+ add_to_archive %Q|endmodule\n\n|
73
+ return
71
74
  end
72
-
75
+
76
+ add_to_archive %Q|module "Veracode::Schema"\n|
73
77
  begin
74
- add_to_archive %Q|module "Veracode::Schema"\n|
75
78
  eval(schema)
76
- add_to_archive %Q|endmodule\n\n|
77
79
  rescue Exception => e
78
80
  puts "Unable to evaluate schema information from 'db/schema.rb'. (#{e.message})"
79
81
  log_error "Unable to evaluate 'db/schema.rb' (#{e.message})"
80
82
  end
83
+ add_to_archive %Q|endmodule\n\n|
81
84
  end
82
85
 
83
- end
86
+ end
@@ -1,4 +1,4 @@
1
1
  module Veracode
2
- VERSION = '1.0.0.alpha3'
2
+ VERSION = '1.0.0.alpha4'
3
3
  ARCHIVE_VERSION = '2012-07-04'
4
4
  end
data/lib/veracode.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'pathname'
2
+ require 'set'
1
3
  require 'zlib'
2
4
  require 'zip/zip'
3
5
  require 'veracode/version'
@@ -6,6 +8,7 @@ require 'veracode/gems'
6
8
 
7
9
  module Veracode
8
10
  @run_id = nil
11
+ @required_libs = Set.new
9
12
 
10
13
  # Metadata and method disassemblies for all Modules (.txt.gz)
11
14
  @disasmlog = nil
@@ -37,6 +40,7 @@ module Veracode
37
40
  def self.init
38
41
  @run_id = Time.now.strftime("%Y%m%d%H%M%S")
39
42
  @archive_dirname = File.join("tmp","veracode-#{@run_id}")
43
+ @required_libs.merge(["pathname", "set", "zlib", "zip/zip", "veracode"])
40
44
 
41
45
  if !Dir.exists?("tmp")
42
46
  begin
@@ -221,20 +225,33 @@ module Veracode
221
225
 
222
226
  ##############################################################################
223
227
  # Helpers
228
+ def self.cond_require(lib)
229
+ if @required_libs.add?(lib)
230
+ return require lib
231
+ end
232
+ return false
233
+ end
234
+
224
235
  def self.glob_require(files)
236
+ any_new = false
237
+ total, count = 0, 0
225
238
  Dir.glob(files) do |f|
226
239
  print "Requiring #{f.to_s} " if $options[:verbose]
227
240
 
228
241
  begin
229
- require File.expand_path(f)
242
+ required = require File.expand_path(f)
230
243
  rescue Exception => e
231
244
  puts "(failed: #{e.message})" if $options[:verbose]
232
245
  log_error "Unable to require #{File.expand_path(f).to_s.dump} (#{e.message})"
233
246
  else
234
- puts "(OK)" if $options[:verbose]
247
+ puts "(OK: #{(required ? "required" : "already required")})" if $options[:verbose]
235
248
  end
236
-
249
+ any_new |= required
250
+ total += 1
251
+ count += 1 if required
237
252
  end
253
+ puts "#{count}/#{total} files were required" if $options[:verbose]
254
+ any_new
238
255
  end
239
256
 
240
257
  def self.safe_name(o)
@@ -242,7 +259,11 @@ module Veracode
242
259
  when o == ActiveSupport::TimeWithZone
243
260
  "ActiveSupport::TimeWithZone"
244
261
  when o.is_a?(Module)
245
- ( o.name.nil? ? o.to_s : o.name )
262
+ begin
263
+ ( o.name.nil? ? o.to_s : o.name.to_s )
264
+ rescue
265
+ o.to_s
266
+ end
246
267
  when o.is_a?(Method), o.is_a?(UnboundMethod)
247
268
  o.name.to_s
248
269
  else
@@ -307,7 +328,13 @@ module Veracode
307
328
  def self.prepare_archive
308
329
  @disasmlog = Zlib::GzipWriter.new(File.open(@disasmlog_filename, "wb"), nil, nil)
309
330
  @disasmlog.puts "#{RUBY_ENGINE}-#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
310
- @disasmlog.puts "# " + `rails --version`.chomp
331
+ if $options[:environment]
332
+ @disasmlog.puts "# EnvironmentDef %s-%s_rails-%s" % [RUBY_ENGINE, RUBY_VERSION, Rails.version]
333
+ else
334
+ @disasmlog.puts "# Environment %s-%s_rails-%s" % [RUBY_ENGINE, RUBY_VERSION, Rails.version]
335
+ end
336
+ @disasmlog.puts "# Ruby #{RUBY_ENGINE}-#{RUBY_VERSION}"
337
+ @disasmlog.puts "# Rails #{Rails.version}"
311
338
  @disasmlog.puts
312
339
  end
313
340
 
@@ -384,7 +411,7 @@ module Veracode
384
411
  m.included_modules.map {|m| "include #{m.inspect.dump}\n" }.join :
385
412
  ""
386
413
  ) +
387
- ( m.singleton_class.included_modules.count > 0 ?
414
+ ( m.respond_to?(:singleton_class) && m.singleton_class.included_modules.count > 0 ?
388
415
  m.singleton_class.included_modules.map {|m| "extend #{m.inspect.dump}\n" }.join :
389
416
  ""
390
417
  )
@@ -420,23 +447,26 @@ module Veracode
420
447
  end
421
448
  end
422
449
 
423
- if m.respond_to?(:global_variables)
424
- m.global_variables.each do |v_symbol|
425
- begin
426
- v = eval(v_symbol.to_s)
427
- formatted_contents += format_variable(v_symbol, v, "global")
428
- rescue Exception => e
429
- log_error "Error archiving global variable #{v_symbol.to_s.dump}: #{e.message}"
430
- formatted_contents += format_variable(v_symbol, :veracode_nil, "global")
431
- end
432
-
450
+ begin
451
+ if m == Kernel
452
+ m.global_variables.each do |v_symbol|
453
+ begin
454
+ v = eval(v_symbol.to_s)
455
+ formatted_contents += format_variable(v_symbol, v, "global")
456
+ rescue Exception => e
457
+ log_error "Error archiving global variable #{v_symbol.to_s.dump}: #{e.message}"
458
+ formatted_contents += format_variable(v_symbol, :veracode_nil, "global")
459
+ end
460
+ end
433
461
  end
462
+ rescue Exception => e
463
+ # m.respond_to?(:global_variables) was throwing exceptions
434
464
  end
435
465
 
436
466
  %w[ public protected private ].each {|p|
437
467
  get_methods = (p + "_instance_methods").to_sym
438
- if m.respond_to?(get_methods) && m.send(get_methods, $options[:include_inherited]).count > 0
439
- m.send(get_methods, $options[:include_inherited]).each do |m_symbol|
468
+ if m.respond_to?(get_methods) && m.__send__(get_methods, $options[:include_inherited]).count > 0
469
+ m.__send__(get_methods, $options[:include_inherited]).each do |m_symbol|
440
470
  begin
441
471
  method = m.instance_method(m_symbol)
442
472
  formatted_contents += format_method(method, "#{p.to_s}_instance", with_disasm)
@@ -488,7 +518,12 @@ module Veracode
488
518
  # Archiving Objects
489
519
  def self.archive(objects, with_disasm=true)
490
520
 
491
- objects = objects - [Veracode]
521
+ objects = objects - [
522
+ Veracode,
523
+ Veracode::ActiveRecord,
524
+ Veracode::ActiveRecord::Model,
525
+ Veracode::ActiveRecord::Schema,
526
+ ]
492
527
 
493
528
  if $options[:verbose]
494
529
  puts "Archiving #{objects.count.to_s} objects" + (with_disasm ? " with disassembly" : "")
@@ -517,8 +552,8 @@ module Veracode
517
552
  def self.compile_templates
518
553
 
519
554
  begin
520
- require 'action_view' unless defined? ActionView
521
- require 'action_controller' unless defined? ActionController
555
+ cond_require 'action_view' unless defined? ActionView
556
+ cond_require 'action_controller' unless defined? ActionController
522
557
  rescue Exception => e
523
558
  log_error "Unable to satisfy haml dependencies (#{e.message})"
524
559
  return
@@ -541,12 +576,13 @@ module Veracode
541
576
  return unless templates.count > 0
542
577
 
543
578
  puts "Found #{templates.count} templates" if $options[:verbose]
579
+ log_error "Found #{templates.count} templates"
544
580
 
545
581
  haml_templates = templates.grep(/\.haml$/)
546
582
  if haml_templates.any?
547
583
  begin
548
- require 'haml' unless defined? Haml
549
- require 'haml/template/plugin' unless defined? Haml::Plugin
584
+ cond_require 'haml' unless defined? Haml
585
+ cond_require 'haml/template/plugin' unless defined? Haml::Plugin
550
586
  rescue Exception => e
551
587
  puts "Unable to satisfy haml dependencies"
552
588
  log_error "Unable to satisfy haml dependencies (#{e.message})"
@@ -563,13 +599,16 @@ module Veracode
563
599
  puts "Compiling template #{template}" if $options[:verbose]
564
600
 
565
601
  begin
602
+ # This render will fail, but will trigger compilation of template
566
603
  view.render(:file => template)
567
604
  rescue Exception => e
568
605
  log_error "Compiled template #{template} #{e.message}"
569
606
  end
570
607
  }
571
608
 
572
- puts "Compiled #{ActionView::CompiledTemplates.instance_methods.count.to_s} templates " if $options[:verbose]
609
+ puts "Compiled #{ActionView::CompiledTemplates.instance_methods.count.to_s} templates" if $options[:verbose]
610
+ log_error "Compiled #{ActionView::CompiledTemplates.instance_methods.count.to_s} templates"
611
+ log_error "Not all templates were compiled" if ActionView::CompiledTemplates.instance_methods.count < templates.count
573
612
  end
574
613
 
575
614
  def self.compile_erb_templates
@@ -644,9 +683,9 @@ module Veracode
644
683
  return unless templates.count > 0
645
684
 
646
685
  begin
647
- require 'action_view'
648
- require 'haml'
649
- require 'haml/template/plugin'
686
+ cond_require 'action_view'
687
+ cond_require 'haml'
688
+ cond_require 'haml/template/plugin'
650
689
  rescue Exception => e
651
690
  log_error "Unable to satisfy haml dependencies (#{e.message})"
652
691
  return
@@ -685,6 +724,59 @@ module Veracode
685
724
 
686
725
  end
687
726
 
727
+ def self.require_libs(lib_paths)
728
+ for lib_path in lib_paths
729
+ dirsToProcess = [Pathname(lib_path)]
730
+ until dirsToProcess.count == 0 || !Dir.exists?(dirsToProcess[0])
731
+ currentDir = dirsToProcess.delete_at(0)
732
+ for child in currentDir.children
733
+ if child.directory?
734
+ dirsToProcess[dirsToProcess.count] = child
735
+ base = child.to_s.partition("#{lib_path}/")[2]
736
+ lib = ""
737
+ for part in base.split('/').reverse
738
+ lib = "#{part}/#{lib}"
739
+ lib = lib[0..lib.length-2] if lib[lib.length-1] == '/'
740
+ begin
741
+ if cond_require lib
742
+ puts "requiring #{lib}" if $options[:verbose]
743
+ end
744
+ rescue Exception => e
745
+ end
746
+ end
747
+ end
748
+ end
749
+ end
750
+ end
751
+ end
752
+
753
+ def self.require_rails(gemdir)
754
+ dirsToProcess = [Pathname(gemdir)]
755
+ until dirsToProcess.count == 0
756
+ currentDir = dirsToProcess.delete_at(0)
757
+ for child in currentDir.children
758
+ if child.directory?
759
+ dirsToProcess[dirsToProcess.count] = child
760
+ end
761
+ base = child.to_s.partition("#{gemdir}/")[2]
762
+ if base.index("action_controller") != nil || base.index("action_view") != nil || base.index("active_record") != nil
763
+ lib = ""
764
+ for part in base.split('/').reverse
765
+ lib = "#{part}/#{lib}"
766
+ lib = lib[0..lib.length-2] if lib[lib.length-1] == '/'
767
+ lib.chomp!(File.extname(lib))
768
+ begin
769
+ if cond_require lib
770
+ puts "requiring #{lib}" if $options[:verbose]
771
+ end
772
+ rescue Exception => e
773
+ end
774
+ end
775
+ end
776
+ end
777
+ end
778
+ end
779
+
688
780
 
689
781
 
690
782
 
@@ -702,16 +794,6 @@ module Veracode
702
794
  puts
703
795
  end
704
796
 
705
- if $options[:disasm]
706
- rbfiles = File.join("**", "*.rb")
707
- Dir[rbfiles].each do |f|
708
- puts RubyVM::InstructionSequence.compile_file(f).disasm
709
- puts
710
- end
711
- exit
712
- end
713
-
714
- prepare_archive
715
797
 
716
798
  ################################################################
717
799
  ## phase 1 - Create baseline
@@ -720,10 +802,6 @@ module Veracode
720
802
  puts "Phase 1 - Initial State" if $options[:verbose]
721
803
  self.stats if $options[:verbose]
722
804
 
723
- if $options[:phase1]
724
- puts "Processing and disassembling Ruby standard classes and modules"
725
- archive(@modules)
726
- end
727
805
  ## /phase 1 - Create baseline
728
806
  ################################################################
729
807
 
@@ -734,10 +812,7 @@ module Veracode
734
812
 
735
813
  puts "Phase 2 - Load Rails" if $options[:verbose]
736
814
  begin
737
- require "rails"
738
- require 'action_controller'
739
- require 'action_view'
740
- require 'active_record'
815
+ require "rails/all"
741
816
  rescue Exception => e
742
817
  puts "Unable to require rails: #{e.message}"
743
818
  log_error "Unable to require rails: #{e.message}"
@@ -746,26 +821,6 @@ module Veracode
746
821
  puts "Required rails" if $options[:verbose]
747
822
  end
748
823
 
749
- self.update
750
-
751
- self.stats if $options[:verbose]
752
-
753
- if $options[:phase2]
754
- puts "Processing and disassembling Rails classes and modules"
755
- archive(@modules)
756
- end
757
-
758
- self.rebaseline
759
- ## /phase 2 - Require rails
760
- ################################################################
761
-
762
-
763
-
764
- ################################################################
765
- # phase 3 - require app
766
-
767
- puts "Phase 3 - Imitate Rails" if $options[:verbose]
768
-
769
824
  ## Imitate script/rails
770
825
  # APP_PATH = File.expand_path('config/application')
771
826
  # APP_PATH is already set in bin/veracode
@@ -779,7 +834,7 @@ module Veracode
779
834
  # require 'rails/commands'
780
835
 
781
836
  ## Imitate rails/commands when console
782
- glob_require 'rails/commands/console'
837
+ cond_require 'rails/commands/console.rb'
783
838
  # require APP_PATH # => config/application.rb
784
839
 
785
840
  glob_require "config/application.rb"
@@ -790,27 +845,62 @@ module Veracode
790
845
 
791
846
  # Imitate Rails::Console.initialize_console
792
847
  # require "pp"
793
- glob_require "rails/console/app"
794
- glob_require "rails/console/helpers"
848
+ cond_require "rails/console/app.rb"
849
+ cond_require "rails/console/helpers.rb"
850
+
851
+ if $options[:environment]
852
+ @stdlib = $:
853
+ @gemdir = Gem.dir
795
854
 
796
- glob_require "app/models/**/*.rb"
797
- glob_require "app/helpers/**/*.rb"
798
- glob_require "app/controllers/application_controller.rb"
799
- glob_require "app/controllers/**/*.rb"
855
+ require_libs(@stdlib)
856
+ require_rails(@gemdir)
857
+ end
858
+
859
+ self.rebaseline
860
+
861
+ self.stats if $options[:verbose]
862
+
863
+ ## /phase 2 - Require rails
864
+ ################################################################
865
+
866
+
867
+
868
+ ################################################################
869
+ # phase 3 - require app
870
+
871
+ puts "Phase 3 - Imitate Rails" if $options[:verbose]
872
+
873
+ any_new = true
874
+ while any_new
875
+ any_new = false
876
+ any_new |= glob_require "lib/**/*.rb"
877
+ any_new |= glob_require "app/**/*.rb"
878
+ puts "new successful requires? #{any_new.to_s}" if $options[:verbose]
879
+ end
800
880
 
801
881
  compile_templates
802
882
 
803
883
  self.update
804
884
  self.stats if $options[:verbose]
805
885
 
806
- if $options[:phase3]
807
- puts "Processing and disassembling #{APP_NAME} classes and modules"
886
+ # Ensure compiled templates are fully disassembled in archive
887
+ @baseline_modules.delete(ActionView::CompiledTemplates)
888
+
889
+ if $options[:environment]
890
+ puts "Processing and disassembling environment"
891
+ archive(@modules.reject {|o| safe_name(o) =~ /^#<(Class|Module):0x[0-9a-f]+>/i }
892
+ .reject {|o| safe_name(o) =~ /^Veracode/ }
893
+ .reject {|o| safe_name(o) =~ /^EmptyRails/ }
894
+ .reject {|o| safe_name(o) =~ /^ActionView::CompiledTemplates$/ }, false)
895
+ else
896
+ puts "Processing Ruby and Rails classes and modules"
808
897
  archive(@baseline_modules, false)
898
+ add_to_archive "\n# Phase 3 - App disassembly\n"
899
+ puts "Processing and disassembling #{APP_NAME} classes and modules"
809
900
  archive(@modules - @baseline_modules, true)
901
+ archive_schema
810
902
  end
811
903
 
812
- archive_schema
813
-
814
904
  ## /phase 3 - require app
815
905
  ################################################################
816
906
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veracode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha3
4
+ version: 1.0.0.alpha4
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-27 00:00:00.000000000 Z
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubyzip
16
- requirement: &70156231653560 !ruby/object:Gem::Requirement
16
+ requirement: &70110652152040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70156231653560
24
+ version_requirements: *70110652152040
25
25
  description: Prepares your Ruby on Rails app for submission to Veracode.
26
26
  email: devcontact@veracode.com
27
27
  executables: