Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Created March 27, 2013 19:27
Show Gist options
  • Save sstelfox/5257265 to your computer and use it in GitHub Desktop.
Save sstelfox/5257265 to your computer and use it in GitHub Desktop.
Safely parse JSON in ruby.
require 'json'
unsafe_json = <<-EOS
{
"json_class": "DangerousClass",
"command": "sudo rm -rf /"
}
EOS
# OH GOD WHERE DID MY SYSTEM GO!?
JSON.parse(unsafe_json)
# Lets make this safe...
JSON.create_id = nil
# Nothing bad happens ^_^ just got a hash...
JSON.parse(unsafe_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment