提交 0337086c authored 作者: Thomas Mueller's avatar Thomas Mueller

Creating a user with a null password, salt, or hash threw a NullPointerException.

上级 cdec23b9
...@@ -20,7 +20,8 @@ Change Log ...@@ -20,7 +20,8 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Foreign key: don't add a single column index if column <ul><li>Creating a user with a null password, salt, or hash threw a NullPointerException.
</li><li>Foreign key: don't add a single column index if column
is leading key of existing index. is leading key of existing index.
</li><li>Pull request #4: Creating and removing temporary tables was getting </li><li>Pull request #4: Creating and removing temporary tables was getting
slower and slower over time, because an internal object id was allocated but slower and slower over time, because an internal object id was allocated but
......
...@@ -12,8 +12,6 @@ import org.h2.engine.Session; ...@@ -12,8 +12,6 @@ import org.h2.engine.Session;
import org.h2.engine.User; import org.h2.engine.User;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.security.SHA256;
import org.h2.util.StringUtils;
/** /**
* This class represents the statements * This class represents the statements
...@@ -63,15 +61,6 @@ public class AlterUser extends DefineCommand { ...@@ -63,15 +61,6 @@ public class AlterUser extends DefineCommand {
this.password = password; this.password = password;
} }
private char[] getCharArray(Expression e) {
return e.optimize(session).getValue(session).getString().toCharArray();
}
private byte[] getByteArray(Expression e) {
return StringUtils.convertHexToBytes(
e.optimize(session).getValue(session).getString());
}
@Override @Override
public int update() { public int update() {
session.commit(true); session.commit(true);
...@@ -82,12 +71,9 @@ public class AlterUser extends DefineCommand { ...@@ -82,12 +71,9 @@ public class AlterUser extends DefineCommand {
session.getUser().checkAdmin(); session.getUser().checkAdmin();
} }
if (hash != null && salt != null) { if (hash != null && salt != null) {
user.setSaltAndHash(getByteArray(salt), getByteArray(hash)); CreateUser.setSaltAndHash(user, session, salt, hash);
} else { } else {
String name = newName == null ? user.getName() : newName; CreateUser.setPassword(user, session, password);
char[] passwordChars = getCharArray(password);
byte[] userPasswordHash = SHA256.getKeyPasswordHash(name, passwordChars);
user.setUserPasswordHash(userPasswordHash);
} }
break; break;
case CommandInterface.ALTER_USER_RENAME: case CommandInterface.ALTER_USER_RENAME:
......
...@@ -45,13 +45,26 @@ public class CreateUser extends DefineCommand { ...@@ -45,13 +45,26 @@ public class CreateUser extends DefineCommand {
this.password = password; this.password = password;
} }
private char[] getCharArray(Expression e) { static void setSaltAndHash(User user, Session session, Expression salt, Expression hash) {
return e.optimize(session).getValue(session).getString().toCharArray(); user.setSaltAndHash(getByteArray(session, salt), getByteArray(session, hash));
} }
private byte[] getByteArray(Expression e) { private static byte[] getByteArray(Session session, Expression e) {
return StringUtils.convertHexToBytes( String s = e.optimize(session).getValue(session).getString();
e.optimize(session).getValue(session).getString()); return s == null ? new byte[0] : StringUtils.convertHexToBytes(s);
}
static void setPassword(User user, Session session, Expression password) {
String pwd = password.optimize(session).getValue(session).getString();
char[] passwordChars = pwd == null ? new char[0] : pwd.toCharArray();
byte[] userPasswordHash;
String userName = user.getName();
if (userName.length() == 0 && passwordChars.length == 0) {
userPasswordHash = new byte[0];
} else {
userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars);
}
user.setUserPasswordHash(userPasswordHash);
} }
@Override @Override
...@@ -73,16 +86,9 @@ public class CreateUser extends DefineCommand { ...@@ -73,16 +86,9 @@ public class CreateUser extends DefineCommand {
user.setAdmin(admin); user.setAdmin(admin);
user.setComment(comment); user.setComment(comment);
if (hash != null && salt != null) { if (hash != null && salt != null) {
user.setSaltAndHash(getByteArray(salt), getByteArray(hash)); setSaltAndHash(user, session, salt, hash);
} else if (password != null) { } else if (password != null) {
char[] passwordChars = getCharArray(password); setPassword(user, session, password);
byte[] userPasswordHash;
if (userName.length() == 0 && passwordChars.length == 0) {
userPasswordHash = new byte[0];
} else {
userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars);
}
user.setUserPasswordHash(userPasswordHash);
} else { } else {
throw DbException.throwInternalError(); throw DbException.throwInternalError();
} }
......
...@@ -34,6 +34,7 @@ public class TestRights extends TestBase { ...@@ -34,6 +34,7 @@ public class TestRights extends TestBase {
@Override @Override
public void test() throws SQLException { public void test() throws SQLException {
testNullPassword();
testLinkedTableMeta(); testLinkedTableMeta();
testGrantMore(); testGrantMore();
testOpenNonAdminWithMode(); testOpenNonAdminWithMode();
...@@ -48,6 +49,17 @@ public class TestRights extends TestBase { ...@@ -48,6 +49,17 @@ public class TestRights extends TestBase {
deleteDb("rights"); deleteDb("rights");
} }
private void testNullPassword() throws SQLException {
deleteDb("rights");
Connection conn = getConnection("rights");
stat = conn.createStatement();
stat.execute("create user test password null");
stat.execute("alter user test set password null");
stat.execute("create user test2 salt null hash null");
stat.execute("alter user test set salt null hash null");
conn.close();
}
private void testLinkedTableMeta() throws SQLException { private void testLinkedTableMeta() throws SQLException {
deleteDb("rights"); deleteDb("rights");
Connection conn = getConnection("rights"); Connection conn = getConnection("rights");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论