提交 5bdfd153 authored 作者: Niklas Mehner's avatar Niklas Mehner

Prevent internal properties (numServer, server*) from being set.

上级 372346d9
......@@ -70,6 +70,9 @@ import java.util.regex.Pattern;
*/
public class JdbcConnection extends TraceObject implements Connection {
private static final String NUM_SERVERS = "numServers";
private static final String PREFIX_SERVER = "server";
private static boolean keepOpenStackTrace;
private final String url;
......@@ -1698,6 +1701,10 @@ public class JdbcConnection extends TraceObject implements Connection {
}
checkClosed();
if (isInternalProperty(name)) {
throw new SQLClientInfoException("Property name '" + name + " is used internally by H2.",
Collections.<String, ClientInfoStatus> emptyMap());
}
Pattern clientInfoNameRegEx = getMode().supportedClientInfoPropertiesRegEx;
if (clientInfoNameRegEx != null && clientInfoNameRegEx.matcher(name).matches()) {
......@@ -1711,6 +1718,10 @@ public class JdbcConnection extends TraceObject implements Connection {
}
}
private boolean isInternalProperty(String name) {
return NUM_SERVERS.equals(name) || name.startsWith(PREFIX_SERVER);
}
private Mode getMode() throws SQLException {
return Mode.getInstance(((JdbcDatabaseMetaData) getMetaData()).getMode());
}
......@@ -1763,15 +1774,16 @@ public class JdbcConnection extends TraceObject implements Connection {
ArrayList<String> serverList = session.getClusterServers();
Properties p = new Properties();
p.setProperty("numServers", String.valueOf(serverList.size()));
for (int i = 0; i < serverList.size(); i++) {
p.setProperty("server" + String.valueOf(i), serverList.get(i));
}
for (Map.Entry<String, String> entry : clientInfo.entrySet()) {
p.setProperty(entry.getKey(), entry.getValue());
}
p.setProperty(NUM_SERVERS, String.valueOf(serverList.size()));
for (int i = 0; i < serverList.size(); i++) {
p.setProperty(PREFIX_SERVER + String.valueOf(i), serverList.get(i));
}
return p;
} catch (Exception e) {
throw logAndConvert(e);
......
......@@ -28,6 +28,14 @@ public class TestConnection extends TestBase {
testGetUnsupportedClientInfo();
testSetSupportedClientInfoProperties();
testSetUnsupportedClientInfoProperties();
testSetInternalProperty();
}
private void testSetInternalProperty() throws SQLException {
Connection conn = getConnection("clientInfoMySQL");
assertThrows(SQLClientInfoException.class, conn).setClientInfo("numServers", "SomeValue");
assertThrows(SQLClientInfoException.class, conn).setClientInfo("server23", "SomeValue");
}
private void testSetUnsupportedClientInfoProperties() throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论