|
| 1 | +using System.Net; |
| 2 | +using System.Text; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace KeetaNet.Anchor.Crypto; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Networked convenience for sharable-attribute external references. The core |
| 9 | +/// ingests external blobs but never performs I/O. This fetches each discovered |
| 10 | +/// reference's stored bytes, decoding <c>data:</c> URLs inline. |
| 11 | +/// </summary> |
| 12 | +public static class ExternalReferences |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Fetch every reference's blob, keyed by the reference's digest id: a |
| 16 | + /// <c>data:</c> URL decodes inline, an http(s) URL is fetched with |
| 17 | + /// <paramref name="httpClient"/>, and a JSON response in the |
| 18 | + /// storage-service <c>{data, mimeType}</c> convention is unwrapped. |
| 19 | + /// </summary> |
| 20 | + /// <exception cref="KeetaException"> |
| 21 | + /// Code <c>REFERENCE_FETCH</c> when a URL cannot be decoded or the server |
| 22 | + /// does not answer 200. |
| 23 | + /// </exception> |
| 24 | + public static async Task<IReadOnlyDictionary<string, byte[]>> FetchBlobs( |
| 25 | + HttpClient httpClient, |
| 26 | + IEnumerable<AttributeReference> references, |
| 27 | + CancellationToken cancellationToken = default) |
| 28 | + { |
| 29 | + var blobs = new Dictionary<string, byte[]>(); |
| 30 | + foreach (AttributeReference reference in references) |
| 31 | + { |
| 32 | + blobs[reference.Id] = await FetchReference(httpClient, reference, cancellationToken).ConfigureAwait(false); |
| 33 | + } |
| 34 | + |
| 35 | + return blobs; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary>Fetch one reference's raw stored bytes from its URL.</summary> |
| 39 | + private static async Task<byte[]> FetchReference( |
| 40 | + HttpClient httpClient, |
| 41 | + AttributeReference reference, |
| 42 | + CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + string url = reference.Url; |
| 45 | + if (url.StartsWith("data:", StringComparison.Ordinal)) |
| 46 | + { |
| 47 | + return DecodeDataUrl(url); |
| 48 | + } |
| 49 | + |
| 50 | + using HttpResponseMessage response = await Get(httpClient, url, cancellationToken).ConfigureAwait(false); |
| 51 | + if (response.StatusCode != HttpStatusCode.OK) |
| 52 | + { |
| 53 | + throw new KeetaException( |
| 54 | + "REFERENCE_FETCH", |
| 55 | + $"the server answered {(int)response.StatusCode} for `{url}`"); |
| 56 | + } |
| 57 | + |
| 58 | + byte[] body = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false); |
| 59 | + return UnwrapContainerPayload(url, body); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary>Issue the GET, surfacing a transport failure as the stable typed error.</summary> |
| 63 | + private static async Task<HttpResponseMessage> Get( |
| 64 | + HttpClient httpClient, |
| 65 | + string url, |
| 66 | + CancellationToken cancellationToken) |
| 67 | + { |
| 68 | + if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri) |
| 69 | + || (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) |
| 70 | + { |
| 71 | + throw new KeetaException("REFERENCE_FETCH", $"the reference URL `{url}` is not a fetchable http(s) URL"); |
| 72 | + } |
| 73 | + |
| 74 | + try |
| 75 | + { |
| 76 | + return await httpClient.GetAsync(uri, cancellationToken).ConfigureAwait(false); |
| 77 | + } |
| 78 | + catch (HttpRequestException error) |
| 79 | + { |
| 80 | + throw new KeetaException("REFERENCE_FETCH", $"the request to `{url}` failed", error); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary>Decode a <c>data:<media type>[;base64],<data></c> URL body.</summary> |
| 85 | + private static byte[] DecodeDataUrl(string url) |
| 86 | + { |
| 87 | + string rest = url["data:".Length..]; |
| 88 | + int separator = rest.IndexOf(',', StringComparison.Ordinal); |
| 89 | + if (separator < 0) |
| 90 | + { |
| 91 | + throw new KeetaException("REFERENCE_FETCH", $"the data URL `{url}` has no payload"); |
| 92 | + } |
| 93 | + |
| 94 | + string header = rest[..separator]; |
| 95 | + string data = rest[(separator + 1)..]; |
| 96 | + if (!header.EndsWith(";base64", StringComparison.Ordinal)) |
| 97 | + { |
| 98 | + return Encoding.UTF8.GetBytes(data); |
| 99 | + } |
| 100 | + |
| 101 | + return DecodeBase64(data, $"the data URL `{url}`"); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Unwrap the storage-service container-payload convention: a JSON body of |
| 106 | + /// exactly <c>{data, mimeType}</c> (both strings) carries the base64 stored |
| 107 | + /// bytes. Anything else is the stored bytes themselves. |
| 108 | + /// </summary> |
| 109 | + /// <exception cref="KeetaException"> |
| 110 | + /// Code <c>REFERENCE_FETCH</c> when the wrapper shape is detected but its |
| 111 | + /// base64 payload does not decode, so a corrupted wrapper surfaces here |
| 112 | + /// instead of as a later decryption failure. |
| 113 | + /// </exception> |
| 114 | + private static byte[] UnwrapContainerPayload(string url, byte[] body) |
| 115 | + { |
| 116 | + string? data = WrappedPayload(body); |
| 117 | + if (data is null) |
| 118 | + { |
| 119 | + return body; |
| 120 | + } |
| 121 | + |
| 122 | + return DecodeBase64(data, $"the wrapped payload from `{url}`"); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary>Decode base64 <paramref name="data"/>, naming <paramref name="source"/> on failure.</summary> |
| 126 | + private static byte[] DecodeBase64(string data, string source) |
| 127 | + { |
| 128 | + try |
| 129 | + { |
| 130 | + return Convert.FromBase64String(data); |
| 131 | + } |
| 132 | + catch (FormatException error) |
| 133 | + { |
| 134 | + throw new KeetaException("REFERENCE_FETCH", $"{source} is not valid base64", error); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// The base64 payload of an exact <c>{data, mimeType}</c> wrapper, or |
| 140 | + /// <c>null</c> when the body is anything else. |
| 141 | + /// </summary> |
| 142 | + private static string? WrappedPayload(byte[] body) |
| 143 | + { |
| 144 | + try |
| 145 | + { |
| 146 | + using JsonDocument parsed = JsonDocument.Parse(body); |
| 147 | + JsonElement root = parsed.RootElement; |
| 148 | + if (root.ValueKind != JsonValueKind.Object) |
| 149 | + { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + string? data = null; |
| 154 | + bool hasMimeType = false; |
| 155 | + int properties = 0; |
| 156 | + using JsonElement.ObjectEnumerator enumerated = root.EnumerateObject(); |
| 157 | + foreach (JsonProperty property in enumerated) |
| 158 | + { |
| 159 | + properties++; |
| 160 | + if (property.Name == "data" && property.Value.ValueKind == JsonValueKind.String) |
| 161 | + { |
| 162 | + data = property.Value.GetString(); |
| 163 | + } |
| 164 | + else if (property.Name == "mimeType" && property.Value.ValueKind == JsonValueKind.String) |
| 165 | + { |
| 166 | + hasMimeType = true; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (properties != 2 || !hasMimeType) |
| 171 | + { |
| 172 | + return null; |
| 173 | + } |
| 174 | + |
| 175 | + return data; |
| 176 | + } |
| 177 | + catch (JsonException) |
| 178 | + { |
| 179 | + // A non-JSON body is the stored bytes themselves (e.g. raw DER). |
| 180 | + return null; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments