Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DConfig add check for AppId #416

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ inline static bool isValidFilename(const QString& filename)
QRegularExpressionMatch match = regex.match(filename);
return match.hasMatch();
}
inline static bool isValidAppId(const QString& appId)
{
if (appId.contains(' '))
18202781743 marked this conversation as resolved.
Show resolved Hide resolved
return false;
return isValidFilename(appId);
}
/*!
@~english
\internal
Expand Down Expand Up @@ -689,6 +695,10 @@ class Q_DECL_HIDDEN DConfigMetaImpl : public DConfigMeta {

bool load(const QString &localPrefix) override
{
if (!isValidAppId(configKey.appId)) {
qCWarning(cfLog, "AppId is invalid, appId=%s", qPrintable(configKey.appId));
return false;
}
if (!isValidFilename(configKey.fileName)) {
qCWarning(cfLog, "Name is invalid, filename=%s", qPrintable(configKey.fileName));
return false;
Expand Down
21 changes: 21 additions & 0 deletions tests/ut_dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,24 @@
std::tuple{QString("org/foo"), false},
std::tuple{QString("./org-foo"), false},
std::tuple{QString("../configs/org-foo"), false}));

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

};

TEST_P(ut_DConfigFileCheckAppId, checkAppId)
{
const auto [appId, isValid] = GetParam();
FileCopyGuard guard(":/data/dconf-example.meta.json", QString("%1/%2/%3.json").arg(noAppidMetaPath, appId, FILE_NAME));

Check warning on line 529 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(appId, FILE_NAME);
ASSERT_EQ(config.load(LocalPrefix), isValid);
}
INSTANTIATE_TEST_SUITE_P(checkAppId, ut_DConfigFileCheckAppId,
::testing::Values(
std::tuple{QString("org-foo"), true},
std::tuple{QString("org foo"), false},
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}));
Loading