diff --git a/lib/livebook_cli/server.ex b/lib/livebook_cli/server.ex index ba8a26a84..a830dcc80 100644 --- a/lib/livebook_cli/server.ex +++ b/lib/livebook_cli/server.ex @@ -158,6 +158,7 @@ defmodule LivebookCLI.Server do defp open_from_args(base_url, [url_or_file_or_dir]) do url = URI.parse(url_or_file_or_dir) + path = Path.expand(url_or_file_or_dir) cond do url.scheme in ~w(http https file) -> @@ -165,15 +166,15 @@ defmodule LivebookCLI.Server do |> Livebook.Utils.notebook_import_url(url_or_file_or_dir) |> Livebook.Utils.browser_open() - File.regular?(url_or_file_or_dir) -> + File.regular?(path) -> base_url |> append_path("open") - |> update_query(%{"path" => url_or_file_or_dir}) + |> update_query(%{"path" => path}) |> Livebook.Utils.browser_open() - File.dir?(url_or_file_or_dir) -> + File.dir?(path) -> base_url - |> update_query(%{"path" => url_or_file_or_dir}) + |> update_query(%{"path" => path}) |> Livebook.Utils.browser_open() true -> diff --git a/lib/livebook_web/plugs/auth_plug.ex b/lib/livebook_web/plugs/auth_plug.ex index ccaaae81a..55a662724 100644 --- a/lib/livebook_web/plugs/auth_plug.ex +++ b/lib/livebook_web/plugs/auth_plug.ex @@ -61,19 +61,22 @@ defmodule LivebookWeb.AuthPlug do end defp authenticate(conn, :token) do - token = Map.get(conn.query_params, "token") + {token, query_params} = Map.pop(conn.query_params, "token") if is_binary(token) and Plug.Crypto.secure_compare(hash(token), expected(:token)) do # Redirect to the same path without query params conn |> store(:token, token) - |> redirect(to: conn.request_path) + |> redirect(to: path_with_query(conn.request_path, query_params)) |> halt() else raise LivebookWeb.InvalidTokenError end end + defp path_with_query(path, params) when params == %{}, do: path + defp path_with_query(path, params), do: path <> "?" <> URI.encode_query(params) + defp key(port, mode), do: "#{port}:#{mode}" defp expected(mode), do: hash(Application.fetch_env!(:livebook, mode)) defp hash(value), do: :crypto.hash(:sha256, value)