Skip to content

Commit 5f95d70

Browse files
kenhysWatson1978
andauthored
Backport(v1.16): parser: use URI#open instead of URI.open (#4848) (#4922)
**Which issue(s) this PR fixes**: Backport #4848 Fixes # **What this PR does / why we need it**: By [CodeQL documentation](https://codeql.github.com/codeql-query-help/ruby/rb-non-constant-kernel-open/), it is safer to avoid using `URI.open`. **Docs Changes**: **Release Note**: Signed-off-by: Shizuo Fujita <[email protected]> Signed-off-by: Kentaro Hayashi <[email protected]> Co-authored-by: Shizuo Fujita <[email protected]>
1 parent 47b126b commit 5f95d70

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/fluent/config/parser.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,23 @@ def process_include(attrs, elems, uri, allow_include = true)
9292
else
9393
basepath = '/'
9494
fname = path
95-
require 'open-uri'
96-
URI.open(uri) {|f|
95+
parser_proc = ->(f) {
9796
Parser.new(basepath, f.each_line, fname).parse!(allow_include, nil, attrs, elems)
9897
}
98+
99+
case u.scheme
100+
when 'http', 'https', 'ftp'
101+
# URI#open can be able to handle URIs for http, https and ftp.
102+
require 'open-uri'
103+
u.open(&parser_proc)
104+
else
105+
# TODO: This case should be handled in the previous if condition. Glob is not applied to some Windows path formats.
106+
# 'c:/path/to/file' will be passed as URI, 'uri' and 'u.path' will be:
107+
# - uri is 'c:/path/to/file'
108+
# - u.path is '/path/to/file' and u.scheme is 'c'
109+
# Therefore, the condition of the if statement above is not met and it is handled here.
110+
File.open(u.path, &parser_proc)
111+
end
99112
end
100113

101114
rescue SystemCallError => e
@@ -104,4 +117,3 @@ def process_include(attrs, elems, uri, allow_include = true)
104117
end
105118
end
106119
end
107-

0 commit comments

Comments
 (0)