Bring path verification and add normalization (#1016)

* Bring path verification and add normalization

* Normalize user home

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
José Valim 2022-02-21 13:18:17 +01:00 committed by GitHub
parent e71f5b8fa6
commit 3bc219e5d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View file

@ -55,9 +55,11 @@ defmodule Livebook.Config do
"""
@spec home() :: String.t()
def home do
Application.get_env(:livebook, :home) || System.user_home() || File.cwd!()
Application.get_env(:livebook, :home) || user_home() || File.cwd!()
end
defp user_home(), do: System.user_home() |> Path.expand()
@doc """
Returns the configuration path.
"""

View file

@ -29,7 +29,13 @@ defmodule Livebook.FileSystem.File do
path =
if path do
FileSystem.resolve_path(file_system, default_path, path)
resolved_path = FileSystem.resolve_path(file_system, default_path, path)
unless path == resolved_path do
raise ArgumentError, "expected an expanded absolute path, got: #{inspect(path)}"
end
path
else
default_path
end

View file

@ -6,18 +6,22 @@ defmodule Livebook.FileSystem.FileTest do
alias Livebook.FileSystem
describe "new/2" do
test "resolves relative paths" do
test "raises an error when a relative path is given" do
file_system = FileSystem.Local.new()
assert FileSystem.File.new(file_system, "file.txt").path ==
Path.join(File.cwd!(), "file.txt")
assert_raise ArgumentError, ~s{expected an expanded absolute path, got: "file.txt"}, fn ->
FileSystem.File.new(file_system, "file.txt")
end
end
test "resolves unexpanded paths" do
test "raises an error when a unexpanded path is given" do
file_system = FileSystem.Local.new()
assert FileSystem.File.new(file_system, "/dir/nested/../file.txt").path ==
Path.expand("/dir/file.txt")
assert_raise ArgumentError,
~s{expected an expanded absolute path, got: "/dir/nested/../file.txt"},
fn ->
FileSystem.File.new(file_system, "/dir/nested/../file.txt")
end
end
test "uses default file system path if non is given" do