Skip to content

Commit cc5e0e1

Browse files
committed
[ui] add security warning when executing scripts
1 parent fcd0157 commit cc5e0e1

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/app/qgisapp.cpp

+44-24
Original file line numberDiff line numberDiff line change
@@ -6177,30 +6177,50 @@ void QgisApp::runScript( const QString &filePath )
61776177
if ( !mPythonUtils || !mPythonUtils->isEnabled() )
61786178
return;
61796179

6180-
mPythonUtils->runString(
6181-
QString( "import sys\n"
6182-
"import inspect\n"
6183-
"from qgis.utils import iface\n"
6184-
"try:\n"
6185-
" from qgis.core import QgsApplication, QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm\n"
6186-
" from processing.gui.AlgorithmDialog import AlgorithmDialog\n"
6187-
"except ImportError:\n"
6188-
" processing_found = False\n"
6189-
"else:\n"
6190-
" processing_found = True\n"
6191-
"d={}\n"
6192-
"exec(open(\"%1\".replace(\"\\\\\", \"/\").encode(sys.getfilesystemencoding())).read(), d)\n"
6193-
"if processing_found:\n"
6194-
" alg = None\n"
6195-
" for k, v in d.items():\n"
6196-
" if inspect.isclass(v) and issubclass(v, (QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm)) and v.__name__ not in (\"QgsProcessingAlgorithm\", \"QgsProcessingFeatureBasedAlgorithm\"):\n"
6197-
" alg = v()\n"
6198-
" break\n"
6199-
" if alg:\n"
6200-
" alg.setProvider(QgsApplication.processingRegistry().providerById(\"script\"))\n"
6201-
" alg.initAlgorithm()\n"
6202-
" dlg = AlgorithmDialog(alg)\n"
6203-
" dlg.show()\n" ).arg( filePath ), tr( "Failed to run Python script:" ), false );
6180+
QgsSettings settings;
6181+
bool showScriptWarning = settings.value( QStringLiteral( "UI/showScriptWarning" ), true ).toBool();
6182+
6183+
QMessageBox msgbox;
6184+
if ( showScriptWarning )
6185+
{
6186+
msgbox.setText( tr( "Security warning: executing a script from an untrusted source can lead to data loss and/or leak. Continue?" ) );
6187+
msgbox.setIcon( QMessageBox::Icon::Warning );
6188+
msgbox.addButton( QMessageBox::Yes );
6189+
msgbox.addButton( QMessageBox::No );
6190+
msgbox.setDefaultButton( QMessageBox::No );
6191+
QCheckBox *cb = new QCheckBox( tr( "Don't show this again." ) );
6192+
msgbox.setCheckBox( cb );
6193+
msgbox.exec();
6194+
settings.setValue( QStringLiteral( "UI/showScriptWarning" ), !msgbox.checkBox()->isChecked() );
6195+
}
6196+
6197+
if ( !showScriptWarning || msgbox.result() == QMessageBox::Yes )
6198+
{
6199+
mPythonUtils->runString(
6200+
QString( "import sys\n"
6201+
"import inspect\n"
6202+
"from qgis.utils import iface\n"
6203+
"try:\n"
6204+
" from qgis.core import QgsApplication, QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm\n"
6205+
" from processing.gui.AlgorithmDialog import AlgorithmDialog\n"
6206+
"except ImportError:\n"
6207+
" processing_found = False\n"
6208+
"else:\n"
6209+
" processing_found = True\n"
6210+
"d={}\n"
6211+
"exec(open(\"%1\".replace(\"\\\\\", \"/\").encode(sys.getfilesystemencoding())).read(), d)\n"
6212+
"if processing_found:\n"
6213+
" alg = None\n"
6214+
" for k, v in d.items():\n"
6215+
" if inspect.isclass(v) and issubclass(v, (QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm)) and v.__name__ not in (\"QgsProcessingAlgorithm\", \"QgsProcessingFeatureBasedAlgorithm\"):\n"
6216+
" alg = v()\n"
6217+
" break\n"
6218+
" if alg:\n"
6219+
" alg.setProvider(QgsApplication.processingRegistry().providerById(\"script\"))\n"
6220+
" alg.initAlgorithm()\n"
6221+
" dlg = AlgorithmDialog(alg)\n"
6222+
" dlg.show()\n" ).arg( filePath ), tr( "Failed to run Python script:" ), false );
6223+
}
62046224
#else
62056225
Q_UNUSED( filePath );
62066226
#endif

0 commit comments

Comments
 (0)