未验证 提交 4ce2e9e8 编写于 作者: wu-sheng's avatar wu-sheng 提交者: GitHub

Fix security issue of the metrics query (#4639)

上级 5646dfbb
...@@ -109,20 +109,24 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO ...@@ -109,20 +109,24 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO
@Override @Override
public IntValues getLinearIntValues(String tableName, DownSampling downsampling, List<String> ids, public IntValues getLinearIntValues(String tableName, DownSampling downsampling, List<String> ids,
String valueCName) throws IOException { String valueCName) throws IOException {
StringBuilder idValues = new StringBuilder(); StringBuilder sql = new StringBuilder("select id, " + valueCName + " from " + tableName + " where id in (");
for (int valueIdx = 0; valueIdx < ids.size(); valueIdx++) { List<Object> parameters = new ArrayList();
if (valueIdx != 0) { for (int i = 0; i < ids.size(); i++) {
idValues.append(","); if (i == 0) {
sql.append("?");
} else {
sql.append(",?");
} }
idValues.append("'").append(ids.get(valueIdx)).append("'"); parameters.add(ids.get(i));
} }
sql.append(")");
IntValues intValues = new IntValues(); IntValues intValues = new IntValues();
try (Connection connection = h2Client.getConnection()) { try (Connection connection = h2Client.getConnection()) {
try (ResultSet resultSet = h2Client.executeQuery( try (ResultSet resultSet = h2Client.executeQuery(
connection, "select id, " + valueCName + " from " + tableName + " where id in (" + idValues connection, sql.toString(), parameters.toArray(new Object[0]))) {
.toString() + ")")) {
while (resultSet.next()) { while (resultSet.next()) {
KVInt kv = new KVInt(); KVInt kv = new KVInt();
kv.setId(resultSet.getString("id")); kv.setId(resultSet.getString("id"));
...@@ -143,13 +147,17 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO ...@@ -143,13 +147,17 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO
List<String> ids, List<String> ids,
final List<Integer> linearIndex, final List<Integer> linearIndex,
String valueCName) throws IOException { String valueCName) throws IOException {
StringBuilder idValues = new StringBuilder(); StringBuilder sql = new StringBuilder("select id, " + valueCName + " from " + tableName + " where id in (");
for (int valueIdx = 0; valueIdx < ids.size(); valueIdx++) { List<Object> parameters = new ArrayList();
if (valueIdx != 0) { for (int i = 0; i < ids.size(); i++) {
idValues.append(","); if (i == 0) {
sql.append("?");
} else {
sql.append(",?");
} }
idValues.append("'").append(ids.get(valueIdx)).append("'"); parameters.add(ids.get(i));
} }
sql.append(")");
IntValues[] intValuesArray = new IntValues[linearIndex.size()]; IntValues[] intValuesArray = new IntValues[linearIndex.size()];
for (int i = 0; i < intValuesArray.length; i++) { for (int i = 0; i < intValuesArray.length; i++) {
...@@ -158,8 +166,7 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO ...@@ -158,8 +166,7 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO
try (Connection connection = h2Client.getConnection()) { try (Connection connection = h2Client.getConnection()) {
try (ResultSet resultSet = h2Client.executeQuery( try (ResultSet resultSet = h2Client.executeQuery(
connection, "select id, " + valueCName + " from " + tableName + " where id in (" + idValues connection, sql.toString(), parameters.toArray(new Object[0]))) {
.toString() + ")")) {
while (resultSet.next()) { while (resultSet.next()) {
String id = resultSet.getString("id"); String id = resultSet.getString("id");
...@@ -211,13 +218,18 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO ...@@ -211,13 +218,18 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO
@Override @Override
public Thermodynamic getThermodynamic(String tableName, DownSampling downsampling, List<String> ids, public Thermodynamic getThermodynamic(String tableName, DownSampling downsampling, List<String> ids,
String valueCName) throws IOException { String valueCName) throws IOException {
StringBuilder idValues = new StringBuilder(); StringBuilder sql = new StringBuilder(
for (int valueIdx = 0; valueIdx < ids.size(); valueIdx++) { "select " + ThermodynamicMetrics.STEP + " step, " + ThermodynamicMetrics.NUM_OF_STEPS + " num_of_steps, " + ThermodynamicMetrics.DETAIL_GROUP + " detail_group, " + "id " + " from " + tableName + " where id in (");
if (valueIdx != 0) { List<Object> parameters = new ArrayList();
idValues.append(","); for (int i = 0; i < ids.size(); i++) {
if (i == 0) {
sql.append("?");
} else {
sql.append(",?");
} }
idValues.append("'").append(ids.get(valueIdx)).append("'"); parameters.add(ids.get(i));
} }
sql.append(")");
List<List<Long>> thermodynamicValueCollection = new ArrayList<>(); List<List<Long>> thermodynamicValueCollection = new ArrayList<>();
Map<String, List<Long>> thermodynamicValueMatrix = new HashMap<>(); Map<String, List<Long>> thermodynamicValueMatrix = new HashMap<>();
...@@ -227,10 +239,7 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO ...@@ -227,10 +239,7 @@ public class H2MetricsQueryDAO extends H2SQLExecutor implements IMetricsQueryDAO
int numOfSteps = 0; int numOfSteps = 0;
int axisYStep = 0; int axisYStep = 0;
try (ResultSet resultSet = h2Client.executeQuery( try (ResultSet resultSet = h2Client.executeQuery(
connection, connection, sql.toString(), parameters.toArray(new Object[0]))) {
"select " + ThermodynamicMetrics.STEP + " step, " + ThermodynamicMetrics.NUM_OF_STEPS + " num_of_steps, " + ThermodynamicMetrics.DETAIL_GROUP + " detail_group, " + "id " + " from " + tableName + " where id in (" + idValues
.toString() + ")"
)) {
while (resultSet.next()) { while (resultSet.next()) {
axisYStep = resultSet.getInt("step"); axisYStep = resultSet.getInt("step");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册