Skip to content

Commit

Permalink
Merge pull request #50 from dtao/safe-yaml-integration
Browse files Browse the repository at this point in the history
updated Crack::JSON.parse to actually use SafeYAML
  • Loading branch information
greatuserongithub committed Jan 24, 2014
2 parents 59bd9e4 + 03a29e7 commit 762dd6c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/crack/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'yaml'
require 'safe_yaml/load'
require 'strscan'

module Crack
Expand All @@ -23,7 +23,12 @@ def self.parser_exceptions
end

def self.parse(json)
YAML.load(unescape(convert_json_to_yaml(json)))
args = [unescape(convert_json_to_yaml(json))]
args << nil if SafeYAML::MULTI_ARGUMENT_YAML_LOAD
args << { :whitelisted_tags => ['!ruby/regexp'] }

SafeYAML.load(*args)

rescue *parser_exceptions
raise ParseError, "Invalid JSON string"
end
Expand Down
5 changes: 5 additions & 0 deletions test/json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
end
end

it "is not vulnerable to YAML deserialization exploits" do
class Foo; end
refute_instance_of(Foo, Crack::JSON.parse("# '---/\n--- !ruby/object:Foo\n foo: bar"))
end

it "raise error for failed decoding" do
assert_raises(Crack::ParseError) {
Crack::JSON.parse(%({: 1}))
Expand Down

0 comments on commit 762dd6c

Please sign in to comment.