Skip to content

Commit

Permalink
feature: isCommonlyUsed password check not hardcoded #4018
Browse files Browse the repository at this point in the history
Signed-off-by: WillardHu <[email protected]>
  • Loading branch information
WillardHu committed Jan 17, 2022
1 parent 96ee53a commit 8b3d6eb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Apollo 2.0.0
* [Add unit tests for Utils](https://github.com/apolloconfig/apollo/pull/4193)
* [Change Copy Right year to 2022](https://github.com/apolloconfig/apollo/pull/4202)
* [Allow disable apollo client cache](https://github.com/apolloconfig/apollo/pull/4199)
* [Make password check not hardcoded](https://github.com/apolloconfig/apollo/pull/4207)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -273,4 +275,12 @@ public String[] webHookUrls() {
public boolean supportSearchByItem() {
return getBooleanProperty("searchByItem.switch", true);
}

public List<String> getUserPasswordNotAllowList() {
String[] value = getArrayProperty("apollo.portal.auth.user-password-not-allow-list", null);
if (value == null || value.length == 0) {
return Collections.emptyList();
}
return Arrays.asList(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.ctrip.framework.apollo.portal.util.checker;

import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.google.common.base.Strings;
import java.util.Arrays;
import java.util.List;
Expand All @@ -28,7 +29,7 @@ public class AuthUserPasswordChecker implements UserPasswordChecker {
private static final Pattern PWD_PATTERN = Pattern
.compile("^(?=.*[0-9].*)(?=.*[a-zA-Z].*).{8,20}$");

private static final List<String> LIST_OF_CODE_FRAGMENT = Arrays.asList(
private static final List<String> DEFAULT_USER_PASSWORD_NOT_ALLOW_LIST = Arrays.asList(
"111", "222", "333", "444", "555", "666", "777", "888", "999", "000",
"001122", "112233", "223344", "334455", "445566", "556677", "667788", "778899", "889900",
"009988", "998877", "887766", "776655", "665544", "554433", "443322", "332211", "221100",
Expand All @@ -37,6 +38,12 @@ public class AuthUserPasswordChecker implements UserPasswordChecker {
"1q2w", "2w3e", "3e4r", "5t6y", "abcd", "qwer", "asdf", "zxcv"
);

private final PortalConfig portalConfig;

public AuthUserPasswordChecker(final PortalConfig portalConfig) {
this.portalConfig = portalConfig;
}

@Override
public CheckResult checkWeakPassword(String password) {
if (!PWD_PATTERN.matcher(password).matches()) {
Expand All @@ -58,7 +65,11 @@ private boolean isCommonlyUsed(String password) {
if (Strings.isNullOrEmpty(password)) {
return true;
}
for (String s : LIST_OF_CODE_FRAGMENT) {
List<String> listOfCodeFragment = portalConfig.getUserPasswordNotAllowList();
if (listOfCodeFragment.isEmpty()) {
listOfCodeFragment = DEFAULT_USER_PASSWORD_NOT_ALLOW_LIST;
}
for (String s : listOfCodeFragment) {
if (password.toLowerCase().contains(s)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@
*/
package com.ctrip.framework.apollo.portal.util;

import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker;
import com.ctrip.framework.apollo.portal.util.checker.CheckResult;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class AuthUserPasswordCheckerTest {

private AuthUserPasswordChecker checker;

@Before
public void setup() {
checker = new AuthUserPasswordChecker();
}

@Test
public void testRegexMatch() {
PortalConfig mock = Mockito.mock(PortalConfig.class);
AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);
List<String> unMatchList = Arrays.asList(
"11111111",
"oibjdiel",
Expand Down Expand Up @@ -63,6 +59,9 @@ public void testRegexMatch() {

@Test
public void testIsWeakPassword() {
PortalConfig mock = Mockito.mock(PortalConfig.class);
AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);

List<String> weakPwdList = Arrays.asList(
"a1234567", "b98765432", "c11111111", "d2222222", "e3333333", "f4444444",
"g5555555", "h6666666", "i7777777", "j8888888", "k9999999", "l0000000",
Expand All @@ -81,4 +80,24 @@ public void testIsWeakPassword() {
Assert.assertTrue(res.isSuccess());
}

@Test
public void testIsWeakPassword2() {
PortalConfig mock = Mockito.mock(PortalConfig.class);
Mockito.when(mock.getUserPasswordNotAllowList()).thenReturn(Arrays.asList("1111", "2222"));

AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);
List<String> weakPwdList = Arrays.asList("a11111111", "a22222222");
String exceptedErrMsg =
"Passwords cannot be consecutive, regular letters or numbers. And cannot be commonly used.";

for (String p : weakPwdList) {
CheckResult res = checker.checkWeakPassword(p);
Assert.assertFalse(res.isSuccess());
Assert.assertTrue(res.getMessage().startsWith(exceptedErrMsg));
}

CheckResult res = checker.checkWeakPassword("a33333333");
Assert.assertTrue(res.isSuccess());
}

}

0 comments on commit 8b3d6eb

Please sign in to comment.