33import io .swagger .v3 .parser .core .models .AuthorizationValue ;
44import io .swagger .v3 .parser .models .RefFormat ;
55import io .swagger .v3 .parser .processors .ExternalRefProcessor ;
6+ import io .swagger .v3 .parser .urlresolver .PermittedUrlsChecker ;
67import org .apache .commons .io .IOUtils ;
7- import org .apache .commons .lang3 .StringUtils ;
8-
9- import static java .nio .charset .StandardCharsets .UTF_8 ;
108
119import java .io .FileInputStream ;
1210import java .io .IOException ;
1816import java .util .List ;
1917import java .util .Optional ;
2018
19+ import static java .nio .charset .StandardCharsets .UTF_8 ;
20+
2121public class RefUtils {
2222
2323 private static final String REFERENCE_SEPARATOR = "#/" ;
@@ -50,10 +50,10 @@ public static String computeDefinitionName(String ref) {
5050 final String [] split = plausibleName .split ("\\ ." );
5151 // Fix for issue-1621 and issue-1865
5252 //validate number of dots
53- if (split .length > 2 ) {
53+ if (split .length > 2 ) {
5454 //Remove dot so ref can be interpreted as internal and relative in Swagger-Core schema class 'set$ref'
5555 plausibleName = String .join ("" , Arrays .copyOf (split , split .length - 1 ));
56- }else {
56+ } else {
5757 plausibleName = split [0 ];
5858 }
5959 }
@@ -66,24 +66,26 @@ public static Optional<String> getExternalPath(String ref) {
6666 return Optional .empty ();
6767 }
6868 return Optional .of (ref .split (REFERENCE_SEPARATOR ))
69- .filter (it -> it .length == 2 )
70- .map (it -> it [0 ])
71- .filter (it -> !it .isEmpty ());
69+ .filter (it -> it .length == 2 )
70+ .map (it -> it [0 ])
71+ .filter (it -> !it .isEmpty ());
7272 }
7373
7474 public static boolean isAnExternalRefFormat (RefFormat refFormat ) {
7575 return refFormat == RefFormat .URL || refFormat == RefFormat .RELATIVE ;
7676 }
7777
7878 public static RefFormat computeRefFormat (String ref ) {
79- RefFormat result = RefFormat . INTERNAL ;
79+ RefFormat result ;
8080 ref = mungedRef (ref );
81- if (ref .startsWith ("http" )|| ref .startsWith ("https" )) {
81+ if (ref .startsWith ("http" ) || ref .startsWith ("https" )) {
8282 result = RefFormat .URL ;
83- } else if (ref .startsWith (REFERENCE_SEPARATOR )) {
83+ } else if (ref .startsWith (REFERENCE_SEPARATOR )) {
8484 result = RefFormat .INTERNAL ;
85- } else if (ref .startsWith ("." ) || ref .startsWith ("/" ) || ref .indexOf (REFERENCE_SEPARATOR ) > 0 ) {
85+ } else if (ref .startsWith ("." ) || ref .startsWith ("/" ) || ref .indexOf (REFERENCE_SEPARATOR ) > 0 ) {
8686 result = RefFormat .RELATIVE ;
87+ } else {
88+ result = RefFormat .INTERNAL ;
8789 }
8890
8991 return result ;
@@ -103,7 +105,7 @@ public static String mungedRef(String refString) {
103105
104106
105107 public static String readExternalUrlRef (String file , RefFormat refFormat , List <AuthorizationValue > auths ,
106- String rootPath ) {
108+ String rootPath , PermittedUrlsChecker permittedUrlsChecker ) {
107109
108110 if (!RefUtils .isAnExternalRefFormat (refFormat )) {
109111 throw new RuntimeException ("Ref is not external" );
@@ -113,12 +115,12 @@ public static String readExternalUrlRef(String file, RefFormat refFormat, List<A
113115
114116 try {
115117 if (refFormat == RefFormat .URL ) {
116- result = RemoteUrl .urlToString (file , auths );
118+ result = RemoteUrl .urlToString (file , auths , permittedUrlsChecker );
117119 } else {
118120 //its assumed to be a relative ref
119121 String url = buildUrl (rootPath , file );
120122
121- return readExternalRef (url , RefFormat .URL , auths , null );
123+ return readExternalRef (url , RefFormat .URL , auths , null , permittedUrlsChecker );
122124 }
123125 } catch (Exception e ) {
124126 throw new RuntimeException ("Unable to load " + refFormat + " ref: " + file , e );
@@ -129,7 +131,7 @@ public static String readExternalUrlRef(String file, RefFormat refFormat, List<A
129131 }
130132
131133 public static String readExternalClasspathRef (String file , RefFormat refFormat , List <AuthorizationValue > auths ,
132- String rootPath ) {
134+ String rootPath , PermittedUrlsChecker permittedUrlsChecker ) {
133135
134136 if (!RefUtils .isAnExternalRefFormat (refFormat )) {
135137 throw new RuntimeException ("Ref is not external" );
@@ -139,7 +141,7 @@ public static String readExternalClasspathRef(String file, RefFormat refFormat,
139141
140142 try {
141143 if (refFormat == RefFormat .URL ) {
142- result = RemoteUrl .urlToString (file , auths );
144+ result = RemoteUrl .urlToString (file , auths , permittedUrlsChecker );
143145 } else {
144146 //its assumed to be a relative ref
145147 String pathRef = ExternalRefProcessor .join (rootPath , file );
@@ -155,24 +157,23 @@ public static String readExternalClasspathRef(String file, RefFormat refFormat,
155157 }
156158
157159 public static String buildUrl (String rootPath , String relativePath ) {
158- if (rootPath == null || relativePath == null ) {
159- return null ;
160- }
161-
162- try {
163- int until = rootPath .lastIndexOf ("/" )+1 ;
164- String root = rootPath .substring (0 , until );
165- URL rootUrl = new URL (root );
166- URL finalUrl = new URL (rootUrl , relativePath );
167- return finalUrl .toString ();
168- }
169- catch (Exception e ) {
170- throw new RuntimeException (e );
171- }
160+ if (rootPath == null || relativePath == null ) {
161+ return null ;
162+ }
163+
164+ try {
165+ int until = rootPath .lastIndexOf ("/" ) + 1 ;
166+ String root = rootPath .substring (0 , until );
167+ URL rootUrl = new URL (root );
168+ URL finalUrl = new URL (rootUrl , relativePath );
169+ return finalUrl .toString ();
170+ } catch (Exception e ) {
171+ throw new RuntimeException (e );
172+ }
172173 }
173174
174175 public static String readExternalRef (String file , RefFormat refFormat , List <AuthorizationValue > auths ,
175- Path parentDirectory ) {
176+ Path parentDirectory , PermittedUrlsChecker permittedUrlsChecker ) {
176177
177178 if (!RefUtils .isAnExternalRefFormat (refFormat )) {
178179 throw new RuntimeException ("Ref is not external" );
@@ -182,12 +183,12 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
182183
183184 try {
184185 if (refFormat == RefFormat .URL ) {
185- result = RemoteUrl .urlToString (file , auths );
186+ result = RemoteUrl .urlToString (file , auths , permittedUrlsChecker );
186187 } else {
187188 //its assumed to be a relative file ref
188189 final Path pathToUse = parentDirectory .resolve (file ).normalize ();
189190
190- if (Files .exists (pathToUse )) {
191+ if (Files .exists (pathToUse )) {
191192 result = readAll (pathToUse );
192193 } else {
193194 String url = file ;
@@ -206,18 +207,18 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
206207 }
207208 final Path pathToUse2 = parentDirectory .resolve (url ).normalize ();
208209
209- if (Files .exists (pathToUse2 )) {
210+ if (Files .exists (pathToUse2 )) {
210211 result = readAll (pathToUse2 );
211212 }
212213 }
213- if (result == null ){
214+ if (result == null ) {
214215 result = ClasspathHelper .loadFileFromClasspath (file );
215216 }
216217
217218
218219 }
219220 } catch (Exception e ) {
220- throw new RuntimeException ("Unable to load " + refFormat + " ref: " + file + " path: " + parentDirectory , e );
221+ throw new RuntimeException ("Unable to load " + refFormat + " ref: " + file + " path: " + parentDirectory , e );
221222 }
222223
223224 return result ;
0 commit comments