Skip to content

Instantly share code, notes, and snippets.

@chuckd
Created May 7, 2014 04:44
Show Gist options
  • Save chuckd/0c51715e4c287156aeac to your computer and use it in GitHub Desktop.
Save chuckd/0c51715e4c287156aeac to your computer and use it in GitHub Desktop.
Using contracts.Ruby to avoid file access vulnerabilities
Contract String => PathUnder["/allowed/path"]
def safe_path(path)
"/allowed/path" + path
end
class PathUnder < CallableClass
def initialize(parent)
@parent = parent
end
def valid?(path)
File.expand_path(path) =~ /^#{@parent}/
end
def to_s
"path to be under #{@parent}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment