From patchwork Mon Jul 27 10:56:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zlomek, Josef" X-Patchwork-Id: 21283 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 A106144AF52 for ; Mon, 27 Jul 2020 13:56:56 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6F69668B7FC; Mon, 27 Jul 2020 13:56:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vps.zlomek.net (vps.zlomek.net [195.181.211.90]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5C36768B7D0 for ; Mon, 27 Jul 2020 13:56:50 +0300 (EEST) Received: by vps.zlomek.net (Postfix, from userid 1000) id A84945FE38; Mon, 27 Jul 2020 12:56:49 +0200 (CEST) From: Josef Zlomek To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Jul 2020 12:56:27 +0200 Message-Id: <20200727105629.5510-1-josef@pex.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/3] avformat/url: fix logic for removing ".." path components 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: Josef Zlomek MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixes: 8814 The logic for removing ".." path components and their corresponding upper directories was reworked. Now, the function trim_double_dot_url splits the path by "/" into components, and processes the components one ny one: - if the component is "..", the last path component in tmp_path is removed - if the component is not empty, it is added to tmp_path The duplicate logic was removed from ff_make_absolute_url. Signed-off-by: Josef Zlomek --- libavformat/url.c | 70 +++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/libavformat/url.c b/libavformat/url.c index 20463a6674..ccaa28a1ed 100644 --- a/libavformat/url.c +++ b/libavformat/url.c @@ -83,8 +83,9 @@ 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; - char *node; + int tmp_len = 0; + const char *sep; + const char *next; /* Get the path root of the url which start by "://" */ if (p && (sep = strstr(p, "://"))) { @@ -93,29 +94,39 @@ static void trim_double_dot_url(char *buf, const char *rel, int size) if (!root) return; } + if (*root == '/') + ++root; + + /* Split the path by "/" and remove ".." and its corresponding directory. */ + for (p = root; *p; p = next) { + next = strchr(p, '/'); + if (!next) + next = p + strlen(p); + + if (next - p == 2 && !strncmp(p, "..", 2)) { + /* remove the last directory from tmp_path */ + while (tmp_len > 0 && tmp_path[--tmp_len] != '/') + ; + tmp_path[tmp_len] = '\0'; + } else if (next > p) { + /* copy the current path component to tmp_path (including '/') */ + if (tmp_len) { + av_strlcpy(tmp_path + tmp_len, "/", sizeof(tmp_path) - tmp_len); + ++tmp_len; + } + av_strlcpy(tmp_path + tmp_len, p, FFMIN(sizeof(tmp_path) - tmp_len, + next - p + 1)); + tmp_len += next - p; + tmp_path[tmp_len] = '\0'; + } - /* set new current position if the root node is changed */ - p = root; - while (p && (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'; + /* skip "/" */ + while (*next == '/') + ++next; } - if (!av_stristart(p, "/", NULL) && root != rel) - av_strlcat(tmp_path, "/", size); - - av_strlcat(tmp_path, p, size); /* start set buf after temp path 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); } @@ -194,26 +205,7 @@ void ff_make_absolute_url(char *buf, int size, const char *base, sep[1] = '\0'; else buf[0] = '\0'; - while (av_strstart(rel, "..", NULL) && sep) { - /* Remove the path delimiter at the end */ - 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 */ - av_strlcat(buf, "/", size); - break; - } - /* Cut off the directory name */ - if (sep) - sep[1] = '\0'; - else - buf[0] = '\0'; - rel += 3; - } av_strlcat(buf, rel, size); trim_double_dot_url(tmp_path, buf, size); memset(buf, 0, size); From patchwork Mon Jul 27 10:56:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zlomek, Josef" X-Patchwork-Id: 21284 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 A6BA644AF52 for ; Mon, 27 Jul 2020 13:57:00 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8CE5068B810; Mon, 27 Jul 2020 13:57:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vps.zlomek.net (vps.zlomek.net [195.181.211.90]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3FA4B68B63A for ; Mon, 27 Jul 2020 13:56:54 +0300 (EEST) Received: by vps.zlomek.net (Postfix, from userid 1000) id CC20F5FE59; Mon, 27 Jul 2020 12:56:53 +0200 (CEST) From: Josef Zlomek To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Jul 2020 12:56:28 +0200 Message-Id: <20200727105629.5510-2-josef@pex.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200727105629.5510-1-josef@pex.com> References: <20200727105629.5510-1-josef@pex.com> Subject: [FFmpeg-devel] [PATCH 2/3] avformat/tests/url: add test cases for handling of 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: Josef Zlomek MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Josef Zlomek --- libavformat/tests/url.c | 2 ++ tests/ref/fate/url | 2 ++ 2 files changed, 4 insertions(+) diff --git a/libavformat/tests/url.c b/libavformat/tests/url.c index 1d961a1b43..d7f14da49e 100644 --- a/libavformat/tests/url.c +++ b/libavformat/tests/url.c @@ -67,6 +67,8 @@ int main(void) 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", "a/b/../c/d/../e../..f/.../other/url/test..mp3"); + test("http://server/foo/bar", "/a/b/../c/d/../e../..f/.../other/url/test..mp3"); test("http://server/foo/bar", "/../../../../../other/url"); test("http://server/foo/bar", "/test/../../../../../other/url"); test("http://server/foo/bar", "/test/../../test/../../../other/url"); diff --git a/tests/ref/fate/url b/tests/ref/fate/url index 533ba2cb1e..8be9979c0b 100644 --- a/tests/ref/fate/url +++ b/tests/ref/fate/url @@ -14,6 +14,8 @@ Testing ff_make_absolute_url: 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 a/b/../c/d/../e../..f/.../other/url/test..mp3 => http://server/foo/a/c/e../..f/.../other/url/test..mp3 + http://server/foo/bar /a/b/../c/d/../e../..f/.../other/url/test..mp3 => http://server/a/c/e../..f/.../other/url/test..mp3 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 From patchwork Mon Jul 27 10:56:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zlomek, Josef" X-Patchwork-Id: 21287 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:80ca:0:0:0:0:0 with SMTP id c10csp4058135ybm; Mon, 27 Jul 2020 03:57:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwH7tdzeVYwHrQivMYxuUFm+HNa6BUpakFypFV7GeoGtQ3hf0BrR+N7WB2RUg7SUndg6ncT X-Received: by 2002:a1c:7f17:: with SMTP id a23mr19281337wmd.28.1595847440133; Mon, 27 Jul 2020 03:57:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595847440; cv=none; d=google.com; s=arc-20160816; b=eiLsIRpHoprfHsU5NhOqiZE9c0FgKUwOXfRwdOU+FO+LziyBtGul2qHKGuDmrhncwp bc9CNSHyG1Dzl9YxjiJHBD5OVmwrsoOD1+khBLWny9SXWJZgQhYIkhV1m8s+jy2scMDN jQ7+YZpzMe1NkakS3TZpxDiMLS3mW4yrzN4xxdbzpQbKCNiZ6uSEtddF2klWDM3cXrL9 Y35ZZxrW7xZVN7qPElqwP470+3A2JlqNx2GjqbTA3ym+4yszquT2ad1QlAUhPYRAJBD/ bQtBMKEdgIZiWyfc51Cn4OwAnhJudEjjta/opBOhTnSqMo2pRMVQ6HAKZZdYGD1xIXzL VSxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to; bh=s7uRdt5/gQQJCP8TDLVRzP+UJfHzYYa5kzV7NOYoYQc=; b=yH79x0rnfoyctrgCThG14CO1DMzqJDuNSAvnDRnSSXw/oLIQPUaV9lEoENKtPe2wcu PPjku/J2hNUlfRS2xBTm1bIGNqbso46/5NbfiAMhc+PpBz66dHRSltM6ABCBQdmtAhVl Pfqp28xvXM9oM8THSuJfTp7c0c0agb+OX3QWux6AwBbmWvV2CglYZO+CY3y0OBQFTfRy 2Py+jzkrncnJ9G9Hxa++z6YdSZnnD+oX5UC7JqjbHtAM5/65r4VV7gsU4CKaopG5svK2 y7YHUJCeoTTXBzpPz+piWNT2Sx7Cb6aHAY1/Xj/vSIa5WEAVpX+U6VhZnpa+DxHSbh4B O8/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=pex.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i2si7098865wrx.116.2020.07.27.03.57.19; Mon, 27 Jul 2020 03:57:20 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=pex.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C9E8468B81D; Mon, 27 Jul 2020 13:57:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vps.zlomek.net (vps.zlomek.net [195.181.211.90]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7F53A68B7D0 for ; Mon, 27 Jul 2020 13:56:55 +0300 (EEST) Received: by vps.zlomek.net (Postfix, from userid 1000) id 2FDD15FEB6; Mon, 27 Jul 2020 12:56:55 +0200 (CEST) From: Josef Zlomek To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Jul 2020 12:56:29 +0200 Message-Id: <20200727105629.5510-3-josef@pex.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200727105629.5510-1-josef@pex.com> References: <20200727105629.5510-1-josef@pex.com> Subject: [FFmpeg-devel] [PATCH 3/3] avformat/url: remove duplicate call to strstr 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: Josef Zlomek MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Y3rF2ybEpHGJ Content-Length: 1496 Signed-off-by: Josef Zlomek --- libavformat/url.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/libavformat/url.c b/libavformat/url.c index ccaa28a1ed..28d12fd3de 100644 --- a/libavformat/url.c +++ b/libavformat/url.c @@ -186,14 +186,11 @@ void ff_make_absolute_url(char *buf, int size, const char *base, 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, '/'); - if (!root) - return; - } + if (p && (sep = strstr(p, "://"))) { + sep += 3; + root = strchr(sep, '/'); + if (!root) + return; } /* Remove the file name from the base url */