@@ -122,18 +122,26 @@ internal static partial class OpenApiV2Deserializer
122
122
{ s => s . StartsWith ( "x-" ) , ( o , p , n ) => o . AddExtension ( p , LoadExtension ( p , n ) ) }
123
123
} ;
124
124
125
- private static void MakeServers ( IList < OpenApiServer > servers , ParsingContext context , Uri defaultUrl )
125
+ private static void MakeServers ( IList < OpenApiServer > servers , ParsingContext context , RootNode rootNode )
126
126
{
127
127
var host = context . GetFromTempStorage < string > ( "host" ) ;
128
128
var basePath = context . GetFromTempStorage < string > ( "basePath" ) ;
129
129
var schemes = context . GetFromTempStorage < List < string > > ( "schemes" ) ;
130
+ Uri defaultUrl = rootNode . Context . BaseUrl ;
130
131
131
132
// If nothing is provided, don't create a server
132
133
if ( host == null && basePath == null && schemes == null )
133
134
{
134
135
return ;
135
136
}
136
137
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
+
137
145
// Fill in missing information based on the defaultUrl
138
146
if ( defaultUrl != null )
139
147
{
@@ -226,7 +234,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
226
234
openApidoc . Servers = new List < OpenApiServer > ( ) ;
227
235
}
228
236
229
- MakeServers ( openApidoc . Servers , openApiNode . Context , rootNode . Context . BaseUrl ) ;
237
+ MakeServers ( openApidoc . Servers , openApiNode . Context , rootNode ) ;
230
238
231
239
FixRequestBodyReferences ( openApidoc ) ;
232
240
return openApidoc ;
@@ -243,6 +251,19 @@ private static void FixRequestBodyReferences(OpenApiDocument doc)
243
251
walker . Walk ( doc ) ;
244
252
}
245
253
}
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
+ }
246
267
}
247
268
248
269
internal class RequestBodyReferenceFixer : OpenApiVisitorBase
0 commit comments