Skip to content

Commit ad381bc

Browse files
claudiamurialdoclaudiamurialdo
andauthored
CodeQL: Sanitize log entries created from user input (#1202)
* Sanitize log entries created by user * Add InfoSanitized. * Santitize both parameters in WarnSanitized and InfoSanitized --------- Co-authored-by: claudiamurialdo <c.murialdo@globant.com>
1 parent 1c2bcf2 commit ad381bc

File tree

7 files changed

+38
-11
lines changed

7 files changed

+38
-11
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3716,7 +3716,7 @@ internal string ClientTimeZoneId
37163716
if (String.IsNullOrEmpty(sTZ))
37173717
{
37183718
sTZ = (string)GetCookie(GX_REQUEST_TIMEZONE);
3719-
GXLogging.Debug(Logger, "ClientTimeZone GX_REQUEST_TIMEZONE cookie:", sTZ);
3719+
GXLogging.DebugSanitized(Logger, "ClientTimeZone GX_REQUEST_TIMEZONE cookie:", sTZ);
37203720
}
37213721
if (!DateTimeUtil.ValidTimeZone(sTZ))
37223722
{

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3999,7 +3999,7 @@ public static bool AbsoluteUri(string fileName, out Uri result)
39993999
GXLogging.Debug(log, "Absolute uri:", fileName, " resolved to " + result);
40004000
return true;
40014001
}
4002-
GXLogging.Info(log, "Uri ", fileName, " resolved to:" + result);
4002+
GXLogging.InfoSanitized(log, "Uri ", fileName, " resolved to:" + result);
40034003
return false; ;
40044004
}
40054005

dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ protected static byte[] GetBinary(string fileNameParm, bool dbBlob)
935935
return binary;
936936
}
937937
}
938-
GXLogging.Debug(log, "GxCommand. An error occurred while getting data from file path ", uri.AbsolutePath, e);
938+
GXLogging.DebugSanitized(log, e, "GxCommand. An error occurred while getting data from file path ", uri.AbsolutePath);
939939
throw e;
940940
}
941941
break;

dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ public override long GetBytes(IGxDbCommand cmd, IDataRecord DR, int i, long fiel
343343
}
344344
public override void SetParameter(IDbDataParameter parameter, object value)
345345
{
346-
if (value is Guid)
346+
if (value is Guid guid)
347347
{
348-
value = value.ToString();
348+
value = guid.ToString();
349349
}
350350
#if NETCORE
351351
else if (value is GxEmbedding embedding)

dotnet/src/dotnetframework/GxClasses/Helpers/GXLogging.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public static void ErrorSanitized(ILog log, string msg, Exception ex)
535535
{
536536
if (log.IsErrorEnabled)
537537
{
538-
log.Error(Utils.StringUtil.Sanitize(msg, Utils.StringUtil.LogUserEntryWhiteList), ex);
538+
log.Error(StringUtil.Sanitize(msg, StringUtil.LogUserEntryWhiteList), ex);
539539
}
540540
}
541541
public static void Error(ILog log, string msg1, string msg2, Exception ex)
@@ -598,7 +598,7 @@ public static void DebugSanitized(ILog log, Exception ex, params string[] list)
598598
StringBuilder msg = new StringBuilder();
599599
foreach (string parm in list)
600600
{
601-
msg.Append(Utils.StringUtil.Sanitize(parm, Utils.StringUtil.LogUserEntryWhiteList));
601+
msg.Append(StringUtil.Sanitize(parm, StringUtil.LogUserEntryWhiteList));
602602
}
603603
if (ex != null)
604604
log.Debug(msg, ex);
@@ -749,7 +749,7 @@ internal static void ErrorSanitized(IGXLogger logger, string msg, Exception ex)
749749
{
750750
if (logger.IsErrorEnabled)
751751
{
752-
logger.LogError(Utils.StringUtil.Sanitize(msg, Utils.StringUtil.LogUserEntryWhiteList), ex);
752+
logger.LogError(StringUtil.Sanitize(msg, StringUtil.LogUserEntryWhiteList), ex);
753753
}
754754
}
755755
}
@@ -832,6 +832,18 @@ public static void Warn(IGXLogger logger, params string[] list)
832832
}
833833
}
834834
}
835+
internal static void WarnSanitized(IGXLogger log, string msg, params string[] list)
836+
{
837+
if (log.IsDebugEnabled)
838+
{
839+
StringBuilder strBuilder = new StringBuilder(StringUtil.Sanitize(msg, StringUtil.LogUserEntryWhiteList));
840+
foreach (string parm in list)
841+
{
842+
strBuilder.Append(StringUtil.Sanitize(parm, StringUtil.LogUserEntryWhiteList));
843+
}
844+
log.LogWarning(strBuilder.ToString());
845+
}
846+
}
835847
public static void Warn(IGXLogger logger, string msg, params string[] list)
836848
{
837849
if (logger != null)
@@ -861,7 +873,7 @@ internal static void DebugSanitized(IGXLogger logger, Exception ex, params strin
861873
StringBuilder msg = new StringBuilder();
862874
foreach (string parm in list)
863875
{
864-
msg.Append(Utils.StringUtil.Sanitize(parm, Utils.StringUtil.LogUserEntryWhiteList));
876+
msg.Append(StringUtil.Sanitize(parm, StringUtil.LogUserEntryWhiteList));
865877
}
866878
if (ex != null)
867879
logger.LogDebug(ex, msg.ToString());
@@ -969,6 +981,21 @@ public static void Info(IGXLogger logger, string msg, params string[] list)
969981
}
970982
}
971983
}
984+
internal static void InfoSanitized(IGXLogger logger, string msg, params string[] list)
985+
{
986+
if (logger != null)
987+
{
988+
if (logger.IsInfoEnabled)
989+
{
990+
StringBuilder stringBuilder = new StringBuilder(StringUtil.Sanitize(msg, StringUtil.LogUserEntryWhiteList));
991+
foreach (string parm in list)
992+
{
993+
stringBuilder.Append(StringUtil.Sanitize(parm, StringUtil.LogUserEntryWhiteList));
994+
}
995+
logger.LogInfo(stringBuilder.ToString());
996+
}
997+
}
998+
}
972999
#endregion
9731000
}
9741001
}

dotnet/src/dotnetframework/GxClasses/Helpers/GXMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static public Type FindType(string defaultAssemblyName, string ns, string clssWi
111111
}
112112
catch(FileNotFoundException)
113113
{
114-
GXLogging.Warn(log, "Assembly: ", defaultAssemblyName, "not found");
114+
GXLogging.WarnSanitized(log, "Assembly: ", defaultAssemblyName, "not found");
115115
}
116116
catch(Exception ex)
117117
{

dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public override void webExecute()
225225
}
226226
if (code != 0)
227227
{
228-
GXLogging.Error(log, "Error executing ", commandType);
228+
GXLogging.WarnSanitized(log, "Error executing ", commandType);
229229
context.HttpContext.Response.Write(ERROR_LINE);
230230
}
231231
}

0 commit comments

Comments
 (0)