Files
sbox-public/engine/Sandbox.Access/AccessSignature.cs
Matt Stevens 3f11417817 Whitelist: explicit Unsafe/Span/Memory (#5077)
* Whitelist System.Memory and System.ReadOnlyMemory (without pinning)
* Be very explicit with System.Span and System.ReadOnlySpan whitelists, do not allow pinnables
* Be completely explicit with Unsafe IL whitelist, allow Unsafe.SizeOf<T>
* BlacklistCodeWalker can have an allow list
* Compiler blacklist explicitly bans CreateSpan, allows Unsafe.SizeOf<T>
* Whitelist System.IO.InvalidDataException
* Better InlineArray whitelisting for compiler generated code
* WhitelistGen: Creates explicit signatures from wildcard rules
* Claude skill for whitelisting, uses the WhitelistGen tool to be very explicit and makes a good effort to security review
2026-06-17 18:28:47 +01:00

49 lines
1.5 KiB
C#

using System.Linq;
using Mono.Cecil;
namespace Sandbox;
/// <summary>
/// Produces the exact "touch name" string that access control matches whitelist/blacklist patterns
/// against. Kept in one place so tooling (the whitelist generator) emits strings that are guaranteed
/// to match what the scanner produces - see <see cref="AssemblyAccess"/>.
/// </summary>
public static class AccessSignature
{
/// <summary>
/// Signature for a type, e.g. <c>System.Private.CoreLib/System.Memory`1</c>.
/// </summary>
public static string Type( TypeDefinition type )
{
return $"{type.Module.Assembly.Name.Name}/{type.FullName}";
}
/// <summary>
/// Signature for a method, e.g. <c>System.Private.CoreLib/System.Memory`1.Slice( System.Int32 )</c>.
/// Constructors, operators and property accessors (<c>get_</c>/<c>set_</c>) are all methods.
/// </summary>
public static string Method( MethodDefinition method )
{
var name = $"{method.Module.Assembly.Name.Name}/{method.DeclaringType.FullName}.{method.Name}";
if ( method.HasGenericParameters )
{
var gparms = string.Join( ",", method.GenericParameters.Select( x => x.Name.ToString() ) );
if ( !string.IsNullOrWhiteSpace( gparms ) ) name += $"<{gparms}>";
}
if ( method.HasParameters )
{
var parms = string.Join( ", ", method.Parameters.Select( x => x.ParameterType.ToString() ) );
if ( !string.IsNullOrWhiteSpace( parms ) ) parms = $" {parms} ";
name += $"({parms})";
}
else
{
name += "()";
}
return name;
}
}