From 87e1aa8774dad3e6b504c641938e84490116088f Mon Sep 17 00:00:00 2001 From: Fabian Vogt Date: Wed, 9 Mar 2022 13:37:49 +0100 Subject: Fix extracting relative symlinks ebdefa2745 tried to fix handling of relative symlinks in archives, but that only addressed targets starting with ".."/. Fix it properly by using asb_utils_sanitise_path only for absolute paths, keep relative paths as-is. Simplify code for handling absolute symlinks by just prepending the root instead of converting it to a relative link. Hardlink targets are returned as paths relative to the archive root, so the sanitization code is correct there. --- data/tests/app-1-1.fc25.x86_64.rpm | Bin 14306 -> 16758 bytes data/tests/app.spec | 7 ++++-- libappstream-builder/asb-utils.c | 49 ++++--------------------------------- 3 files changed, 10 insertions(+), 46 deletions(-) diff --git a/data/tests/app-1-1.fc25.x86_64.rpm b/data/tests/app-1-1.fc25.x86_64.rpm index ac601ce..b2d770c 100644 Binary files a/data/tests/app-1-1.fc25.x86_64.rpm and b/data/tests/app-1-1.fc25.x86_64.rpm differ diff --git a/data/tests/app.spec b/data/tests/app.spec index 26dbfb9..b40519a 100644 --- a/data/tests/app.spec +++ b/data/tests/app.spec @@ -53,11 +53,13 @@ cd $RPM_BUILD_ROOT ln -s %{_datadir}/app/app-48x48.png usr/share/icons/hicolor/48x48/apps/app.png cd - -# test decompressing a relative symlink destination +# test decompressing relative symlink destinations install -Dp %{SOURCE18} $RPM_BUILD_ROOT%{_datadir}/app/app-128x128.png mkdir -p $RPM_BUILD_ROOT%{_datadir}/icons/hicolor/128x128/apps cd $RPM_BUILD_ROOT -ln -s ../../../../app/app-128x128.png usr/share/icons/hicolor/128x128/apps/app.png +# Test links in the same directory as well as outside +ln -s ../../../../app/app-128x128.png usr/share/icons/hicolor/128x128/apps/app-linked.png +ln -s app-linked.png usr/share/icons/hicolor/128x128/apps/app.png cd - install -Dp %{SOURCE3} $RPM_BUILD_ROOT/%{_datadir}/appdata/app.appdata.xml @@ -93,6 +95,7 @@ install -Dp %{SOURCE16} $RPM_BUILD_ROOT/%{_datadir}/applications/console2.deskto %{_datadir}/app/app-48x48.png %{_datadir}/app/app-128x128.png %{_datadir}/icons/hicolor/48x48/apps/app.png +%{_datadir}/icons/hicolor/128x128/apps/app-linked.png %{_datadir}/icons/hicolor/128x128/apps/app.png %files extra diff --git a/libappstream-builder/asb-utils.c b/libappstream-builder/asb-utils.c index d1ee2ad..deb2858 100644 --- a/libappstream-builder/asb-utils.c +++ b/libappstream-builder/asb-utils.c @@ -155,32 +155,6 @@ asb_utils_ensure_exists_and_empty (const gchar *directory, GError **error) return TRUE; } -static guint -asb_utils_count_directories_deep (const gchar *path) -{ - guint cnt = 0; - guint i; - - for (i = 0; path[i] != '\0'; i++) { - if (path[i] != '/') - continue; - cnt++; - } - return cnt; -} - -static gchar * -asb_utils_get_back_to_root (guint levels) -{ - GString *str; - guint i; - - str = g_string_new (""); - for (i = 0; i < levels; i++) - g_string_append (str, "../"); - return g_string_free (str, FALSE); -} - static gchar * asb_utils_sanitise_path (const gchar *path) { @@ -243,25 +217,12 @@ asb_utils_explode_file (struct archive_entry *entry, const gchar *dir) archive_entry_update_hardlink_utf8 (entry, buf_link); } - /* update symlinks */ + /* update absolute symlinks */ tmp = archive_entry_symlink (entry); - if (tmp != NULL) { - g_autofree gchar *path_link = NULL; - - path_link = asb_utils_sanitise_path (tmp); - if (g_path_is_absolute (path_link)) { - guint symlink_depth; - g_autofree gchar *back_up = NULL; - g_autofree gchar *buf_link = NULL; - - symlink_depth = asb_utils_count_directories_deep (path) - 1; - back_up = asb_utils_get_back_to_root (symlink_depth); - buf_link = g_build_filename (back_up, tmp, NULL); - - archive_entry_update_symlink_utf8 (entry, buf_link); - } else { - archive_entry_update_symlink_utf8 (entry, path_link); - } + if (tmp != NULL && g_path_is_absolute (tmp)) { + g_autofree gchar *buf_link = NULL; + buf_link = g_build_filename (dir, tmp, NULL); + archive_entry_update_symlink_utf8 (entry, buf_link); } return TRUE; } -- cgit v1.2.1