Skip to content

Commit

Permalink
Ensure project path is quoted when required when exporting algorithm
Browse files Browse the repository at this point in the history
as qgis_process command
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Jan 10, 2023
1 parent 29a6b82 commit 5b1d5a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/core/processing/qgsprocessingcontext.cpp
Expand Up @@ -156,6 +156,22 @@ QVariantMap QgsProcessingContext::exportToMap() const

QStringList QgsProcessingContext::asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlags flags ) const
{
auto escapeIfNeeded = []( const QString & input ) -> QString
{
// play it safe and escape everything UNLESS it's purely alphanumeric characters (and a very select scattering of other common characters!)
const thread_local QRegularExpression nonAlphaNumericRx( QStringLiteral( "[^a-zA-Z0-9.\\-/_]" ) );
if ( nonAlphaNumericRx.match( input ).hasMatch() )
{
QString escaped = input;
escaped.replace( '\'', QLatin1String( "'\\''" ) );
return QStringLiteral( "'%1'" ).arg( escaped );
}
else
{
return input;
}
};

QStringList res;
if ( mDistanceUnit != QgsUnitTypes::DistanceUnknownUnit )
res << QStringLiteral( "--distance_units=%1" ).arg( QgsUnitTypes::encodeUnit( mDistanceUnit ) );
Expand All @@ -166,7 +182,7 @@ QStringList QgsProcessingContext::asQgisProcessArguments( QgsProcessingContext::

if ( mProject && flags & ProcessArgumentFlag::IncludeProjectPath )
{
res << QStringLiteral( "--project_path=%1" ).arg( mProject->fileName() );
res << QStringLiteral( "--project_path=%1" ).arg( escapeIfNeeded( mProject->fileName() ) );
}

return res;
Expand Down
8 changes: 8 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -1383,6 +1383,14 @@ void TestQgsProcessing::contextToProcessArguments()
QStringLiteral( "--distance_units=meters" ), QStringLiteral( "--area_units=m2" ), QStringLiteral( "--ellipsoid=NONE" ),
QStringLiteral( "--project_path=%1" ).arg( TEST_DATA_DIR + QStringLiteral( "/projects/custom_crs.qgs" ) )
} ) );

QTemporaryDir tmpDir;
p.write( tmpDir.filePath( QStringLiteral( "project name with spaces.qgs" ) ) );
QCOMPARE( context2.asQgisProcessArguments( QgsProcessingContext::ProcessArgumentFlag::IncludeProjectPath ), QStringList(
{
QStringLiteral( "--distance_units=meters" ), QStringLiteral( "--area_units=m2" ), QStringLiteral( "--ellipsoid=NONE" ),
QStringLiteral( "--project_path='%1'" ).arg( p.fileName() )
} ) );
}

void TestQgsProcessing::contextToMap()
Expand Down

0 comments on commit 5b1d5a7

Please sign in to comment.