1
0
mirror of https://github.com/bitwarden/server.git synced 2025-03-12 13:29:14 +01: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:
Matt Gibson 2021-07-02 17:11:33 -04:00 committed by GitHub
parent 7cfa54ba14
commit 2c9a5bb4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 19 deletions

View File

@ -12,11 +12,13 @@ using Azure.Storage.Queues.Models;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Bit.Core.Utilities;
namespace Bit.Admin.HostedServices namespace Bit.Admin.HostedServices
{ {
public class AzureQueueMailHostedService : IHostedService public class AzureQueueMailHostedService : IHostedService
{ {
private readonly JsonSerializer _jsonSerializer;
private readonly ILogger<AzureQueueMailHostedService> _logger; private readonly ILogger<AzureQueueMailHostedService> _logger;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly IMailService _mailService; private readonly IMailService _mailService;
@ -33,6 +35,11 @@ namespace Bit.Admin.HostedServices
_logger = logger; _logger = logger;
_mailService = mailService; _mailService = mailService;
_globalSettings = globalSettings; _globalSettings = globalSettings;
_jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
Converters = new[] { new EncodedStringConverter() },
});
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
@ -72,7 +79,7 @@ namespace Bit.Admin.HostedServices
var token = JToken.Parse(message.MessageText); var token = JToken.Parse(message.MessageText);
if (token is JArray) if (token is JArray)
{ {
foreach (var mailQueueMessage in token.ToObject<List<MailQueueMessage>>()) foreach (var mailQueueMessage in token.ToObject<List<MailQueueMessage>>(_jsonSerializer))
{ {
await _mailService.SendEnqueuedMailMessageAsync(mailQueueMessage); await _mailService.SendEnqueuedMailMessageAsync(mailQueueMessage);
} }

View File

@ -1,9 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Azure.Storage.Queues; using Azure.Storage.Queues;
using IdentityServer4.Extensions; using Bit.Core.Utilities;
using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Internal;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -18,13 +17,13 @@ namespace Bit.Core.Services
{ {
_queueClient = queueClient; _queueClient = queueClient;
_jsonSettings = jsonSettings; _jsonSettings = jsonSettings;
if (!_jsonSettings.Converters.Any(c => c.GetType() == typeof(EncodedStringConverter)))
{
_jsonSettings.Converters.Add(new EncodedStringConverter());
}
} }
public async Task CreateAsync(T message) public async Task CreateAsync(T message) => await CreateManyAsync(new[] { message });
{
var json = JsonConvert.SerializeObject(message, _jsonSettings);
await _queueClient.SendMessageAsync(json);
}
public async Task CreateManyAsync(IEnumerable<T> messages) public async Task CreateManyAsync(IEnumerable<T> messages)
{ {
@ -33,25 +32,21 @@ namespace Bit.Core.Services
return; return;
} }
if (!messages.Skip(1).Any()) foreach (var json in SerializeMany(messages))
{
await CreateAsync(messages.First());
return;
}
foreach (var json in SerializeMany(messages, _jsonSettings))
{ {
await _queueClient.SendMessageAsync(json); 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 messagesLists = new List<List<T>> { new List<T>() };
var strings = new List<string>(); var strings = new List<string>();
var ListMessageLength = 2; // to account for json array brackets "[]" 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 var messageLength = jsonEvent.Length + 1; // To account for json array comma
@ -66,7 +61,7 @@ namespace Bit.Core.Services
ListMessageLength += messageLength; ListMessageLength += messageLength;
} }
} }
return messagesLists.Select(l => JsonConvert.SerializeObject(l, jsonSettings)); return messagesLists.Select(l => JsonConvert.SerializeObject(l, _jsonSettings));
} }
} }
} }

View 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));
}
}
}

View File

@ -12,11 +12,13 @@ using Microsoft.Extensions.Logging;
using Azure.Storage.Queues; using Azure.Storage.Queues;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Bit.Core.Utilities;
namespace Bit.EventsProcessor namespace Bit.EventsProcessor
{ {
public class AzureQueueHostedService : IHostedService, IDisposable public class AzureQueueHostedService : IHostedService, IDisposable
{ {
private readonly JsonSerializer _jsonSerializer;
private readonly ILogger<AzureQueueHostedService> _logger; private readonly ILogger<AzureQueueHostedService> _logger;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
@ -31,6 +33,11 @@ namespace Bit.EventsProcessor
{ {
_logger = logger; _logger = logger;
_configuration = configuration; _configuration = configuration;
_jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
Converters = new[] { new EncodedStringConverter() },
});
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
@ -111,7 +118,7 @@ namespace Bit.EventsProcessor
var token = JToken.Parse(message); var token = JToken.Parse(message);
if (token is JArray) if (token is JArray)
{ {
var indexedEntities = token.ToObject<List<EventMessage>>() var indexedEntities = token.ToObject<List<EventMessage>>(_jsonSerializer)
.SelectMany(e => EventTableEntity.IndexEvent(e)); .SelectMany(e => EventTableEntity.IndexEvent(e));
events.AddRange(indexedEntities); events.AddRange(indexedEntities);
} }