| | | 1 | | using System.Reflection; |
| | | 2 | | using Xunit; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Testing.Shared.Authorization; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// The fail-closed endpoint gate, as a reusable assertion. Omitting a declaration inherits the |
| | | 8 | | /// FastEndpoints default with no Elsa-level fallback, so an endpoint can reach production ungated with |
| | | 9 | | /// nobody noticing. This asserts every endpoint in an assembly states its access explicitly. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Reflection over <c>Configure()</c> is deliberate. Booting a host would prove more, but would require |
| | | 13 | | /// every endpoint-bearing module as a dependency of one test project, which is what stopped such a gate |
| | | 14 | | /// existing. A module opts in with a single test calling <see cref="AssertEveryEndpointDeclaresAccess"/>. |
| | | 15 | | /// </remarks> |
| | | 16 | | public static class EndpointCoverage |
| | | 17 | | { |
| | | 18 | | // The declaration helpers are protected, so they are matched by name. |
| | 0 | 19 | | private static readonly string[] Declarations = |
| | 0 | 20 | | [ |
| | 0 | 21 | | "RequirePermission", |
| | 0 | 22 | | "RequireAuthenticatedOnly", |
| | 0 | 23 | | "ConfigurePermissions", |
| | 0 | 24 | | "AllowAnonymous" |
| | 0 | 25 | | ]; |
| | | 26 | | |
| | | 27 | | /// <summary>Asserts every Elsa endpoint in <paramref name="assembly"/> declares its access.</summary> |
| | | 28 | | public static void AssertEveryEndpointDeclaresAccess(Assembly assembly) |
| | | 29 | | { |
| | 0 | 30 | | var endpoints = FindEndpoints(assembly).ToArray(); |
| | | 31 | | |
| | | 32 | | // A reflection gate that silently matches nothing passes forever. |
| | 0 | 33 | | Assert.True(endpoints.Length > 0, $"No Elsa endpoints found in {assembly.GetName().Name}. The gate is not lookin |
| | | 34 | | |
| | 0 | 35 | | var undeclared = endpoints.Where(x => !DeclaresAccess(x)).Select(x => x.FullName).OrderBy(x => x, StringComparer |
| | | 36 | | |
| | 0 | 37 | | Assert.True( |
| | 0 | 38 | | undeclared.Length == 0, |
| | 0 | 39 | | $"{undeclared.Length} endpoint(s) declare no access: {string.Join(", ", undeclared)}. " |
| | 0 | 40 | | + "Every endpoint must call exactly one of RequirePermission, RequireAuthenticatedOnly, or AllowAnonymous. " |
| | 0 | 41 | | + "There is no exemption list: an endpoint that states nothing is indistinguishable from one whose author fo |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>The Elsa endpoint types declared in <paramref name="assembly"/>.</summary> |
| | | 45 | | public static IEnumerable<Type> FindEndpoints(Assembly assembly) |
| | | 46 | | { |
| | | 47 | | Type[] types; |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 0 | 51 | | types = assembly.GetTypes(); |
| | 0 | 52 | | } |
| | | 53 | | catch (ReflectionTypeLoadException ex) |
| | | 54 | | { |
| | 0 | 55 | | types = ex.Types.Where(x => x is not null).ToArray()!; |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return types.Where(IsEndpoint); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | private static bool IsEndpoint(Type type) |
| | | 62 | | { |
| | 0 | 63 | | if (type is not { IsClass: true, IsAbstract: false }) |
| | 0 | 64 | | return false; |
| | | 65 | | |
| | 0 | 66 | | for (var baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType) |
| | | 67 | | { |
| | 0 | 68 | | if (baseType.FullName?.StartsWith("Elsa.Abstractions.ElsaEndpoint", StringComparison.Ordinal) == true) |
| | 0 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private static bool DeclaresAccess(Type endpointType) |
| | | 76 | | { |
| | 0 | 77 | | var configure = endpointType.GetMethod("Configure", BindingFlags.Public | BindingFlags.Instance | BindingFlags.D |
| | | 78 | | |
| | | 79 | | // An endpoint inheriting Configure from an abstract base declares through that base. |
| | 0 | 80 | | if (configure is null) |
| | 0 | 81 | | return endpointType.BaseType is not null && endpointType.BaseType.IsAbstract && DeclaresAccessOnAnyBase(endp |
| | | 82 | | |
| | 0 | 83 | | return ReferencesDeclaration(configure); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static bool DeclaresAccessOnAnyBase(Type baseType) |
| | | 87 | | { |
| | 0 | 88 | | for (var type = baseType; type is not null; type = type.BaseType) |
| | | 89 | | { |
| | 0 | 90 | | var configure = type.GetMethod("Configure", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Decla |
| | | 91 | | |
| | 0 | 92 | | if (configure is not null && ReferencesDeclaration(configure)) |
| | 0 | 93 | | return true; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return false; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static bool ReferencesDeclaration(MethodInfo configure) |
| | | 100 | | { |
| | 0 | 101 | | var body = configure.GetMethodBody(); |
| | | 102 | | |
| | 0 | 103 | | if (body is null) |
| | 0 | 104 | | return false; |
| | | 105 | | |
| | 0 | 106 | | var module = configure.Module; |
| | 0 | 107 | | var il = body.GetILAsByteArray() ?? []; |
| | | 108 | | |
| | 0 | 109 | | for (var i = 0; i < il.Length - 4; i++) |
| | | 110 | | { |
| | | 111 | | // 0x28 call, 0x6F callvirt. |
| | 0 | 112 | | if (il[i] is not (0x28 or 0x6F)) |
| | | 113 | | continue; |
| | | 114 | | |
| | | 115 | | try |
| | | 116 | | { |
| | 0 | 117 | | var called = module.ResolveMethod(BitConverter.ToInt32(il, i + 1)); |
| | | 118 | | |
| | 0 | 119 | | if (called is not null && Declarations.Contains(called.Name, StringComparer.Ordinal)) |
| | 0 | 120 | | return true; |
| | 0 | 121 | | } |
| | 0 | 122 | | catch (Exception ex) when (ex is ArgumentException or BadImageFormatException or MissingMethodException) |
| | | 123 | | { |
| | | 124 | | // The bytes at this offset are not a resolvable method token; keep scanning. |
| | 0 | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | return false; |
| | 0 | 129 | | } |
| | | 130 | | } |