From 0970b610b1ab5aa75134f98c8f5957fe1d8bb9ed Mon Sep 17 00:00:00 2001 From: Diogo Rolo Date: Wed, 20 Jul 2022 13:40:38 +0100 Subject: [PATCH] Fix null reference access when ContentType is null --- Octokit.Tests/Exceptions/ApiExceptionTests.cs | 11 +++++++++++ Octokit/Exceptions/ApiException.cs | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Octokit.Tests/Exceptions/ApiExceptionTests.cs b/Octokit.Tests/Exceptions/ApiExceptionTests.cs index bad38f72fe..7daa4aec38 100644 --- a/Octokit.Tests/Exceptions/ApiExceptionTests.cs +++ b/Octokit.Tests/Exceptions/ApiExceptionTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Net; #if !NO_SERIALIZABLE @@ -141,6 +142,16 @@ public void DoesNotThrowIfBodyIsNotDefined() Assert.NotNull(stringRepresentation); } + [Fact] + public void DoesNotThrowIfContentTypeIsNotDefined() + { + var response = CreateResponse(HttpStatusCode.GatewayTimeout, null, new Dictionary(), null); + + var exception = new ApiException(response); + var stringRepresentation = exception.ToString(); + Assert.NotNull(stringRepresentation); + } + [Fact] public void DoesNotPrintImageContent() { diff --git a/Octokit/Exceptions/ApiException.cs b/Octokit/Exceptions/ApiException.cs index 4dd08d27e4..9c99713605 100644 --- a/Octokit/Exceptions/ApiException.cs +++ b/Octokit/Exceptions/ApiException.cs @@ -200,7 +200,7 @@ protected string HttpResponseBodySafe { get { - return HttpResponse != null + return HttpResponse?.ContentType != null && !HttpResponse.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) && HttpResponse.Body is string ? (string)HttpResponse.Body : string.Empty;