Commit acf5df01b2 for ffmpeg
commit acf5df01b2437a27dfcf3fe7ee6dab5fe887107e
Author: Tim Rae <perceptualchaos2@gmail.com>
Date: Sun Oct 11 17:48:21 2026 +0200
avformat/libssh: percent-decode the URL path
The path from av_url_split() was passed to the SFTP server as-is, so
any percent-encoded character in the URL was looked up literally. This
breaks URLs produced by applications that encode paths per RFC 3986,
e.g. sftp://host/My%20Movie.mkv fails with "SFTP server: No such file",
while sftp://host/My Movie.mkv works.
Decode the path in libssh_connect(), and the destination path in
libssh_move(), so both refer to the same file the URL names. Paths
without escapes are unchanged. A file whose name contains a literal
'%' followed by two hex digits must now be written with the '%'
escaped as %25.
Reject paths that decode to an embedded NUL byte (%00), as RFC 3986
section 7.3 advises, instead of silently truncating them; a truncated
path could open, truncate, delete or rename an unintended file.
Document the decoding.
Signed-off-by: Tim Rae <perceptualchaos2@gmail.com>
diff --git a/doc/protocols.texi b/doc/protocols.texi
index 4eb9e9a4c5..144cac9310 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -1303,6 +1303,12 @@ Following syntax is required.
sftp://[user[:password]@@]server[:port]/path/to/remote/resource.mpeg
@end example
+The URL path is percent-decoded once. For example, @code{My%20Movie.mkv}
+expands to @code{My Movie.mkv}. Encode literal percent signs as @code{%25};
+for example, @code{My%2520Movie.mkv} expands to @code{My%20Movie.mkv}.
+Literal @code{+} characters remain unchanged. A path containing an encoded
+NUL byte (@code{%00}) is rejected.
+
This protocol accepts the following options.
@table @option
diff --git a/libavformat/libssh.c b/libavformat/libssh.c
index 2b219a8362..d46bec7c07 100644
--- a/libavformat/libssh.c
+++ b/libavformat/libssh.c
@@ -29,6 +29,7 @@
#include "avformat.h"
#include "internal.h"
#include "url.h"
+#include "urldecode.h"
typedef struct {
const AVClass *class;
@@ -230,6 +231,16 @@ static av_cold int libssh_close(URLContext *h)
return 0;
}
+static int libssh_decode_path(char *path, size_t path_size)
+{
+ int len = ff_urldecode_len(path, path_size, path, strlen(path), 0);
+
+ if (len < 0)
+ return len;
+ /* An encoded NUL (%00) would silently truncate the path. */
+ return memchr(path, 0, len) ? AVERROR(EINVAL) : 0;
+}
+
static av_cold int libssh_connect(URLContext *h, const char *url, char *path, size_t path_size)
{
LIBSSHContext *libssh = h->priv_data;
@@ -245,6 +256,9 @@ static av_cold int libssh_connect(URLContext *h, const char *url, char *path, si
path, path_size,
url);
+ if ((ret = libssh_decode_path(path, path_size)) < 0)
+ return ret;
+
if (!(*path))
av_strlcpy(path, "/", path_size);
@@ -498,6 +512,9 @@ static int libssh_move(URLContext *h_src, URLContext *h_dst)
return AVERROR(EINVAL);
}
+ if ((ret = libssh_decode_path(path_dst, sizeof(path_dst))) < 0)
+ return ret;
+
if ((ret = libssh_connect(h_src, h_src->filename, path_src, sizeof(path_src))) < 0)
goto cleanup;