
JGe356144 (Community Member) asked a question.
VeraCode scan reported several CWE 117 flaws in our application. So I did the research on VeraCode site and found the solution to cleanse the log before writing it to file. The code to cleanse the log is as following:
public static class LogSanitizer
{
/// <summary>
/// Remove the CRLF from the logentry and html encode the logentry.
/// </summary>
/// <param name="logEntry">The log message.</param>
/// <returns>The sanitized log message.</returns>
[CRLFCleanser]
public static string Sanitize(string logEntry)
{
var cleanLogEntry = WebUtility.HtmlEncode(logEntry.Replace("\r", "_").Replace("\n", "_"));
if (!logEntry.Equals(cleanLogEntry, StringComparison.OrdinalIgnoreCase))
{
cleanLogEntry += " (Sanitized)";
}
return cleanLogEntry;
}
}
Then in the application, I have the code to use the cleansing function:
var cleanMessage = LogSanitizer.Sanitize(message);
Logger.Error(cleanMessage);
However, VeraCode scan still report the same CWE 117 flaw on line Logger.Error(cleanMessage);
Is there anything I did wrong? Why VeraCode scan cannot recognize the fix?
Thanks,
.png)
Hi @JGe356144 (Community Member) ,
Great question! Once the custom cleanser feature is enabled for your account, the Security Lead or Veracode Administrator will need to decide on what should happen when the Veracode Platform sees a Veracode Annotation within your application scan. Here are the 3 possible options:
For more information on custom cleansers, please review https://community.veracode.com/s/article/How-To-Use-Custom-Cleanser .
Based on your description, either the default setting of "None" is still in place for your account or you might already find some auto-proposed mitigations for these flaws, which need to be approved by your security team for the flaw to not affect your policy anymore.
Thank you,
Florian Walter
Hi, Florian,
Thanks for the reply. Yes, they are in the proposed state. However, the code I put in should solve the issue, not only propose the mitigation.
WebUtility.HtmlEncode() is VeraCode approved cleanser method for log. I also removed the \n and \r in the message writing to the log. I expected VeraCode scan recognized the code and remove the issues.
Thanks,
Jason
Hi @JGe356144 (Community Member) ,
Happy to hear that the custom cleanser works as intended. This flaw being reported with WebUtility.HtmlEncode() in place actually does look odd to me, too. I would like more information about this and would recommend you contact our technical support team. Here's how you can log a case:
1. Navigate to the upper right corner of any page in the Community, click on your user avatar.
2. Select Contact Support from the drop-down menu.
Thank you,
Florian Walter
Hi Florian,
We are thinking of creating the set of 100% effective custom cleansers for Redirect , 117 Log cleaning and get that reviewed by our security team and then get a 3. Approve settings so that we don't need to worry during each code change. Just imagine a line is changed on a controller and all the mitigated issues has to be readdressed!
So we are looking to get an method which will satisfy the scanner and ensure the 117 threat is properly addressed. We tried extension methods and we were told the scanner may not detect it , so created the direct method and use it every where we need to log entry which has a user input. What else needs to be changed?
/// <summary>
/// This string extension is used to remove line feeds and encode HTML, Used to log - mitigate CWE ID 117
/// </summary>
/// <param name="logentry">string to be logged</param>
/// <returns>cleaned string</returns>
[CRLFCleanser]
public static string SanitizeLogEntry(this string logentry)
{
logentry = logentry.Replace(Environment.NewLine, "New Line Feed removed");
return WebUtility.HtmlEncode(HttpUtility.UrlEncode(logentry));
}
/// <summary>
/// This function extension is used to remove line feeds and encode HTML, Used to log - mitigate CWE ID 117
/// </summary>
/// <param name="logentry">string to be logged</param>
/// <returns>cleaned string</returns>
[CRLFCleanser]
public static string Sanitize(string logentry)
{
logentry = logentry.Replace(Environment.NewLine, "New Line Feed removed");
return WebUtility.HtmlEncode(HttpUtility.UrlEncode(logentry));
}
Hi @NM257998 (Community Member) ,
As you mentioned WebUtility.HtmlEncode is a supported cleansing function, its consistent use on all input into a logging entry should automatically close a flaw. If it does not I would recommend you contact our technical support team. Here's how you can log a case:
1. Navigate to the upper right corner of any page in the Community, click on your user avatar.
2. Select Contact Support from the drop-down menu.
I would also recommend not using a Custom Cleanser attribute if you're already using a supported cleansing function, this should not be necessary.
Thank you,
Boy Baukema
Add the the above situation - inconsistent scan behavior noticed.
public static class CustomCleanserExtensions
{
/// <summary>
/// This string extension is used to remove line feeds and encode HTML, Used to log - mitigate CWE ID 117
/// </summary>
/// <param name="logentry">string to be logged</param>
/// <returns>cleaned string</returns>
[CRLFCleanser]
public static string SanitizeLogEntry(this string logentry)
{
logentry = logentry.Replace(Environment.NewLine, "New Line Feed removed");
return WebUtility.HtmlEncode(HttpUtility.UrlEncode(logentry));
}
/// <summary>
/// This function extension is used to remove line feeds and encode HTML, Used to log - mitigate CWE ID 117
/// </summary>
/// <param name="logentry">string to be logged</param>
/// <returns>cleaned string</returns>
[CRLFCleanser]
public static string Sanitize(string logentry)
{
logentry = logentry.Replace(Environment.NewLine, "New Line Feed removed");
return WebUtility.HtmlEncode(HttpUtility.UrlEncode(logentry));
}
}
Scanner didn't pick up this as Mitigation status = Proposed - extension method not detected.
_logger.LogError(message, HttpUtility.UrlEncode($"Unable to send SMS to {model.MobileNumber.SanitizeLogEntry()} for {model.UserName.SanitizeLogEntry()}"), null);
Scanner picked up this as Mitigation status = Proposed.
_logger.LogError(e, $"Profile Error for: {CustomCleanserExtensions.Sanitize(model.UserName)}", null);
I would recommend removing the custom cleanser attribute and if this does not resolve the flaw I would recommend you contact our technical support team. Here's how you can log a case:
1. Navigate to the upper right corner of any page in the Community, click on your user avatar.
2. Select Contact Support from the drop-down menu.
Thank you,
Boy Baukema