Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 25 additions & 48 deletions src/browser/RedirectReissuePolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

#pragma once

#include <QByteArray>
#include <QUrl>

namespace openlock {

// Decides which main-frame redirects must be re-issued as fresh requests in SEB mode.
// Decides which main-frame redirects must be re-issued as fresh navigations in SEB mode.
//
// QtWebEngine (seen on 6.2) keeps the headers the request interceptor set for the original
// request when it follows a redirect, so the redirect target would be sent the
// X-SafeExamBrowser-*Hash of the previous URL and the LMS would reject it. Re-issuing the
// target as a new navigation makes the interceptor hash the URL actually requested.
// QtWebEngine (seen on 6.2) sends a redirected request before the request interceptor
// can change or block it: the redirect target gets the X-SafeExamBrowser-*Hash headers
// of the previous URL, so the LMS rejects it, and blocking it in the interceptor is too
// late because the server has already run it (Moodle's access log shows the leg, then a
// client abort). On Moodle's login a duplicated "testsession" leg uses up the page the
// student asked for and lands them on the Dashboard instead of the quiz.
//
// Only GET legs are re-issued, because a new navigation is always a GET. A 307/308 after a
// POST keeps its method and body and is left to Chromium (its hash stays stale; LMS logins
// answer a POST with 302/303, which arrive here as GET).
//
// Flow: SecurePage reports every main-frame navigation decision; the request interceptor
// asks shouldReissue() for every main-frame request and blocks it when told to, after
// which the caller loads the URL again. Kept free of QtWebEngine types for unit tests.
// So redirects are refused in acceptNavigationRequest(), before Chromium follows them,
// and the target is loaded again as a new navigation, which the interceptor hashes for
// the URL actually requested. A new navigation is a GET: a 307/308 after a POST loses its
// method and body (LMS form posts answer with 302/303, which browsers follow with GET
// anyway). Kept free of QtWebEngine types for unit tests.
class RedirectReissuePolicy {
public:
// Matches Chromium's own redirect limit.
Expand All @@ -31,59 +30,37 @@ class RedirectReissuePolicy {
void setEnabled(bool enabled) { m_enabled = enabled; }
bool isEnabled() const { return m_enabled; }

// From acceptNavigationRequest() for main-frame navigations that are allowed.
void onMainFrameNavigation(const QUrl& url, bool isRedirect)
// From acceptNavigationRequest() for every main-frame navigation that passed the
// other checks. True: refuse this navigation and load `url` as a new navigation.
bool shouldReissue(const QUrl& url, bool isRedirect)
{
if (isRedirect) {
if (m_enabled) {
m_pendingRedirect = url;
if (!isRedirect) {
if (!m_reissuedUrl.isEmpty() && sameRequest(url, m_reissuedUrl)) {
// Our own re-issued load: same redirect chain, keep counting.
m_reissuedUrl.clear();
return false;
}
return;
}
m_pendingRedirect.clear();
if (!m_reissuedUrl.isEmpty() && sameRequest(url, m_reissuedUrl)) {
// Our own re-issued load: same redirect chain, keep counting.
m_reissuedUrl.clear();
return;
}
m_reissuedUrl.clear();
m_reissues = 0;
}

// From the request interceptor for a main-frame request. True: block this request and
// load `*loadUrl` (the redirect target as navigated, fragment included) again as a new
// navigation.
bool shouldReissue(const QUrl& url, const QByteArray& method, QUrl* loadUrl)
{
if (!m_enabled || m_pendingRedirect.isEmpty() || !sameRequest(url, m_pendingRedirect)) {
return false;
}
const QUrl target = m_pendingRedirect;
m_pendingRedirect.clear();
if (method != QByteArrayLiteral("GET")) {
m_reissues = 0;
return false;
}
if (m_reissues >= kMaxReissues) {
// Leave the loop to Chromium, which stops it with its own redirect limit.
if (!m_enabled || m_reissues >= kMaxReissues) {
// Past the cap, Chromium's own redirect limit ends the loop.
return false;
}
++m_reissues;
m_reissuedUrl = target;
if (loadUrl) {
*loadUrl = target;
}
m_reissuedUrl = url;
return true;
}

private:
// The network request never carries the fragment, the navigation can.
// The navigation can carry a fragment the network request never does.
static bool sameRequest(const QUrl& a, const QUrl& b)
{
return a.adjusted(QUrl::RemoveFragment) == b.adjusted(QUrl::RemoveFragment);
}

bool m_enabled = false;
QUrl m_pendingRedirect;
QUrl m_reissuedUrl;
int m_reissues = 0;
};
Expand Down
33 changes: 6 additions & 27 deletions src/browser/SecureBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <QWebEngineScriptCollection>
#include <QKeyEvent>
#include <QTimer>
#include <QThread>
#include <QDebug>

#include <libintl.h>
Expand All @@ -45,22 +44,6 @@ void SecurePage::setReissueRedirects(bool enabled)
m_redirects.setEnabled(enabled);
}

bool SecurePage::takeRedirectForReissue(const QUrl& url, const QByteArray& method)
{
// Qt 6 calls profile interceptors on the UI thread (measured on 6.2). Page state is
// not locked, so if a Qt release ever calls from elsewhere, do nothing rather than race.
if (QThread::currentThread() != thread()) {
qWarning() << "Request interceptor called off the UI thread; redirect not re-issued";
return false;
}
QUrl target;
if (!m_redirects.shouldReissue(url, method, &target)) {
return false;
}
QTimer::singleShot(0, this, [this, target]() { load(target); });
return true;
}

bool SecurePage::acceptNavigationRequest(const QUrl& url, NavigationType type, bool isMainFrame)
{
// Block dangerous URL schemes
Expand All @@ -78,11 +61,12 @@ bool SecurePage::acceptNavigationRequest(const QUrl& url, NavigationType type, b
return false;
}

if (isMainFrame) {
// Chromium follows redirects itself, so 307/308 keep their method and body. The
// request interceptor then asks takeRedirectForReissue() whether the redirected
// request must be sent again with fresh SEB hashes.
m_redirects.onMainFrameNavigation(url, type == NavigationTypeRedirect);
if (isMainFrame && m_redirects.shouldReissue(url, type == NavigationTypeRedirect)) {
// Refused before Chromium follows it, so the stale-hash request is never sent
// (see RedirectReissuePolicy). Loaded on the next event-loop turn, outside this
// navigation decision.
QTimer::singleShot(0, this, [this, url]() { load(url); });
return false;
}

return true;
Expand Down Expand Up @@ -235,11 +219,6 @@ QWebEngineView* SecureBrowser::webView() const
return m_webView;
}

bool SecureBrowser::takeRedirectForReissue(const QUrl& url, const QByteArray& method)
{
return m_page && m_page->takeRedirectForReissue(url, method);
}

bool SecureBrowser::eventFilter(QObject* obj, QEvent* event)
{
// Keyboard event filter installed on webView->focusProxy()
Expand Down
11 changes: 2 additions & 9 deletions src/browser/SecureBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ class SecurePage : public QWebEnginePage {
public:
explicit SecurePage(QWebEngineProfile* profile, QObject* parent = nullptr);

// When SEB headers are injected, GET legs of main-frame redirects are re-issued as
// fresh loads so the per-URL key hashes match the redirect target
// When SEB headers are injected, main-frame redirects are refused and their targets
// loaded as fresh navigations so the per-URL key hashes match the URL requested
// (see RedirectReissuePolicy).
void setReissueRedirects(bool enabled);

// Called by the request interceptor for each main-frame request. True: the caller must
// block the request; the page has scheduled the same URL as a new navigation.
bool takeRedirectForReissue(const QUrl& url, const QByteArray& method);

protected:
bool acceptNavigationRequest(const QUrl& url, NavigationType type, bool isMainFrame) override;
QWebEnginePage* createWindow(WebWindowType type) override;
Expand Down Expand Up @@ -62,9 +58,6 @@ class SecureBrowser : public QWidget {

QWebEngineView* webView() const;

// See SecurePage::takeRedirectForReissue.
bool takeRedirectForReissue(const QUrl& url, const QByteArray& method);

signals:
void urlChanged(const QUrl& url);
void navigationBlocked(const QUrl& url, const QString& reason);
Expand Down
4 changes: 0 additions & 4 deletions src/core/LockdownEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ bool LockdownEngine::initialize(const QString& configPath)
if (m_browser->navigationFilter()) {
interceptor->setNavigationFilter(m_browser->navigationFilter());
}
interceptor->setMainFrameRequestHook(
[browser = m_browser.get()](const QUrl& url, const QByteArray& method) {
return browser->takeRedirectForReissue(url, method);
});
// Get the profile from the browser's web view and install interceptor
if (m_browser->webView() && m_browser->webView()->page()) {
m_browser->webView()->page()->profile()->setUrlRequestInterceptor(interceptor);
Expand Down
11 changes: 0 additions & 11 deletions src/protocol/SEBRequestInterceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ void SEBRequestInterceptor::setNavigationFilter(NavigationFilter* filter)
m_navFilter = filter;
}

void SEBRequestInterceptor::setMainFrameRequestHook(MainFrameRequestHook hook)
{
m_mainFrameHook = std::move(hook);
}

void SEBRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info)
{
QUrl url = info.requestUrl();
Expand All @@ -58,12 +53,6 @@ void SEBRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info)
}
}

if (m_mainFrameHook && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame
&& m_mainFrameHook(url, info.requestMethod())) {
info.block(true);
return;
}

// Inject SEB headers if protocol is active
if (m_protocol) {
// Browser Exam Key request hash — URL-specific, hex-encoded
Expand Down
9 changes: 0 additions & 9 deletions src/protocol/SEBRequestInterceptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

#include <QWebEngineUrlRequestInterceptor>
#include <QByteArray>
#include <QUrl>

#include <functional>

namespace openlock {

Expand All @@ -25,19 +22,13 @@ class SEBRequestInterceptor : public QWebEngineUrlRequestInterceptor {
void setSEBProtocol(SEBProtocol* protocol);
void setNavigationFilter(NavigationFilter* filter);

// Asked for every main-frame request; returning true blocks it (the hook's owner
// re-issues it). Used to resend redirect targets with fresh SEB hashes.
using MainFrameRequestHook = std::function<bool(const QUrl& url, const QByteArray& method)>;
void setMainFrameRequestHook(MainFrameRequestHook hook);

void interceptRequest(QWebEngineUrlRequestInfo& info) override;

private:
bool isBlockedScheme(const QString& scheme) const;

SEBProtocol* m_protocol = nullptr;
NavigationFilter* m_navFilter = nullptr;
MainFrameRequestHook m_mainFrameHook;
};

} // namespace openlock
68 changes: 32 additions & 36 deletions tests/unit/test_redirect_reissue_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,43 @@ RedirectReissuePolicy enabledPolicy()
{
RedirectReissuePolicy p;
p.setEnabled(true);
p.onMainFrameNavigation(kStart, false);
EXPECT_FALSE(p.shouldReissue(kStart, false));
return p;
}
} // namespace

TEST(RedirectReissuePolicyTest, ReissuesGetLegOfRedirect)
TEST(RedirectReissuePolicyTest, RefusesRedirectsAndAcceptsTheReissuedLoad)
{
auto p = enabledPolicy();
p.onMainFrameNavigation(kTarget, true);
EXPECT_TRUE(p.shouldReissue(kTarget, "GET", nullptr));
// The re-issued load is a normal navigation and must not be re-issued again.
p.onMainFrameNavigation(kTarget, false);
EXPECT_FALSE(p.shouldReissue(kTarget, "GET", nullptr));
EXPECT_TRUE(p.shouldReissue(kTarget, true));
// The re-issued load is a normal navigation and goes through.
EXPECT_FALSE(p.shouldReissue(kTarget, false));
}

// 307/308 after a POST: Chromium repeats the POST with its body; re-issuing would turn it
// into a GET and drop the body.
TEST(RedirectReissuePolicyTest, LeavesMethodPreservingRedirectsAlone)
// Moodle's login: POST -> 303 testsession -> 303 wanted page. Each leg is refused before
// it is sent, so the server sees every URL once.
TEST(RedirectReissuePolicyTest, RefusesEveryLegOfAChain)
{
auto p = enabledPolicy();
p.onMainFrameNavigation(kTarget, true);
EXPECT_FALSE(p.shouldReissue(kTarget, "POST", nullptr));
EXPECT_FALSE(p.shouldReissue(kTarget, "GET", nullptr)) << "a pending redirect is consumed once";
const QUrl test(QStringLiteral("http://lms.example/login/index.php?testsession=3"));
EXPECT_TRUE(p.shouldReissue(test, true));
EXPECT_FALSE(p.shouldReissue(test, false));
EXPECT_TRUE(p.shouldReissue(kTarget, true));
EXPECT_FALSE(p.shouldReissue(kTarget, false));
}

TEST(RedirectReissuePolicyTest, IgnoresRequestsThatAreNotTheRedirectTarget)
TEST(RedirectReissuePolicyTest, LeavesNavigationsThatAreNotRedirectsAlone)
{
auto p = enabledPolicy();
EXPECT_FALSE(p.shouldReissue(kTarget, "GET", nullptr)) << "no redirect was reported";
p.onMainFrameNavigation(kTarget, true);
EXPECT_FALSE(p.shouldReissue(QUrl(QStringLiteral("http://lms.example/other")), "GET", nullptr));
EXPECT_FALSE(p.shouldReissue(kTarget, false));
EXPECT_FALSE(p.shouldReissue(kStart, false));
}

TEST(RedirectReissuePolicyTest, DoesNothingWhenDisabled)
{
RedirectReissuePolicy p;
p.onMainFrameNavigation(kStart, false);
p.onMainFrameNavigation(kTarget, true);
EXPECT_FALSE(p.shouldReissue(kTarget, "GET", nullptr));
EXPECT_FALSE(p.shouldReissue(kStart, false));
EXPECT_FALSE(p.shouldReissue(kTarget, true));
}

TEST(RedirectReissuePolicyTest, CapsALoopAndResetsOnANewNavigation)
Expand All @@ -63,31 +61,29 @@ TEST(RedirectReissuePolicyTest, CapsALoopAndResetsOnANewNavigation)
int reissued = 0;
for (int i = 0; i < RedirectReissuePolicy::kMaxReissues + 5; ++i) {
const QUrl hop(QStringLiteral("http://lms.example/loop/%1").arg(i));
p.onMainFrameNavigation(hop, true);
if (p.shouldReissue(hop, "GET", nullptr)) {
if (p.shouldReissue(hop, true)) {
++reissued;
p.onMainFrameNavigation(hop, false); // our own reload
EXPECT_FALSE(p.shouldReissue(hop, false)); // our own reload
}
}
EXPECT_EQ(reissued, RedirectReissuePolicy::kMaxReissues);

// A navigation the user or page starts begins a new chain.
p.onMainFrameNavigation(kStart, false);
p.onMainFrameNavigation(kTarget, true);
EXPECT_TRUE(p.shouldReissue(kTarget, "GET", nullptr));
EXPECT_FALSE(p.shouldReissue(kStart, false));
EXPECT_TRUE(p.shouldReissue(kTarget, true));
}

// The navigation can carry a fragment the network request never does: still the same
// request, and the reload keeps the fragment.
TEST(RedirectReissuePolicyTest, MatchesRedirectTargetsWithFragments)
// The redirect target can carry a fragment; the reload with it is still our own load.
TEST(RedirectReissuePolicyTest, MatchesReissuedLoadsWithFragments)
{
auto p = enabledPolicy();
const QUrl withFragment(QStringLiteral("http://lms.example/mod/quiz/view.php?id=1#region-main"));
p.onMainFrameNavigation(withFragment, true);
QUrl load;
ASSERT_TRUE(p.shouldReissue(kTarget, "GET", &load));
EXPECT_EQ(load, withFragment);
p.onMainFrameNavigation(withFragment, false);
EXPECT_FALSE(p.shouldReissue(kTarget, "GET", nullptr));
for (int i = 0; i < RedirectReissuePolicy::kMaxReissues - 1; ++i) {
ASSERT_TRUE(p.shouldReissue(withFragment, true));
EXPECT_FALSE(p.shouldReissue(kTarget, false));
}
// Still the same chain: one re-issue left before the cap.
EXPECT_TRUE(p.shouldReissue(withFragment, true));
EXPECT_FALSE(p.shouldReissue(withFragment, false));
EXPECT_FALSE(p.shouldReissue(withFragment, true)) << "cap reached";
}