-
Notifications
You must be signed in to change notification settings - Fork 47
Check file path prefixes #1844
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
Check file path prefixes #1844
Changes from all commits
e3b052f
ab58a84
0e425c4
7981ef0
5435330
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -380,6 +380,7 @@ def self.zip_target(target_name, zip, scope:) | |
|
||
def self.put_target_file(path, io_or_string, scope:) | ||
full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{path}" | ||
return unless File.expand_path(full_folder_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
FileUtils.mkdir_p(File.dirname(full_folder_path)) | ||
File.open(full_folder_path, 'wb') do |file| | ||
if String === io_or_string | ||
|
@@ -393,7 +394,10 @@ def self.put_target_file(path, io_or_string, scope:) | |
|
||
def self.open_local_file(path, scope:) | ||
full_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/targets_modified/#{path}" | ||
return File.open(full_path, 'rb') | ||
if File.expand_path(full_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
return File.open(full_path, 'rb') | ||
end | ||
nil | ||
rescue Errno::ENOENT | ||
nil | ||
end | ||
|
@@ -446,14 +450,17 @@ def self.sync_tool_config() | |
def self.save_tool_config(scope, tool, name, data) | ||
json = JSON.parse(data, :allow_nan => true, :create_additions => true) | ||
config_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/tool_config/#{tool}/#{name}.json" | ||
return unless File.expand_path(config_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're building this so isn't it always going to start with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also we should implement these same checks in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, this is better protection against traversing up the file tree. Basic attacks will be caught by this, but calling Ah I see, I missed part of local_mode.py, I'll update that... Is there a reason most of that file is commented out? Can we remove the large blocks of commented out code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anything that's part of the plugin installation process can be removed because that's Ruby only. So yeah you can remove it all. |
||
FileUtils.mkdir_p(File.dirname(config_path)) | ||
File.open(config_path, 'w') do |file| | ||
file.write(JSON.pretty_generate(json, :allow_nan => true)) | ||
end | ||
end | ||
|
||
def self.delete_tool_config(scope, tool, name) | ||
FileUtils.rm_f("#{OPENC3_LOCAL_MODE_PATH}/#{scope}/tool_config/#{tool}/#{name}.json") | ||
config_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/tool_config/#{tool}/#{name}.json" | ||
return unless File.expand_path(config_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
FileUtils.rm_f(config_path) | ||
end | ||
|
||
def self.sync_settings() | ||
|
@@ -471,6 +478,7 @@ def self.sync_settings() | |
|
||
def self.save_setting(scope, name, data) | ||
config_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/settings/#{name}.json" | ||
return unless File.expand_path(config_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
FileUtils.mkdir_p(File.dirname(config_path)) | ||
# Anything can be stored as a setting so write it out directly | ||
File.write(config_path, data) | ||
|
@@ -480,19 +488,22 @@ def self.save_setting(scope, name, data) | |
|
||
def self.sync_remote_to_local(bucket, key) | ||
local_path = "#{OPENC3_LOCAL_MODE_PATH}/#{key}" | ||
return unless File.expand_path(local_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
FileUtils.mkdir_p(File.dirname(local_path)) | ||
bucket.get_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, path: local_path) | ||
end | ||
|
||
def self.sync_local_to_remote(bucket, key) | ||
local_path = "#{OPENC3_LOCAL_MODE_PATH}/#{key}" | ||
return unless File.expand_path(local_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
File.open(local_path, 'rb') do |read_file| | ||
bucket.put_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, body: read_file) | ||
end | ||
end | ||
|
||
def self.delete_local(key) | ||
local_path = "#{OPENC3_LOCAL_MODE_PATH}/#{key}" | ||
return unless File.expand_path(local_path).start_with?(OPENC3_LOCAL_MODE_PATH) | ||
File.delete(local_path) if File.exist?(local_path) | ||
nil | ||
end | ||
|
Uh oh!
There was an error while loading. Please reload this page.