From patchwork Tue Apr 28 15:31:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Steven X-Patchwork-Id: 19337 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6D2F244A873 for ; Tue, 28 Apr 2020 18:31:23 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 47BA568BE7D; Tue, 28 Apr 2020 18:31:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbgeu1.qq.com (smtpbgeu1.qq.com [52.59.177.22]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6A88A68BE6F for ; Tue, 28 Apr 2020 18:31:16 +0300 (EEST) X-QQ-mid: bizesmtp29t1588087870tyuwx7b3 Received: from localhost (unknown [103.107.216.233]) by esmtp10.qq.com (ESMTP) with id ; Tue, 28 Apr 2020 23:31:10 +0800 (CST) X-QQ-SSF: 01100000002000Y0ZWF0B00A0000000 X-QQ-FEAT: 67rUI+SwxC197M/uXLIRdTZ7V/6GPZAIoWlWJsI2EVrERZwyH5X3snjPcVupD 1a2qROs14o791YGYynH08xFWYT2lki/ctnDgT8djaIe8Sdj+LvOrZaSQi2hKH+WyBrvcIF3 igoIPCsxqmIk9UBJTvW+siwnc2b4DoKpsL/qRpRZFmQddVFa8Tr4P8iP7Hn12rJs2ChU3S1 eCk7fBc2Ge+2/pk7KKmgikuxP6rFfxf2KFnW6Mi56sLt0HuSUhlkGhnDoGzBQKjBF1H9wcF sad4gL+GUkQjwNovnKnP8coE6ruis9CIk5uh/7v04rkRAzsBwTNM8HWGU9eiS9QuwH0Q== X-QQ-GoodBg: 0 From: Steven Liu To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 23:31:06 +0800 Message-Id: <20200428153106.33714-1-lq@chinaffmpeg.org> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:chinaffmpeg.org:qybgforeign:qybgforeign6 X-QQ-Bgrelay: 1 Subject: [FFmpeg-devel] [PATCH v4] avformat/url: check url root node when rel include double dot and trim double dot X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Steven Liu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" fix ticket: 8625 and add testcase into url for double dot corner case Signed-off-by: Steven Liu --- libavformat/tests/url.c | 5 +++ libavformat/url.c | 80 ++++++++++++++++++++++++++++++++++++++--- tests/ref/fate/url | 5 +++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/libavformat/tests/url.c b/libavformat/tests/url.c index 5e484fd428..1d961a1b43 100644 --- a/libavformat/tests/url.c +++ b/libavformat/tests/url.c @@ -56,6 +56,7 @@ int main(void) test("/foo/bar", "baz"); test("/foo/bar", "../baz"); test("/foo/bar", "/baz"); + test("/foo/bar", "../../../baz"); test("http://server/foo/", "baz"); test("http://server/foo/bar", "baz"); test("http://server/foo/", "../baz"); @@ -65,6 +66,10 @@ int main(void) test("http://server/foo/bar?param=value/with/slashes", "/baz"); test("http://server/foo/bar?param&otherparam", "?someparam"); test("http://server/foo/bar", "//other/url"); + test("http://server/foo/bar", "../../../../../other/url"); + test("http://server/foo/bar", "/../../../../../other/url"); + test("http://server/foo/bar", "/test/../../../../../other/url"); + test("http://server/foo/bar", "/test/../../test/../../../other/url"); printf("\nTesting av_url_split:\n"); test2("/foo/bar"); diff --git a/libavformat/url.c b/libavformat/url.c index 596fb49cfc..0c857ccd2c 100644 --- a/libavformat/url.c +++ b/libavformat/url.c @@ -21,6 +21,7 @@ #include "avformat.h" +#include "internal.h" #include "config.h" #include "url.h" #if CONFIG_NETWORK @@ -77,10 +78,56 @@ int ff_url_join(char *str, int size, const char *proto, return strlen(str); } +static void trim_double_dot_url(char *buf, const char *rel, int size) +{ + const char *p = rel; + const char *root = rel; + char tmp_path[MAX_URL_SIZE] = {0, }; + char *sep; + + /* Get the path root of the url which start by "://" */ + if (p && strstr(p, "://")) { + sep = strstr(p, "://"); + if (sep) { + sep += 3; + root = strchr(sep, '/'); + } + } + + /* set new current position if the root node is changed */ + p = root; + while (p && strstr(p, "..")) { + char *node = strstr(p, ".."); + av_strlcat(tmp_path, p, node - p + strlen(tmp_path)); + p = node + 3; + sep = strrchr(tmp_path, '/'); + if (sep) + sep[0] = '\0'; + else + tmp_path[0] = '\0'; + } + + if (!av_stristart(p, "/", NULL) && root != rel) + av_strlcat(tmp_path, "/", size); + + av_strlcat(tmp_path, p, size); + /* start set buf after temp patch process. */ + av_strlcpy(buf, rel, root - rel + 1); + + if (!av_stristart(tmp_path, "/", NULL) && root != rel) + av_strlcat(buf, "/", size); + + av_strlcat(buf, tmp_path, size); +} + void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel) { char *sep, *path_query; + char *root, *p; + char tmp_path[MAX_URL_SIZE]; + + memset(tmp_path, 0, sizeof(tmp_path)); /* Absolute path, relative to the current server */ if (base && strstr(base, "://") && rel[0] == '/') { if (base != buf) @@ -99,11 +146,14 @@ void ff_make_absolute_url(char *buf, int size, const char *base, } } av_strlcat(buf, rel, size); + trim_double_dot_url(tmp_path, buf, size); + memset(buf, 0, size); + av_strlcpy(buf, tmp_path, size); return; } /* If rel actually is an absolute url, just copy it */ if (!base || strstr(rel, "://") || rel[0] == '/') { - av_strlcpy(buf, rel, size); + trim_double_dot_url(buf, rel, size); return; } if (base != buf) @@ -117,19 +167,38 @@ void ff_make_absolute_url(char *buf, int size, const char *base, /* Is relative path just a new query part? */ if (rel[0] == '?') { av_strlcat(buf, rel, size); + trim_double_dot_url(tmp_path, buf, size); + memset(buf, 0, size); + av_strlcpy(buf, tmp_path, size); return; } + root = p = buf; + /* Get the path root of the url which start by "://" */ + if (p && strstr(p, "://")) { + sep = strstr(p, "://"); + if (sep) { + sep += 3; + root = strchr(sep, '/'); + } + } + /* Remove the file name from the base url */ sep = strrchr(buf, '/'); + if (sep <= root) + sep = root; + if (sep) sep[1] = '\0'; else buf[0] = '\0'; - while (av_strstart(rel, "../", NULL) && sep) { + while (av_strstart(rel, "..", NULL) && sep) { /* Remove the path delimiter at the end */ - sep[0] = '\0'; - sep = strrchr(buf, '/'); + if (sep > root) { + sep[0] = '\0'; + sep = strrchr(buf, '/'); + } + /* If the next directory name to pop off is "..", break here */ if (!strcmp(sep ? &sep[1] : buf, "..")) { /* Readd the slash we just removed */ @@ -144,6 +213,9 @@ void ff_make_absolute_url(char *buf, int size, const char *base, rel += 3; } av_strlcat(buf, rel, size); + trim_double_dot_url(tmp_path, buf, size); + memset(buf, 0, size); + av_strlcpy(buf, tmp_path, size); } AVIODirEntry *ff_alloc_dir_entry(void) diff --git a/tests/ref/fate/url b/tests/ref/fate/url index 980b2ce1f9..533ba2cb1e 100644 --- a/tests/ref/fate/url +++ b/tests/ref/fate/url @@ -3,6 +3,7 @@ Testing ff_make_absolute_url: /foo/bar baz => /foo/baz /foo/bar ../baz => /baz /foo/bar /baz => /baz + /foo/bar ../../../baz => /baz http://server/foo/ baz => http://server/foo/baz http://server/foo/bar baz => http://server/foo/baz http://server/foo/ ../baz => http://server/baz @@ -12,6 +13,10 @@ Testing ff_make_absolute_url: http://server/foo/bar?param=value/with/slashes /baz => http://server/baz http://server/foo/bar?param&otherparam ?someparam => http://server/foo/bar?someparam http://server/foo/bar //other/url => http://other/url + http://server/foo/bar ../../../../../other/url => http://server/other/url + http://server/foo/bar /../../../../../other/url => http://server/other/url + http://server/foo/bar /test/../../../../../other/url => http://server/other/url + http://server/foo/bar /test/../../test/../../../other/url => http://server/other/url Testing av_url_split: /foo/bar => -1 /foo/bar