Skip to content

Commit

Permalink
Security fixes (#3903)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/mapserver/branches/branch-5-0@11894 7532c77e-422f-0410-93f4-f0b67bdd69e2
  • Loading branch information
Assefa Yewondwossen committed Jul 12, 2011
1 parent b5eae87 commit 77aadb0
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 160 deletions.
8 changes: 8 additions & 0 deletions HISTORY.TXT
Expand Up @@ -13,6 +13,14 @@ For a complete change history, please see the Subversion log comments.
Current Version (SVN branch, may never be released):
----------------------------------------------------

IMPORTANT SECURITY FIXE:

- Fixes to prevent SQL injections through OGC filter encoding (in WMS, WFS
and SOS), as well as a potential SQL injection in WMS time support.
Your system may be vulnerable if it has MapServer with OGC protocols
enabled, with layers connecting to an SQL RDBMS backend, either
natively or via OGR (#3903)

- Disabled some insecure (and potentially exploitable) mapserv command-line
debug arguments (#3485). The --enable-cgi-cl-debug-args configure switch
can be used to re-enable them for devs who really cannot get away without
Expand Down
111 changes: 111 additions & 0 deletions maplayer.c
Expand Up @@ -1079,6 +1079,85 @@ LayerDefaultGetNumFeatures(layerObj *layer)
return MS_FAILURE;
}


/************************************************************************/
/* LayerDefaultEscapeSQLParam */
/* */
/* Default function used to escape strings and avoid sql */
/* injection. Specific drivers should redefine if an escaping */
/* function is available in the driver. */
/************************************************************************/
char *LayerDefaultEscapeSQLParam(layerObj *layer, const char* pszString)
{
char *pszEscapedStr=NULL;
if (pszString)
{
int nSrcLen;
char c;
int i=0, j=0;
nSrcLen = (int)strlen(pszString);
pszEscapedStr = (char*) malloc( 2 * nSrcLen + 1);
for(i = 0, j = 0; i < nSrcLen; i++)
{
c = pszString[i];
if (c == '\'')
{
pszEscapedStr[j++] = '\'';
pszEscapedStr[j++] = '\'';
}
else if (c == '\\')
{
pszEscapedStr[j++] = '\\';
pszEscapedStr[j++] = '\\';
}
else
pszEscapedStr[j++] = c;
}
pszEscapedStr[j] = 0;
}
return pszEscapedStr;
}

/************************************************************************/
/* LayerDefaultEscapePropertyName */
/* */
/* Return the property name in a properly escaped and quoted form. */
/************************************************************************/
char *LayerDefaultEscapePropertyName(layerObj *layer, const char* pszString)
{
char* pszEscapedStr=NULL;
int i, j = 0;

if (layer && pszString && strlen(pszString) > 0)
{
int nLength = strlen(pszString);

pszEscapedStr = (char*) malloc( 1 + 2 * nLength + 1 + 1);
pszEscapedStr[j++] = '"';

for (i=0; i<nLength; i++)
{
char c = pszString[i];
if (c == '"')
{
pszEscapedStr[j++] = '"';
pszEscapedStr[j++] ='"';
}
else if (c == '\\')
{
pszEscapedStr[j++] = '\\';
pszEscapedStr[j++] = '\\';
}
else
pszEscapedStr[j++] = c;
}
pszEscapedStr[j++] = '"';
pszEscapedStr[j++] = 0;

}
return pszEscapedStr;
}

/*
* msConnectLayer
*
Expand Down Expand Up @@ -1136,6 +1215,10 @@ populateVirtualTable(layerVTableObj *vtable)

vtable->LayerGetNumFeatures = LayerDefaultGetNumFeatures;

vtable->LayerEscapeSQLParam = LayerDefaultEscapeSQLParam;

vtable->LayerEscapePropertyName = LayerDefaultEscapePropertyName;

return MS_SUCCESS;
}

Expand Down Expand Up @@ -1300,6 +1383,31 @@ int msINLINELayerGetNumFeatures(layerObj *layer)
return i;
}


/*
Returns an escaped string
*/
char *msLayerEscapeSQLParam(layerObj *layer, const char*pszString)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return "";
}
return layer->vtable->LayerEscapeSQLParam(layer, pszString);
}

char *msLayerEscapePropertyName(layerObj *layer, const char*pszString)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return "";
}
return layer->vtable->LayerEscapePropertyName(layer, pszString);
}


int
msINLINELayerInitializeVirtualTable(layerObj *layer)
{
Expand Down Expand Up @@ -1331,5 +1439,8 @@ msINLINELayerInitializeVirtualTable(layerObj *layer)
/* layer->vtable->LayerCreateItems, use default */
layer->vtable->LayerGetNumFeatures = msINLINELayerGetNumFeatures;

/*layer->vtable->LayerEscapeSQLParam, use default*/
/*layer->vtable->LayerEscapePropertyName, use default*/

return MS_SUCCESS;
}

0 comments on commit 77aadb0

Please sign in to comment.