Skip to content

Commit

Permalink
FileSystem driver has new allow_relative_home param
Browse files Browse the repository at this point in the history
If set to true, then `home` can be a relative path (relative to current working dir).

Relative path needed for convenience in dummy test app test setup. Added new param to make it opt-in, to make sure it's completely backwards compat and does not introduce any potential security issues -- you need to opt in to new supported behavior with new allow_relative_home param.
  • Loading branch information
jrochkind committed Jun 16, 2022
1 parent d7db9f6 commit b86417d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ AllCops:
- 'spec/dummy_test_app/**/*'
- 'bin/rails'

Style/GuardClause:
Enabled: false

StyleI/IfUnlessModifier:
Enabled: false

Bundler/DuplicatedGem:
Exclude:
- 'Gemfile'
Expand Down
17 changes: 15 additions & 2 deletions lib/browse_everything/driver/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ def icon

def validate_config
raise BrowseEverything::InitializationError, 'FileSystem driver requires a :home argument' if config[:home].blank?

unless config[:home].start_with?("/") || config[:allow_relative_home] == true
raise BrowseEverything::InitializationError, 'FileSystem driver :home argument must be absolute unless :allow_relative_home is set'
end
end

# Retrieve the contents of a directory
# @param path [String] the path to a file system resource
# @return [Array<BrowseEverything::FileEntry>]
def contents(path = '')
real_path = File.join(config[:home], path)
real_path = File.join(home_path, path)
values = if File.directory?(real_path)
make_directory_entry real_path
else
Expand Down Expand Up @@ -55,6 +59,15 @@ def details(path, display = File.basename(path))

private

def home_path
@home_path ||= if config[:allow_relative_home] == true
# expand relative to current working directory
File.expand_path(config[:home])
else
config[:home]
end
end

# Construct an array of FileEntry objects for the contents of a
# directory
# @param real_path [String] path to the file system directory
Expand All @@ -65,7 +78,7 @@ def make_directory_entry(real_path)
end

def make_pathname(path)
Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(config[:home]))
Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(home_path))
end

def file_size(path)
Expand Down
3 changes: 2 additions & 1 deletion spec/dummy_test_app/config/browse_everything_providers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# The file_system provider can be a path to any directory on the server where your application is running.
#
file_system:
home: /Users/jrochkind/code/browse-everything/.internal_test_app
allow_relative_home: true
home: ./
# dropbox:
# client_id: YOUR_DROPBOX_APP_KEY
# client_secret: YOUR_DROPBOX_APP_SECRET
Expand Down

0 comments on commit b86417d

Please sign in to comment.