mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[AC-1637] Added HtmlEncodingStringConverter to encode/decode special chars on JSON serialization/deserialization
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Web;
|
||||
using NS = Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Core.Utilities;
|
||||
@ -204,3 +205,33 @@ public class PermissiveStringEnumerableConverter : JsonConverter<IEnumerable<str
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes incoming strings using HTML encoding
|
||||
/// and decodes outgoing strings using HTML decoding.
|
||||
/// </summary>
|
||||
public class HtmlEncodingStringConverter : JsonConverter<string>
|
||||
{
|
||||
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
var originalValue = reader.GetString();
|
||||
return HttpUtility.HtmlEncode(originalValue);
|
||||
}
|
||||
return reader.GetString();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
var encodedValue = HttpUtility.HtmlDecode(value);
|
||||
writer.WriteStringValue(encodedValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user