Skip to content

Commit 239f8fc

Browse files
authored
Codeql : Fix to remove exception details from the response (#10671)
1 parent 09f4edd commit 239f8fc

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/WebJobs.Script.WebHost/Management/VirtualFileSystem.cs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ protected Task<HttpResponseMessage> CreateFileDeleteResponse(HttpRequest request
464464

465465
/// <summary>
466466
/// Indicates whether this is a conditional range request containing an
467-
/// If-Range header with a matching etag and a Range header indicating the
468-
/// desired ranges
467+
/// If-Range header with a matching etag and a Range header indicating the desired ranges.
469468
/// </summary>
470469
protected bool IsRangeRequest(HttpRequest request, Net.Http.Headers.EntityTagHeaderValue currentEtag)
471470
{
@@ -531,7 +530,7 @@ private static Stream GetFileDeleteStream(FileInfoBase file)
531530
}
532531

533532
/// <summary>
534-
/// Create unique etag based on the last modified UTC time
533+
/// Create unique etag based on the last modified UTC time.
535534
/// </summary>
536535
private static Microsoft.Net.Http.Headers.EntityTagHeaderValue CreateEntityTag(FileSystemInfoBase sysInfo)
537536
{
@@ -641,10 +640,52 @@ private IEnumerable<VfsStatEntry> GetDirectoryResponse(HttpRequest request, File
641640
protected HttpResponseMessage CreateResponse(HttpStatusCode statusCode, object payload = null)
642641
{
643642
var response = new HttpResponseMessage(statusCode);
644-
if (payload != null)
643+
try
645644
{
646-
var content = payload is string ? payload as string : JsonConvert.SerializeObject(payload);
647-
response.Content = new StringContent(content, Encoding.UTF8, "application/json");
645+
if (payload != null)
646+
{
647+
// Use safe serialization settings
648+
var jsonSerializerSettings = new JsonSerializerSettings
649+
{
650+
NullValueHandling = NullValueHandling.Ignore,
651+
DefaultValueHandling = DefaultValueHandling.Include,
652+
Formatting = Formatting.None
653+
};
654+
655+
// Check if the payload is a string or an exception
656+
payload = payload switch
657+
{
658+
string str => str,
659+
Exception ex => ex.Message,
660+
_ => payload
661+
};
662+
663+
// Sanitize the payload if it's an object
664+
var content = payload switch
665+
{
666+
string str => str,
667+
_ => JsonConvert.SerializeObject(payload, jsonSerializerSettings)
668+
};
669+
response.Content = new StringContent(content, Encoding.UTF8, "application/json");
670+
}
671+
}
672+
catch (JsonSerializationException je)
673+
{
674+
// Return a generic error message to avoid exposing sensitive details
675+
_logger.LogError(je, je.Message);
676+
response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
677+
{
678+
Content = new StringContent("An error occurred while processing the response payload.", Encoding.UTF8, "text/plain")
679+
};
680+
}
681+
catch (Exception ex)
682+
{
683+
// Return a generic error message to avoid exposing sensitive details
684+
_logger.LogError(ex, ex.Message);
685+
response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
686+
{
687+
Content = new StringContent("An unexpected error occurred.", Encoding.UTF8, "text/plain")
688+
};
648689
}
649690
return response;
650691
}

0 commit comments

Comments
 (0)