| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Minimizes untrusted metadata returned by host-owned managed-key clients. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class ManagedKeyProviderMetadataFilter |
| | | 9 | | { |
| | | 10 | | internal const int MaxMetadataCount = 16; |
| | | 11 | | internal const int MaxKeyLength = 64; |
| | | 12 | | internal const int MaxValueLength = 256; |
| | | 13 | | internal const int MaxAggregateLength = 2048; |
| | | 14 | | |
| | 2 | 15 | | private static readonly ReadOnlyDictionary<string, string> EmptyMetadata = |
| | 2 | 16 | | new(new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 17 | | |
| | 2 | 18 | | private static readonly Dictionary<string, string> AllowedKeys = new(StringComparer.OrdinalIgnoreCase) |
| | 2 | 19 | | { |
| | 2 | 20 | | ["provider_region"] = "provider_region", |
| | 2 | 21 | | ["provider_zone"] = "provider_zone", |
| | 2 | 22 | | ["provider_service"] = "provider_service", |
| | 2 | 23 | | ["provider_request_id"] = "provider_request_id", |
| | 2 | 24 | | ["provider_status_code"] = "provider_status_code", |
| | 2 | 25 | | ["provider_key_state"] = "provider_key_state" |
| | 2 | 26 | | }; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Returns a bounded, allowlisted copy of provider metadata. |
| | | 30 | | /// </summary> |
| | | 31 | | internal static IReadOnlyDictionary<string, string> Filter( |
| | | 32 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 33 | | { |
| | 82 | 34 | | if (metadata is null || metadata.Count == 0) |
| | | 35 | | { |
| | 24 | 36 | | return EmptyMetadata; |
| | | 37 | | } |
| | | 38 | | |
| | 58 | 39 | | Dictionary<string, string> filtered = new(StringComparer.Ordinal); |
| | 58 | 40 | | int aggregateLength = 0; |
| | | 41 | | |
| | 448 | 42 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 43 | | { |
| | 166 | 44 | | if (filtered.Count >= MaxMetadataCount) |
| | | 45 | | { |
| | 0 | 46 | | break; |
| | | 47 | | } |
| | | 48 | | |
| | 166 | 49 | | string candidateKey = item.Key.Trim(); |
| | 166 | 50 | | if (candidateKey.Length is 0 or > MaxKeyLength) |
| | | 51 | | { |
| | | 52 | | continue; |
| | | 53 | | } |
| | | 54 | | |
| | 164 | 55 | | if (!AllowedKeys.TryGetValue(candidateKey, out string? canonicalKey)) |
| | | 56 | | { |
| | | 57 | | continue; |
| | | 58 | | } |
| | | 59 | | |
| | 38 | 60 | | string candidateValue = item.Value?.Trim() ?? string.Empty; |
| | 38 | 61 | | if (candidateValue.Length > MaxValueLength || ContainsControlCharacter(candidateValue)) |
| | | 62 | | { |
| | | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 34 | 66 | | int entryLength = canonicalKey.Length + candidateValue.Length; |
| | 34 | 67 | | if (aggregateLength + entryLength > MaxAggregateLength) |
| | | 68 | | { |
| | | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | |
| | 34 | 72 | | if (filtered.TryAdd(canonicalKey, candidateValue)) |
| | | 73 | | { |
| | 34 | 74 | | aggregateLength += entryLength; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 58 | 78 | | return filtered.Count == 0 |
| | 58 | 79 | | ? EmptyMetadata |
| | 58 | 80 | | : new ReadOnlyDictionary<string, string>(filtered); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | private static bool ContainsControlCharacter(string value) |
| | | 84 | | { |
| | 6594 | 85 | | foreach (char character in value) |
| | | 86 | | { |
| | 3262 | 87 | | if (char.IsControl(character)) |
| | | 88 | | { |
| | 2 | 89 | | return true; |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | 34 | 93 | | return false; |
| | | 94 | | } |
| | | 95 | | } |