Skip to content
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

Use more secure methods #3291

Merged
merged 2 commits into from Mar 12, 2021
Merged

Use more secure methods #3291

merged 2 commits into from Mar 12, 2021

Conversation

cosmo0920
Copy link
Contributor

Which issue(s) this PR fixes:
None

What this PR does / why we need it:
DeepSource complains that fluentd uses insecure methods:

Docs Changes:
No need.

Release Note:

Same as title.

@cosmo0920 cosmo0920 requested review from ashie and kenhys March 12, 2021 01:37
@cosmo0920 cosmo0920 changed the title Use more strict methods Use more secure methods Mar 12, 2021
@@ -186,7 +186,7 @@ def self.hash_value(val, opts = {}, name = nil)
return nil if val.nil?

param = if val.is_a?(String)
val.start_with?('{') ? JSON.load(val) : Hash[val.strip.split(/\s*,\s*/).map{|v| v.split(':', 2)}]
val.start_with?('{') ? JSON.parse(val) : Hash[val.strip.split(/\s*,\s*/).map{|v| v.split(':', 2)}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memo:
JSON.load wraps JSON.parse and it modifies some default options of JSON::Parser:
https://github.com/flori/json/blob/7b452b290502a5cca8fc6403f31275c83e0e3d48/lib/json/common.rb#L569
https://github.com/flori/json/blob/7b452b290502a5cca8fc6403f31275c83e0e3d48/lib/json/common.rb#L422
https://docs.ruby-lang.org/ja/latest/class/JSON=3a=3aParser.html
So that the behavior is also changed a bit but I believe it's no problem on this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. Yeah, in this case, using JSON.parse instead of JSON.load does not cause issues.

Copy link
Contributor

@kenhys kenhys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a reference to why it should be fixed in the commit message?
Then LGTM.

@kenhys
Copy link
Contributor

kenhys commented Mar 12, 2021

I doubt whether https://deepsource.io/gh/cosmo0920/fluentd/issue/RB-SC1002 is consistent or not.

How about quoting the following:

`Kernel#open` and `URI.open` enable not only file access but also process invocation by prefixing a pipe symbol (e.g., `open(“| ls”)`). So, it may lead to a serious security risk by using variable input to the argument of `Kernel#open` and `URI.open`. It would be better to use `File.open`, `IO.popen` or `URI.parse#open` explicitly.
Autocorrect is disabled by default because it's potentially dangerous. If using a stream, like `JSON.load(open('file'))`, it will need to call `#read` manually, like `JSON.parse(open('file').read)`. If reading single values (rather than proper JSON objects), like `JSON.load('false')`, it will need to pass the `quirks_mode: true` option, like `JSON.parse('false', quirks_mode: true)`. Other similar issues may apply.

* https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Security/Open

```
`Kernel#open` and `URI.open` enable not only file access but also process invocation
by prefixing a pipe symbol (e.g., `open(“| ls”)`).
So, it may lead to a serious security risk by using variable input
to the argument of `Kernel#open` and `URI.open`.
It would be better to use `File.open`, `IO.popen` or `URI.parse#open` explicitly.
```

Signed-off-by: Hiroshi Hatake <hatake@clear-code.com>
* https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Security/JSONLoad

```
Autocorrect is disabled by default because it's potentially dangerous.
If using a stream, like `JSON.load(open('file'))`, it will need to call `#read` manually,
like `JSON.parse(open('file').read)`.
If reading single values (rather than proper JSON objects), like `JSON.load('false')`,
it will need to pass the `quirks_mode: true` option, like `JSON.parse('false', quirks_mode: true)`.
Other similar issues may apply.
```

Signed-off-by: Hiroshi Hatake <hatake@clear-code.com>
Copy link
Contributor

@kenhys kenhys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kenhys kenhys merged commit 4752cc1 into master Mar 12, 2021
@ashie ashie deleted the use-more-strict-methods branch March 12, 2021 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants