Skip to content

Commit

Permalink
Deserialize compressed documents correctly (#16415)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Jul 11, 2024
1 parent d51f74a commit 5c74898
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using OrchardCore.Data.Documents;
Expand All @@ -12,15 +13,19 @@ namespace OrchardCore.Documents
/// </summary>
public class DefaultDocumentSerializer : IDocumentSerializer
{
public static readonly DefaultDocumentSerializer Instance = new();
private static readonly byte[] _gZipHeaderBytes = [0x1f, 0x8b];

private readonly JsonSerializerOptions _serializerOptions;

public DefaultDocumentSerializer()
public DefaultDocumentSerializer(JsonSerializerOptions serializerOptions)
{
_serializerOptions = serializerOptions;
}

public Task<byte[]> SerializeAsync<TDocument>(TDocument document, int compressThreshold = int.MaxValue) where TDocument : class, IDocument, new()
public Task<byte[]> SerializeAsync<TDocument>(TDocument document, int compressThreshold = int.MaxValue)
where TDocument : class, IDocument, new()
{
var data = Encoding.UTF8.GetBytes(JConvert.SerializeObject(document));
var data = JsonSerializer.SerializeToUtf8Bytes(document, _serializerOptions);
if (data.Length >= compressThreshold)
{
data = Compress(data);
Expand All @@ -29,22 +34,32 @@ public DefaultDocumentSerializer()
return Task.FromResult(data);
}

public Task<TDocument> DeserializeAsync<TDocument>(byte[] data) where TDocument : class, IDocument, new()
public Task<TDocument> DeserializeAsync<TDocument>(byte[] data)
where TDocument : class, IDocument, new()
{
if (IsCompressed(data))
{
data = Decompress(data);
}

var document = JConvert.DeserializeObject<TDocument>(Encoding.UTF8.GetString(data));
using var ms = new MemoryStream(data);

var document = JsonSerializer.Deserialize<TDocument>(ms, _serializerOptions);

return Task.FromResult(document);
}

private static readonly byte[] _gZipHeaderBytes = [0x1f, 0x8b];
internal static bool IsCompressed(byte[] data)
{
// Ensure data is at least as long as the GZip header
if (data.Length >= _gZipHeaderBytes.Length)
{
// Compare the header bytes.
return data.Take(_gZipHeaderBytes.Length).SequenceEqual(_gZipHeaderBytes);
}

internal static bool IsCompressed(byte[] data) =>
data.Length < _gZipHeaderBytes.Length && data[0..1] == _gZipHeaderBytes;
return false;
}

internal static byte[] Compress(byte[] data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Json;

namespace OrchardCore.Documents.Options
{
Expand All @@ -11,13 +12,20 @@ public class DocumentOptionsSetup : IConfigureNamedOptions<DocumentOptions>
public static readonly TimeSpan DefaultFailoverRetryLatency = TimeSpan.FromSeconds(30);

private readonly IShellConfiguration _shellConfiguration;
private readonly DocumentJsonSerializerOptions _documentJsonSerializerOptions;

public DocumentOptionsSetup(IShellConfiguration shellConfiguration)
private DefaultDocumentSerializer _defaultDocumentSerializer;

public DocumentOptionsSetup(
IShellConfiguration shellConfiguration,
IOptions<DocumentJsonSerializerOptions> documentJsonSerializerOptions)
{
_shellConfiguration = shellConfiguration;
_documentJsonSerializerOptions = documentJsonSerializerOptions.Value;
}

public void Configure(DocumentOptions options) => Configure(string.Empty, options);
public void Configure(DocumentOptions options)
=> Configure(string.Empty, options);

public void Configure(string name, DocumentOptions options)
{
Expand Down Expand Up @@ -46,7 +54,7 @@ public void Configure(string name, DocumentOptions options)
?? sharedConfig.SynchronizationLatency
?? TimeSpan.FromSeconds(1);

options.Serializer = DefaultDocumentSerializer.Instance;
options.Serializer = _defaultDocumentSerializer ??= new DefaultDocumentSerializer(_documentJsonSerializerOptions.SerializerOptions);

options.CompressThreshold = namedConfig.CompressThreshold;
if (options.CompressThreshold == 0)
Expand Down

0 comments on commit 5c74898

Please sign in to comment.