strawberry/app/main_window_policy.cpp
trvon 76ec40e4f5
Replace web UI with a Qt tray app
Remove the browser UI, its Python server and the web API contract test.
Add the Qt6 tray app (status, findings, process search, policy
controls, notifications) and EvidenceStore, the read-only view of the
daemon's evidence that the app and MCP server share. The GUI demo
config becomes strawberry.demo.conf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:03 -06:00

689 lines
35 KiB
C++

// Rules (feeds) and Allowlist tabs of the main window.
#include "main_window.hpp"
#include <QCheckBox>
#include <QComboBox>
#include <QDateTime>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QJsonArray>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMessageBox>
#include <QPushButton>
#include <QRegularExpression>
#include <QTableWidget>
#include <QVBoxLayout>
#include <pwd.h>
#include <map>
namespace strawberry::app {
namespace {
QString when(qint64 ns) {
if (ns <= 0) {
return QStringLiteral("—");
}
return QDateTime::fromMSecsSinceEpoch(ns / 1'000'000).toString(QStringLiteral("yyyy-MM-dd HH:mm"));
}
QString user_name(qint64 uid) {
if (const passwd* pw = ::getpwuid(static_cast<uid_t>(uid)); pw != nullptr && pw->pw_name != nullptr) {
return QString::fromLocal8Bit(pw->pw_name);
}
return QStringLiteral("uid %1").arg(uid);
}
QTableWidget* make_table(QWidget* parent, const QStringList& headers, int stretch_column) {
auto* table = new QTableWidget(0, static_cast<int>(headers.size()), parent);
table->setHorizontalHeaderLabels(headers);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->verticalHeader()->setVisible(false);
for (int i = 0; i < headers.size(); ++i) {
table->horizontalHeader()->setSectionResizeMode(
i, i == stretch_column ? QHeaderView::Stretch : QHeaderView::ResizeToContents);
}
return table;
}
QString selected_id(QTableWidget* table) {
const auto rows = table->selectionModel()->selectedRows();
return rows.isEmpty() ? QString() : table->item(rows.first().row(), 0)->data(Qt::UserRole).toString();
}
QString colored(bool ok, const QString& text) {
return QStringLiteral("<span style=\"color:%1\">%2</span>")
.arg(ok ? QStringLiteral("#2ea043") : QStringLiteral("#d72638"), text.toHtmlEscaped());
}
} // namespace
QWidget* MainWindow::build_rules_tab() {
auto* page = new QWidget(this);
auto* layout = new QVBoxLayout(page);
rules_summary_ = new QLabel(page);
rules_summary_->setWordWrap(true);
rules_summary_->setTextFormat(Qt::RichText);
layout->addWidget(rules_summary_);
feeds_table_ = make_table(page, {QStringLiteral("Feed"), QStringLiteral("Kind"), QStringLiteral("Status"),
QStringLiteral("Entries"), QStringLiteral("Version"), QStringLiteral("Updated"),
QStringLiteral("Enabled"), QStringLiteral("Source")},
2);
layout->addWidget(feeds_table_, 1);
auto* bar = new QHBoxLayout();
auto* add = new QPushButton(QStringLiteral("Add feed…"), page);
auto* remove = new QPushButton(QStringLiteral("Remove"), page);
auto* toggle = new QPushButton(QStringLiteral("Enable / Disable"), page);
auto* update = new QPushButton(QStringLiteral("Update now"), page);
auto* reload = new QPushButton(QStringLiteral("Reload rules"), page);
for (auto* button : {add, remove, toggle, update, reload}) {
bar->addWidget(button);
}
bar->addStretch();
layout->addLayout(bar);
rules_result_ = new QLabel(page);
rules_result_->setWordWrap(true);
layout->addWidget(rules_result_);
auto* hint = new QLabel(QStringLiteral(
"Feeds are downloaded by the sandboxed <code>strawberry-update</code> service (daily, and on \"Update now\"). "
"Each release is checked against its published SHA-256 and compiled before it is activated; a broken update "
"never replaces working rules. Local rules live in <code>/etc/strawberry/rules</code>."), page);
hint->setWordWrap(true);
hint->setStyleSheet(QStringLiteral("color: #888"));
layout->addWidget(hint);
connect(add, &QPushButton::clicked, this, &MainWindow::add_feed_dialog);
connect(remove, &QPushButton::clicked, this, [this]() {
const auto id = selected_id(feeds_table_);
if (id.isEmpty()) {
return;
}
if (QMessageBox::question(this, QStringLiteral("Remove feed"),
QStringLiteral("Remove rule feed \"%1\"? Its rules stop being used immediately.").arg(id))
== QMessageBox::Yes) {
emit policy_change_requested(QStringLiteral("feed-remove"), QJsonObject{{QStringLiteral("id"), id}});
}
});
connect(toggle, &QPushButton::clicked, this, [this]() {
const auto id = selected_id(feeds_table_);
if (id.isEmpty()) {
return;
}
for (const auto& value : policy_.value(QStringLiteral("policy")).toObject().value(QStringLiteral("feeds")).toArray()) {
const auto feed = value.toObject();
if (feed.value(QStringLiteral("id")).toString() == id) {
emit policy_change_requested(QStringLiteral("feed-set-enabled"),
QJsonObject{{QStringLiteral("id"), id}, {QStringLiteral("enabled"), !feed.value(QStringLiteral("enabled")).toBool()}});
}
}
});
connect(update, &QPushButton::clicked, this, [this]() {
rules_result_->setText(QStringLiteral("Requesting rule update…"));
emit command_requested(QStringLiteral("update-rules"));
});
connect(reload, &QPushButton::clicked, this, [this]() {
rules_result_->setText(QStringLiteral("Reloading rules…"));
emit command_requested(QStringLiteral("reload-rules"));
});
return page;
}
QWidget* MainWindow::build_allowlist_tab() {
auto* page = new QWidget(this);
auto* layout = new QVBoxLayout(page);
auto* intro = new QLabel(QStringLiteral(
"Allowlisted matches are not stored as detections. Prefer <b>hash</b> entries (one exact binary) over "
"<b>path</b> or <b>rule</b> entries. Existing findings stay as history."), page);
intro->setWordWrap(true);
layout->addWidget(intro);
allow_table_ = make_table(page, {QStringLiteral("Kind"), QStringLiteral("Value"), QStringLiteral("Rule"),
QStringLiteral("Note"), QStringLiteral("Added by"), QStringLiteral("Added")},
1);
allow_table_->setTextElideMode(Qt::ElideMiddle);
allow_table_->horizontalHeader()->setMinimumSectionSize(60);
layout->addWidget(allow_table_, 2);
auto* bar = new QHBoxLayout();
auto* add = new QPushButton(QStringLiteral("Add…"), page);
auto* remove = new QPushButton(QStringLiteral("Remove"), page);
bar->addWidget(add);
bar->addWidget(remove);
bar->addStretch();
layout->addLayout(bar);
allow_result_ = new QLabel(page);
allow_result_->setWordWrap(true);
layout->addWidget(allow_result_);
layout->addWidget(new QLabel(QStringLiteral("<b>Recent policy changes</b>"), page));
history_list_ = new QListWidget(page);
layout->addWidget(history_list_, 1);
connect(add, &QPushButton::clicked, this, &MainWindow::add_allowlist_dialog);
connect(remove, &QPushButton::clicked, this, [this]() {
const auto id = selected_id(allow_table_);
if (id.isEmpty()) {
return;
}
if (QMessageBox::question(this, QStringLiteral("Remove allowlist entry"),
QStringLiteral("Remove this entry? Matching detections will be recorded again."))
== QMessageBox::Yes) {
emit policy_change_requested(QStringLiteral("allowlist-remove"), QJsonObject{{QStringLiteral("id"), id}});
}
});
return page;
}
QWidget* MainWindow::build_response_tab() {
auto* page = new QWidget(this);
auto* layout = new QVBoxLayout(page);
response_status_ = new QLabel(page);
response_status_->setWordWrap(true);
response_status_->setTextFormat(Qt::RichText);
layout->addWidget(response_status_);
auto* settings = new QFormLayout();
contain_auto_ = new QCheckBox(QStringLiteral("Automatically block network access of flagged binaries (OpenSnitch)"), page);
contain_severity_ = new QComboBox(page);
prevention_mode_ = new QComboBox(page);
prevention_severity_ = new QComboBox(page);
for (auto* combo : {contain_severity_, prevention_severity_}) {
combo->addItem(QStringLiteral("critical"), QStringLiteral("critical"));
combo->addItem(QStringLiteral("high and above"), QStringLiteral("high"));
combo->addItem(QStringLiteral("medium and above"), QStringLiteral("medium"));
}
prevention_mode_->addItem(QStringLiteral("Off"), QStringLiteral("off"));
prevention_mode_->addItem(QStringLiteral("Audit (alert, never block)"), QStringLiteral("audit"));
prevention_mode_->addItem(QStringLiteral("Enforce (block execution)"), QStringLiteral("enforce"));
settings->addRow(contain_auto_);
settings->addRow(QStringLiteral("Contain at severity"), contain_severity_);
settings->addRow(QStringLiteral("Execution prevention"), prevention_mode_);
settings->addRow(QStringLiteral("Prevent at severity"), prevention_severity_);
auto* apply = new QPushButton(QStringLiteral("Apply protection settings"), page);
settings->addRow(apply);
layout->addLayout(settings);
auto* note = new QLabel(QStringLiteral(
"System locations (/usr, /bin, /lib, /etc, /boot) are never blocked automatically, only alerted on. "
"Allowlisted binaries are never contained. Start prevention in Audit and switch to Enforce once no "
"legitimate program shows up as \"would block\"."), page);
note->setWordWrap(true);
note->setStyleSheet(QStringLiteral("color: #888"));
layout->addWidget(note);
layout->addWidget(new QLabel(QStringLiteral("<b>Contained binaries</b> (network denied via OpenSnitch)"), page));
contain_table_ = make_table(page, {QStringLiteral("Path"), QStringLiteral("Reason"), QStringLiteral("Severity"),
QStringLiteral("Pinned by"), QStringLiteral("By"), QStringLiteral("When"),
QStringLiteral("SHA-256")},
0);
layout->addWidget(contain_table_, 1);
auto* bar = new QHBoxLayout();
auto* undo = new QPushButton(QStringLiteral("Undo (allow network)"), page);
bar->addWidget(undo);
bar->addStretch();
layout->addLayout(bar);
layout->addWidget(new QLabel(QStringLiteral("<b>Quarantined binaries</b> (execution always blocked, network denied; "
"files are kept for forensics)"), page));
quarantine_table_ = make_table(page, {QStringLiteral("Path"), QStringLiteral("By"), QStringLiteral("When"),
QStringLiteral("SHA-256")},
0);
layout->addWidget(quarantine_table_, 1);
auto* qbar = new QHBoxLayout();
auto* release = new QPushButton(QStringLiteral("Release from quarantine"), page);
qbar->addWidget(release);
qbar->addStretch();
layout->addLayout(qbar);
connect(release, &QPushButton::clicked, this, [this]() {
const auto sha = selected_id(quarantine_table_);
if (!sha.isEmpty()
&& QMessageBox::question(this, QStringLiteral("Release"), QStringLiteral("Allow this binary to run again?\n\n%1").arg(sha))
== QMessageBox::Yes) {
emit policy_change_requested(QStringLiteral("quarantine-remove"), QJsonObject{{QStringLiteral("sha256"), sha}});
}
});
response_result_ = new QLabel(page);
response_result_->setWordWrap(true);
layout->addWidget(response_result_);
const auto mark_dirty = [this]() { protection_dirty_ = true; };
connect(contain_auto_, &QCheckBox::toggled, this, mark_dirty);
for (auto* combo : {contain_severity_, prevention_mode_, prevention_severity_}) {
connect(combo, &QComboBox::activated, this, mark_dirty);
}
connect(apply, &QPushButton::clicked, this, [this]() {
const auto mode = prevention_mode_->currentData().toString();
if (mode == QStringLiteral("enforce")
&& QMessageBox::question(this, QStringLiteral("Enforce prevention"),
QStringLiteral("Block execution of matching programs? Make sure Audit mode has shown "
"no false \"would block\" alerts first."))
!= QMessageBox::Yes) {
return;
}
protection_dirty_ = false;
response_result_->setText(QStringLiteral("Saving…"));
emit policy_change_requested(QStringLiteral("protection-set"), QJsonObject{
{QStringLiteral("contain_auto"), contain_auto_->isChecked()},
{QStringLiteral("contain_min_severity"), contain_severity_->currentData().toString()},
{QStringLiteral("prevention_mode"), mode},
{QStringLiteral("prevention_min_severity"), prevention_severity_->currentData().toString()},
});
});
connect(undo, &QPushButton::clicked, this, [this]() {
const auto id = selected_id(contain_table_);
if (!id.isEmpty()) {
emit policy_change_requested(QStringLiteral("contain-remove"), QJsonObject{{QStringLiteral("id"), id}});
}
});
return page;
}
void MainWindow::render_response() {
if (response_status_ == nullptr) {
return;
}
const auto os = status_.value(QStringLiteral("opensnitch")).toObject();
QString text;
if (status_.isEmpty()) {
text = QStringLiteral("Scanner not reachable.");
} else if (!os.value(QStringLiteral("integration")).toBool()) {
text = QStringLiteral("OpenSnitch integration is disabled in strawberry.conf.");
} else if (!os.value(QStringLiteral("installed")).toBool()) {
text = QStringLiteral("<b>OpenSnitch not found</b> — install it to enable network containment and blocklists.");
} else {
const auto entries = os.value(QStringLiteral("blocklists")).toObject().value(QStringLiteral("entries")).toObject();
text = QStringLiteral("<b>OpenSnitch connected.</b> Blocklists: %1 domains, %2 IPs, %3 networks. %4")
.arg(entries.value(QStringLiteral("domains")).toInt())
.arg(entries.value(QStringLiteral("ips")).toInt())
.arg(entries.value(QStringLiteral("nets")).toInt())
.arg(os.value(QStringLiteral("checksums_enabled")).toBool()
? QStringLiteral("Containment is pinned to each binary's hash.")
: colored(false, QStringLiteral("Checksums are off in OpenSnitch, so containment is by path only "
"(run: sudo strawberry --opensnitch-enable-checksums).")));
}
const auto exec_guard = status_.value(QStringLiteral("prevention")).toObject();
if (!exec_guard.isEmpty()) {
text += QStringLiteral("<br><b>Execution prevention:</b> %1 — %2 checked, %3 blocked, %4 would-block, %5 timeouts%6")
.arg(exec_guard.value(QStringLiteral("mode")).toString())
.arg(exec_guard.value(QStringLiteral("checked")).toDouble(), 0, 'f', 0)
.arg(exec_guard.value(QStringLiteral("blocked")).toDouble(), 0, 'f', 0)
.arg(exec_guard.value(QStringLiteral("would_block")).toDouble(), 0, 'f', 0)
.arg(exec_guard.value(QStringLiteral("timeouts")).toDouble(), 0, 'f', 0)
.arg(exec_guard.value(QStringLiteral("active")).toBool()
? QString()
: QStringLiteral(" ") + colored(false, QStringLiteral("(inactive: ")
+ exec_guard.value(QStringLiteral("error")).toString() + QStringLiteral(")")));
}
response_status_->setText(text);
const auto policy = policy_.value(QStringLiteral("policy")).toObject();
const auto protection = policy.value(QStringLiteral("protection")).toObject();
if (!protection_dirty_ && !protection.isEmpty()) {
const auto select = [](QComboBox* combo, const QString& value) {
const int index = combo->findData(value);
if (index >= 0) {
combo->setCurrentIndex(index);
}
};
contain_auto_->blockSignals(true);
contain_auto_->setChecked(protection.value(QStringLiteral("contain_auto")).toBool(true));
contain_auto_->blockSignals(false);
select(contain_severity_, protection.value(QStringLiteral("contain_min_severity")).toString());
select(prevention_mode_, protection.value(QStringLiteral("prevention_mode")).toString());
select(prevention_severity_, protection.value(QStringLiteral("prevention_min_severity")).toString());
}
const auto selected = selected_id(contain_table_);
contain_table_->setRowCount(0);
for (const auto& value : policy.value(QStringLiteral("containment")).toArray()) {
const auto c = value.toObject();
const int row = contain_table_->rowCount();
contain_table_->insertRow(row);
auto* path = new QTableWidgetItem(c.value(QStringLiteral("path")).toString());
path->setData(Qt::UserRole, c.value(QStringLiteral("id")).toString());
path->setToolTip(path->text());
contain_table_->setItem(row, 0, path);
contain_table_->setItem(row, 1, new QTableWidgetItem(c.value(QStringLiteral("reason")).toString()));
contain_table_->setItem(row, 2, new QTableWidgetItem(c.value(QStringLiteral("severity")).toString()));
contain_table_->setItem(row, 3, new QTableWidgetItem(c.value(QStringLiteral("hash_pinned")).toBool()
? QStringLiteral("path + hash") : QStringLiteral("path only")));
contain_table_->setItem(row, 4, new QTableWidgetItem(c.value(QStringLiteral("automatic")).toBool()
? QStringLiteral("automatic")
: user_name(static_cast<qint64>(c.value(QStringLiteral("created_by")).toDouble()))));
contain_table_->setItem(row, 5, new QTableWidgetItem(when(static_cast<qint64>(c.value(QStringLiteral("created_at")).toDouble()))));
contain_table_->setItem(row, 6, new QTableWidgetItem(c.value(QStringLiteral("sha256")).toString().left(16)));
if (path->data(Qt::UserRole).toString() == selected) {
contain_table_->selectRow(row);
}
}
const auto selected_q = selected_id(quarantine_table_);
quarantine_table_->setRowCount(0);
for (const auto& value : policy.value(QStringLiteral("quarantine")).toArray()) {
const auto q = value.toObject();
const int row = quarantine_table_->rowCount();
quarantine_table_->insertRow(row);
auto* path = new QTableWidgetItem(q.value(QStringLiteral("path")).toString());
path->setData(Qt::UserRole, q.value(QStringLiteral("sha256")).toString());
path->setToolTip(path->text());
quarantine_table_->setItem(row, 0, path);
quarantine_table_->setItem(row, 1, new QTableWidgetItem(user_name(static_cast<qint64>(q.value(QStringLiteral("created_by")).toDouble()))));
quarantine_table_->setItem(row, 2, new QTableWidgetItem(when(static_cast<qint64>(q.value(QStringLiteral("created_at")).toDouble()))));
quarantine_table_->setItem(row, 3, new QTableWidgetItem(q.value(QStringLiteral("sha256")).toString().left(16)));
if (path->data(Qt::UserRole).toString() == selected_q) {
quarantine_table_->selectRow(row);
}
}
}
void MainWindow::update_policy(const QJsonObject& policy, const QString& error) {
if (!error.isEmpty()) {
policy_error_ = error;
} else {
policy_ = policy;
policy_error_.clear();
}
render_rules();
render_allowlist();
render_response();
}
void MainWindow::show_policy_result(const QString& verb, bool ok, const QString& message) {
auto* target = verb.startsWith(QStringLiteral("feed-")) ? rules_result_
: (verb == QStringLiteral("scan-memory") || verb.startsWith(QStringLiteral("process-"))
|| verb == QStringLiteral("quarantine")) ? process_result_
: (verb.startsWith(QStringLiteral("contain-")) || verb.startsWith(QStringLiteral("quarantine"))
|| verb == QStringLiteral("protection-set")) ? response_result_
: allow_result_;
target->setText(colored(ok, message));
}
void MainWindow::render_rules() {
const auto rules = status_.value(QStringLiteral("rules")).toObject();
QString summary;
if (!status_error_.isEmpty() || status_.isEmpty()) {
summary = QStringLiteral("<b>Scanner not reachable</b> — rule status unavailable.");
} else {
summary = QStringLiteral("<b>%1 YARA rules</b> and <b>%5 known-malware hashes</b> active, from %2 file(s), generation %3, loaded %4.")
.arg(rules.value(QStringLiteral("count")).toInt())
.arg(rules.value(QStringLiteral("files")).toInt())
.arg(rules.value(QStringLiteral("generation")).toInt())
.arg(when(static_cast<qint64>(rules.value(QStringLiteral("loaded_at_ns")).toDouble())))
.arg(rules.value(QStringLiteral("ioc_hashes")).toInt());
const auto rules_error = rules.value(QStringLiteral("last_error")).toString();
if (!rules_error.isEmpty()) {
summary += QStringLiteral("<br>%1").arg(colored(false, QStringLiteral("Last reload failed (previous rules still active): ") + rules_error));
}
}
if (!policy_error_.isEmpty()) {
summary += QStringLiteral("<br>%1").arg(colored(false, QStringLiteral("Policy unavailable: ") + policy_error_));
}
rules_summary_->setText(summary);
std::map<QString, QJsonObject> states;
for (const auto& value : policy_.value(QStringLiteral("feed_state")).toObject().value(QStringLiteral("feeds")).toArray()) {
const auto state = value.toObject();
states[state.value(QStringLiteral("id")).toString()] = state;
}
const auto selected = selected_id(feeds_table_);
feeds_table_->setRowCount(0);
for (const auto& value : policy_.value(QStringLiteral("policy")).toObject().value(QStringLiteral("feeds")).toArray()) {
const auto feed = value.toObject();
const auto id = feed.value(QStringLiteral("id")).toString();
const auto state = states[id];
const bool enabled = feed.value(QStringLiteral("enabled")).toBool();
QStringList urls;
for (const auto& url : feed.value(QStringLiteral("urls")).toArray()) {
urls << url.toString();
}
const auto source = feed.value(QStringLiteral("type")).toString() == QStringLiteral("github-release")
? QStringLiteral("GitHub %1 · %2").arg(feed.value(QStringLiteral("repo")).toString(), feed.value(QStringLiteral("asset")).toString())
: urls.join(QStringLiteral(", "));
const auto kind = feed.value(QStringLiteral("kind")).toString(QStringLiteral("yara"));
QString status_text;
const auto error = state.value(QStringLiteral("error")).toString();
if (!enabled) {
status_text = QStringLiteral("disabled");
} else if (!error.isEmpty()) {
status_text = QStringLiteral("⚠ ") + error;
} else if (state.value(QStringLiteral("version")).toString().isEmpty()) {
status_text = QStringLiteral("waiting for first download");
} else {
status_text = QStringLiteral("✔ up to date (checked %1)").arg(when(static_cast<qint64>(state.value(QStringLiteral("checked_at")).toDouble())));
}
const int row = feeds_table_->rowCount();
feeds_table_->insertRow(row);
auto* name = new QTableWidgetItem(feed.value(QStringLiteral("name")).toString() + QStringLiteral(" (") + id + QStringLiteral(")"));
name->setData(Qt::UserRole, id);
name->setToolTip(source);
feeds_table_->setItem(row, 0, name);
auto* status_item = new QTableWidgetItem(status_text);
status_item->setToolTip(status_text);
if (!error.isEmpty()) {
status_item->setForeground(QColor(0xd7, 0x26, 0x38));
}
feeds_table_->setItem(row, 1, new QTableWidgetItem(kind == QStringLiteral("yara") ? QStringLiteral("YARA rules") : kind));
feeds_table_->setItem(row, 2, status_item);
feeds_table_->setItem(row, 3, new QTableWidgetItem(QString::number(state.value(QStringLiteral("rules")).toInt())));
feeds_table_->setItem(row, 4, new QTableWidgetItem(state.value(QStringLiteral("version")).toString()));
feeds_table_->setItem(row, 5, new QTableWidgetItem(when(static_cast<qint64>(state.value(QStringLiteral("updated_at")).toDouble()))));
feeds_table_->setItem(row, 6, new QTableWidgetItem(enabled ? QStringLiteral("yes") : QStringLiteral("no")));
feeds_table_->setItem(row, 7, new QTableWidgetItem(source));
if (id == selected) {
feeds_table_->selectRow(row);
}
}
}
void MainWindow::render_allowlist() {
const auto policy = policy_.value(QStringLiteral("policy")).toObject();
const auto selected = selected_id(allow_table_);
allow_table_->setRowCount(0);
for (const auto& value : policy.value(QStringLiteral("allowlist")).toArray()) {
const auto entry = value.toObject();
const int row = allow_table_->rowCount();
allow_table_->insertRow(row);
auto* kind = new QTableWidgetItem(entry.value(QStringLiteral("kind")).toString());
kind->setData(Qt::UserRole, entry.value(QStringLiteral("id")).toString());
allow_table_->setItem(row, 0, kind);
auto* value_item = new QTableWidgetItem(entry.value(QStringLiteral("value")).toString());
value_item->setToolTip(value_item->text());
allow_table_->setItem(row, 1, value_item);
allow_table_->setItem(row, 2, new QTableWidgetItem(entry.value(QStringLiteral("rule")).toString()));
allow_table_->setItem(row, 3, new QTableWidgetItem(entry.value(QStringLiteral("note")).toString()));
allow_table_->setItem(row, 4, new QTableWidgetItem(user_name(static_cast<qint64>(entry.value(QStringLiteral("added_by")).toDouble()))));
allow_table_->setItem(row, 5, new QTableWidgetItem(when(static_cast<qint64>(entry.value(QStringLiteral("added_at")).toDouble()))));
if (kind->data(Qt::UserRole).toString() == selected) {
allow_table_->selectRow(row);
}
}
history_list_->clear();
const auto history = policy.value(QStringLiteral("history")).toArray();
for (qsizetype i = history.size() - 1; i >= 0 && history_list_->count() < 30; --i) {
const auto change = history[i].toObject();
history_list_->addItem(QStringLiteral("%1 %2 %3 %4")
.arg(when(static_cast<qint64>(change.value(QStringLiteral("at")).toDouble())),
user_name(static_cast<qint64>(change.value(QStringLiteral("uid")).toDouble())),
change.value(QStringLiteral("action")).toString(),
change.value(QStringLiteral("summary")).toString()));
}
}
void MainWindow::add_feed_dialog() {
QDialog dialog(this);
dialog.setWindowTitle(QStringLiteral("Add rule feed"));
auto* form = new QFormLayout(&dialog);
auto* kind = new QComboBox(&dialog);
kind->addItem(QStringLiteral("YARA rules"), QStringLiteral("yara"));
kind->addItem(QStringLiteral("Malware SHA-256 hashes"), QStringLiteral("hashes"));
kind->addItem(QStringLiteral("Malicious domains (OpenSnitch blocklist)"), QStringLiteral("domains"));
kind->addItem(QStringLiteral("Malicious IPs (OpenSnitch blocklist)"), QStringLiteral("ips"));
kind->addItem(QStringLiteral("Malicious networks / CIDR (OpenSnitch blocklist)"), QStringLiteral("nets"));
auto* type = new QComboBox(&dialog);
type->addItem(QStringLiteral("GitHub release asset"), QStringLiteral("github-release"));
type->addItem(QStringLiteral("HTTPS URL(s) (.yar/.yara/.zip, or a text list)"), QStringLiteral("url"));
auto* id = new QLineEdit(&dialog);
id->setPlaceholderText(QStringLiteral("e.g. yara-forge-extended (a-z, 0-9, -)"));
id->setValidator(new QRegularExpressionValidator(QRegularExpression(QStringLiteral("[a-z0-9-]{1,32}")), id));
auto* name = new QLineEdit(&dialog);
auto* repo = new QLineEdit(&dialog);
repo->setPlaceholderText(QStringLiteral("owner/name, e.g. YARAHQ/yara-forge"));
auto* asset = new QLineEdit(&dialog);
asset->setPlaceholderText(QStringLiteral("e.g. yara-forge-rules-extended.zip"));
auto* url = new QLineEdit(&dialog);
url->setPlaceholderText(QStringLiteral("https://… (several: separate with spaces)"));
auto* sha = new QLineEdit(&dialog);
sha->setPlaceholderText(QStringLiteral("optional: pin the expected SHA-256"));
form->addRow(QStringLiteral("Kind"), kind);
form->addRow(QStringLiteral("Source"), type);
form->addRow(QStringLiteral("ID"), id);
form->addRow(QStringLiteral("Name"), name);
form->addRow(QStringLiteral("Repository"), repo);
form->addRow(QStringLiteral("Asset"), asset);
form->addRow(QStringLiteral("URL"), url);
form->addRow(QStringLiteral("SHA-256"), sha);
auto* warning = new QLabel(QStringLiteral(
"Only add feeds you trust: their rules decide what is flagged. GitHub releases must publish a SHA-256 digest; "
"URL feeds are only integrity-checked if you pin a SHA-256."), &dialog);
warning->setWordWrap(true);
form->addRow(warning);
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog);
form->addRow(buttons);
const auto sync = [&]() {
const bool github = type->currentData().toString() == QStringLiteral("github-release");
repo->setEnabled(github);
asset->setEnabled(github);
url->setEnabled(!github);
sha->setEnabled(!github);
};
sync();
connect(type, &QComboBox::currentIndexChanged, &dialog, sync);
connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
if (dialog.exec() != QDialog::Accepted) {
return;
}
QJsonObject payload{{QStringLiteral("id"), id->text().trimmed()},
{QStringLiteral("kind"), kind->currentData().toString()},
{QStringLiteral("type"), type->currentData().toString()}};
if (!name->text().trimmed().isEmpty()) {
payload.insert(QStringLiteral("name"), name->text().trimmed());
}
if (type->currentData().toString() == QStringLiteral("github-release")) {
payload.insert(QStringLiteral("repo"), repo->text().trimmed());
payload.insert(QStringLiteral("asset"), asset->text().trimmed());
} else {
QJsonArray urls;
for (const auto& part : url->text().split(QRegularExpression(QStringLiteral("[\\s,]+")), Qt::SkipEmptyParts)) {
urls.append(part);
}
payload.insert(QStringLiteral("urls"), urls);
if (!sha->text().trimmed().isEmpty()) {
payload.insert(QStringLiteral("sha256"), sha->text().trimmed().toLower());
}
}
rules_result_->setText(QStringLiteral("Adding feed…"));
emit policy_change_requested(QStringLiteral("feed-add"), payload);
}
void MainWindow::add_allowlist_dialog() {
QDialog dialog(this);
dialog.setWindowTitle(QStringLiteral("Add allowlist entry"));
auto* form = new QFormLayout(&dialog);
auto* kind = new QComboBox(&dialog);
kind->addItem(QStringLiteral("Binary hash (SHA-256) — recommended"), QStringLiteral("sha256"));
kind->addItem(QStringLiteral("Path (exact, or directory ending in /)"), QStringLiteral("path"));
kind->addItem(QStringLiteral("Rule name (everywhere)"), QStringLiteral("rule"));
auto* value = new QLineEdit(&dialog);
auto* rule = new QLineEdit(&dialog);
rule->setPlaceholderText(QStringLiteral("optional: only this rule"));
auto* note = new QLineEdit(&dialog);
note->setPlaceholderText(QStringLiteral("why this is safe"));
auto* warning = new QLabel(&dialog);
warning->setWordWrap(true);
warning->setStyleSheet(QStringLiteral("color: #c07a00"));
form->addRow(QStringLiteral("Kind"), kind);
form->addRow(QStringLiteral("Value"), value);
form->addRow(QStringLiteral("Rule"), rule);
form->addRow(QStringLiteral("Note"), note);
form->addRow(warning);
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog);
form->addRow(buttons);
const auto sync = [&]() {
const auto k = kind->currentData().toString();
rule->setEnabled(k != QStringLiteral("rule"));
value->setPlaceholderText(k == QStringLiteral("sha256") ? QStringLiteral("64 hex characters")
: k == QStringLiteral("path") ? QStringLiteral("/usr/bin/tool or /opt/vendor/")
: QStringLiteral("YARA rule name"));
warning->setText(k == QStringLiteral("rule")
? QStringLiteral("Suppresses this rule on every file and process. Malware matching it will not be reported.")
: k == QStringLiteral("path") ? QStringLiteral("Anything placed at this path is trusted; prefer a hash entry.")
: QString());
};
sync();
connect(kind, &QComboBox::currentIndexChanged, &dialog, sync);
connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
if (dialog.exec() != QDialog::Accepted) {
return;
}
QJsonObject payload{{QStringLiteral("kind"), kind->currentData().toString()},
{QStringLiteral("value"), value->text().trimmed()}};
if (rule->isEnabled() && !rule->text().trimmed().isEmpty()) {
payload.insert(QStringLiteral("rule"), rule->text().trimmed());
}
if (!note->text().trimmed().isEmpty()) {
payload.insert(QStringLiteral("note"), note->text().trimmed());
}
allow_result_->setText(QStringLiteral("Adding entry…"));
emit policy_change_requested(QStringLiteral("allowlist-add"), payload);
}
void MainWindow::propose_allowlist(const QString& kind, const QString& value, const QString& rule, const QString& context) {
const auto answer = QMessageBox::question(
this, QStringLiteral("Allowlist"),
QStringLiteral("Stop reporting %1?\n\n%2\n\nOnly do this if you know the file is legitimate.").arg(context, value));
if (answer != QMessageBox::Yes) {
return;
}
QJsonObject payload{{QStringLiteral("kind"), kind}, {QStringLiteral("value"), value},
{QStringLiteral("note"), QStringLiteral("added from the Strawberry app")}};
if (!rule.isEmpty()) {
payload.insert(QStringLiteral("rule"), rule);
}
tabs_->setCurrentWidget(allow_table_->parentWidget());
allow_result_->setText(QStringLiteral("Adding entry…"));
emit policy_change_requested(QStringLiteral("allowlist-add"), payload);
}
} // namespace strawberry::app