Fix json_marshaller
Universal Deserialisation Gadget of user-controlled data
#3183
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
newrelic-ruby-agent/lib/new_relic/agent/new_relic_service/json_marshaller.rb
Lines 45 to 46 in a19f26d
To fix the issue, replace the unsafe
JSON.load
method with the saferJSON.parse
method. TheJSON.parse
method does not allow the deserialization of arbitrary objects, making it a secure alternative for handling untrusted JSON data. Additionally, ensure that the input data is validated or sanitized before parsing.The changes will be made in the
load
method of theJsonMarshaller
class inlib/new_relic/agent/new_relic_service/json_marshaller.rb
. Specifically:JSON.load(data)
withJSON.parse(data)
.data
is a valid JSON string before parsing.Deserializing untrusted data using any method that allows the construction of arbitrary objects is easily exploitable and, in many cases, allows an attacker to execute arbitrary code.
POC
The following calls the
Marshal.load
,JSON.load
,YAML.load
,Oj.load
andOx.parse_obj
methods on data from an HTTP request. Since these methods are capable of deserializing to arbitrary objects, this is inherently unsafe.Using
JSON.parse
andYAML.safe_load
instead, as in the following example, removes the vulnerability. Similarly, callingOj.load
with any mode other than:object
is safe, as is callingOj.safe_load
. Note that there is no safe way to deserialize untrusted data usingMarshal
.References
Overview
Describe the changes present in the pull request
Submitter Checklist:
Testing
The agent includes a suite of unit and functional tests which should be used to
verify your changes don't break existing functionality. These tests will run with
GitHub Actions when a pull request is made. More details on running the tests locally can be found
here for our unit tests,
and here for our functional tests.
For most contributions it is strongly recommended to add additional tests which
exercise your changes.
Reviewer Checklist