@@ -464,8 +464,7 @@ protected Task<HttpResponseMessage> CreateFileDeleteResponse(HttpRequest request
464
464
465
465
/// <summary>
466
466
/// 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.
469
468
/// </summary>
470
469
protected bool IsRangeRequest ( HttpRequest request , Net . Http . Headers . EntityTagHeaderValue currentEtag )
471
470
{
@@ -531,7 +530,7 @@ private static Stream GetFileDeleteStream(FileInfoBase file)
531
530
}
532
531
533
532
/// <summary>
534
- /// Create unique etag based on the last modified UTC time
533
+ /// Create unique etag based on the last modified UTC time.
535
534
/// </summary>
536
535
private static Microsoft . Net . Http . Headers . EntityTagHeaderValue CreateEntityTag ( FileSystemInfoBase sysInfo )
537
536
{
@@ -641,10 +640,52 @@ private IEnumerable<VfsStatEntry> GetDirectoryResponse(HttpRequest request, File
641
640
protected HttpResponseMessage CreateResponse ( HttpStatusCode statusCode , object payload = null )
642
641
{
643
642
var response = new HttpResponseMessage ( statusCode ) ;
644
- if ( payload != null )
643
+ try
645
644
{
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
+ } ;
648
689
}
649
690
return response ;
650
691
}
0 commit comments