Skip to content

Commit ad40cc3

Browse files
Tomas Weinfurtcarlossanlop
Tomas Weinfurt
authored andcommitted
Merged PR 32989: [release/6.0] limit AIA download size
This prevents using unlimited resources from evil sources. I originally wanted to split limits and have them separately for certificates, OCSP and CRLs. However, the HttpClient.MaxResponseContentBufferSize can be set only once so I decided to keep it simple for servicing. We could split the HttpClient and have one for small and one for large downloads. Or alternatively we can handle the body directly. But it is going to be unpleseant with the reflection and sync & async flavors. port of https://dev.azure.com/dnceng/internal/_git/dotnet-runtime/pullrequest/32920
1 parent 683c579 commit ad40cc3

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CertificateAssetDownloader.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Internal.Cryptography.Pal
1515
{
1616
internal static class CertificateAssetDownloader
1717
{
18+
private const long DefaultAiaDownloadLimit = 100 * 1024 * 1024;
19+
20+
private static long AiaDownloadLimit { get; } = GetValue("System.Security.Cryptography.AiaDownloadLimit", DefaultAiaDownloadLimit);
1821
private static readonly Func<string, CancellationToken, byte[]?>? s_downloadBytes = CreateDownloadBytesFunc();
1922

2023
internal static X509Certificate2? DownloadCertificate(string uri, TimeSpan downloadTimeout)
@@ -161,6 +164,7 @@ internal static class CertificateAssetDownloader
161164
PropertyInfo? requestUriProp = httpRequestMessageType.GetProperty("RequestUri");
162165
ConstructorInfo? httpRequestMessageCtor = httpRequestMessageType.GetConstructor(Type.EmptyTypes);
163166
MethodInfo? sendMethod = httpClientType.GetMethod("Send", new Type[] { httpRequestMessageType, typeof(CancellationToken) });
167+
PropertyInfo? maxResponseContentBufferSizeProp = httpClientType.GetProperty("MaxResponseContentBufferSize");
164168
PropertyInfo? responseContentProp = httpResponseMessageType.GetProperty("Content");
165169
PropertyInfo? responseStatusCodeProp = httpResponseMessageType.GetProperty("StatusCode");
166170
PropertyInfo? responseHeadersProp = httpResponseMessageType.GetProperty("Headers");
@@ -169,7 +173,7 @@ internal static class CertificateAssetDownloader
169173

170174
if (socketsHttpHandlerCtor == null || pooledConnectionIdleTimeoutProp == null || allowAutoRedirectProp == null || httpClientCtor == null ||
171175
requestUriProp == null || httpRequestMessageCtor == null || sendMethod == null || responseContentProp == null || responseStatusCodeProp == null ||
172-
responseHeadersProp == null || responseHeadersLocationProp == null || readAsStreamMethod == null)
176+
responseHeadersProp == null || responseHeadersLocationProp == null || readAsStreamMethod == null || maxResponseContentBufferSizeProp == null)
173177
{
174178
Debug.Fail("Unable to load required member.");
175179
return null;
@@ -190,6 +194,7 @@ internal static class CertificateAssetDownloader
190194
pooledConnectionIdleTimeoutProp.SetValue(socketsHttpHandler, TimeSpan.FromSeconds(PooledConnectionIdleTimeoutSeconds));
191195
allowAutoRedirectProp.SetValue(socketsHttpHandler, false);
192196
object? httpClient = httpClientCtor.Invoke(new object?[] { socketsHttpHandler });
197+
maxResponseContentBufferSizeProp.SetValue(httpClient, AiaDownloadLimit);
193198

194199
return (string uriString, CancellationToken cancellationToken) =>
195200
{
@@ -313,5 +318,24 @@ private static bool IsAllowedScheme(string scheme)
313318
{
314319
return string.Equals(scheme, "http", StringComparison.OrdinalIgnoreCase);
315320
}
321+
322+
private static long GetValue(string name, long defaultValue)
323+
{
324+
object? data = AppContext.GetData(name);
325+
326+
if (data is null)
327+
{
328+
return defaultValue;
329+
}
330+
331+
try
332+
{
333+
return Convert.ToInt64(data);
334+
}
335+
catch
336+
{
337+
return defaultValue;
338+
}
339+
}
316340
}
317341
}

0 commit comments

Comments
 (0)