Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codeql : Fix to remove exception details from the response #10671

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/WebJobs.Script.WebHost/Management/VirtualFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ protected Task<HttpResponseMessage> CreateFileDeleteResponse(HttpRequest request

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

/// <summary>
/// Create unique etag based on the last modified UTC time
/// Create unique etag based on the last modified UTC time.
/// </summary>
private static Microsoft.Net.Http.Headers.EntityTagHeaderValue CreateEntityTag(FileSystemInfoBase sysInfo)
{
Expand Down Expand Up @@ -641,10 +640,40 @@ private IEnumerable<VfsStatEntry> GetDirectoryResponse(HttpRequest request, File
protected HttpResponseMessage CreateResponse(HttpStatusCode statusCode, object payload = null)
namanimsft marked this conversation as resolved.
Show resolved Hide resolved
{
var response = new HttpResponseMessage(statusCode);
if (payload != null)
try
{
if (payload != null)
{
// Use safe serialization settings
var jsonSerializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
Formatting = Formatting.None
};

// Sanitize the payload if it's an object
namanimsft marked this conversation as resolved.
Show resolved Hide resolved
var content = payload is string ? payload as string : JsonConvert.SerializeObject(payload, jsonSerializerSettings);
namanimsft marked this conversation as resolved.
Show resolved Hide resolved
response.Content = new StringContent(content, Encoding.UTF8, "application/json");
}
}
catch (JsonSerializationException je)
{
var content = payload is string ? payload as string : JsonConvert.SerializeObject(payload);
response.Content = new StringContent(content, Encoding.UTF8, "application/json");
// Return a generic error message to avoid exposing sensitive details
namanimsft marked this conversation as resolved.
Show resolved Hide resolved
_logger.LogError(je, je.Message);
response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("An error occurred while processing the response payload.", Encoding.UTF8, "text/plain")
};
}
catch (Exception ex)
{
// Return a generic error message to avoid exposing sensitive details
_logger.LogError(ex, ex.Message);
response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("An unexpected error occurred.", Encoding.UTF8, "text/plain")
};
}
return response;
}
Expand Down
Loading