From b946a89415053d5aa0885e5c29f28275938cf2e1 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 4 Nov 2024 15:25:32 +0000 Subject: [PATCH] Correctly open uris containing spaces (#777) * Do not escape uri when sanitizing * Fix dropping possibly escaped and/or quoted uris * Handle ShellError when unquoting * Amend comments --------- Co-authored-by: Jeremy Wootten Co-authored-by: Ryo Nakano --- src/Utils.vala | 3 +-- src/Widgets/TerminalWidget.vala | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Utils.vala b/src/Utils.vala index 0d239923e4..93c9f7e530 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -21,7 +21,6 @@ namespace Terminal.Utils { public string? sanitize_path (string _path, string shell_location, bool add_file_scheme = true) { /* Remove trailing whitespace, ensure scheme, substitute leading "~" and "..", remove extraneous "/" */ string scheme = "", path = ""; - var parts_scheme = _path.split ("://", 2); if (parts_scheme.length == 2) { scheme = parts_scheme[0] + "://"; @@ -63,7 +62,7 @@ namespace Terminal.Utils { parts_sep[index] = construct_parent_path (shell_location); } - var result = escape_uri (scheme + string.joinv (Path.DIR_SEPARATOR_S, parts_sep).replace ("//", "/")); + var result = scheme + string.joinv (Path.DIR_SEPARATOR_S, parts_sep).replace ("//", "/"); return result; } diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index 2b1f15f073..58df443c05 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -776,21 +776,35 @@ namespace Terminal { var uris = selection_data.get_uris (); string path; File file; - for (var i = 0; i < uris.length; i++) { - file = File.new_for_uri (uris[i]); - if ((path = file.get_path ()) != null) { - uris[i] = Shell.quote (path) + " "; + // Get unquoted path as some apps may drop uris that are escaped + // and quoted. + string? unquoted_uri; + try { + unquoted_uri = Shell.unquote (uris[i]); + } catch (Error e) { + warning ("Error unquoting %s. %s", uris[i], e.message); + unquoted_uri = uris[i]; + } + // Sanitize the path as we do not want the `file://` scheme included + // and we assume dropped paths are absolute. + file = File.new_for_uri (Utils.sanitize_path (unquoted_uri, "", false)); + path = file.get_path (); + if (path != null) { + uris[i] = Shell.quote (path) + " "; + } else { + // Ignore unvalid paths + uris[i] = ""; } } var uris_s = string.joinv ("", uris); this.feed_child (uris_s.data); break; + case DropTargets.STRING: case DropTargets.TEXT: var data = selection_data.get_text (); - if (data != null) { this.feed_child (data.data); }