From 6d9254c08cd9e943a8b93ea60d78a16f5c8b5519 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sat, 31 Aug 2024 18:17:30 +0100 Subject: [PATCH 1/4] Do not escape uri when sanitizing --- src/Utils.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils.vala b/src/Utils.vala index 0d239923e4..15c6b2134c 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -63,7 +63,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; } From f80803fa69ceab4e1bb5d5aec30ed4d6ceb4b62a Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 1 Oct 2024 11:54:50 +0100 Subject: [PATCH 2/4] Fix dropping possibly escaped and/or quoted uris --- src/Utils.vala | 1 - src/Widgets/TerminalWidget.vala | 16 +++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Utils.vala b/src/Utils.vala index 15c6b2134c..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] + "://"; diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index e0a14589ff..b1da2ea535 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -728,21 +728,27 @@ 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 sanitized unquoted path (Files sends uris that are escaped and quoted) + // We do not want the `file://` scheme included + // We assume dropped paths are absolute + file = File.new_for_uri (Utils.sanitize_path (Shell.unquote (uris[i]), "", 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); } From af4d8b8029415111df29227438776e007e5f29f7 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 3 Nov 2024 12:46:23 +0000 Subject: [PATCH 3/4] Handle ShellError when unquoting --- src/Widgets/TerminalWidget.vala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index 2479421147..905e2d7686 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -780,7 +780,14 @@ namespace Terminal { // Get sanitized unquoted path (Files sends uris that are escaped and quoted) // We do not want the `file://` scheme included // We assume dropped paths are absolute - file = File.new_for_uri (Utils.sanitize_path (Shell.unquote (uris[i]), "", false)); + 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]; + } + file = File.new_for_uri (Utils.sanitize_path (unquoted_uri, "", false)); path = file.get_path (); if (path != null) { uris[i] = Shell.quote (path) + " "; From d625acadc787c89b3272c376ee9432b30b977ee0 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 3 Nov 2024 12:52:02 +0000 Subject: [PATCH 4/4] Amend comments --- src/Widgets/TerminalWidget.vala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index 905e2d7686..58df443c05 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -777,9 +777,8 @@ namespace Terminal { string path; File file; for (var i = 0; i < uris.length; i++) { - // Get sanitized unquoted path (Files sends uris that are escaped and quoted) - // We do not want the `file://` scheme included - // We assume dropped paths are absolute + // Get unquoted path as some apps may drop uris that are escaped + // and quoted. string? unquoted_uri; try { unquoted_uri = Shell.unquote (uris[i]); @@ -787,6 +786,8 @@ namespace Terminal { 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) {