Skip to content

Commit

Permalink
Prevent external codecs from serializing types which are abstract, ge…
Browse files Browse the repository at this point in the history
…nerated, or from the framework itself (#8765)
  • Loading branch information
ReubenBond authored Dec 13, 2023
1 parent eca20f5 commit d6894ae
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 119 deletions.
5 changes: 5 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@
<SourceLinkCreate>true</SourceLinkCreate>
<SourceLinkOriginUrl>https://github.com/dotnet/orleans</SourceLinkOriginUrl>
</PropertyGroup>

<ItemGroup>
<AssemblyAttribute Include="Orleans.Metadata.FrameworkPartAttribute" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions src/Orleans.Analyzers/Orleans.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Remove="Orleans.Metadata.FrameworkPartAttribute"/>
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions src/Orleans.CodeGenerator/Orleans.CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Remove="Orleans.Metadata.FrameworkPartAttribute"/>
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/Orleans.Serialization.Abstractions/FrameworkPartAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.ComponentModel;

namespace Orleans.Metadata;

/// <summary>
/// Specifies that an assembly does not contain application code.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class FrameworkPartAttribute : Attribute
{
}
13 changes: 13 additions & 0 deletions src/Orleans.Serialization.NewtonsoftJson/NewtonsoftJsonCodec.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Orleans.Metadata;
using Orleans.Serialization.Buffers;
using Orleans.Serialization.Buffers.Adaptors;
using Orleans.Serialization.Cloning;
Expand Down Expand Up @@ -124,6 +127,11 @@ bool IGeneralizedCodec.IsSupportedType(Type type)
return true;
}

if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type))
{
return false;
}

foreach (var selector in _serializableTypeSelectors)
{
if (selector.IsSupportedType(type))
Expand Down Expand Up @@ -173,6 +181,11 @@ object IDeepCopier.DeepCopy(object input, CopyContext context)
/// <inheritdoc/>
bool IGeneralizedCopier.IsSupportedType(Type type)
{
if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type))
{
return false;
}

foreach (var selector in _copyableTypeSelectors)
{
if (selector.IsSupportedType(type))
Expand Down
12 changes: 12 additions & 0 deletions src/Orleans.Serialization.SystemTextJson/JsonCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.Options;
using Orleans.Metadata;
using Orleans.Serialization.Buffers;
using Orleans.Serialization.Buffers.Adaptors;
using Orleans.Serialization.Cloning;
Expand Down Expand Up @@ -158,6 +160,11 @@ bool IGeneralizedCodec.IsSupportedType(Type type)
return true;
}

if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type))
{
return false;
}

foreach (var selector in _serializableTypeSelectors)
{
if (selector.IsSupportedType(type))
Expand Down Expand Up @@ -204,6 +211,11 @@ object IDeepCopier.DeepCopy(object input, CopyContext context)
/// <inheritdoc/>
bool IGeneralizedCopier.IsSupportedType(Type type)
{
if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type))
{
return false;
}

foreach (var selector in _copyableTypeSelectors)
{
if (selector.IsSupportedType(type))
Expand Down
31 changes: 31 additions & 0 deletions src/Orleans.Serialization/Codecs/CommonCodecTypeFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.CodeDom.Compiler;
using System.Linq;
using System.Reflection;

using Orleans.Metadata;

namespace Orleans.Serialization.Codecs;

/// <summary>
/// Defines common type filtering operations.
/// </summary>
public class CommonCodecTypeFilter
{
/// <summary>
/// Returns true if the provided type is a framework or abstract type.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns><see langword="true"/> if the type is a framework or abstract type, otherwise <see langword="false"/>.</returns>
public static bool IsAbstractOrFrameworkType(Type type)
{
if (type.IsAbstract
|| type.GetCustomAttributes<GeneratedCodeAttribute>().Any(a => a.Tool.Equals("OrleansCodeGen"))
|| type.Assembly.GetCustomAttribute<FrameworkPartAttribute>() is not null)
{
return true;
}

return false;
}
}
12 changes: 12 additions & 0 deletions src/Serializers/Orleans.Serialization.Protobuf/ProtobufCodec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Google.Protobuf;
using Orleans.Metadata;
using Orleans.Serialization.Buffers;
using Orleans.Serialization.Cloning;
using Orleans.Serialization.Codecs;
Expand All @@ -8,6 +9,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Orleans.Serialization;

Expand Down Expand Up @@ -68,6 +70,11 @@ bool IGeneralizedCodec.IsSupportedType(Type type)
return true;
}

if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type))
{
return false;
}

foreach (var selector in _serializableTypeSelectors)
{
if (selector.IsSupportedType(type))
Expand All @@ -82,6 +89,11 @@ bool IGeneralizedCodec.IsSupportedType(Type type)
/// <inheritdoc/>
bool IGeneralizedCopier.IsSupportedType(Type type)
{
if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type))
{
return false;
}

foreach (var selector in _copyableTypeSelectors)
{
if (selector.IsSupportedType(type))
Expand Down
2 changes: 2 additions & 0 deletions test/NonSilo.Tests/NonSilo.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ProjectReference Include="$(SourceRoot)test\Tester\Tester.csproj" />
<ProjectReference Include="$(SourceRoot)src\Orleans.Serialization.NewtonsoftJson\Orleans.Serialization.NewtonsoftJson.csproj" />
<ProjectReference Include="$(SourceRoot)test\Misc\TestInternalDtosRefOrleans\TestInternalDtosRefOrleans.csproj" />
<ProjectReference Include="..\..\src\Orleans.Serialization.SystemTextJson\Orleans.Serialization.SystemTextJson.csproj" />
<ProjectReference Include="..\..\src\Serializers\Orleans.Serialization.Protobuf\Orleans.Serialization.Protobuf.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit d6894ae

Please sign in to comment.