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

UIResponseWriter - Do not encode non-ASCII characters #1984

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion build/versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<HealthChecksUIPostgreSQLStorage>7.0.0</HealthChecksUIPostgreSQLStorage>
<HealthCheckSystem>7.0.0</HealthCheckSystem>
<HealthCheckUI>7.0.1</HealthCheckUI>
<HealthCheckUIClient>7.0.0</HealthCheckUIClient>
<HealthCheckUIClient>7.1.0</HealthCheckUIClient>
<HealthCheckUICore>7.0.0</HealthCheckUICore>
<HealthCheckUIData>7.0.0</HealthCheckUIData>
<HealthCheckUIInMemoryStorage>7.0.0</HealthCheckUIInMemoryStorage>
Expand Down
3 changes: 3 additions & 0 deletions src/HealthChecks.UI.Client/UIResponseWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using HealthChecks.UI.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down Expand Up @@ -55,6 +57,7 @@ private static JsonSerializerOptions CreateJsonOptions()
{
AllowTrailingCommas = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Could be potentially breaking.

DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

Expand Down
24 changes: 24 additions & 0 deletions test/HealthChecks.UI.Client.Tests/UIResponseWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ public async Task should_response_with_obfuscated_exception_when_exception_messa
healthReport.ShouldNotBeNull().Entries[healthReportKey].Exception.ShouldBe(exceptionMessage);
}

[Theory]
[InlineData("你好")]
[InlineData("жарко")]
[InlineData("ąężćś")]
public async Task should_not_encode_unicode_characters(string healthReportKey)
{
var httpContext = new DefaultHttpContext();
httpContext.Response.Body = new MemoryStream();
var entries = new Dictionary<string, HealthReportEntry>
{
{ healthReportKey, new HealthReportEntry(HealthStatus.Healthy, null, TimeSpan.FromSeconds(1), null, null) }
};
var report = new HealthReport(entries, TimeSpan.FromSeconds(1));

await UIResponseWriter.WriteHealthCheckUIResponse(httpContext, report).ConfigureAwait(false);

httpContext.Response.Body.Seek(0, SeekOrigin.Begin);

var responseStreamReader = new StreamReader(httpContext.Response.Body);
var responseAsText = await responseStreamReader.ReadToEndAsync().ConfigureAwait(false);

responseAsText.ShouldContain(healthReportKey);
}

private static JsonSerializerOptions CreateJsonOptions()
{
var options = new JsonSerializerOptions
Expand Down
Loading