Skip to content

Commit 18a6f5a

Browse files
authored
[Ruby] force users to specify the temp folder path to address security concerns (#8730)
* address security issue when downloading files in the ruby client * update samples * fix double quote
1 parent 095019a commit 18a6f5a

7 files changed

Lines changed: 60 additions & 3 deletions

File tree

modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ module {{moduleName}}
7171
{{/isFaraday}}
7272
{{#isFaraday}}
7373
if return_type == 'File'
74+
# throw an exception if the temp folder path is not defined
75+
# to avoid using the default temp directory which can be read by anyone
76+
if @config.temp_folder_path.nil?
77+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"]) " +
78+
"to avoid dowloading the file to a location readable by everyone."
79+
end
80+
7481
content_disposition = response.headers['Content-Disposition']
7582
if content_disposition && content_disposition =~ /filename=/i
7683
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]

modules/openapi-generator/src/main/resources/ruby-client/api_client_typhoeus_partial.mustache

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252

5353
{{#hasAuthMethods}}
5454
update_params_for_auth! header_params, query_params, opts[:auth_names]
55-
{{/hasAuthMethods}}
5655

56+
{{/hasAuthMethods}}
5757
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
5858
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
5959

@@ -122,6 +122,13 @@
122122
#
123123
# @see Configuration#temp_folder_path
124124
def download_file(request)
125+
# throw an exception if the temp folder path is not defined
126+
# to avoid using the default temp directory which can be read by anyone
127+
if @config.temp_folder_path.nil?
128+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"])" +
129+
"to avoid dowloading the file to a location readable by everyone."
130+
end
131+
125132
tempfile = nil
126133
encoding = nil
127134
request.on_headers do |response|
@@ -137,10 +144,12 @@
137144
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
138145
@tempfile = tempfile
139146
end
147+
140148
request.on_body do |chunk|
141149
chunk.force_encoding(encoding)
142150
tempfile.write(chunk)
143151
end
152+
144153
request.on_complete do |response|
145154
if tempfile
146155
tempfile.close

samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ def deserialize(response, return_type)
203203
# handle file downloading - return the File instance processed in request callbacks
204204
# note that response body is empty when the file is written in chunks in request on_body callback
205205
if return_type == 'File'
206+
# throw an exception if the temp folder path is not defined
207+
# to avoid using the default temp directory which can be read by anyone
208+
if @config.temp_folder_path.nil?
209+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"]) " +
210+
"to avoid dowloading the file to a location readable by everyone."
211+
end
212+
206213
content_disposition = response.headers['Content-Disposition']
207214
if content_disposition && content_disposition =~ /filename=/i
208215
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]

samples/client/petstore/ruby/lib/petstore/api_client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def build_request_body(header_params, form_params, body)
164164
#
165165
# @see Configuration#temp_folder_path
166166
def download_file(request)
167+
# throw an exception if the temp folder path is not defined
168+
# to avoid using the default temp directory which can be read by anyone
169+
if @config.temp_folder_path.nil?
170+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"])" +
171+
"to avoid dowloading the file to a location readable by everyone."
172+
end
173+
167174
tempfile = nil
168175
encoding = nil
169176
request.on_headers do |response|
@@ -179,10 +186,12 @@ def download_file(request)
179186
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
180187
@tempfile = tempfile
181188
end
189+
182190
request.on_body do |chunk|
183191
chunk.force_encoding(encoding)
184192
tempfile.write(chunk)
185193
end
194+
186195
request.on_complete do |response|
187196
if tempfile
188197
tempfile.close

samples/openapi3/client/extensions/x-auth-id-alias/ruby-client/lib/x_auth_id_alias/api_client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def build_request_body(header_params, form_params, body)
164164
#
165165
# @see Configuration#temp_folder_path
166166
def download_file(request)
167+
# throw an exception if the temp folder path is not defined
168+
# to avoid using the default temp directory which can be read by anyone
169+
if @config.temp_folder_path.nil?
170+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"])" +
171+
"to avoid dowloading the file to a location readable by everyone."
172+
end
173+
167174
tempfile = nil
168175
encoding = nil
169176
request.on_headers do |response|
@@ -179,10 +186,12 @@ def download_file(request)
179186
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
180187
@tempfile = tempfile
181188
end
189+
182190
request.on_body do |chunk|
183191
chunk.force_encoding(encoding)
184192
tempfile.write(chunk)
185193
end
194+
186195
request.on_complete do |response|
187196
if tempfile
188197
tempfile.close

samples/openapi3/client/features/dynamic-servers/ruby/lib/dynamic_servers/api_client.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def build_request(http_method, path, opts = {})
9494
query_params = opts[:query_params] || {}
9595
form_params = opts[:form_params] || {}
9696

97-
9897
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
9998
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
10099

@@ -163,6 +162,13 @@ def build_request_body(header_params, form_params, body)
163162
#
164163
# @see Configuration#temp_folder_path
165164
def download_file(request)
165+
# throw an exception if the temp folder path is not defined
166+
# to avoid using the default temp directory which can be read by anyone
167+
if @config.temp_folder_path.nil?
168+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"])" +
169+
"to avoid dowloading the file to a location readable by everyone."
170+
end
171+
166172
tempfile = nil
167173
encoding = nil
168174
request.on_headers do |response|
@@ -178,10 +184,12 @@ def download_file(request)
178184
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
179185
@tempfile = tempfile
180186
end
187+
181188
request.on_body do |chunk|
182189
chunk.force_encoding(encoding)
183190
tempfile.write(chunk)
184191
end
192+
185193
request.on_complete do |response|
186194
if tempfile
187195
tempfile.close

samples/openapi3/client/features/generate-alias-as-model/ruby-client/lib/petstore/api_client.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def build_request(http_method, path, opts = {})
9494
query_params = opts[:query_params] || {}
9595
form_params = opts[:form_params] || {}
9696

97-
9897
# set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
9998
_verify_ssl_host = @config.verify_ssl_host ? 2 : 0
10099

@@ -163,6 +162,13 @@ def build_request_body(header_params, form_params, body)
163162
#
164163
# @see Configuration#temp_folder_path
165164
def download_file(request)
165+
# throw an exception if the temp folder path is not defined
166+
# to avoid using the default temp directory which can be read by anyone
167+
if @config.temp_folder_path.nil?
168+
raise "@config.temp_folder_path must be setup first (e.g. ENV[\"HOME\"], ENV[\"HOMEPATH\"])" +
169+
"to avoid dowloading the file to a location readable by everyone."
170+
end
171+
166172
tempfile = nil
167173
encoding = nil
168174
request.on_headers do |response|
@@ -178,10 +184,12 @@ def download_file(request)
178184
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
179185
@tempfile = tempfile
180186
end
187+
181188
request.on_body do |chunk|
182189
chunk.force_encoding(encoding)
183190
tempfile.write(chunk)
184191
end
192+
185193
request.on_complete do |response|
186194
if tempfile
187195
tempfile.close

0 commit comments

Comments
 (0)