Skip to content

Commit 69ff99e

Browse files
authored
Merge pull request #154 from terryquigleysas/additional_exception_handling
Additional Exception Handling for Restricted Environments
2 parents f60d2e4 + f7e3cea commit 69ff99e

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/main/java/com/password4j/PropertyReader.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.FileNotFoundException;
2424
import java.io.IOException;
2525
import java.io.InputStream;
26+
import java.security.AccessControlException;
2627
import java.util.Properties;
2728

2829

@@ -113,19 +114,29 @@ private static String readString(String key)
113114

114115
static void init()
115116
{
116-
String customPath = System.getProperty(CONFIGURATION_KEY, null);
117+
String customPath = null;
117118

118-
InputStream in;
119-
if (customPath == null || customPath.length() == 0)
120-
{
121-
in = getResource('/' + FILE_NAME);
122-
}
123-
else
124-
{
125-
in = getResource(customPath);
119+
try {
120+
customPath = System.getProperty(CONFIGURATION_KEY, null);
121+
} catch (AccessControlException ex) {
122+
LOG.debug("Cannot access configuration key property", ex);
126123
}
127124

125+
InputStream in = null;
128126
Properties props = new Properties();
127+
try {
128+
if (customPath == null || customPath.length() == 0)
129+
{
130+
in = getResource('/' + FILE_NAME);
131+
}
132+
else
133+
{
134+
in = getResource(customPath);
135+
}
136+
} catch (AccessControlException ex) {
137+
LOG.debug("Cannot access properties file", ex);
138+
props.setProperty("global.banner", "false");
139+
}
129140

130141
if (in != null)
131142
{

src/test/com/password4j/PasswordTest.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.PrintStream;
2121
import java.nio.charset.StandardCharsets;
22+
import java.security.CodeSource;
23+
import java.security.PermissionCollection;
24+
import java.security.Permissions;
25+
import java.security.Policy;
2226
import java.security.Provider;
2327
import java.security.SecureRandom;
2428
import java.security.Security;
2529
import java.util.Random;
26-
import java.util.Set;
2730

2831
import org.junit.Assert;
2932
import org.junit.Test;
@@ -1233,6 +1236,30 @@ public void testBalloon4()
12331236
Assert.assertTrue(Password.check(plainTextPassword, hashed).addPepper(pepper).addSalt(salt).withBalloonHashing());
12341237
}
12351238

1239+
@Test
1240+
public void testRestrictedPermissions()
1241+
{
1242+
// GIVEN
1243+
Policy.setPolicy(new Policy(){
1244+
@Override
1245+
public PermissionCollection getPermissions(CodeSource codesource) {
1246+
Permissions permissions = new Permissions();
1247+
permissions.add(new RuntimePermission("setSecurityManager"));
1248+
return permissions;
1249+
}
1250+
});
1251+
System.setSecurityManager(new SecurityManager());
1252+
1253+
// WHEN
1254+
Hash hash1 = Password.hash(PASSWORD).addPepper(PEPPER).addSalt(SALT).withPBKDF2();
1255+
Hash hash2 = Password.hash(PASSWORD).addPepper(PEPPER).withBcrypt();
1256+
Hash hash3 = Password.hash(PASSWORD).addPepper(PEPPER).addSalt(SALT).withScrypt();
12361257

1258+
// THEN
1259+
assertTrue(Password.check(PASSWORD, hash1));
1260+
assertTrue(Password.check(PASSWORD, hash2));
1261+
assertTrue(Password.check(PASSWORD, hash3));
12371262

1263+
System.setSecurityManager(null);
1264+
}
12381265
}

0 commit comments

Comments
 (0)