Restrict explicitly opted-in plain HTTP Garage connections to private IPv4 or loopback

This commit is contained in:
OpenGenesis (MPL 2.0) committed 2026-11-16 18:21:47 +01:00
1 parent dfd49b87f3
commit 761941142e
1 file changed
+17 -4
+17 -4
View File
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
using System.Security.Cryptography;
using System.Net;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
@@ -20,10 +21,22 @@ public sealed class OglS3ObjectMirror : IDisposable
if (endpoint == null || !endpoint.IsAbsoluteUri ||
(endpoint.Scheme != Uri.UriSchemeHttps && endpoint.Scheme != Uri.UriSchemeHttp))
throw new ArgumentException("An absolute S3 HTTP(S) endpoint is required.", nameof(endpoint));
if (endpoint.Scheme == Uri.UriSchemeHttp &&
!string.Equals(Environment.GetEnvironmentVariable("OGL_S3_ALLOW_PRIVATE_HTTP"), "true",
StringComparison.Ordinal))
throw new InvalidOperationException("Plain HTTP requires explicit private-network test opt-in.");
if (endpoint.Scheme == Uri.UriSchemeHttp)
{
if (!string.Equals(Environment.GetEnvironmentVariable("OGL_S3_ALLOW_PRIVATE_HTTP"), "true",
StringComparison.Ordinal))
throw new InvalidOperationException("Plain HTTP requires explicit private-network test opt-in.");
if (!IPAddress.TryParse(endpoint.Host, out IPAddress? address) ||
address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
throw new InvalidOperationException("Plain HTTP requires an explicit private IPv4 address.");
byte[] octets = address.GetAddressBytes();
bool privateNetwork = octets[0] == 10 ||
(octets[0] == 172 && octets[1] >= 16 && octets[1] <= 31) ||
(octets[0] == 192 && octets[1] == 168) ||
octets[0] == 127;
if (!privateNetwork)
throw new InvalidOperationException("Plain HTTP is restricted to private or loopback IPv4.");
}
if (string.IsNullOrWhiteSpace(bucket) || string.IsNullOrWhiteSpace(accessKey) ||
string.IsNullOrWhiteSpace(secretKey))
throw new ArgumentException("Bucket and credentials must be configured.");