Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Exception Handling for Restricted Environments #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/main/java/com/password4j/PropertyReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.AccessControlException;
import java.util.Properties;


Expand Down Expand Up @@ -113,19 +114,29 @@ private static String readString(String key)

static void init()
{
String customPath = System.getProperty(CONFIGURATION_KEY, null);
String customPath = null;

InputStream in;
if (customPath == null || customPath.length() == 0)
{
in = getResource('/' + FILE_NAME);
}
else
{
in = getResource(customPath);
try {
customPath = System.getProperty(CONFIGURATION_KEY, null);
} catch (AccessControlException ex) {
LOG.debug("Cannot access configuration key property", ex);
}

InputStream in = null;
Properties props = new Properties();
try {
if (customPath == null || customPath.length() == 0)
{
in = getResource('/' + FILE_NAME);
}
else
{
in = getResource(customPath);
}
} catch (AccessControlException ex) {
LOG.debug("Cannot access properties file", ex);
props.setProperty("global.banner", "false");
}

if (in != null)
{
Expand Down
29 changes: 28 additions & 1 deletion src/test/com/password4j/PasswordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Random;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -1233,6 +1236,30 @@ public void testBalloon4()
Assert.assertTrue(Password.check(plainTextPassword, hashed).addPepper(pepper).addSalt(salt).withBalloonHashing());
}

@Test
public void testRestrictedPermissions()
{
// GIVEN
Policy.setPolicy(new Policy(){
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
Permissions permissions = new Permissions();
permissions.add(new RuntimePermission("setSecurityManager"));
return permissions;
}
});
System.setSecurityManager(new SecurityManager());

// WHEN
Hash hash1 = Password.hash(PASSWORD).addPepper(PEPPER).addSalt(SALT).withPBKDF2();
Hash hash2 = Password.hash(PASSWORD).addPepper(PEPPER).withBcrypt();
Hash hash3 = Password.hash(PASSWORD).addPepper(PEPPER).addSalt(SALT).withScrypt();

// THEN
assertTrue(Password.check(PASSWORD, hash1));
assertTrue(Password.check(PASSWORD, hash2));
assertTrue(Password.check(PASSWORD, hash3));

System.setSecurityManager(null);
}
}
Loading