Skip to content

Commit

Permalink
Merge pull request #2787 from manyfold3d/fix-library-path-detect
Browse files Browse the repository at this point in the history
Fix library path safety check
  • Loading branch information
Floppy committed Sep 25, 2024
2 parents a3aa685 + 52d22dd commit 754df23
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
31 changes: 17 additions & 14 deletions app/validators/safe_path_validator.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
class SafePathValidator < ActiveModel::EachValidator
UNSAFE = [
"/bin",
"/boot",
"/dev",
"/etc",
"/lib",
"/lost",
"/proc",
"/root",
"/run",
"/sbin",
"/selinux",
"/srv",
"/usr"
nil,
"bin",
"boot",
"dev",
"etc",
"lib",
"lost",
"proc",
"root",
"run",
"sbin",
"selinux",
"srv",
"usr"
]

def validate_each(record, attribute, value)
record.errors.add attribute, :unsafe if value === "/" || UNSAFE.any? { |x| value&.starts_with?(x) }
return if value.nil?
start = Pathname.new(value).each_filename.to_a.first
record.errors.add attribute, :unsafe if UNSAFE.any?(start)
end
end
6 changes: 6 additions & 0 deletions spec/models/library_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
end
end

it "allows paths that *begin* with a filtered path" do
library = build(:library, path: "/libraries")
library.valid?
expect(library.errors[:path]).not_to include "cannot be a privileged system path"
end

it "disallows root folder" do
library = build(:library, path: "/")
library.valid?
Expand Down

0 comments on commit 754df23

Please sign in to comment.