Skip to content

Commit 0334170

Browse files
committed
Fix race condition during initial admin account creation
1 parent 709f36e commit 0334170

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

core/src/main/java/hudson/security/HudsonPrivateSecurityRealm.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,25 +356,34 @@ private String getErrorMessages(SignupInfo si) {
356356
return messages.toString();
357357
}
358358

359+
/**
360+
* Lock used to make initial admin account creation atomic.
361+
*/
362+
private static final Object CREATE_FIRST_ACCOUNT_LOCK = new Object();
363+
359364
/**
360365
* Creates a first admin user account.
361366
*
362367
* <p>
363368
* This can be run by anyone, but only to create the very first user account.
364369
*/
365370
@RequirePOST
366-
public void doCreateFirstAccount(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException {
367-
if (hasSomeUser()) {
368-
rsp.sendError(SC_UNAUTHORIZED, "First user was already created");
369-
return;
370-
}
371-
User u = createAccount(req, rsp, false, "firstUser.jelly");
372-
if (u != null) {
373-
tryToMakeAdmin(u);
374-
loginAndTakeBack(req, rsp, u);
371+
public void doCreateFirstAccount(StaplerRequest2 req, StaplerResponse2 rsp)
372+
throws IOException, ServletException {
373+
synchronized (CREATE_FIRST_ACCOUNT_LOCK) {
374+
if (hasSomeUser()) {
375+
rsp.sendError(SC_UNAUTHORIZED, "First user was already created");
376+
return;
377+
}
378+
User u = createAccount(req, rsp, false, "firstUser.jelly");
379+
if (u != null) {
380+
tryToMakeAdmin(u);
381+
loginAndTakeBack(req, rsp, u);
382+
}
375383
}
376384
}
377385

386+
378387
/**
379388
* Try to make this user a super-user
380389
*/

0 commit comments

Comments
 (0)