Validate input of APPLICATION_SERVER_URL.

Tries to clean double quotes and output JSON parse error.

Signed-off-by: Camila Ayres <hello@camilasan.com>
This commit is contained in:
Camila Ayres 2024-09-25 18:25:00 +02:00 коммит произвёл Matthieu Gallien
Родитель e467fc87f6
Коммит ee351aa132
1 изменённых файлов: 18 добавлений и 5 удалений

Просмотреть файл

@ -384,8 +384,7 @@ Theme::Theme()
_forceOverrideServerUrl = true; _forceOverrideServerUrl = true;
#endif #endif
#ifdef APPLICATION_SERVER_URL #ifdef APPLICATION_SERVER_URL
_overrideServerUrl = QString::fromLatin1(APPLICATION_SERVER_URL); setOverrideServerUrl(QString::fromLatin1(APPLICATION_SERVER_URL));
updateMultipleOverrideServers();
#endif #endif
} }
@ -438,7 +437,12 @@ bool Theme::forceOverrideServerUrl() const
void Theme::updateMultipleOverrideServers() void Theme::updateMultipleOverrideServers()
{ {
const auto json = overrideServerUrl().toUtf8(); const auto json = overrideServerUrl().toUtf8();
const auto doc = QJsonDocument::fromJson(json); QJsonParseError jsonParseError;
const auto doc = QJsonDocument::fromJson(json, &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError) {
qDebug() << "Parsing array of server urls from APPLICATION_SERVER_URL failed:" << jsonParseError.error << jsonParseError.errorString();
}
_multipleOverrideServers = doc.isArray() && !doc.array().empty(); _multipleOverrideServers = doc.isArray() && !doc.array().empty();
} }
@ -967,8 +971,17 @@ bool Theme::darkMode()
void Theme::setOverrideServerUrl(const QString &overrideServerUrl) void Theme::setOverrideServerUrl(const QString &overrideServerUrl)
{ {
if (_overrideServerUrl != overrideServerUrl) { auto validOverrideServerUrl = overrideServerUrl;
_overrideServerUrl = overrideServerUrl; if (validOverrideServerUrl.startsWith("\"")) {
validOverrideServerUrl.remove(0, 1);
}
if (validOverrideServerUrl.endsWith("\"")) {
validOverrideServerUrl.chop(1);
}
if (_overrideServerUrl != validOverrideServerUrl) {
_overrideServerUrl = validOverrideServerUrl;
updateMultipleOverrideServers(); updateMultipleOverrideServers();
emit overrideServerUrlChanged(); emit overrideServerUrlChanged();
} }