mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
Encode into b64 to avoid illegal xml encoding when sending to Azure (#1425)
* Encode into b64 to avoid illegal xml encoding when sending to Azure
* Revert "Encode into b64 to avoid illegal xml encoding when sending to Azure"
This reverts commit d50de941da
.
* HtmlEncode strings if they use multi-byte characters
* Add serializer to event processor
* Rename to used class
* Formatting
* PR feedback
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Azure.Storage.Queues;
|
||||
using IdentityServer4.Extensions;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@ -18,13 +17,13 @@ namespace Bit.Core.Services
|
||||
{
|
||||
_queueClient = queueClient;
|
||||
_jsonSettings = jsonSettings;
|
||||
if (!_jsonSettings.Converters.Any(c => c.GetType() == typeof(EncodedStringConverter)))
|
||||
{
|
||||
_jsonSettings.Converters.Add(new EncodedStringConverter());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateAsync(T message)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(message, _jsonSettings);
|
||||
await _queueClient.SendMessageAsync(json);
|
||||
}
|
||||
public async Task CreateAsync(T message) => await CreateManyAsync(new[] { message });
|
||||
|
||||
public async Task CreateManyAsync(IEnumerable<T> messages)
|
||||
{
|
||||
@ -33,25 +32,21 @@ namespace Bit.Core.Services
|
||||
return;
|
||||
}
|
||||
|
||||
if (!messages.Skip(1).Any())
|
||||
{
|
||||
await CreateAsync(messages.First());
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var json in SerializeMany(messages, _jsonSettings))
|
||||
foreach (var json in SerializeMany(messages))
|
||||
{
|
||||
await _queueClient.SendMessageAsync(json);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected IEnumerable<string> SerializeMany(IEnumerable<T> messages, JsonSerializerSettings jsonSettings)
|
||||
private IEnumerable<string> SerializeMany(IEnumerable<T> messages)
|
||||
{
|
||||
string SerializeMessage(T message) => JsonConvert.SerializeObject(message, _jsonSettings);
|
||||
|
||||
var messagesLists = new List<List<T>> { new List<T>() };
|
||||
var strings = new List<string>();
|
||||
var ListMessageLength = 2; // to account for json array brackets "[]"
|
||||
foreach (var (message, jsonEvent) in messages.Select(e => (e, JsonConvert.SerializeObject(e, jsonSettings))))
|
||||
foreach (var (message, jsonEvent) in messages.Select(m => (m, SerializeMessage(m))))
|
||||
{
|
||||
|
||||
var messageLength = jsonEvent.Length + 1; // To account for json array comma
|
||||
@ -66,7 +61,7 @@ namespace Bit.Core.Services
|
||||
ListMessageLength += messageLength;
|
||||
}
|
||||
}
|
||||
return messagesLists.Select(l => JsonConvert.SerializeObject(l, jsonSettings));
|
||||
return messagesLists.Select(l => JsonConvert.SerializeObject(l, _jsonSettings));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
35
src/Core/Utilities/EncodedStringConverter.cs
Normal file
35
src/Core/Utilities/EncodedStringConverter.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
public class EncodedStringConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType) => objectType == typeof(string);
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return existingValue;
|
||||
}
|
||||
|
||||
var value = reader.Value as string;
|
||||
return System.Net.WebUtility.HtmlDecode(value);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
if (serializer.NullValueHandling == NullValueHandling.Include)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteValue(System.Net.WebUtility.HtmlEncode((string)value));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user