hash_parser 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b5e8eb3670d11b0061cae4ebc2bfd239dfa9836
4
+ data.tar.gz: afb5f547791bec5ae475772b9a90308214e8adec
5
+ SHA512:
6
+ metadata.gz: '087ff07ce0d7a782b214eda070626f1f8d867deb46dab4573053c6d18d18d184f06e2578ca4d590d5b92bc98b8295732c382d4fc74c732cae33247c622252955'
7
+ data.tar.gz: 49b39ea2053ace60d3345d929782bacfd863006c993e32a64a192efdf5e921ea447eedcb4d6ad5f0dfb4c7c2ae444b4fdceaeb5a0be09e7b18060bfa8ebe1b87
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ === 0.0.1 / 2017-03-29
2
+
3
+ * Initial implementation
4
+
5
+ The whitelist based implementation only loads the classes Psych::safe_load loads.
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/hash_parser.rb
6
+ test/test_hash_parser.rb
data/README.txt ADDED
@@ -0,0 +1,74 @@
1
+ = blah
2
+
3
+ home :: https://github.com/bibstha/ruby_hash_parser
4
+ code :: https://github.com/bibstha/ruby_hash_parser
5
+ bugs :: https://github.com/bibstha/ruby_hash_parser
6
+ ... etc ...
7
+
8
+ == DESCRIPTION:
9
+
10
+ Parses a hash string of the format `'{ :a => "something" }'` into an actual ruby hash object `{ a: "something" }`.
11
+ This is useful when you by mistake serialize hashes and save it in database column or a text file and you want to
12
+ convert them back to hashes without the security issues of executing `eval(hash_string)`.
13
+
14
+ By default only following classes are allowed to be deserialized:
15
+
16
+ * TrueClass
17
+ * FalseClass
18
+ * NilClass
19
+ * Numeric
20
+ * String
21
+ * Array
22
+ * Hash
23
+
24
+ A HashParser::BadHash exception is thrown if unserializable values are present.
25
+
26
+ == FEATURES/PROBLEMS:
27
+
28
+ * Any potential security issues?
29
+
30
+ == INSTALL:
31
+
32
+ * Add to Gemfile: `gem 'hash_parser'`
33
+
34
+ == DEVELOPERS:
35
+
36
+ require 'hash_parser'
37
+
38
+ # This successfully parses the hash
39
+ a = "{ :key_a => { :key_1a => 'value_1a', :key_2a => 'value_2a' },
40
+ :key_b => { :key_1b => 'value_1b' } }"
41
+ p HashParser.new.safe_load(a)
42
+
43
+ # This throws a HashParser::BadHash exception
44
+ a = "{ :key_a => system('ls') }"
45
+ p HashParser.new.safe_load(a)
46
+
47
+ == TODO:
48
+
49
+ * Allow objects of certain types to be deserialized
50
+
51
+ == LICENSE:
52
+
53
+ (The MIT License)
54
+
55
+ Copyright (c) 2017 FIX
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ 'Software'), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
72
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
73
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
74
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'hoe'
4
+
5
+ Hoe.spec 'hash_parser' do
6
+ developer 'Bibek Shrestha', 'bibekshrestha@gmail.com'
7
+
8
+ license "MIT"
9
+
10
+ dependency "ruby_parser", "~> 3.8.4"
11
+ end
12
+
13
+ # vim: syntax=ruby
@@ -0,0 +1,30 @@
1
+ require "ruby_parser"
2
+
3
+ # Whiltelist based hash string parser
4
+ class HashParser
5
+ VERSION = "0.0.2"
6
+
7
+ # a literal is strings, regex, numeric
8
+ # https://github.com/seattlerb/ruby_parser/blob/master/lib/ruby19_parser.y#L890
9
+ ALLOWED_CLASSES = [ :true, :false, :nil, :lit, :str, :array, :hash ].freeze
10
+
11
+ BadHash = Class.new(StandardError)
12
+
13
+ def safe_load(string)
14
+ raise BadHash, "#{ string } is a bad hash" unless safe?(string)
15
+ eval(string)
16
+ end
17
+
18
+ private
19
+
20
+ def safe?(string)
21
+ expression = RubyParser.new.parse(string)
22
+ return false unless expression.head == :hash # root has to be a hash
23
+
24
+ # can be optimized to do an ACTUAL_CLASSES - ALLOWED_CLASSES == []
25
+ expression.deep_each.all? do |child|
26
+ ALLOWED_CLASSES.include?(child.head)
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,83 @@
1
+ require "minitest/autorun"
2
+ require "minitest/spec"
3
+ require "minitest/pride"
4
+ require "hash_parser"
5
+
6
+ describe HashParser do
7
+ before do
8
+ @parser = HashParser.new
9
+ end
10
+
11
+ describe "#safe_load" do
12
+ it "fails if it is not a hash" do
13
+ strs = ["[]", "Book.delete_all", "puts 'hello'", '"#{puts 123}"', "system('ls /')"]
14
+ strs.each do |bad_str|
15
+ assert_raises HashParser::BadHash, "#{ bad_str } should not be safe" do
16
+ @parser.safe_load(bad_str)
17
+ end
18
+ end
19
+ end
20
+
21
+ it "fails if there are more than one expression, it fails" do
22
+ strs = ["{ :a => 1 }; 'hello'", "{ :a => 1 }\n{ :b => 2 }"]
23
+ strs.each do |bad_str|
24
+ assert_raises HashParser::BadHash, "#{ bad_str } should not be safe" do
25
+ @parser.safe_load(bad_str)
26
+ end
27
+ end
28
+ end
29
+
30
+ it "Does not define or redefine any methods" do
31
+ str = '{ :a => refine(BadHash) { def safe?; "HAHAHA"; end } }'
32
+ assert_raises HashParser::BadHash, "#{ str } should not be safe" do
33
+ @parser.safe_load(str)
34
+ end
35
+
36
+ str = '{ :a => def HashParser.safe?; "HAHAHA"; end }'
37
+ assert_raises HashParser::BadHash, "#{ str } should not be safe" do
38
+ @parser.safe_load(str)
39
+ end
40
+ end
41
+
42
+ it "fails if it has assignment" do
43
+ strs = ['{ :a => (HashParser::TEST_CONSTANT = 1) }',
44
+ '{ :a => (hello_world = 1) }']
45
+ strs.each do |str|
46
+ assert_raises HashParser::BadHash, "#{ str } should not be safe" do
47
+ @parser.safe_load(str)
48
+ end
49
+ end
50
+ end
51
+
52
+ it "fails if a hash has a method call" do
53
+ strs = ["{ :a => 2 * 2 }",
54
+ "{ :a => SOME_CONST }",
55
+ "{ :a => system('ls /') }",
56
+ "{ :a => Book.delete_all }",
57
+ '{ :a => "#{500}" }',
58
+ '{ :a => "#{ Book.delete_all }" }',
59
+ '{ :a => refine(BadHash) { def safe?; "HAHAHA"; end } }',
60
+ ]
61
+ strs.each do |bad_str|
62
+ assert_raises HashParser::BadHash, "#{ bad_str } should not be safe" do
63
+ @parser.safe_load(bad_str)
64
+ end
65
+ end
66
+ end
67
+
68
+ it "passes for hashes" do
69
+ strs = ["{}", '{ "a" => "A" }', '{ :a => 123 }', '{ :a => true, :b => false, :c => true, "d" => nil }']
70
+ parsed = [{}, { "a" => "A" }, { a: 123 }, { a: true, b: false, c: true, "d" => nil }]
71
+ strs.each.with_index do |good_str, i|
72
+ assert_equal parsed[i], @parser.safe_load(good_str), "#{ good_str } should be safe"
73
+ end
74
+ end
75
+
76
+ it "passes for hashes with sub hashes" do
77
+ str = '{ :a => [1, 2, { "x" => "y" }] }'
78
+ parsed = { a: [1, 2, { "x" => "y" }] }
79
+ assert_equal parsed, @parser.safe_load(str)
80
+ end
81
+ end
82
+ end
83
+
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bibek Shrestha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby_parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.8.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.8.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hoe
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.16'
55
+ description: |-
56
+ Parses a hash string of the format `'{ :a => "something" }'` into an actual ruby hash object `{ a: "something" }`.
57
+ This is useful when you by mistake serialize hashes and save it in database column or a text file and you want to
58
+ convert them back to hashes without the security issues of executing `eval(hash_string)`.
59
+
60
+ By default only following classes are allowed to be deserialized:
61
+
62
+ * TrueClass
63
+ * FalseClass
64
+ * NilClass
65
+ * Numeric
66
+ * String
67
+ * Array
68
+ * Hash
69
+
70
+ A HashParser::BadHash exception is thrown if unserializable values are present.
71
+ email:
72
+ - bibekshrestha@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files:
76
+ - History.txt
77
+ - Manifest.txt
78
+ - README.txt
79
+ files:
80
+ - History.txt
81
+ - Manifest.txt
82
+ - README.txt
83
+ - Rakefile
84
+ - lib/hash_parser.rb
85
+ - test/test_hash_parser.rb
86
+ homepage: https://github.com/bibstha/ruby_hash_parser
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options:
92
+ - "--main"
93
+ - README.txt
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.10
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: 'Parses a hash string of the format `''{ :a => "something" }''` into an actual
112
+ ruby hash object `{ a: "something" }`'
113
+ test_files: []