Skip to content

Commit 46721b2

Browse files
committed
Merge remote-tracking branch 'origin/main' into python-fix-classvar-schema
2 parents 7139056 + cc8a15f commit 46721b2

29 files changed

Lines changed: 6617 additions & 4804 deletions

File tree

‎.gitattributes‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
# Bash scripts
55
*.sh text eol=lf
66
*.cmd text eol=crlf
7+
8+
# Generated dependency lock files
9+
**/uv.lock linguist-generated=true

‎dotnet/nuget/nuget-package.props‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project>
22
<PropertyGroup>
33
<!-- Central version prefix - applies to all nuget packages. -->
4-
<VersionPrefix>1.80.1</VersionPrefix>
4+
<VersionPrefix>1.81.0</VersionPrefix>
55
<PackageVersion Condition="'$(VersionSuffix)' != ''">$(VersionPrefix)-$(VersionSuffix)</PackageVersion>
66
<PackageVersion Condition="'$(VersionSuffix)' == ''">$(VersionPrefix)</PackageVersion>
77

88
<Configurations>Debug;Release;Publish</Configurations>
99
<IsPackable>true</IsPackable>
1010

1111
<!-- Package validation. Baseline Version should be the latest version available on NuGet. -->
12-
<PackageValidationBaselineVersion>1.80.0</PackageValidationBaselineVersion>
12+
<PackageValidationBaselineVersion>1.80.1</PackageValidationBaselineVersion>
1313
<!-- Validate assembly attributes only for Publish builds -->
1414
<NoWarn Condition="'$(Configuration)' != 'Publish'">$(NoWarn);CP0003</NoWarn>
1515
<!-- Do not validate reference assemblies -->

‎dotnet/src/Functions/Functions.OpenApi/ServerUrlValidator.cs‎

Lines changed: 6 additions & 294 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Net;
6-
using System.Net.Sockets;
76
using System.Threading;
87
using System.Threading.Tasks;
8+
using Microsoft.SemanticKernel.Http;
99

1010
namespace Microsoft.SemanticKernel.Plugins.OpenApi;
1111

@@ -71,7 +71,11 @@ public static async Task ValidateAsync(
7171
return;
7272
}
7373

74-
await EnsurePublicHostAsync(url, cancellationToken, dnsResolver).ConfigureAwait(false);
74+
await PublicNetworkAddressValidator.ValidateAsync(
75+
url, cancellationToken, dnsResolver,
76+
"To allow this URL, add it to " +
77+
$"{nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowedBaseUrls)} " +
78+
$"or set {nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowPrivateNetworkAccess)} = true.").ConfigureAwait(false);
7579
}
7680

