diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-03-14 00:16:59 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-03-14 12:13:16 +0200 |
commit | f020fa0e91eda02c3d15b6ae27954e0573265a6a (patch) | |
tree | e903a5acb8bccd70f3766e507733a597b7c076c2 | |
parent | 9c27e165993cf673e48f6514ffc210f046c9d5fc (diff) | |
download | qtbase-f020fa0e91eda02c3d15b6ae27954e0573265a6a.tar.gz |
QStandardPaths/unix: ignore relative paths in all $XDG_* env vars
This is a continuation of commit 5c9d671bfb5b511106.
[ChangeLog][QtCore][QStandardPaths] Improved conformance to the
Freedesktop basedir spec by ignoring any relative paths in XDG_*
environment variables.
Fixes: QTBUG-58043
Change-Id: I7c34143ced97d6d3de6ecbf13bccf9e935462d1e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit d4f72b4de63fe0622908c7657f9724bad359574e)
(cherry picked from commit 80a6048ab1806e695764abebe1d9d682ac346a78)
-rw-r--r-- | src/corelib/io/qstandardpaths_unix.cpp | 25 | ||||
-rw-r--r-- | tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp | 27 |
2 files changed, 47 insertions, 5 deletions
diff --git a/src/corelib/io/qstandardpaths_unix.cpp b/src/corelib/io/qstandardpaths_unix.cpp index 07325ae9c8..55150f9e93 100644 --- a/src/corelib/io/qstandardpaths_unix.cpp +++ b/src/corelib/io/qstandardpaths_unix.cpp @@ -185,6 +185,9 @@ QString QStandardPaths::writableLocation(StandardLocation type) // http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME")); + if (!xdgCacheHome.startsWith(u'/')) + xdgCacheHome.clear(); // spec says relative paths should be ignored + if (xdgCacheHome.isEmpty()) xdgCacheHome = QDir::homePath() + "/.cache"_L1; if (type == QStandardPaths::CacheLocation) @@ -199,6 +202,9 @@ QString QStandardPaths::writableLocation(StandardLocation type) return QDir::homePath() + "/.qttest/share"_L1; QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME")); + if (!xdgDataHome.startsWith(u'/')) + xdgDataHome.clear(); // spec says relative paths should be ignored + if (xdgDataHome.isEmpty()) xdgDataHome = QDir::homePath() + "/.local/share"_L1; if (type == AppDataLocation || type == AppLocalDataLocation) @@ -214,6 +220,9 @@ QString QStandardPaths::writableLocation(StandardLocation type) // http://standards.freedesktop.org/basedir-spec/latest/ QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME")); + if (!xdgConfigHome.startsWith(u'/')) + xdgConfigHome.clear(); // spec says relative paths should be ignored + if (xdgConfigHome.isEmpty()) xdgConfigHome = QDir::homePath() + "/.config"_L1; if (type == AppConfigLocation) @@ -223,6 +232,9 @@ QString QStandardPaths::writableLocation(StandardLocation type) case RuntimeLocation: { QString xdgRuntimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR")); + if (!xdgRuntimeDir.startsWith(u'/')) + xdgRuntimeDir.clear(); // spec says relative paths should be ignored + bool fromEnv = !xdgRuntimeDir.isEmpty(); if (xdgRuntimeDir.isEmpty() || !checkXdgRuntimeDir(xdgRuntimeDir)) { // environment variable not set or is set to something unsuitable @@ -249,6 +261,9 @@ QString QStandardPaths::writableLocation(StandardLocation type) #if QT_CONFIG(regularexpression) // http://www.freedesktop.org/wiki/Software/xdg-user-dirs QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME")); + if (!xdgConfigHome.startsWith(u'/')) + xdgConfigHome.clear(); // spec says relative paths should be ignored + if (xdgConfigHome.isEmpty()) xdgConfigHome = QDir::homePath() + "/.config"_L1; QFile file(xdgConfigHome + "/user-dirs.dirs"_L1); @@ -361,13 +376,13 @@ static QStringList xdgDataDirs() static QStringList xdgConfigDirs() { - QStringList dirs; // http://standards.freedesktop.org/basedir-spec/latest/ const QString xdgConfigDirs = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS")); - if (xdgConfigDirs.isEmpty()) - dirs.append(QString::fromLatin1("/etc/xdg")); - else - dirs = xdgConfigDirs.split(u':'); + + QStringList dirs = dirsList(xdgConfigDirs); + if (dirs.isEmpty()) + dirs.push_back(u"/etc/xdg"_s); + return dirs; } diff --git a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp index 3b06175aa3..d37651bd7a 100644 --- a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp +++ b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp @@ -24,6 +24,8 @@ #define Q_XDG_PLATFORM #endif +using namespace Qt::StringLiterals; + // Update this when adding new enum values; update enumNames too static const int MaxStandardLocation = QStandardPaths::AppConfigLocation; @@ -724,6 +726,31 @@ void tst_qstandardpaths::testXdgPathCleanup() QVERIFY(!appsDirs.contains("/applications")); QVERIFY(!appsDirs.contains(uncleanGlobalAppDir + "/applications")); QVERIFY(!appsDirs.contains("relative/path/applications")); + + const QString uncleanGlobalConfigDir = "/./" + QFile::encodeName(m_globalConfigDir); + qputenv("XDG_CONFIG_DIRS", QFile::encodeName(uncleanGlobalConfigDir) + "::relative/path"); + const QStringList configDirs = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation); + QVERIFY(!configDirs.contains("relative/path"_L1)); + QVERIFY(!configDirs.contains(""_L1)); + + // Relative paths in XDG_* env vars are ignored + const QString relative("./someRelativeDir"); + + qputenv("XDG_CACHE_HOME", relative.toLatin1()); + const QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); + QCOMPARE_NE(cacheDir, relative); + + qputenv("XDG_DATA_HOME", relative.toLatin1()); + const QString localDataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); + QCOMPARE_NE(localDataDir, relative); + + qputenv("XDG_CONFIG_HOME", relative.toLatin1()); + const QString localConfig = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); + QCOMPARE_NE(localConfig, relative); + + qputenv("XDG_RUNTIME_DIR", relative.toLatin1()); + const QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); + QCOMPARE_NE(runtimeDir, relative); #endif } |