Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #9646 from pblottiere/server_exceptions
Browse files Browse the repository at this point in the history
[server] Sanitize WMS exceptions
  • Loading branch information
pblottiere committed Mar 29, 2019
2 parents 559a4cb + 9ae06a5 commit 43ed203
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 119 deletions.
1 change: 1 addition & 0 deletions src/server/services/wms/CMakeLists.txt
Expand Up @@ -25,6 +25,7 @@ SET (wms_SRCS

SET (wms_MOC_HDRS
qgswmsparameters.h
qgswmsserviceexception.h
)

########################################################
Expand Down
1 change: 1 addition & 0 deletions src/server/services/wms/qgswms.cpp
Expand Up @@ -21,6 +21,7 @@

#include "qgsmodule.h"
#include "qgsdxfwriter.h"
#include "qgswmsserviceexception.h"
#include "qgswmsgetcapabilities.h"
#include "qgswmsgetmap.h"
#include "qgswmsgetstyles.h"
Expand Down
8 changes: 4 additions & 4 deletions src/server/services/wms/qgswmsgetlegendgraphics.cpp
Expand Up @@ -19,6 +19,7 @@
* *
***************************************************************************/
#include "qgswmsutils.h"
#include "qgswmsserviceexception.h"
#include "qgswmsgetlegendgraphics.h"
#include "qgswmsrenderer.h"

Expand Down Expand Up @@ -65,8 +66,8 @@ namespace QgsWms
saveFormat = "JPEG";
break;
default:
throw QgsServiceException( "InvalidFormat",
QString( "Output format '%1' is not supported in the GetLegendGraphic request" ).arg( format ) );
throw QgsBadRequestException( QgsServiceException::OGC_INVALID_FORMAT,
parameters[QgsWmsParameter::FORMAT] );
break;
}

Expand Down Expand Up @@ -95,8 +96,7 @@ namespace QgsWms
}
else
{
throw QgsServiceException( QStringLiteral( "UnknownError" ),
QStringLiteral( "Failed to compute GetLegendGraphics image" ) );
throw QgsException( QStringLiteral( "Failed to compute GetLegendGraphics image" ) );
}
}
} // namespace QgsWms
4 changes: 2 additions & 2 deletions src/server/services/wms/qgswmsgetmap.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgswmsutils.h"
#include "qgswmsgetmap.h"
#include "qgswmsrenderer.h"
#include "qgswmsserviceexception.h"

#include <QImage>

Expand Down Expand Up @@ -56,8 +57,7 @@ namespace QgsWms
}
else
{
throw QgsServiceException( QStringLiteral( "UnknownError" ),
QStringLiteral( "Failed to compute GetMap image" ) );
throw QgsException( QStringLiteral( "Failed to compute GetMap image" ) );
}
}
} // namespace QgsWms
11 changes: 6 additions & 5 deletions src/server/services/wms/qgswmsgetprint.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgswmsutils.h"
#include "qgswmsgetprint.h"
#include "qgswmsrenderer.h"
#include "qgswmsserviceexception.h"

namespace QgsWms
{
Expand All @@ -29,10 +30,10 @@ namespace QgsWms
QgsServerResponse &response )
{
// get wms parameters from query
const QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
const QgsWmsParameters parameters( QUrlQuery( request.url() ) );

// GetPrint supports svg/png/pdf
const QgsWmsParameters::Format format = wmsParameters.format();
const QgsWmsParameters::Format format = parameters.format();
QString contentType;
switch ( format )
{
Expand All @@ -49,8 +50,8 @@ namespace QgsWms
contentType = QStringLiteral( "application/pdf" );
break;
default:
throw QgsServiceException( QStringLiteral( "InvalidFormat" ),
QString( "Output format %1 is not supported by the GetPrint request" ).arg( wmsParameters.formatAsString() ) );
throw QgsBadRequestException( QgsServiceException::OGC_INVALID_FORMAT,
parameters[QgsWmsParameter::FORMAT] );
break;
}

Expand All @@ -63,7 +64,7 @@ namespace QgsWms
context.setFlag( QgsWmsRenderContext::SetAccessControl );
context.setFlag( QgsWmsRenderContext::AddHighlightLayers );
context.setFlag( QgsWmsRenderContext::AddExternalLayers );
context.setParameters( wmsParameters );
context.setParameters( parameters );

// rendering
QgsRenderer renderer( context );
Expand Down
16 changes: 8 additions & 8 deletions src/server/services/wms/qgswmsgetstyles.cpp
Expand Up @@ -59,15 +59,15 @@ namespace QgsWms

if ( layersName.isEmpty() )
{
throw QgsBadRequestException( QStringLiteral( "LayerNotSpecified" ),
QStringLiteral( "Layers is mandatory for GetStyles operation" ) );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::LAYERS );
}