7781
private static bool TryMatchAllowedBaseUrl(Uri url, IReadOnlyList<Uri>? allowedBaseUrls)
@@ -112,296 +116,4 @@ private static bool TryMatchAllowedBaseUrl(Uri url, IReadOnlyList<Uri>? allowedB
112116

113117
return false;
114118
}
115-
116-
private static async Task EnsurePublicHostAsync(
117-
Uri url,
118-
CancellationToken cancellationToken,
119-
Func<string, CancellationToken, Task<IPAddress[]>>? dnsResolver)
120-
{
121-
var host = url.DnsSafeHost;
122-
123-
// Case 1: literal IP in the URL.
124-
if (url.HostNameType == UriHostNameType.IPv4 || url.HostNameType == UriHostNameType.IPv6)
125-
{
126-
if (!IPAddress.TryParse(host, out var ip))
127-
{
128-
// Should be unreachable — .NET's Uri already classified this as an IP address.
129-
// Fail closed: block the request rather than silently skipping validation.
130-
throw new InvalidOperationException(
131-
$"The server URL '{url}' has a host identified as an IP address but could not be parsed. The request is blocked as a precaution.");
132-
}
133-
134-
EnsurePublicAddress(url, ip);
135-
return;
136-
}
137-
138-
// Case 2: hostname - resolve and validate every returned address (defeats DNS rebinding).
139-
IPAddress[] addresses;
140-
try
141-
{
142-
if (dnsResolver is not null)
143-
{
144-
addresses = await dnsResolver(host, cancellationToken).ConfigureAwait(false);
145-
}
146-
else
147-
{
148-
#if NET
149-
addresses = await Dns.GetHostAddressesAsync(host, cancellationToken).ConfigureAwait(false);
150-
#else
151-
addresses = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
152-
cancellationToken.ThrowIfCancellationRequested();
153-
#endif
154-
}
155-
}
156-
catch (SocketException)
157-
{
158-
// DNS resolution failed. Fail closed: block the request rather than allowing
159-
// it to proceed unvalidated. A TOCTOU race could otherwise allow a private
160-
// target to be reached if DNS fails here but succeeds when HttpClient resolves.
161-
throw new InvalidOperationException(
162-
$"The request URI '{url}' is not allowed: DNS resolution for host '{host}' failed. " +
163-
"The request is blocked as a precaution to prevent potential access to private network addresses. " +
164-
"To allow this URL, add it to " +
165-
$"{nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowedBaseUrls)} " +
166-
$"or set {nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowPrivateNetworkAccess)} = true.");
167-
}
168-
169-
if (addresses is null || addresses.Length == 0)
170-
{
171-
// No addresses returned. Fail closed for the same TOCTOU reason.
172-
throw new InvalidOperationException(
173-
$"The request URI '{url}' is not allowed: DNS resolution for host '{host}' returned no addresses. " +
174-
"The request is blocked as a precaution. To allow this URL, add it to " +
175-
$"{nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowedBaseUrls)} " +
176-
$"or set {nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowPrivateNetworkAccess)} = true.");
177-
}
178-
179-
foreach (var address in addresses)
180-
{
181-
EnsurePublicAddress(url, address);
182-
}
183-
}
184-
185-
private static void EnsurePublicAddress(Uri url, IPAddress address)
186-
{
187-
if (TryCategorizeNonPublicAddress(address, out var category))
188-
{
189-
throw new InvalidOperationException(
190-
$"The request URI '{url}' is not allowed: host resolves to a {category} address ({address}), " +
191-
"which is blocked by default to prevent Server-Side Request Forgery (SSRF). " +
192-
"To allow this URL, add it to " +
193-
$"{nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowedBaseUrls)} " +
194-
$"or set {nameof(RestApiOperationServerUrlValidationOptions)}.{nameof(RestApiOperationServerUrlValidationOptions.AllowPrivateNetworkAccess)} = true.");
195-
}
196-
}
197-
198-
/// <summary>
199-
/// Returns true and sets <paramref name="category"/> to a human-readable label when the
200-
/// supplied address is in a non-public IP range that should be blocked by default.
201-
/// </summary>
202-
internal static bool TryCategorizeNonPublicAddress(IPAddress address, out string category)
203-
{
204-
// Normalize IPv4-mapped IPv6 (::ffff:a.b.c.d) to its IPv4 form so the v4 checks apply.
205-
if (address.AddressFamily == AddressFamily.InterNetworkV6 && IsIPv4MappedToIPv6(address))
206-
{
207-
address = MapToIPv4(address);
208-
}
209-
210-
if (address.AddressFamily == AddressFamily.InterNetwork)
211-
{
212-
return TryClassifyIPv4(address, out category);
213-
}
214-
215-
if (address.AddressFamily == AddressFamily.InterNetworkV6)
216-
{
217-
return TryClassifyIPv6(address, out category);
218-
}
219-
220-
// Not IPv4 or IPv6 - reject conservatively.
221-
category = "non-IP";
222-
return true;
223-
}
224-
225-
private static bool TryClassifyIPv4(IPAddress address, out string category)
226-
{
227-
var bytes = address.GetAddressBytes();
228-
var b0 = bytes[0];
229-
var b1 = bytes[1];
230-
var b2 = bytes[2];
231-
232-
// 0.0.0.0/8 - "this network", unspecified.
233-
if (b0 == 0)
234-
{
235-
category = "unspecified";
236-
return true;
237-
}
238-
239-
// 10.0.0.0/8
240-
if (b0 == 10)
241-
{
242-
category = "private (RFC1918)";
243-
return true;
244-
}
245-
246-
// 127.0.0.0/8 - loopback.
247-
if (b0 == 127)
248-
{
249-
category = "loopback";
250-
return true;
251-
}
252-
253-
// 169.254.0.0/16 - link-local (includes cloud metadata 169.254.169.254).
254-
if (b0 == 169 && b1 == 254)
255-
{
256-
category = "link-local";
257-
return true;
258-
}
259-
260-
// 172.16.0.0/12
261-
if (b0 == 172 && b1 >= 16 && b1 <= 31)
262-
{
263-
category = "private (RFC1918)";
264-
return true;
265-
}
266-
267-
// 192.168.0.0/16
268-
if (b0 == 192 && b1 == 168)
269-
{
270-
category = "private (RFC1918)";
271-
return true;
272-
}
273-
274-
// 100.64.0.0/10 - carrier-grade NAT.
275-
if (b0 == 100 && b1 >= 64 && b1 <= 127)
276-
{
277-
category = "carrier-grade NAT";
278-
return true;
279-
}
280-
281-
// 198.18.0.0/15 - benchmarking.
282-
if (b0 == 198 && (b1 == 18 || b1 == 19))
283-
{
284-
category = "benchmarking";
285-
return true;
286-
}
287-
288-
// 192.0.0.0/24 (IETF protocol assignments) and 192.0.2.0/24 (TEST-NET-1).
289-
if (b0 == 192 && b1 == 0 && (b2 == 0 || b2 == 2))
290-
{
291-
category = "reserved";
292-
return true;
293-
}
294-
295-
// 198.51.100.0/24 (TEST-NET-2).
296-
if (b0 == 198 && b1 == 51 && b2 == 100)
297-
{
298-
category = "reserved";
299-
return true;
300-
}
301-
302-
// 203.0.113.0/24 (TEST-NET-3).
303-
if (b0 == 203 && b1 == 0 && b2 == 113)
304-
{
305-
category = "reserved";
306-
return true;
307-
}
308-
309-
// 224.0.0.0/4 - multicast.
310-
if (b0 >= 224 && b0 <= 239)
311-
{
312-
category = "multicast";
313-
return true;
314-
}
315-
316-
// 240.0.0.0/4 - reserved (includes 255.255.255.255 broadcast).
317-
if (b0 >= 240)
318-
{
319-
category = "reserved";
320-
return true;
321-
}
322-
323-
category = string.Empty;
324-
return false;
325-
}
326-
327-
private static bool TryClassifyIPv6(IPAddress address, out string category)
328-
{
329-
if (IPAddress.IsLoopback(address))
330-
{
331-
category = "loopback";
332-
return true;
333-
}
334-
335-
var bytes = address.GetAddressBytes();
336-
337-
// :: (unspecified)
338-
if (address.Equals(IPAddress.IPv6None))
339-
{
340-
category = "unspecified";
341-
return true;
342-
}
343-
344-
// fe80::/10 - link-local.
345-
if (bytes[0] == 0xfe && (bytes[1] & 0xC0) == 0x80)
346-
{
347-
category = "link-local";
348-
return true;
349-
}
350-
351-
// fc00::/7 - unique local (private).
352-
if ((bytes[0] & 0xfe) == 0xfc)
353-
{
354-
category = "private (IPv6 ULA)";
355-
return true;
356-
}
357-
358-
// ff00::/8 - multicast.
359-
if (bytes[0] == 0xff)
360-
{
361-
category = "multicast";
362-
return true;
363-
}
364-
365-
// 2001:db8::/32 - documentation.
366-
if (bytes[0] == 0x20 && bytes[1] == 0x01 && bytes[2] == 0x0d && bytes[3] == 0xb8)
367-
{
368-
category = "reserved";
369-
return true;
370-
}
371-
372-
category = string.Empty;
373-
return false;
374-
}
375-
376-
private static bool IsIPv4MappedToIPv6(IPAddress address)
377-
{
378-
#if NET
379-
return address.IsIPv4MappedToIPv6;
380-
#else
381-
var bytes = address.GetAddressBytes();
382-
if (bytes.Length != 16)
383-
{
384-
return false;
385-
}
386-
for (int i = 0; i < 10; i++)
387-
{
388-
if (bytes[i] != 0)
389-
{
390-
return false;
391-
}
392-
}
393-
return bytes[10] == 0xff && bytes[11] == 0xff;
394-
#endif
395-
}
396-
397-
private static IPAddress MapToIPv4(IPAddress address)
398-
{
399-
#if NET
400-
return address.MapToIPv4();
401-
#else
402-
var bytes = address.GetAddressBytes();
403-
var v4 = new byte[4] { bytes[12], bytes[13], bytes[14], bytes[15] };
404-
return new IPAddress(v4);
405-
#endif
406-
}
407119
}

0 commit comments

Comments
 (0)