Skip to content

Commit bf179e6

Browse files
committed
Merge branch 'rcaa-master' into master.
2 parents 9a51d2b + 2be2c2c commit bf179e6

13 files changed

+136
-12
lines changed

src/main/java/com/gitblit/ConfigUserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ protected synchronized void read() {
898898
user.countryCode = config.getString(USER, username, COUNTRYCODE);
899899
user.cookie = config.getString(USER, username, COOKIE);
900900
if (StringUtils.isEmpty(user.cookie) && !StringUtils.isEmpty(user.password)) {
901-
user.cookie = StringUtils.getSHA1(user.username + user.password);
901+
user.cookie = user.createCookie();
902902
}
903903

904904
// preferences

src/main/java/com/gitblit/auth/AuthenticationProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public String getServiceName() {
7878

7979
public abstract AuthenticationType getAuthenticationType();
8080

81-
protected void setCookie(UserModel user, char [] password) {
81+
protected void setCookie(UserModel user) {
8282
// create a user cookie
83-
if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
84-
user.cookie = StringUtils.getSHA1(user.username + new String(password));
83+
if (StringUtils.isEmpty(user.cookie)) {
84+
user.cookie = user.createCookie();
8585
}
8686
}
8787

src/main/java/com/gitblit/auth/HtpasswdAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ else if (supportPlaintextPwd() && storedPwd.equals(passwd)){
196196
}
197197

198198
// create a user cookie
199-
setCookie(user, password);
199+
setCookie(user);
200200

201201
// Set user attributes, hide password from backing user service.
202202
user.password = Constants.EXTERNAL_ACCOUNT;

src/main/java/com/gitblit/auth/LdapAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public UserModel authenticate(String username, char[] password) {
307307
}
308308

309309
// create a user cookie
310-
setCookie(user, password);
310+
setCookie(user);
311311

312312
if (!supportsTeamMembershipChanges()) {
313313
getTeamsFromLdap(ldapConnection, simpleUsername, loggingInUser, user);

src/main/java/com/gitblit/auth/PAMAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public UserModel authenticate(String username, char[] password) {
122122
}
123123

124124
// create a user cookie
125-
setCookie(user, password);
125+
setCookie(user);
126126

127127
// update user attributes from UnixUser
128128
user.accountType = getAccountType();

src/main/java/com/gitblit/auth/RedmineAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public UserModel authenticate(String username, char[] password) {
139139
}
140140

141141
// create a user cookie
142-
setCookie(user, password);
142+
setCookie(user);
143143

144144
// update user attributes from Redmine
145145
user.accountType = getAccountType();

src/main/java/com/gitblit/auth/SalesforceAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public UserModel authenticate(String username, char[] password) {
6666
user = new UserModel(simpleUsername);
6767
}
6868

69-
setCookie(user, password);
69+
setCookie(user);
7070
setUserAttributes(user, info);
7171

7272
updateUser(user);

src/main/java/com/gitblit/auth/WindowsAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public UserModel authenticate(String username, char[] password) {
153153
}
154154

155155
// create a user cookie
156-
setCookie(user, password);
156+
setCookie(user);
157157

158158
// update user attributes from Windows identity
159159
user.accountType = getAccountType();

src/main/java/com/gitblit/client/EditUserDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private boolean validateFields() {
330330
}
331331

332332
// change the cookie
333-
user.cookie = StringUtils.getSHA1(user.username + password);
333+
user.cookie = user.createCookie();
334334

335335
String type = settings.get(Keys.realm.passwordStorage).getString("md5");
336336
if (type.equalsIgnoreCase("md5")) {

src/main/java/com/gitblit/models/UserModel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.Serializable;
1919
import java.security.Principal;
20+
import java.security.SecureRandom;
2021
import java.util.ArrayList;
2122
import java.util.Collections;
2223
import java.util.HashSet;
@@ -36,6 +37,7 @@
3637
import com.gitblit.Constants.RegistrantType;
3738
import com.gitblit.utils.ArrayUtils;
3839
import com.gitblit.utils.ModelUtils;
40+
import com.gitblit.utils.SecureRandom;
3941
import com.gitblit.utils.StringUtils;
4042

4143
/**
@@ -52,6 +54,8 @@ public class UserModel implements Principal, Serializable, Comparable<UserModel>
5254

5355
public static final UserModel ANONYMOUS = new UserModel();
5456

57+
private static final SecureRandom RANDOM = new SecureRandom();
58+
5559
// field names are reflectively mapped in EditUser page
5660
public String username;
5761
public String password;
@@ -660,4 +664,8 @@ public boolean isMyPersonalRepository(String repository) {
660664
String projectPath = StringUtils.getFirstPathElement(repository);
661665
return !StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase(getPersonalPath());
662666
}
667+
668+
public String createCookie() {
669+
return StringUtils.getSHA1(RANDOM.randomBytes(32));
670+
}
663671
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2016 gitblit.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.gitblit.utils;
17+
18+
/**
19+
* Wrapper class for java.security.SecureRandom, which will periodically reseed
20+
* the PRNG in case an instance of the class has been running for a long time.
21+
*
22+
* @author Florian Zschocke
23+
*/
24+
public class SecureRandom {
25+
26+
/** Period (in ms) after which a new SecureRandom will be created in order to get a fresh random seed. */
27+
private static final long RESEED_PERIOD = 24 * 60 * 60 * 1000; /* 24 hours */
28+
29+
30+
private long last;
31+
private java.security.SecureRandom random;
32+
33+
34+
35+
public SecureRandom() {
36+
// Make sure the SecureRandom is seeded right from the start.
37+
// This also lets any blocks during seeding occur at creation
38+
// and prevents it from happening when getting next random bytes.
39+
seed();
40+
}
41+
42+
43+
44+
public byte[] randomBytes(int num) {
45+
byte[] bytes = new byte[num];
46+
nextBytes(bytes);
47+
return bytes;
48+
}
49+
50+
51+
public void nextBytes(byte[] bytes) {
52+
random.nextBytes(bytes);
53+
reseed(false);
54+
}
55+
56+
57+
void reseed(boolean forced) {
58+
long ts = System.currentTimeMillis();
59+
if (forced || (ts - last) > RESEED_PERIOD) {
60+
last = ts;
61+
runReseed();
62+
}
63+
}
64+
65+
66+
67+
private void seed() {
68+
random = new java.security.SecureRandom();
69+
random.nextBytes(new byte[0]);
70+
last = System.currentTimeMillis();
71+
}
72+
73+
74+
private void runReseed() {
75+
// Have some other thread hit the penalty potentially incurred by reseeding,
76+
// so that we can immediately return and not block the operation in progress.
77+
new Thread() {
78+
public void run() {
79+
seed();
80+
}
81+
}.start();
82+
}
83+
}

src/main/java/com/gitblit/wicket/pages/EditUserPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ protected void onSubmit() {
156156
}
157157

158158
// change the cookie
159-
userModel.cookie = StringUtils.getSHA1(userModel.username + password);
159+
userModel.cookie = userModel.createCookie();
160160

161161
// Optionally store the password MD5 digest.
162162
String type = app().settings().getString(Keys.realm.passwordStorage, "md5");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.gitblit.utils;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.Arrays;
6+
7+
import org.junit.Test;
8+
9+
public class SecureRandomTest {
10+
11+
@Test
12+
public void testRandomBytes() {
13+
SecureRandom sr = new SecureRandom();
14+
byte[] bytes1 = sr.randomBytes(10);
15+
assertEquals(10, bytes1.length);
16+
byte[] bytes2 = sr.randomBytes(10);
17+
assertEquals(10, bytes2.length);
18+
assertFalse(Arrays.equals(bytes1, bytes2));
19+
20+
assertEquals(0, sr.randomBytes(0).length);
21+
assertEquals(200, sr.randomBytes(200).length);
22+
}
23+
24+
@Test
25+
public void testNextBytes() {
26+
SecureRandom sr = new SecureRandom();
27+
byte[] bytes1 = new byte[32];
28+
sr.nextBytes(bytes1);
29+
byte[] bytes2 = new byte[32];
30+
sr.nextBytes(bytes2);
31+
assertFalse(Arrays.equals(bytes1, bytes2));
32+
}
33+
}

0 commit comments

Comments
 (0)