Skip to content

Use more secure methods#3291

Merged
kenhys merged 2 commits into
masterfrom
use-more-strict-methods
Mar 12, 2021
Merged

Use more secure methods#3291
kenhys merged 2 commits into
masterfrom
use-more-strict-methods

Conversation

@cosmo0920

Copy link
Copy Markdown
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

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
Copy Markdown
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
Copy Markdown
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.

@kenhys kenhys left a comment

Copy link
Copy Markdown
Contributor

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.

@cosmo0920 cosmo0920 force-pushed the use-more-strict-methods branch from 27ed7dd to a6af121 Compare March 12, 2021 03:19
@kenhys

kenhys commented Mar 12, 2021

Copy link
Copy Markdown
Contributor

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>
@cosmo0920 cosmo0920 force-pushed the use-more-strict-methods branch from a6af121 to 77d79e9 Compare March 12, 2021 05:00

@kenhys kenhys left a comment

Copy link
Copy Markdown
Contributor

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.

3 participants