From d8b40a524d47bc1855f33d6d1ecf9d8f1e814a9d Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Wed, 28 Jan 2026 14:11:18 +0800 Subject: [PATCH] fix: validate subpath format in dconfig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Added validation to ensure subpath starts with "/" character 2. Added debug logging when loading meta files 3. The subpath validation prevents invalid relative paths from being processed 4. Debug logging helps track meta file loading for troubleshooting Log: Fixed subpath validation to ensure proper format Influence: 1. Test configuration loading with subpaths starting with "/" 2. Test configuration loading with subpaths not starting with "/" (should fail) 3. Verify debug logging appears when loading meta files 4. Test empty subpath handling (should still work) 5. Test various valid subpath formats to ensure they pass validation fix: 修复dconfig中subpath格式验证 1. 添加验证确保subpath以"/"字符开头 2. 添加加载元文件时的调试日志记录 3. subpath验证防止处理无效的相对路径 4. 调试日志有助于跟踪元文件加载过程以进行故障排除 Log: 修复subpath验证以确保正确的格式 Influence: 1. 测试以"/"开头的subpath的配置加载 2. 测试不以"/"开头的subpath的配置加载(应该失败) 3. 验证加载元文件时是否出现调试日志 4. 测试空subpath处理(应该仍然有效) 5. 测试各种有效的subpath格式以确保它们通过验证 --- src/dconfigfile.cpp | 4 ++++ tests/ut_dconfigfile.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/dconfigfile.cpp b/src/dconfigfile.cpp index 7874c90c..bac32b24 100644 --- a/src/dconfigfile.cpp +++ b/src/dconfigfile.cpp @@ -42,6 +42,9 @@ inline static bool subpathIsValid(const QString &subpath, const QDir &dir) if (subpath.isEmpty()) return true; + if (!subpath.startsWith(QLatin1Char('/'))) + return false; + const QDir subDir(dir.filePath(subpath.mid(1))); return subDir.absolutePath().startsWith(dir.absolutePath()); } @@ -710,6 +713,7 @@ class Q_DECL_HIDDEN DConfigMetaImpl : public DConfigMeta { qCWarning(cfLog, "Can't load meta file from local prefix: \"%s\"", qPrintable(localPrefix)); return false; } + qCDebug(cfLog, "Load meta file: \"%s\"", qPrintable(path)); QScopedPointer meta(new QFile(path)); diff --git a/tests/ut_dconfigfile.cpp b/tests/ut_dconfigfile.cpp index 9efb5d24..45c007d8 100644 --- a/tests/ut_dconfigfile.cpp +++ b/tests/ut_dconfigfile.cpp @@ -473,6 +473,10 @@ TEST_F(ut_DConfigFile, setSubpath) { DConfigFile config(APP_ID, FILE_NAME); ASSERT_TRUE(config.load(LocalPrefix)); } + { + DConfigFile config(APP_ID, FILE_NAME, "a/b"); + ASSERT_FALSE(config.load(LocalPrefix)); + } { DConfigFile config(APP_ID, FILE_NAME, "/a/b"); ASSERT_TRUE(config.load(LocalPrefix));