2525
2626import static com .fasterxml .jackson .annotation .JsonAutoDetect .Visibility .ANY ;
2727import static com .fasterxml .jackson .annotation .JsonAutoDetect .Visibility .NONE ;
28- import static java .net .HttpURLConnection .HTTP_BAD_REQUEST ;
29- import static java .net .HttpURLConnection .HTTP_UNAUTHORIZED ;
28+ import static java .net .HttpURLConnection .*;
3029import static java .util .logging .Level .*;
3130import static org .apache .commons .lang3 .StringUtils .defaultString ;
3231
@@ -392,7 +391,7 @@ public <T> GitHubResponse<T> sendRequest(GitHubRequest request, @CheckForNull Bo
392391 connectorRequest = e .connectorRequest ;
393392 }
394393 } catch (SocketException | SocketTimeoutException | SSLHandshakeException e ) {
395- // These transient errors the
394+ // These transient errors thrown by HttpURLConnection
396395 if (retries > 0 ) {
397396 logRetryConnectionError (e , request .url (), retries );
398397 continue ;
@@ -413,6 +412,7 @@ private void detectKnownErrors(GitHubConnectorResponse connectorResponse,
413412 boolean detectStatusCodeError ) throws IOException {
414413 detectOTPRequired (connectorResponse );
415414 detectInvalidCached404Response (connectorResponse , request );
415+ detectRedirect (connectorResponse );
416416 if (rateLimitHandler .isError (connectorResponse )) {
417417 rateLimitHandler .onError (connectorResponse );
418418 throw new RetryRequestException ();
@@ -425,6 +425,19 @@ private void detectKnownErrors(GitHubConnectorResponse connectorResponse,
425425 }
426426 }
427427
428+ private void detectRedirect (GitHubConnectorResponse connectorResponse ) throws IOException {
429+ if (connectorResponse .statusCode () == HTTP_MOVED_PERM || connectorResponse .statusCode () == HTTP_MOVED_TEMP ) {
430+ // GitHubClient depends on GitHubConnector implementations to follow any redirects automatically
431+ // If this is not done and a redirect is requested, throw in order to maintain security and consistency
432+ throw new HttpException (
433+ "GitHubConnnector did not automatically follow redirect.\n "
434+ + "Change your http client configuration to automatically follow redirects as appropriate." ,
435+ connectorResponse .statusCode (),
436+ "Redirect" ,
437+ connectorResponse .request ().url ().toString ());
438+ }
439+ }
440+
428441 private GitHubConnectorRequest prepareConnectorRequest (GitHubRequest request ) throws IOException {
429442 GitHubRequest .Builder <?> builder = request .toBuilder ();
430443 // if the authentication is needed but no credential is given, try it anyway (so that some calls
0 commit comments