From 9742e12b91eadf6d9b10809c2c41ca2d4ca6f6a8 Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Fri, 23 Jan 2026 18:27:48 +0800 Subject: [PATCH] fix: add JSON validation for text input fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Added isValidTextJsonValue helper function to validate JSON strings 2. Integrated JSON validation in three locations: main editor window, OEM dialog, and CLI set command 3. Prevent invalid JSON input from being processed by returning early with warning messages 4. Maintain existing functionality for valid JSON inputs Log: Fixed issue where invalid JSON could be entered in text fields Influence: 1. Test entering valid JSON in text fields - should work normally 2. Test entering invalid JSON in text fields - should be rejected with warning 3. Verify CLI set command rejects invalid JSON with appropriate error message 4. Check that valid JSON with various structures (objects, arrays, strings) is accepted 5. Test edge cases like empty strings, malformed JSON syntax 6. Ensure existing functionality for non-text data types remains unaffected fix: 为文本输入字段添加JSON验证 1. 新增isValidTextJsonValue辅助函数用于验证JSON字符串 2. 在三个位置集成了JSON验证:主编辑器窗口、OEM对话框和CLI设置命令 3. 通过提前返回和警告消息防止处理无效的JSON输入 4. 保持有效JSON输入的现有功能 Log: 修复了文本字段可能输入无效JSON的问题 Influence: 1. 测试在文本字段中输入有效JSON - 应正常工作 2. 测试在文本字段中输入无效JSON - 应被拒绝并显示警告 3. 验证CLI设置命令拒绝无效JSON并显示适当的错误消息 4. 检查各种结构(对象、数组、字符串)的有效JSON是否被接受 5. 测试边缘情况,如空字符串、格式错误的JSON语法 6. 确保非文本数据类型的现有功能不受影响 PMS: BUG-317315 Change-Id: Ifaf792b26b4bcb6c3c950e4ca2b0aad249c9ae4e --- dconfig-center/common/helper.hpp | 7 +++++++ dconfig-center/dde-dconfig-editor/mainwindow.cpp | 4 ++++ dconfig-center/dde-dconfig-editor/oemdialog.cpp | 4 ++++ dconfig-center/dde-dconfig/main.cpp | 4 ++++ 4 files changed, 19 insertions(+) diff --git a/dconfig-center/common/helper.hpp b/dconfig-center/common/helper.hpp index ed6dbed..8b9d8f6 100644 --- a/dconfig-center/common/helper.hpp +++ b/dconfig-center/common/helper.hpp @@ -228,6 +228,13 @@ static QVariant stringToQVariant(const QString &s) return s; } +static bool isValidTextJsonValue(const QString &s) +{ + QJsonParseError error; + QJsonDocument::fromJson(s.toUtf8(), &error); + return error.error == QJsonParseError::NoError; +} + static QString qvariantToCmd(const QVariant &v) { auto stringValue = qvariantToStringCompact(v); diff --git a/dconfig-center/dde-dconfig-editor/mainwindow.cpp b/dconfig-center/dde-dconfig-editor/mainwindow.cpp index 295a1db..231b278 100644 --- a/dconfig-center/dde-dconfig-editor/mainwindow.cpp +++ b/dconfig-center/dde-dconfig-editor/mainwindow.cpp @@ -722,6 +722,10 @@ void KeyContent::setBaseInfo(ConfigGetter *getter, const QString &language) auto widget = new DLineEdit(this); widget->setEnabled(canWrite); connect(widget, &DLineEdit::editingFinished, widget, [this, widget](){ + if (!isValidTextJsonValue(widget->text())) { + qWarning() << "invalid json value" << widget->text(); + return; + } widget->clearFocus(); emit valueChanged(stringToQVariant(widget->text())); }); diff --git a/dconfig-center/dde-dconfig-editor/oemdialog.cpp b/dconfig-center/dde-dconfig-editor/oemdialog.cpp index 01965d2..17c7b7e 100644 --- a/dconfig-center/dde-dconfig-editor/oemdialog.cpp +++ b/dconfig-center/dde-dconfig-editor/oemdialog.cpp @@ -392,6 +392,10 @@ QWidget *OEMDialog::getItemWidget(ConfigGetter *getter, DStandardItem *item) widget->setText(qvariantToString(v)); widget->setEnabled(canWrite); connect(widget, &DLineEdit::textChanged, widget, [this, item](const QString &text){ + if (!isValidTextJsonValue(text)) { + qWarning() << "invalid json value" << text; + return; + } item->setData(stringToQVariant(text), ValueRole); treeItemChanged(item); }); diff --git a/dconfig-center/dde-dconfig/main.cpp b/dconfig-center/dde-dconfig/main.cpp index 8de0774..2e4defa 100644 --- a/dconfig-center/dde-dconfig/main.cpp +++ b/dconfig-center/dde-dconfig/main.cpp @@ -292,6 +292,10 @@ int CommandManager::setCommand() #endif manager->setValue(key, value.toDouble()); } else { + if (!isValidTextJsonValue(value)) { + outpuSTDError(QString("the value:[%1] is not a valid json text.").arg(value)); + return 1; + } manager->setValue(key, stringToQVariant(value)); } } else {