From 3bc219e5d89a42719b829656138f9143243396e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 21 Feb 2022 13:18:17 +0100 Subject: [PATCH] Bring path verification and add normalization (#1016) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Bring path verification and add normalization * Normalize user home Co-authored-by: Jonatan Kłosko --- lib/livebook/config.ex | 4 +++- lib/livebook/file_system/file.ex | 8 +++++++- test/livebook/file_system/file_test.exs | 16 ++++++++++------ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index ef6b0ed21..84b7bcbd9 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -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. """ diff --git a/lib/livebook/file_system/file.ex b/lib/livebook/file_system/file.ex index 0be8f9b55..cfc270339 100644 --- a/lib/livebook/file_system/file.ex +++ b/lib/livebook/file_system/file.ex @@ -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 diff --git a/test/livebook/file_system/file_test.exs b/test/livebook/file_system/file_test.exs index 7e8ad71e1..4da94e0b5 100644 --- a/test/livebook/file_system/file_test.exs +++ b/test/livebook/file_system/file_test.exs @@ -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