From c529134c5b38f493e07b8557b2692a3262cc8081 Mon Sep 17 00:00:00 2001 From: Kevin Hendricks Date: Thu, 3 Mar 2022 13:18:56 -0500 Subject: [PATCH] use hardened replacement for topLevelWidgets in interceptor --- src/Misc/URLInterceptor.cpp | 43 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/Misc/URLInterceptor.cpp b/src/Misc/URLInterceptor.cpp index 901e0be1af..c92eb53384 100644 --- a/src/Misc/URLInterceptor.cpp +++ b/src/Misc/URLInterceptor.cpp @@ -1,6 +1,6 @@ /************************************************************************ ** -** Copyright (C) 2020-2021 Kevin B. Hendricks, Stratford, ON, Canada +** Copyright (C) 2020-2022 Kevin B. Hendricks, Stratford, ON, Canada ** ** This file is part of Sigil. ** @@ -84,25 +84,28 @@ void URLInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) QString usercssfolder = Utility::DefinePrefsDir() + "/"; QString sourcefolder = sourceurl.toLocalFile(); DBG qDebug() << "sourcefolder: " << sourcefolder; - const QWidgetList topwidgets = qApp->topLevelWidgets(); - foreach(QWidget* widget, topwidgets) { - MainWindow * mw = qobject_cast(widget); - if (mw) { - QSharedPointer book = mw->GetCurrentBook(); - if (!book.isNull()) { - QString path_to_book = book->GetFolderKeeper()->GetFullPathToMainFolder() + "/"; - DBG qDebug() << "path_to_book: " << path_to_book; - QString path_to_mathjax = mw->GetMathJaxFolder(); - if (sourcefolder.startsWith(path_to_book)) { - bookfolder = path_to_book; - mathjaxfolder = path_to_mathjax; - DBG qDebug() << "mainwin: " << mw; - DBG qDebug() << "book: " << bookfolder; - DBG qDebug() << "mathjax: " << mathjaxfolder; - DBG qDebug() << "usercss: " << usercssfolder; - DBG qDebug() << "party: " << info.firstPartyUrl(); - DBG qDebug() << "source: " << sourcefolder; - break; + // create a topLevelWidgets equivalent to screen out stale QWidgets more safely + const QWidgetList all_widgets = QApplication::allWidgets(); + foreach(QWidget* w, all_widgets) { + if (w && w->isWindow() && w->windowType() != Qt::Desktop) { + MainWindow * mw = qobject_cast(w); + if (mw) { + QSharedPointer book = mw->GetCurrentBook(); + if (!book.isNull()) { + QString path_to_book = book->GetFolderKeeper()->GetFullPathToMainFolder() + "/"; + DBG qDebug() << "path_to_book: " << path_to_book; + QString path_to_mathjax = mw->GetMathJaxFolder(); + if (sourcefolder.startsWith(path_to_book)) { + bookfolder = path_to_book; + mathjaxfolder = path_to_mathjax; + DBG qDebug() << "mainwin: " << mw; + DBG qDebug() << "book: " << bookfolder; + DBG qDebug() << "mathjax: " << mathjaxfolder; + DBG qDebug() << "usercss: " << usercssfolder; + DBG qDebug() << "party: " << info.firstPartyUrl(); + DBG qDebug() << "source: " << sourcefolder; + break; + } } } }