From bcd99e02640cc35bb9a3f9ec9ad919b51b1c3482 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 7 Feb 2024 18:25:48 +0100 Subject: Add black/whitelist feature to browser-based login --- src/webview.cpp | 60 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 17 deletions(-) (limited to 'src/webview.cpp') diff --git a/src/webview.cpp b/src/webview.cpp index e0b47f3..b178d73 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -1,4 +1,7 @@ #include "webview.h" +#include "nam.h" +#include "global.h" + #include #include #include @@ -11,11 +14,12 @@ #include #include #include -#include static QRegularExpression R_USER("^[a-z_A-Z][a-zA-Z0-9_@.-]{1,32}$"); static QRegularExpression R_PASS("^[a-z0-9]{1,32}$"); +static QRegularExpression urlListToRegExp(const QStringList &list); + // Override user-agent to make it appear mobile class UaWebPage : public QWebPage { @@ -27,19 +31,6 @@ public: } }; -class Nam : public QNetworkAccessManager -{ -public: - explicit Nam(QObject *parent = nullptr) : QNetworkAccessManager(parent) {} -protected: - virtual QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, - QIODevice *outgoingData = nullptr) override { - auto cp(request); - cp.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, QVariant(false)); - return QNetworkAccessManager::createRequest(op, cp, outgoingData); - } -}; - QRegularExpression UaWebPage::re("(\\S+)$"); WebView::WebView(QWidget* parent) @@ -49,12 +40,15 @@ WebView::WebView(QWidget* parent) _inErrorState(false), _timerReset(new QTimer(this)), _firstLoad(false) - { +{ this->setPage(new UaWebPage); _timerAbortMessage->setSingleShot(true); _timerReset->setSingleShot(true); connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested())); - page()->setNetworkAccessManager(new Nam(this)); + auto bl = Global::urlBlacklist(); + auto wl = Global::urlWhitelist(); + page()->setNetworkAccessManager(new SlxNetworkAccessManager(urlListToRegExp(bl), + urlListToRegExp(wl), this)); page()->setForwardUnsupportedContent(true); page()->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true); //page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); @@ -181,7 +175,6 @@ void WebView::reset(const QString baseUrl) q.addQueryItem("token", _token); url.setQuery(q); _urls.clear(); - qDebug() << "Bahnahne"; this->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar); this->setUrl(url); _firstLoad = true; @@ -191,3 +184,36 @@ void WebView::reset(const QString baseUrl) _timerReset->stop(); }); } + +static QRegularExpression urlListToRegExp(const QStringList &list) +{ + if (list.isEmpty()) + return QRegularExpression("(["); // Return an invalid regex, we use .isValid to check if the list is to be used + // We search in the escaped string, so actually look for \*\* and \* + // Capture char before that because it must not be another backslash, as that + // means the star was already escaped in the list. + // Since these are C strings there are some additional backslashes here. + static const QRegularExpression STARSTAR("(^|[^\\\\])\\\\\\*\\\\\\*"); + static const QRegularExpression STAR("(^|[^\\\\])\\\\\\*"); + static const QRegularExpression QUEST("(^|[^\\\\])\\\\\\?"); + static const QString STARSTAR_REP("\\1.*"); + static const QString STAR_REP("\\1[^/]*"); + static const QString QUEST_REP("\\1.?"); + QStringList regexes; // All my regex's live in Regtexas + for (const QString &str : list) { + QString mangled; + if (str.contains(QLatin1String("//"))) { + mangled = str; + } else if (str.contains(QLatin1Char('/')) || str.contains(QLatin1String("**"))) { + mangled = "*//" + str; + } else { + mangled = "*//" + str + "/**"; + } + mangled = QRegularExpression::escape(mangled); + mangled = mangled.replace(STARSTAR, STARSTAR_REP).replace(STAR, STAR_REP) + .replace(QUEST, QUEST_REP); + regexes << mangled; + } + qDebug() << regexes; + return QRegularExpression("^(" + regexes.join('|') + ")$"); +} -- cgit v1.2.3-55-g7522