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 14, 2022
1 parent 96ee53a commit 538199f
Show file tree
Hide file tree
Showing 4 changed files with 69 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> listOfCodeFragment() {
String[] value = getArrayProperty("auth.user-password-checker.list-of-code-fragment", 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_LIST_OF_CODE_FRAGMENT = 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.listOfCodeFragment();
if (listOfCodeFragment.isEmpty()) {
listOfCodeFragment = DEFAULT_LIST_OF_CODE_FRAGMENT;
}
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,20 @@
*/
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.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class AuthUserPasswordCheckerTest {

private AuthUserPasswordChecker checker;

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

@Test
public void testRegexMatch() {
AuthUserPasswordChecker checker = new AuthUserPasswordChecker(PortalConfigFaker.empty());
List<String> unMatchList = Arrays.asList(
"11111111",
"oibjdiel",
Expand Down Expand Up @@ -63,6 +58,8 @@ public void testRegexMatch() {

@Test
public void testIsWeakPassword() {
AuthUserPasswordChecker checker = new AuthUserPasswordChecker(PortalConfigFaker.empty());

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

@Test
public void testIsWeakPassword2() {
AuthUserPasswordChecker checker = new AuthUserPasswordChecker(
PortalConfigFaker.build("1111", "2222"));
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());
}

static class PortalConfigFaker extends PortalConfig {

private final List<String> listOfCodeFragment;

public PortalConfigFaker(List<String> listOfCodeFragment) {
super(null);
this.listOfCodeFragment = listOfCodeFragment;
}

static PortalConfigFaker empty() {
return new PortalConfigFaker(Collections.emptyList());
}

static PortalConfigFaker build(String... codeFragments) {
return new PortalConfigFaker(Arrays.asList(codeFragments));
}

@Override
public List<String> listOfCodeFragment() {
return listOfCodeFragment;
}
}
}

0 comments on commit 538199f

Please sign in to comment.