QStringList layerList = layersName.split( ',', QString::SkipEmptyParts );
if ( layerList.isEmpty() )
{
throw QgsBadRequestException( QStringLiteral( "LayerNotSpecified" ),
QStringLiteral( "Layers is mandatory for GetStyles operation" ) );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::LAYERS );
}

return getStyledLayerDescriptorDocument( serverIface, project, layerList );
Expand Down Expand Up @@ -96,14 +96,14 @@ namespace QgsWms

if ( styleName.isEmpty() )
{
throw QgsServiceException( QStringLiteral( "StyleNotSpecified" ),
QStringLiteral( "Style is mandatory for GetStyle operation" ), 400 );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::STYLE );
}

if ( layerName.isEmpty() )
{
throw QgsServiceException( QStringLiteral( "LayerNotSpecified" ),
QStringLiteral( "Layer is mandatory for GetStyle operation" ), 400 );
throw QgsBadRequestException( QgsServiceException::QGIS_MISSING_PARAMETER_VALUE,
QgsWmsParameter::LAYERS );
}

QStringList layerList;
Expand Down
13 changes: 12 additions & 1 deletion src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgswmsparameters.h"
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
#include "qgswmsserviceexception.h"

const QString EXTERNAL_LAYER_PREFIX = QStringLiteral( "EXTERNAL_WMS:" );

Expand Down Expand Up @@ -184,6 +185,11 @@ namespace QgsWms
return val;
}

QString QgsWmsParameter::name() const
{
return QgsWmsParameter::name( mName );
}

QString QgsWmsParameter::name( const QgsWmsParameter::Name name )
{
const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWmsParameter::Name>() );
Expand Down Expand Up @@ -536,6 +542,11 @@ namespace QgsWms
}
}

QgsWmsParameter QgsWmsParameters::operator[]( QgsWmsParameter::Name name ) const
{
return mWmsParameters[name];
}

bool QgsWmsParameters::loadParameter( const QString &key, const QString &value )
{
bool loaded = false;
Expand Down Expand Up @@ -1820,7 +1831,7 @@ namespace QgsWms

void QgsWmsParameters::raiseError( const QString &msg ) const
{
throw QgsBadRequestException( QStringLiteral( "Invalid WMS Parameter" ), msg );
throw QgsBadRequestException( QgsServiceException::QGIS_INVALID_PARAMETER_VALUE, msg );
}

QgsWmsParameter QgsWmsParameters::idParameter( const QgsWmsParameter::Name name, const int id ) const
Expand Down
13 changes: 12 additions & 1 deletion src/server/services/wms/qgswmsparameters.h
Expand Up @@ -23,7 +23,6 @@
#include <QColor>

#include "qgsrectangle.h"
#include "qgswmsserviceexception.h"
#include "qgslegendsettings.h"
#include "qgsprojectversion.h"
#include "qgsogcutils.h"
Expand Down Expand Up @@ -284,6 +283,12 @@ namespace QgsWms
*/
void raiseError() const;

/**
* Returns the name of the parameter.
* \since QGIS 3.8
*/
QString name() const;

/**
* Converts a parameter's name into its string representation.
*/
Expand Down Expand Up @@ -351,6 +356,12 @@ namespace QgsWms

virtual ~QgsWmsParameters() = default;

/**
* Returns the parameter corresponding to \a name.
* \since QGIS 3.8
*/
QgsWmsParameter operator[]( QgsWmsParameter::Name name ) const;

/**
* Dumps parameters.
*/
Expand Down
13 changes: 9 additions & 4 deletions src/server/services/wms/qgswmsrendercontext.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgslayertree.h"

#include "qgswmsrendercontext.h"
#include "qgswmsserviceexception.h"
#include "qgsserverprojectutils.h"

using namespace QgsWms;
Expand Down Expand Up @@ -395,8 +396,10 @@ void QgsWmsRenderContext::searchLayersToRenderSld()
}
else
{
throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ),
QStringLiteral( "Layer \"%1\" does not exist" ).arg( lname ) );
QgsWmsParameter param( QgsWmsParameter::LAYER );
param.mValue = lname;
throw QgsBadRequestException( QgsServiceException::OGC_LAYER_NOT_DEFINED,
param );
}
}
}
Expand Down Expand Up @@ -439,8 +442,10 @@ void QgsWmsRenderContext::searchLayersToRenderStyle()
}
else
{
throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ),
QStringLiteral( "Layer \"%1\" does not exist" ).arg( nickname ) );
QgsWmsParameter param( QgsWmsParameter::LAYER );
param.mValue = nickname;
throw QgsBadRequestException( QgsServiceException::OGC_LAYER_NOT_DEFINED,
param );
}
}
}
Expand Down

0 comments on commit 43ed203

Please sign in to comment.