psych_shield 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/LICENSE +11 -0
  2. data/lib/psych_shield.rb +71 -0
  3. metadata +49 -0
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Copyright (C) 2013 HD Moore < hdm[at]rapid7.com >
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+ * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,71 @@
1
+ #
2
+ # PsychShield provides a filter around the Psych class
3
+ # that can prevent exploitation of YAML.load calls.
4
+ #
5
+
6
+ require 'yaml'
7
+
8
+ unless YAML.name == "Psych"
9
+ raise RuntimeError, "psych-shield only works with the Psych parser"
10
+ end
11
+
12
+ class PsychShield
13
+
14
+ # Generally regarded as safe for YAML loads
15
+ @@allowed_objects = %W{
16
+ Hash Array String Range
17
+ Numeric Fixnum Integer Bignum Float Rational Complex
18
+ Time DateTime
19
+ NilClass TrueClass FalseClass
20
+ }
21
+
22
+ def self.add(name)
23
+ @@allowed_objects << name
24
+ end
25
+
26
+ def self.remove(name)
27
+ @@allowed_objects.delete(name)
28
+ end
29
+
30
+ def self.clear
31
+ @@allowed_objects = []
32
+ end
33
+
34
+ def self.allowed?(o)
35
+ res = @@allowed_objects.include?(o.class.to_s)
36
+ @callback.call(o.class.to_s, res) if @callback
37
+ res
38
+ end
39
+
40
+ def self.callback=(cb)
41
+ @@callback = cb
42
+ end
43
+
44
+ class DeniedObject < Hash
45
+ end
46
+
47
+ end
48
+
49
+ module Psych
50
+ module Visitors
51
+ class ToRuby
52
+
53
+ alias_method :shielded_revive_hash, :revive_hash
54
+ def revive_hash hash, o
55
+ unless PsychShield.allowed?(hash)
56
+ return PsychShield::DeniedObject.new
57
+ end
58
+ shielded_revive_hash(hash,o)
59
+ end
60
+
61
+ alias_method :shielded_init_with, :init_with
62
+ def init_with o, h, node
63
+ unless PsychShield.allowed?(o)
64
+ return PsychShield::DeniedObject.new
65
+ end
66
+ shielded_init_with(o,h,node)
67
+ end
68
+
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psych_shield
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - HD Moore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'The psych_shield gem provides a configurable filter for the default
15
+ Psych YAML parser in Ruby 1.9 '
16
+ email:
17
+ - hdm@rapid7.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/psych_shield.rb
23
+ - LICENSE
24
+ homepage: ''
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: This gem provides a filter for the Psych YAML parser
48
+ test_files: []
49
+ has_rdoc: