-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Security/JSONLoad autocorrect potential runtime error #3521
Comments
Or just disable it by default. After all the auto-correct is not reliable, because the cop is not reliable. I'd also extend the cop's description a bit. |
I disagree. The cop's position that Forcing the author to change their code to either |
Fair enough. Let's agree that the solution is to extend the cop's description and disable its auto-correct by default. |
Just to clarify this, you're talking about removing the I'll try to put in a PR for this within the next week though. |
All cops have an |
Right, think I've got it, the |
…ult. The autocorrect on this cop is dangerous for at least two reasons depending on the value being passed to the `JSON` class method, so this disabled it by default. One known reason is where we pass in a stream such as `JSON.load(open('file'))`, this cannot be swapped out for `JSON.parse` without calling `#read` on the stream. Another known reason is where the JSON string is a single value, rather than a full JSON object, such as `JSON.load('false')`, this cannot be swapped out for `JSON.parse` without adding the `quirks_mode: true` option. Also slightly improve the description in the cop and `enabled.yml`. Also fix the offence message.
…ult (rubocop#3584) The autocorrect on this cop is dangerous for at least two reasons depending on the value being passed to the `JSON` class method, so this disabled it by default. One known reason is where we pass in a stream such as `JSON.load(open('file'))`, this cannot be swapped out for `JSON.parse` without calling `#read` on the stream. Another known reason is where the JSON string is a single value, rather than a full JSON object, such as `JSON.load('false')`, this cannot be swapped out for `JSON.parse` without adding the `quirks_mode: true` option. Also slightly improve the description in the cop and `enabled.yml`. Also fix the offence message.
The autocorrect on Security/JSONLoad will replace
JSON.load(request)
withJSON.parse(request)
. One difference between these two methods is thatJSON.load
works with any object that responds to the#read
method, such as an OpenURI HTTP request or a file handler. Here's an example:This will crash if replaced with
JSON.parse
.The above is the exact issue I experienced, however I think there are other potential issues with this autocorrect. For example, consider the following difference between
JSON.load
andJSON.parse
:JSON.load
allows primitives to be parsed, without requiring them to be in an actual JSON object.JSON.parse
requires the:quirks_mode
option to be true for this to work. So this type of use could also cause runtime errors in applications.I think that the solution is to remove autocorrect for this cop.
RuboCop version
The text was updated successfully, but these errors were encountered: