55using System . Net ;
66using System . Net . Http ;
77using System . Net . Http . Headers ;
8+ using System . Net . Sockets ;
89using System . Threading ;
910using System . Threading . Tasks ;
1011using log4net ;
@@ -149,11 +150,14 @@ private async Task FollowReferralAsync(RDAPResult result, TimeSpan timeout, Canc
149150
150151 try
151152 {
152- result . Referral = await GetAsync ( result . Query , referralUrl , timeout , cancellationToken )
153+ result . Referral = await GetAsync ( result . Query , referralUrl , timeout , cancellationToken , true )
153154 . ConfigureAwait ( false ) ;
154155 }
155156 catch ( RDAPException ex )
156157 {
158+ if ( ex . Kind is RDAPErrorKind . InsecureReferral or RDAPErrorKind . PrivateAddress )
159+ Log . Warn ( $ "Referral to the registrar not followed ({ ex . Kind } ): { ex . RequestUrl } ") ;
160+
157161 // The registry response is still valid if the registrar fails.
158162 result . ReferralError = ex ;
159163 }
@@ -162,8 +166,16 @@ private async Task FollowReferralAsync(RDAPResult result, TimeSpan timeout, Canc
162166 /// <summary>
163167 /// Sends a GET request, follows redirects and parses the response.
164168 /// </summary>
169+ /// <param name="query">Query.</param>
170+ /// <param name="url">URL to request.</param>
171+ /// <param name="timeout">Timeout for each request.</param>
172+ /// <param name="cancellationToken">Cancellation token.</param>
173+ /// <param name="isReferral">
174+ /// The URL comes from a response (registrar referral) and is untrusted: only HTTPS and no local or private
175+ /// addresses are allowed, for the referral and each redirect.
176+ /// </param>
165177 private async Task < RDAPResult > GetAsync ( RDAPQuery query , string url , TimeSpan timeout ,
166- CancellationToken cancellationToken )
178+ CancellationToken cancellationToken , bool isReferral = false )
167179 {
168180 var visited = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
169181 var currentUrl = new Uri ( url ) ;
@@ -173,6 +185,9 @@ private async Task<RDAPResult> GetAsync(RDAPQuery query, string url, TimeSpan ti
173185 if ( ! visited . Add ( currentUrl . AbsoluteUri ) || redirects > MaxRedirects )
174186 throw new RDAPException ( RDAPErrorKind . TooManyRedirects ) { RequestUrl = currentUrl . AbsoluteUri } ;
175187
188+ if ( isReferral )
189+ await ValidateReferralTargetAsync ( currentUrl , cancellationToken ) . ConfigureAwait ( false ) ;
190+
176191 using var timeoutSource = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
177192 timeoutSource . CancelAfter ( timeout ) ;
178193
@@ -220,6 +235,67 @@ private async Task<RDAPResult> GetAsync(RDAPQuery query, string url, TimeSpan ti
220235 }
221236 }
222237
238+ /// <summary>
239+ /// Checks the target of a referral, which is sent by the registry and therefore untrusted. Only HTTPS is allowed
240+ /// (no downgrade to plain text) and the host must not resolve to a local or private address, so a response
241+ /// cannot make the client send requests into the local network.
242+ /// </summary>
243+ private static async Task ValidateReferralTargetAsync ( Uri url , CancellationToken cancellationToken )
244+ {
245+ if ( url . Scheme != Uri . UriSchemeHttps )
246+ throw new RDAPException ( RDAPErrorKind . InsecureReferral , url . AbsoluteUri ) { RequestUrl = url . AbsoluteUri } ;
247+
248+ IPAddress [ ] addresses ;
249+
250+ if ( url . IsLoopback )
251+ {
252+ addresses = [ IPAddress . Loopback ] ;
253+ }
254+ else if ( IPAddress . TryParse ( url . DnsSafeHost , out var ipAddress ) )
255+ {
256+ addresses = [ ipAddress ] ;
257+ }
258+ else
259+ {
260+ try
261+ {
262+ addresses = await Dns . GetHostAddressesAsync ( url . DnsSafeHost , cancellationToken ) . ConfigureAwait ( false ) ;
263+ }
264+ catch ( SocketException )
265+ {
266+ // The request itself fails with a proper network error.
267+ return ;
268+ }
269+ }
270+
271+ if ( addresses . Any ( IsLocalOrPrivateAddress ) )
272+ throw new RDAPException ( RDAPErrorKind . PrivateAddress , url . AbsoluteUri ) { RequestUrl = url . AbsoluteUri } ;
273+ }
274+
275+ /// <summary>
276+ /// Loopback, private (RFC 1918, RFC 4193), shared (RFC 6598), link-local, unspecified and multicast addresses.
277+ /// </summary>
278+ private static bool IsLocalOrPrivateAddress ( IPAddress address )
279+ {
280+ if ( address . IsIPv4MappedToIPv6 )
281+ address = address . MapToIPv4 ( ) ;
282+
283+ if ( IPAddress . IsLoopback ( address ) )
284+ return true ;
285+
286+ var bytes = address . GetAddressBytes ( ) ;
287+
288+ if ( address . AddressFamily == AddressFamily . InterNetwork )
289+ return bytes [ 0 ] is 0 or 10 or >= 224 ||
290+ bytes [ 0 ] == 172 && ( bytes [ 1 ] & 0xF0 ) == 16 ||
291+ bytes [ 0 ] == 192 && bytes [ 1 ] == 168 ||
292+ bytes [ 0 ] == 169 && bytes [ 1 ] == 254 ||
293+ bytes [ 0 ] == 100 && ( bytes [ 1 ] & 0xC0 ) == 64 ;
294+
295+ return address . Equals ( IPAddress . IPv6Any ) || address . IsIPv6LinkLocal || address . IsIPv6SiteLocal ||
296+ address . IsIPv6Multicast || ( bytes [ 0 ] & 0xFE ) == 0xFC ;
297+ }
298+
223299 private static bool IsRedirect ( HttpStatusCode statusCode )
224300 {
225301 return statusCode is HttpStatusCode . MovedPermanently or HttpStatusCode . Found or HttpStatusCode . SeeOther
0 commit comments