Skip to content

Commit

Permalink
fix: DConfig add check for name
Browse files Browse the repository at this point in the history
  user can open non-root controlled files when `name` is relative path.

Issue: https://bugzilla.suse.com/show_bug.cgi?id=1211374
  • Loading branch information
18202781743 committed Mar 1, 2024
1 parent 9c96f05 commit ba89f7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ inline static bool subpathIsValid(const QString &subpath, const QDir &dir)
const QDir subDir(dir.filePath(subpath.mid(1)));
return subDir.absolutePath().startsWith(dir.absolutePath());
}
// name must be a valid filename.
inline static bool isValidFilename(const QString& filename)
{
static const QRegularExpression regex("^[\\w\\-\\.\\ ]+$");
QRegularExpressionMatch match = regex.match(filename);
return match.hasMatch();
}
/*!
@~english
\internal
Expand Down Expand Up @@ -682,6 +689,10 @@ class Q_DECL_HIDDEN DConfigMetaImpl : public DConfigMeta {

bool load(const QString &localPrefix) override
{
if (!isValidFilename(configKey.fileName)) {
qCWarning(cfLog, "Name is invalid, filename=%s", qPrintable(configKey.fileName));
return false;
}
bool useAppIdForOverride = true;
QString path = metaPath(localPrefix, &useAppIdForOverride);
if (path.isEmpty()) {
Expand Down
23 changes: 23 additions & 0 deletions tests/ut_dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,26 @@ TEST_F(ut_DConfigFile, userPublic) {
ASSERT_FALSE(config.meta()->flags("canExit").testFlag(DConfigFile::UserPublic));
}
}

class ut_DConfigFileCheckName : public ut_DConfigFile, public ::testing::WithParamInterface<std::tuple<QString, bool>>
{

};

TEST_P(ut_DConfigFileCheckName, checkName)
{
QString fileName;
bool isValid;
std::tie(fileName, isValid) = GetParam();
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2.json").arg(metaPath, fileName));

Check warning on line 510 in tests/ut_dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'guard' is assigned a value that is never used.
DConfigFile config(APP_ID, fileName);
ASSERT_EQ(config.load(LocalPrefix), isValid);
}
INSTANTIATE_TEST_SUITE_P(checkName, ut_DConfigFileCheckName,
::testing::Values(
std::tuple{QString("org-foo"), true},
std::tuple{QString("org foo"), true},
std::tuple{QString("org.foo2"), true},
std::tuple{QString("org/foo"), false},
std::tuple{QString("./org-foo"), false},
std::tuple{QString("../configs/org-foo"), false}));

0 comments on commit ba89f7b

Please sign in to comment.