File tree Expand file tree Collapse file tree 3 files changed +39
-3
lines changed
che-core-api-factory-gitlab-common
src/main/java/org/eclipse/che/api/factory/server/gitlab
che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab Expand file tree Collapse file tree 3 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 3131 <groupId >com.fasterxml.jackson.core</groupId >
3232 <artifactId >jackson-databind</artifactId >
3333 </dependency >
34+ <dependency >
35+ <groupId >com.google.code.gson</groupId >
36+ <artifactId >gson</artifactId >
37+ </dependency >
3438 <dependency >
3539 <groupId >com.google.guava</groupId >
3640 <artifactId >guava</artifactId >
Original file line number Diff line number Diff line change 1616import static java .util .regex .Pattern .compile ;
1717import static org .eclipse .che .commons .lang .StringUtils .trimEnd ;
1818
19+ import com .google .gson .JsonParser ;
1920import jakarta .validation .constraints .NotNull ;
2021import java .net .URI ;
2122import java .util .ArrayList ;
@@ -112,15 +113,27 @@ private boolean isApiRequestRelevant(String repositoryUrl) {
112113 // belongs to Gitlab.
113114 gitlabApiClient .getOAuthTokenInfo ("" );
114115 } catch (ScmUnauthorizedException e ) {
115- // the error message is a JSON if it is a response from Gitlab.
116- return e .getMessage ().startsWith ("{" );
116+ // Some Git providers e.g. Azure Devops Server, may return unauthorized exception on invalid
117+ // API request, but Gitlab API returns unauthorized error message in JSON format, so to be
118+ // sure that the URL belongs to Gitlab, we need to check if the error message is a valid
119+ // JSON.
120+ return isJsonValid (e .getMessage ());
117121 } catch (ScmItemNotFoundException | IllegalArgumentException | ScmCommunicationException e ) {
118122 return false ;
119123 }
120124 }
121125 return false ;
122126 }
123127
128+ private boolean isJsonValid (String message ) {
129+ try {
130+ new JsonParser ().parse (message ).getAsJsonObject ();
131+ return true ;
132+ } catch (Exception exception ) {
133+ return false ;
134+ }
135+ }
136+
124137 private Optional <Matcher > getPatternMatcherByUrl (String url ) {
125138 URI uri =
126139 URI .create (
Original file line number Diff line number Diff line change @@ -101,7 +101,11 @@ public void shouldValidateUrlByApiRequest() {
101101 String url = wireMockServer .url ("/user/repo" );
102102 stubFor (
103103 get (urlEqualTo ("/oauth/token/info" ))
104- .willReturn (aResponse ().withStatus (401 ).withBody ("{error}" )));
104+ .willReturn (
105+ aResponse ()
106+ .withStatus (401 )
107+ .withBody (
108+ "{\" error\" :\" invalid_token\" ,\" error_description\" :\" The access token is invalid\" ,\" state\" :\" unauthorized\" }" )));
105109
106110 // when
107111 boolean result = gitlabUrlParser .isValid (url );
@@ -110,6 +114,21 @@ public void shouldValidateUrlByApiRequest() {
110114 assertTrue (result );
111115 }
112116
117+ @ Test
118+ public void shouldNotValidateUrlByApiRequestWithPlainStringResponse () {
119+ // given
120+ String url = wireMockServer .url ("/user/repo" );
121+ stubFor (
122+ get (urlEqualTo ("/oauth/token/info" ))
123+ .willReturn (aResponse ().withStatus (401 ).withBody ("plain string error" )));
124+
125+ // when
126+ boolean result = gitlabUrlParser .isValid (url );
127+
128+ // then
129+ assertFalse (result );
130+ }
131+
113132 @ Test
114133 public void shouldNotValidateUrlByApiRequest () {
115134 // given
You can’t perform that action at this time.
0 commit comments