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-4@11892 7532c77e-422f-0410-93f4-f0b67bdd69e2
  • Loading branch information
Assefa Yewondwossen committed Jul 12, 2011
1 parent 716eb3c commit bf302b8
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 162 deletions.
9 changes: 9 additions & 0 deletions HISTORY.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ For a complete change history, please see the Subversion log comments.

Current Version:
----------------

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)

- WFS: check if map projection is properly set before using it (#3897)

- Fix for the memory corruption when mapping the string data type in the Java bindings (#3491)
Expand Down
111 changes: 111 additions & 0 deletions maplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,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 @@ -1167,6 +1246,10 @@ populateVirtualTable(layerVTableObj *vtable)

vtable->LayerGetNumFeatures = LayerDefaultGetNumFeatures;

vtable->LayerEscapeSQLParam = LayerDefaultEscapeSQLParam;

vtable->LayerEscapePropertyName = LayerDefaultEscapePropertyName;

return MS_SUCCESS;
}

Expand Down Expand Up @@ -1345,6 +1428,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 @@ -1376,5 +1484,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;
}
Loading

0 comments on commit bf302b8

Please sign in to comment.