QStandardPaths/unix: ignore relative paths in all $XDG_* env vars

This is a continuation of commit 5c9d671bfb.

[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>
This commit is contained in:
Ahmad Samir 2023-01-29 21:47:25 +02:00 committed by Thiago Macieira
parent 1f1380b79c
commit d4f72b4de6
2 changed files with 47 additions and 5 deletions

View File

@ -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;
}

View File

@ -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
}