@@ -122,18 +122,26 @@ internal static partial class OpenApiV2Deserializer
122122 { s => s . StartsWith ( "x-" ) , ( o , p , n ) => o . AddExtension ( p , LoadExtension ( p , n ) ) }
123123 } ;
124124
125- private static void MakeServers ( IList < OpenApiServer > servers , ParsingContext context , Uri defaultUrl )
125+ private static void MakeServers ( IList < OpenApiServer > servers , ParsingContext context , RootNode rootNode )
126126 {
127127 var host = context . GetFromTempStorage < string > ( "host" ) ;
128128 var basePath = context . GetFromTempStorage < string > ( "basePath" ) ;
129129 var schemes = context . GetFromTempStorage < List < string > > ( "schemes" ) ;
130+ Uri defaultUrl = rootNode . Context . BaseUrl ;
130131
131132 // If nothing is provided, don't create a server
132133 if ( host == null && basePath == null && schemes == null )
133134 {
134135 return ;
135136 }
136137
138+ //Validate host
139+ if ( host != null && ! IsHostValid ( host ) )
140+ {
141+ rootNode . Diagnostic . Errors . Add ( new OpenApiError ( rootNode . Context . GetLocation ( ) , "Invalid host" ) ) ;
142+ return ;
143+ }
144+
137145 // Fill in missing information based on the defaultUrl
138146 if ( defaultUrl != null )
139147 {
@@ -226,7 +234,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
226234 openApidoc . Servers = new List < OpenApiServer > ( ) ;
227235 }
228236
229- MakeServers ( openApidoc . Servers , openApiNode . Context , rootNode . Context . BaseUrl ) ;
237+ MakeServers ( openApidoc . Servers , openApiNode . Context , rootNode ) ;
230238
231239 FixRequestBodyReferences ( openApidoc ) ;
232240 return openApidoc ;
@@ -243,6 +251,19 @@ private static void FixRequestBodyReferences(OpenApiDocument doc)
243251 walker . Walk ( doc ) ;
244252 }
245253 }
254+
255+ private static bool IsHostValid ( string host )
256+ {
257+ //Check if the host contains ://
258+ if ( host . Contains ( Uri . SchemeDelimiter ) )
259+ {
260+ return false ;
261+ }
262+
263+ //Check if the host (excluding port number) is a valid dns/ip address.
264+ var hostPart = host . Split ( ':' ) . First ( ) ;
265+ return Uri . CheckHostName ( hostPart ) != UriHostNameType . Unknown ;
266+ }
246267 }
247268
248269 internal class RequestBodyReferenceFixer : OpenApiVisitorBase
0 commit comments