From 9e3e79876757d87b09cb88f871b76cd5d482543f Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 7 Jul 2023 15:58:10 -0500 Subject: [PATCH 1/2] Broken test demonstrating bug #1362 --- .../Events/EventTests.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/IdentityServer.UnitTests/Events/EventTests.cs diff --git a/test/IdentityServer.UnitTests/Events/EventTests.cs b/test/IdentityServer.UnitTests/Events/EventTests.cs new file mode 100644 index 000000000..9bd85eb23 --- /dev/null +++ b/test/IdentityServer.UnitTests/Events/EventTests.cs @@ -0,0 +1,45 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + + +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Duende.IdentityServer.Configuration; +using Duende.IdentityServer.Endpoints.Results; +using Duende.IdentityServer.Models; +using Duende.IdentityServer.ResponseHandling; +using Duende.IdentityServer.Validation; +using FluentAssertions; +using IdentityModel; +using UnitTests.Common; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.WebUtilities; +using Xunit; +using Duende.IdentityServer.Services; +using Duende.IdentityServer; +using Duende.IdentityServer.Events; + +namespace UnitTests.Endpoints.Results; + +public class EventTests +{ + + [Fact] + public void UnhandledExceptionEventCanCallToString() + { + try + { + throw new Exception(); + } + catch (Exception ex) + { + var unhandledExceptionEvent = new UnhandledExceptionEvent(ex); + + var s = unhandledExceptionEvent.ToString(); + + s.Should().NotBeNullOrEmpty(); + } + } +} \ No newline at end of file From 523723ee9c0d1351f07a2665406ecd5f2bc62ad0 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Wed, 20 Sep 2023 09:43:16 -0500 Subject: [PATCH 2/2] Suppress serialization of exceptions in events This fixes https://github.com/DuendeSoftware/IdentityServer/issues/1362 Thrown exceptions can't be serialized by System.Text.Json, because they contain pointers that it won't allow. This fix (suppressing serialization) is specific to the 6.3 release. In 7.0, we will remove the property entirely. --- src/IdentityServer/Events/UnhandledExceptionEvent.cs | 2 ++ test/IdentityServer.UnitTests/Events/EventTests.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/IdentityServer/Events/UnhandledExceptionEvent.cs b/src/IdentityServer/Events/UnhandledExceptionEvent.cs index 3bf5c6740..107b0e4ab 100644 --- a/src/IdentityServer/Events/UnhandledExceptionEvent.cs +++ b/src/IdentityServer/Events/UnhandledExceptionEvent.cs @@ -3,6 +3,7 @@ using System; +using System.Text.Json.Serialization; namespace Duende.IdentityServer.Events; @@ -41,5 +42,6 @@ public UnhandledExceptionEvent(Exception ex) /// /// The exception. /// + [JsonIgnore] // Don't try to serialize exceptions, because System.Text.Json will fail (it doesn't support the pointers inside a call stack) public Exception Exception { get; set; } } \ No newline at end of file diff --git a/test/IdentityServer.UnitTests/Events/EventTests.cs b/test/IdentityServer.UnitTests/Events/EventTests.cs index 9bd85eb23..e3ebb833f 100644 --- a/test/IdentityServer.UnitTests/Events/EventTests.cs +++ b/test/IdentityServer.UnitTests/Events/EventTests.cs @@ -31,7 +31,7 @@ public void UnhandledExceptionEventCanCallToString() { try { - throw new Exception(); + throw new InvalidOperationException("Boom"); } catch (Exception ex) {