Commit eec20f0b authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

move checking of execution to plugin

parent e121a4c3
......@@ -12,10 +12,13 @@
#include <KConfigGroup>
#include <KDirWatch>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KSharedConfig>
#include <QCheckBox>
#include <QDir>
#include <QMessageBox>
#include <QStandardPaths>
static const QString CONFIG_LSPCLIENT{QStringLiteral("lspclient")};
......@@ -169,4 +172,49 @@ void LSPClientPlugin::writeConfig() const
Q_EMIT update();
}
bool LSPClientPlugin::isCommandLineAllowed(const QStringList &cmdline)
{
// check our allow list
const QString fullCommandLineString = cmdline.join(QStringLiteral(" "));
const auto it = m_serverCommandLineToAllowedState.find(fullCommandLineString);
bool startServerAllowed = false;
bool remembered = false;
if (it == m_serverCommandLineToAllowedState.end()) {
// ask user if the start should be allowed, allow to have this remembered to be permanent pain
QMessageBox msgBox;
msgBox.setText(i18n("LSP server start requested"));
msgBox.setInformativeText(i18n("Do you want the LSP server to be started? Full command line is '%1'", fullCommandLineString));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
msgBox.setCheckBox(new QCheckBox(i18n("Remember this for this command line."), &msgBox));
msgBox.checkBox()->setChecked(true);
// are we allowed?
startServerAllowed = (msgBox.exec() == QMessageBox::Yes);
// remember the state if wanted, ensure we store config directly to not loose this
if (msgBox.checkBox()->isChecked()) {
remembered = true;
m_serverCommandLineToAllowedState.emplace(fullCommandLineString, startServerAllowed);
writeConfig();
}
} else {
// use already stored result
remembered = true;
startServerAllowed = it->second;
}
// inform user this command line is forbidden
if (!startServerAllowed) {
QString message;
if (remembered) {
message = i18n("User permanently blocked start of: '%1'.\nUse the config page of the plugin to undo this block.", fullCommandLineString);
} else {
message = i18n("User blocked start of: '%1'", fullCommandLineString);
}
Q_EMIT showMessage(KTextEditor::Message::Information, message);
}
return startServerAllowed;
}
#include "lspclientplugin.moc"
......@@ -11,6 +11,7 @@
#include <QUrl>
#include <QVariant>
#include <KTextEditor/Message>
#include <KTextEditor/Plugin>
class LSPClientPlugin : public KTextEditor::Plugin
......@@ -70,10 +71,19 @@ public:
return m_configPath.isEmpty() ? m_defaultConfigPath : m_configPath;
}
private:
/**
* Check if given command line is allowed to be executed.
* Might ask the user for permission.
* @param cmdline full command line including program to check
* @return execution allowed?
*/
bool isCommandLineAllowed(const QStringList &cmdline);
Q_SIGNALS:
// signal settings update
void update() const;
void showMessage(KTextEditor::Message::MessageType level, const QString &msg);
};
#endif
......@@ -597,7 +597,7 @@ public:
connect(m_mainWindow, &KTextEditor::MainWindow::viewChanged, this, &self_type::updateState);
connect(m_mainWindow, &KTextEditor::MainWindow::unhandledShortcutOverride, this, &self_type::handleEsc);
connect(m_serverManager.data(), &LSPClientServerManager::serverChanged, this, &self_type::onServerChanged);
connect(m_serverManager.data(), &LSPClientServerManager::showMessage, this, &self_type::onShowMessage);
connect(m_plugin, &LSPClientPlugin::showMessage, this, &self_type::onShowMessage);
connect(m_serverManager.data(), &LSPClientServerManager::serverShowMessage, this, &self_type::onMessage);
connect(m_serverManager.data(), &LSPClientServerManager::serverLogMessage, this, &self_type::onMessage);
connect(m_serverManager.data(), &LSPClientServerManager::serverWorkDoneProgress, this, &self_type::onWorkDoneProgress);
......
......@@ -19,7 +19,6 @@
#include <KTextEditor/MovingInterface>
#include <KTextEditor/View>
#include <QCheckBox>
#include <QDir>
#include <QEventLoop>
#include <QFileInfo>
......@@ -27,7 +26,6 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QMessageBox>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QThread>
......@@ -422,7 +420,7 @@ private:
void showMessage(const QString &msg, KTextEditor::Message::MessageType level)
{
// inform interested view(er) which will decide how/where to show
Q_EMIT LSPClientServerManager::showMessage(level, msg);
Q_EMIT m_plugin->showMessage(level, msg);
}
// caller ensures that servers are no longer present in m_servers
......@@ -736,47 +734,8 @@ private:
// use full path to avoid security issues
cmdline[0] = cmd;
// check our allow list
const QString fullCommandLineString = cmdline.join(QStringLiteral(" "));
const auto it = m_plugin->m_serverCommandLineToAllowedState.find(fullCommandLineString);
bool startServerAllowed = false;
bool remembered = false;
if (it == m_plugin->m_serverCommandLineToAllowedState.end()) {
// ask user if the start should be allowed, allow to have this remembered to be permanent pain
QMessageBox msgBox;
msgBox.setText(i18n("LSP server start requested"));
msgBox.setInformativeText(i18n("Do you want the LSP server to be started? Full command line is '%1'", fullCommandLineString));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
msgBox.setCheckBox(new QCheckBox(i18n("Remember this for this command line."), &msgBox));
msgBox.checkBox()->setChecked(true);
// are we allowed?
startServerAllowed = (msgBox.exec() == QMessageBox::Yes);
// remember the state if wanted, ensure we store config directly to not loose this
if (msgBox.checkBox()->isChecked()) {
remembered = true;
m_plugin->m_serverCommandLineToAllowedState.emplace(fullCommandLineString, startServerAllowed);
m_plugin->writeConfig();
}
} else {
// use already stored result
remembered = true;
startServerAllowed = it->second;
}
// abort start with proper log message if not allowed
if (!startServerAllowed) {
QString message;
if (remembered) {
message =
i18n("User permanently blocked start of: '%1'.\nUse the config page of the plugin to undo this block.", fullCommandLineString);
} else {
message = i18n("User blocked start of: '%1'", fullCommandLineString);
}
showMessage(message, KTextEditor::Message::Information);
} else {
// check if allowed to start, function will query user if needed and emit messages
if (m_plugin->isCommandLineAllowed(cmdline)) {
// an empty list is always passed here (or null)
// the initial list is provided/updated using notification after start
// since that is what a server is more aware of
......
......@@ -72,7 +72,6 @@ public:
public:
Q_SIGNALS:
void serverChanged();
void showMessage(KTextEditor::Message::MessageType level, const QString &msg);
// proxy server signals in case those are emitted very early
void serverShowMessage(LSPClientServer *server, const LSPShowMessageParams &);
void serverLogMessage(LSPClientServer *server, const LSPShowMessageParams &);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment