From patchwork Mon Apr 27 11:12:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Steven X-Patchwork-Id: 19294 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 82DD544B943 for ; Mon, 27 Apr 2020 14:12:33 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6BAFA68C2B9; Mon, 27 Apr 2020 14:12:33 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbgsg2.qq.com (smtpbgsg2.qq.com [54.254.200.128]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D0E8868BCB3 for ; Mon, 27 Apr 2020 14:12:26 +0300 (EEST) X-QQ-mid: bizesmtp26t1587985940txfffwo3 Received: from localhost (unknown [221.216.234.164]) by esmtp10.qq.com (ESMTP) with id ; Mon, 27 Apr 2020 19:12:18 +0800 (CST) X-QQ-SSF: 01100000002000K0ZUF0000A0000000 X-QQ-FEAT: tvOgVvG7QYCs7IkDN6Q8xnK2Dfb0NFRuuaOlZEZbMeJulPJgFokJTjkhko+s6 2et6a6UkHnea1098b7mBuQNbWTSDqeHujr374DnQ8QkzEX6spkk0L4jEBbxhXs97F3PHWSW ftVBxSVo2HzH3oHtol+KJtlPZUGjo8nSLouGr2M3az4D9pcmZRlr9vHrNcfVEw1YCwZUSy2 2zyktrODAZ3NDykBhRN7xQU4+PVfgVl3oAYop3K5opJkJnZaTT+2gwExflpBQdTuz1eqdA3 8jLm3pWIMc5jmMmbdLl1TN5akBJKHCtGHW1dmXMwRuayoGEFTxuFfpoj29tYAmA+gnVe7Ys PlGsZG+nqwf7GIj8JrHScfSLwUOKg== X-QQ-GoodBg: 0 From: Steven Liu To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Apr 2020 19:12:16 +0800 Message-Id: <20200427111216.11179-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:qybgforeign7 X-QQ-Bgrelay: 1 Subject: [FFmpeg-devel] [PATCH v3] avformat/url: check url root node when rel include 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 , 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 | 3 +++ libavformat/url.c | 21 +++++++++++++++++++-- tests/ref/fate/url | 3 +++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/libavformat/tests/url.c b/libavformat/tests/url.c index 5e484fd428..02d0d59aa8 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,8 @@ 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"); printf("\nTesting av_url_split:\n"); test2("/foo/bar"); diff --git a/libavformat/url.c b/libavformat/url.c index 596fb49cfc..0aa50ab9a7 100644 --- a/libavformat/url.c +++ b/libavformat/url.c @@ -81,6 +81,7 @@ void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel) { char *sep, *path_query; + char *root, *p; /* Absolute path, relative to the current server */ if (base && strstr(base, "://") && rel[0] == '/') { if (base != buf) @@ -120,16 +121,32 @@ void ff_make_absolute_url(char *buf, int size, const char *base, 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) { /* 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 */ diff --git a/tests/ref/fate/url b/tests/ref/fate/url index 980b2ce1f9..c8260a97be 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,8 @@ 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 Testing av_url_split: /foo/bar => -1 /foo/bar