diff --git a/lib/crack/json.rb b/lib/crack/json.rb index 5171c56..7bb890c 100644 --- a/lib/crack/json.rb +++ b/lib/crack/json.rb @@ -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 @@ -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 diff --git a/test/json_test.rb b/test/json_test.rb index d500c9c..08de1f7 100644 --- a/test/json_test.rb +++ b/test/json_test.rb @@ -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}))