Prevent screen capture on Windows and macOS

* Closes #5859
This commit is contained in:
smlu 2021-02-04 16:39:13 -05:00 committed by Jonathan White
parent 9a8a5a0006
commit a5094dd3ea
11 changed files with 100 additions and 5 deletions

View file

@ -19,6 +19,7 @@
#include <QCommandLineParser>
#include <QFile>
#include <QTextStream>
#include <QWindow>
#include "cli/Utils.h"
#include "config-keepassx.h"
@ -28,6 +29,7 @@
#include "gui/Application.h"
#include "gui/MainWindow.h"
#include "gui/MessageBox.h"
#include "gui/osutils/OSUtils.h"
#if defined(WITH_ASAN) && defined(WITH_LSAN)
#include <sanitizer/lsan_interface.h>
@ -65,6 +67,8 @@ int main(int argc, char** argv)
"localconfig", QObject::tr("path to a custom local config file"), "localconfig");
QCommandLineOption keyfileOption("keyfile", QObject::tr("key file of the database"), "keyfile");
QCommandLineOption pwstdinOption("pw-stdin", QObject::tr("read password of the database from stdin"));
QCommandLineOption allowScreenCaptureOption("allow-screencapture",
QObject::tr("allow app screen recordering and screenshots"));
QCommandLineOption helpOption = parser.addHelpOption();
QCommandLineOption versionOption = parser.addVersionOption();
@ -75,6 +79,10 @@ int main(int argc, char** argv)
parser.addOption(pwstdinOption);
parser.addOption(debugInfoOption);
if (osUtils->canPreventScreenCapture()) {
parser.addOption(allowScreenCaptureOption);
}
Application app(argc, argv);
// don't set organizationName as that changes the return value of
// QStandardPaths::writableLocation(QDesktopServices::DataLocation)
@ -132,6 +140,22 @@ int main(int argc, char** argv)
MainWindow mainWindow;
#ifndef QT_DEBUG
// Disable screen capture if capable and not explicitly allowed
if (osUtils->canPreventScreenCapture() && !parser.isSet(allowScreenCaptureOption)) {
// This ensures any top-level windows (Main Window, Modal Dialogs, etc.) are excluded from screenshots
QObject::connect(&app, &QGuiApplication::focusWindowChanged, &mainWindow, [&](QWindow* window) {
if (window) {
if (!osUtils->setPreventScreenCapture(window, true)) {
mainWindow.displayGlobalMessage(
QObject::tr("Warning: Failed to prevent screenshots on a top level window!"),
MessageWidget::Error);
}
}
});
}
#endif
const bool pwstdin = parser.isSet(pwstdinOption);
for (const QString& filename : fileNames) {
QString password;