Skip to content

Commit 8c6df8a

Browse files
authored
Redact password= content in msError() and msDebug() messages (#6621)
1 parent eea8eba commit 8c6df8a

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

mapdebug.c

+9-11
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,18 @@ void msDebug( const char * pszFormat, ... )
324324
{
325325
va_list args;
326326
debugInfoObj *debuginfo = msGetDebugInfoObj();
327+
char szMessage[MESSAGELENGTH];
327328

328329
if (debuginfo == NULL || debuginfo->debug_mode == MS_DEBUGMODE_OFF)
329330
return; /* Don't waste time here! */
330331

332+
va_start(args, pszFormat);
333+
vsnprintf( szMessage, MESSAGELENGTH, pszFormat, args );
334+
va_end(args);
335+
szMessage[MESSAGELENGTH-1] = '\0';
336+
337+
msRedactCredentials(szMessage);
338+
331339
if (debuginfo->fp) {
332340
/* Writing to a stdio file handle */
333341

@@ -345,21 +353,11 @@ void msDebug( const char * pszFormat, ... )
345353
msStringChop(ctime(&t)), (long)tv.tv_usec);
346354
}
347355

348-
va_start(args, pszFormat);
349-
msIO_vfprintf(debuginfo->fp, pszFormat, args);
350-
va_end(args);
356+
msIO_fprintf(debuginfo->fp, "%s", szMessage);
351357
}
352358
#ifdef _WIN32
353359
else if (debuginfo->debug_mode == MS_DEBUGMODE_WINDOWSDEBUG) {
354360
/* Writing to Windows Debug Console */
355-
356-
char szMessage[MESSAGELENGTH];
357-
358-
va_start(args, pszFormat);
359-
vsnprintf( szMessage, MESSAGELENGTH, pszFormat, args );
360-
va_end(args);
361-
362-
szMessage[MESSAGELENGTH-1] = '\0';
363361
OutputDebugStringA(szMessage);
364362
}
365363
#endif

maperror.c

+14
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ char *msGetErrorString(char *delimiter)
324324
return(errstr);
325325
}
326326

327+
void msRedactCredentials(char* str)
328+
{
329+
char* password = strstr(str, "password=");
330+
if (password != NULL) {
331+
char* ptr = password + strlen("password=");
332+
while (*ptr != '\0' && *ptr != ' ') {
333+
*ptr = '*';
334+
ptr++;
335+
}
336+
}
337+
}
338+
327339
void msSetError(int code, const char *message_fmt, const char *routine, ...)
328340
{
329341
errorObj *ms_error;
@@ -356,6 +368,8 @@ void msSetError(int code, const char *message_fmt, const char *routine, ...)
356368
else
357369
++ms_error->errorcount;
358370

371+
msRedactCredentials(ms_error->message);
372+
359373
/* Log a copy of errors to MS_ERRORFILE if set (handled automatically inside msDebug()) */
360374
msDebug("%s: %s %s\n", ms_error->routine, ms_errorCodes[ms_error->code], ms_error->message);
361375

maperror.h

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ extern "C" {
125125
MS_DLL_EXPORT char *msGetErrorString(char *delimiter);
126126

127127
#ifndef SWIG
128+
MS_DLL_EXPORT void msRedactCredentials(char* str);
128129
MS_DLL_EXPORT void msSetError(int code, const char *message, const char *routine, ...) MS_PRINT_FUNC_FORMAT(2,4) ;
129130
MS_DLL_EXPORT void msWriteError(FILE *stream);
130131
MS_DLL_EXPORT void msWriteErrorXML(FILE *stream);

mappostgis.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -2598,25 +2598,13 @@ int msPostGISLayerOpen(layerObj *layer)
25982598
** Connection failed, return error message with passwords ***ed out.
25992599
*/
26002600
if (!layerinfo->pgconn || PQstatus(layerinfo->pgconn) == CONNECTION_BAD) {
2601-
char *index, *maskeddata;
26022601
if (layer->debug)
26032602
msDebug("msPostGISLayerOpen: Connection failure.\n");
26042603

2605-
maskeddata = msStrdup(layer->connection);
2606-
index = strstr(maskeddata, "password=");
2607-
if (index != NULL) {
2608-
index = (char*)(index + 9);
2609-
while (*index != '\0' && *index != ' ') {
2610-
*index = '*';
2611-
index++;
2612-
}
2613-
}
2614-
2615-
msDebug( "Database connection failed (%s) with connect string '%s'\nIs the database running? Is it allowing connections? Does the specified user exist? Is the password valid? Is the database on the standard port? in msPostGISLayerOpen()", PQerrorMessage(layerinfo->pgconn), maskeddata);
2604+
msDebug( "Database connection failed (%s) with connect string '%s'\nIs the database running? Is it allowing connections? Does the specified user exist? Is the password valid? Is the database on the standard port? in msPostGISLayerOpen()", PQerrorMessage(layerinfo->pgconn), layer->connection);
26162605
msSetError(MS_QUERYERR, "Database connection failed. Check server logs for more details.Is the database running? Is it allowing connections? Does the specified user exist? Is the password valid? Is the database on the standard port?", "msPostGISLayerOpen()");
26172606

26182607
if(layerinfo->pgconn) PQfinish(layerinfo->pgconn);
2619-
free(maskeddata);
26202608
free(layerinfo);
26212609
return MS_FAILURE;
26222610
}

0 commit comments

Comments
 (0)