@@ -79,6 +79,7 @@ of this software and associated documentation files (the "Software"), to deal
79
79
import org .kohsuke .stapler .HttpRedirect ;
80
80
import org .kohsuke .stapler .HttpResponse ;
81
81
import org .kohsuke .stapler .HttpResponses ;
82
+ import org .kohsuke .stapler .QueryParameter ;
82
83
import org .kohsuke .stapler .StaplerRequest ;
83
84
import org .springframework .dao .DataAccessException ;
84
85
import org .springframework .dao .DataRetrievalFailureException ;
@@ -93,6 +94,7 @@ of this software and associated documentation files (the "Software"), to deal
93
94
import java .util .logging .Logger ;
94
95
import javax .annotation .Nonnull ;
95
96
import javax .annotation .Nullable ;
97
+ import javax .servlet .http .HttpSession ;
96
98
97
99
/**
98
100
*
@@ -333,9 +335,18 @@ public String getOauthScopes() {
333
335
return oauthScopes ;
334
336
}
335
337
336
- public HttpResponse doCommenceLogin (StaplerRequest request , @ Header ("Referer" ) final String referer )
338
+ public HttpResponse doCommenceLogin (StaplerRequest request , @ QueryParameter String from , @ Header ("Referer" ) final String referer )
337
339
throws IOException {
338
- request .getSession ().setAttribute (REFERER_ATTRIBUTE ,referer );
340
+ String redirectOnFinish ;
341
+ if (from != null && Util .isSafeToRedirectTo (from )) {
342
+ redirectOnFinish = from ;
343
+ } else if (referer != null && (referer .startsWith (Jenkins .getInstance ().getRootUrl ()) || Util .isSafeToRedirectTo (referer ))) {
344
+ redirectOnFinish = referer ;
345
+ } else {
346
+ redirectOnFinish = Jenkins .getInstance ().getRootUrl ();
347
+ }
348
+
349
+ request .getSession ().setAttribute (REFERER_ATTRIBUTE , redirectOnFinish );
339
350
340
351
Set <String > scopes = new HashSet <>();
341
352
for (GitHubOAuthScope s : getJenkins ().getExtensionList (GitHubOAuthScope .class )) {
@@ -361,6 +372,7 @@ public HttpResponse doCommenceLogin(StaplerRequest request, @Header("Referer") f
361
372
public HttpResponse doFinishLogin (StaplerRequest request )
362
373
throws IOException {
363
374
String code = request .getParameter ("code" );
375
+ String referer = (String )request .getSession ().getAttribute (REFERER_ATTRIBUTE );
364
376
365
377
if (code == null || code .trim ().length () == 0 ) {
366
378
Log .info ("doFinishLogin: missing code." );
@@ -372,6 +384,14 @@ public HttpResponse doFinishLogin(StaplerRequest request)
372
384
if (accessToken != null && accessToken .trim ().length () > 0 ) {
373
385
// only set the access token if it exists.
374
386
GithubAuthenticationToken auth = new GithubAuthenticationToken (accessToken , getGithubApiUri ());
387
+
388
+ HttpSession session = request .getSession (false );
389
+ if (session != null ){
390
+ // avoid session fixation
391
+ session .invalidate ();
392
+ }
393
+ request .getSession (true );
394
+
375
395
SecurityContextHolder .getContext ().setAuthentication (auth );
376
396
377
397
GHMyself self = auth .getMyself ();
@@ -409,7 +429,6 @@ public HttpResponse doFinishLogin(StaplerRequest request)
409
429
Log .info ("Github did not return an access token." );
410
430
}
411
431
412
- String referer = (String )request .getSession ().getAttribute (REFERER_ATTRIBUTE );
413
432
if (referer !=null ) return HttpResponses .redirectTo (referer );
414
433
return HttpResponses .redirectToContextRoot (); // referer should be always there, but be defensive
415
434
}
0 commit comments