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 Pick-to: 6.4 6.2 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)
This commit is contained in:
parent
c01296e5f1
commit
80a6048ab1
@ -185,6 +185,9 @@ QString QStandardPaths::writableLocation(StandardLocation type)
|
|||||||
|
|
||||||
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
|
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
|
||||||
QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME"));
|
QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME"));
|
||||||
|
if (!xdgCacheHome.startsWith(u'/'))
|
||||||
|
xdgCacheHome.clear(); // spec says relative paths should be ignored
|
||||||
|
|
||||||
if (xdgCacheHome.isEmpty())
|
if (xdgCacheHome.isEmpty())
|
||||||
xdgCacheHome = QDir::homePath() + "/.cache"_L1;
|
xdgCacheHome = QDir::homePath() + "/.cache"_L1;
|
||||||
if (type == QStandardPaths::CacheLocation)
|
if (type == QStandardPaths::CacheLocation)
|
||||||
@ -199,6 +202,9 @@ QString QStandardPaths::writableLocation(StandardLocation type)
|
|||||||
return QDir::homePath() + "/.qttest/share"_L1;
|
return QDir::homePath() + "/.qttest/share"_L1;
|
||||||
|
|
||||||
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
|
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
|
||||||
|
if (!xdgDataHome.startsWith(u'/'))
|
||||||
|
xdgDataHome.clear(); // spec says relative paths should be ignored
|
||||||
|
|
||||||
if (xdgDataHome.isEmpty())
|
if (xdgDataHome.isEmpty())
|
||||||
xdgDataHome = QDir::homePath() + "/.local/share"_L1;
|
xdgDataHome = QDir::homePath() + "/.local/share"_L1;
|
||||||
if (type == AppDataLocation || type == AppLocalDataLocation)
|
if (type == AppDataLocation || type == AppLocalDataLocation)
|
||||||
@ -214,6 +220,9 @@ QString QStandardPaths::writableLocation(StandardLocation type)
|
|||||||
|
|
||||||
// http://standards.freedesktop.org/basedir-spec/latest/
|
// http://standards.freedesktop.org/basedir-spec/latest/
|
||||||
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
|
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
|
||||||
|
if (!xdgConfigHome.startsWith(u'/'))
|
||||||
|
xdgConfigHome.clear(); // spec says relative paths should be ignored
|
||||||
|
|
||||||
if (xdgConfigHome.isEmpty())
|
if (xdgConfigHome.isEmpty())
|
||||||
xdgConfigHome = QDir::homePath() + "/.config"_L1;
|
xdgConfigHome = QDir::homePath() + "/.config"_L1;
|
||||||
if (type == AppConfigLocation)
|
if (type == AppConfigLocation)
|
||||||
@ -223,6 +232,9 @@ QString QStandardPaths::writableLocation(StandardLocation type)
|
|||||||
case RuntimeLocation:
|
case RuntimeLocation:
|
||||||
{
|
{
|
||||||
QString xdgRuntimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR"));
|
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();
|
bool fromEnv = !xdgRuntimeDir.isEmpty();
|
||||||
if (xdgRuntimeDir.isEmpty() || !checkXdgRuntimeDir(xdgRuntimeDir)) {
|
if (xdgRuntimeDir.isEmpty() || !checkXdgRuntimeDir(xdgRuntimeDir)) {
|
||||||
// environment variable not set or is set to something unsuitable
|
// environment variable not set or is set to something unsuitable
|
||||||
@ -249,6 +261,9 @@ QString QStandardPaths::writableLocation(StandardLocation type)
|
|||||||
#if QT_CONFIG(regularexpression)
|
#if QT_CONFIG(regularexpression)
|
||||||
// http://www.freedesktop.org/wiki/Software/xdg-user-dirs
|
// http://www.freedesktop.org/wiki/Software/xdg-user-dirs
|
||||||
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
|
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
|
||||||
|
if (!xdgConfigHome.startsWith(u'/'))
|
||||||
|
xdgConfigHome.clear(); // spec says relative paths should be ignored
|
||||||
|
|
||||||
if (xdgConfigHome.isEmpty())
|
if (xdgConfigHome.isEmpty())
|
||||||
xdgConfigHome = QDir::homePath() + "/.config"_L1;
|
xdgConfigHome = QDir::homePath() + "/.config"_L1;
|
||||||
QFile file(xdgConfigHome + "/user-dirs.dirs"_L1);
|
QFile file(xdgConfigHome + "/user-dirs.dirs"_L1);
|
||||||
@ -361,13 +376,13 @@ static QStringList xdgDataDirs()
|
|||||||
|
|
||||||
static QStringList xdgConfigDirs()
|
static QStringList xdgConfigDirs()
|
||||||
{
|
{
|
||||||
QStringList dirs;
|
|
||||||
// http://standards.freedesktop.org/basedir-spec/latest/
|
// http://standards.freedesktop.org/basedir-spec/latest/
|
||||||
const QString xdgConfigDirs = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS"));
|
const QString xdgConfigDirs = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS"));
|
||||||
if (xdgConfigDirs.isEmpty())
|
|
||||||
dirs.append(QString::fromLatin1("/etc/xdg"));
|
QStringList dirs = dirsList(xdgConfigDirs);
|
||||||
else
|
if (dirs.isEmpty())
|
||||||
dirs = xdgConfigDirs.split(u':');
|
dirs.push_back(u"/etc/xdg"_s);
|
||||||
|
|
||||||
return dirs;
|
return dirs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,8 @@
|
|||||||
#define Q_XDG_PLATFORM
|
#define Q_XDG_PLATFORM
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
// Update this when adding new enum values; update enumNames too
|
// Update this when adding new enum values; update enumNames too
|
||||||
static const int MaxStandardLocation = QStandardPaths::AppConfigLocation;
|
static const int MaxStandardLocation = QStandardPaths::AppConfigLocation;
|
||||||
|
|
||||||
@ -724,6 +726,31 @@ void tst_qstandardpaths::testXdgPathCleanup()
|
|||||||
QVERIFY(!appsDirs.contains("/applications"));
|
QVERIFY(!appsDirs.contains("/applications"));
|
||||||
QVERIFY(!appsDirs.contains(uncleanGlobalAppDir + "/applications"));
|
QVERIFY(!appsDirs.contains(uncleanGlobalAppDir + "/applications"));
|
||||||
QVERIFY(!appsDirs.contains("relative/path/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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user