#3121 BindMaxLength validation

At deploy time derive a BindMaxLength property to use
per BeanProperty
This commit is contained in:
Rob Bygrave
2023-06-26 21:16:32 +12:00
parent 4683877e0b
commit df7b04877d
7 changed files with 136 additions and 65 deletions
@@ -7,9 +7,9 @@ import java.io.InputStream;
* *
* @author Roland Praml, FOCONIS AG * @author Roland Praml, FOCONIS AG
*/ */
public class InputStreamInfo { public final class InputStreamInfo {
private final InputStream stream;
private final InputStream stream;
private final long length; private final long length;
public InputStreamInfo(InputStream stream, long length) { public InputStreamInfo(InputStream stream, long length) {
@@ -132,37 +132,37 @@ public class DataBind implements DataBinder {
@Override @Override
public final void setInt(int value) throws SQLException { public final void setInt(int value) throws SQLException {
pstmt.setInt(++pos, value); pstmt.setInt(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
public final void setLong(long value) throws SQLException { public final void setLong(long value) throws SQLException {
pstmt.setLong(++pos, value); pstmt.setLong(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
public final void setShort(short value) throws SQLException { public final void setShort(short value) throws SQLException {
pstmt.setShort(++pos, value); pstmt.setShort(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
public final void setFloat(float value) throws SQLException { public final void setFloat(float value) throws SQLException {
pstmt.setFloat(++pos, value); pstmt.setFloat(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
public final void setDouble(double value) throws SQLException { public final void setDouble(double value) throws SQLException {
pstmt.setDouble(++pos, value); pstmt.setDouble(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
public final void setBigDecimal(BigDecimal value) throws SQLException { public final void setBigDecimal(BigDecimal value) throws SQLException {
pstmt.setBigDecimal(++pos, value); pstmt.setBigDecimal(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
@@ -173,7 +173,7 @@ public class DataBind implements DataBinder {
} else { } else {
pstmt.setDate(++pos, value); pstmt.setDate(++pos, value);
} }
lastObject = value; lastObject = null;
} }
@Override @Override
@@ -184,7 +184,7 @@ public class DataBind implements DataBinder {
} else { } else {
pstmt.setTimestamp(++pos, value); pstmt.setTimestamp(++pos, value);
} }
lastObject = value; lastObject = null;
} }
@Override @Override
@@ -195,13 +195,13 @@ public class DataBind implements DataBinder {
} else { } else {
pstmt.setTime(++pos, value); pstmt.setTime(++pos, value);
} }
lastObject = value; lastObject = null;
} }
@Override @Override
public void setBoolean(boolean value) throws SQLException { public void setBoolean(boolean value) throws SQLException {
pstmt.setBoolean(++pos, value); pstmt.setBoolean(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
@@ -213,13 +213,13 @@ public class DataBind implements DataBinder {
@Override @Override
public void setByte(byte value) throws SQLException { public void setByte(byte value) throws SQLException {
pstmt.setByte(++pos, value); pstmt.setByte(++pos, value);
lastObject = value; lastObject = null;
} }
@Override @Override
public void setChar(char value) throws SQLException { public void setChar(char value) throws SQLException {
pstmt.setString(++pos, String.valueOf(value)); pstmt.setString(++pos, String.valueOf(value));
lastObject = value; lastObject = null;
} }
@Override @Override
@@ -252,7 +252,7 @@ public class DataBind implements DataBinder {
@Override @Override
public void setArray(String arrayType, Object[] elements) throws SQLException { public void setArray(String arrayType, Object[] elements) throws SQLException {
pstmt.setArray(++pos, connection.createArrayOf(arrayType, elements)); pstmt.setArray(++pos, connection.createArrayOf(arrayType, elements));
lastObject = elements; lastObject = null;
} }
@Override @Override
@@ -105,6 +105,7 @@ public final class BeanDescriptorManager implements BeanDescriptorMap, SpiBeanTy
private final String asOfViewSuffix; private final String asOfViewSuffix;
private final boolean jacksonCorePresent; private final boolean jacksonCorePresent;
private final int queryPlanTTLSeconds; private final int queryPlanTTLSeconds;
private final BindMaxLength bindMaxLength;
private int entityBeanCount; private int entityBeanCount;
private List<BeanDescriptor<?>> immutableDescriptorList; private List<BeanDescriptor<?>> immutableDescriptorList;
/** /**
@@ -159,6 +160,19 @@ public final class BeanDescriptorManager implements BeanDescriptorMap, SpiBeanTy
this.changeLogListener = config.changeLogListener(bootupClasses.getChangeLogListener()); this.changeLogListener = config.changeLogListener(bootupClasses.getChangeLogListener());
this.changeLogRegister = config.changeLogRegister(bootupClasses.getChangeLogRegister()); this.changeLogRegister = config.changeLogRegister(bootupClasses.getChangeLogRegister());
this.jacksonCorePresent = config.isJacksonCorePresent(); this.jacksonCorePresent = config.isJacksonCorePresent();
this.bindMaxLength = initMaxLength();
}
BindMaxLength initMaxLength() {
LengthCheck lengthCheck = this.config.getLengthCheck();
switch (lengthCheck) {
case OFF:
return null;
case UTF8:
return BindMaxLength.ofUtf8();
default:
return BindMaxLength.ofStandard();
}
} }
@Override @Override
@@ -1502,6 +1516,10 @@ public final class BeanDescriptorManager implements BeanDescriptorMap, SpiBeanTy
return list; return list;
} }
public BindMaxLength bindMaxLength() {
return bindMaxLength;
}
/** /**
* Comparator to sort the BeanDescriptors by name. * Comparator to sort the BeanDescriptors by name.
*/ */
@@ -171,6 +171,7 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
*/ */
private final String dbComment; private final String dbComment;
private final DbEncryptFunction dbEncryptFunction; private final DbEncryptFunction dbEncryptFunction;
private final BindMaxLength bindMaxLength;
private int deployOrder; private int deployOrder;
final boolean jsonSerialize; final boolean jsonSerialize;
final boolean jsonDeserialize; final boolean jsonDeserialize;
@@ -263,6 +264,7 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
} }
this.jsonSerialize = deploy.isJsonSerialize(); this.jsonSerialize = deploy.isJsonSerialize();
this.jsonDeserialize = deploy.isJsonDeserialize(); this.jsonDeserialize = deploy.isJsonDeserialize();
this.bindMaxLength = deploy.bindMaxLength();
} }
private String tableAliasIntern(BeanDescriptor<?> descriptor, String s, boolean dbEncrypted, String dbColumn) { private String tableAliasIntern(BeanDescriptor<?> descriptor, String s, boolean dbEncrypted, String dbColumn) {
@@ -350,6 +352,7 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
this.elPlaceHolderEncrypted = override.replace(source.elPlaceHolderEncrypted, source.dbColumn); this.elPlaceHolderEncrypted = override.replace(source.elPlaceHolderEncrypted, source.dbColumn);
this.jsonSerialize = source.jsonSerialize; this.jsonSerialize = source.jsonSerialize;
this.jsonDeserialize = source.jsonDeserialize; this.jsonDeserialize = source.jsonDeserialize;
this.bindMaxLength = source.bindMaxLength;
} }
/** /**
@@ -559,20 +562,16 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void bind(DataBind b, Object value) throws SQLException { public void bind(DataBind b, Object value) throws SQLException {
scalarType.bind(b, value); scalarType.bind(b, value);
if (bindMaxLength != null) {
if (needsLengthCheck()) { Object obj = b.popLastObject();
LengthCheck lengthCheck = descriptor().config().getLengthCheck(); long length = bindMaxLength.length(dbLength, obj);
if (lengthCheck != LengthCheck.OFF) { if (length > dbLength) {
Object obj = b.popLastObject(); b.closeInputStreams();
long l = getLength(obj, lengthCheck == LengthCheck.UTF8); String s = String.valueOf(value); // take original bind value here.
if (l > dbLength) { if (s.length() > 50) {
b.closeInputStreams(); s = s.substring(0, 47) + "...";
String s = String.valueOf(value); // take original bind value here.
if (s.length() > 100) {
s = s.substring(0, 97) + "...";
}
throw new DataIntegrityException("Cannot bind value '" + s + "' (effective length=" + l + ") to column '" + dbColumn + "' (length=" + dbLength + ")");
} }
throw new DataIntegrityException("Cannot bind value '" + s + "' (effective length=" + length + ") to column '" + dbColumn + "' (length=" + dbLength + ")");
} }
} }
} }
@@ -586,43 +585,6 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
return scalarType.readData(dataInput); return scalarType.readData(dataInput);
} }
/**
* Returns, if this property needs a length check.
*/
boolean needsLengthCheck() {
if (dbLength == 0) {
return false;
}
switch (dbType) {
case Types.VARCHAR:
case Types.BLOB:
case ExtraDbTypes.JSON:
return true;
default:
return false;
}
}
/**
* Returns the length of <code>obj</code>. Note: for UTF8 strings -1 will be retuned, if the string length is lower than 1/4th of db length
*/
private long getLength(Object obj, boolean utf8) {
if (obj instanceof String) {
String s = (String) obj;
if (utf8) {
return s.length() * 4 <= dbLength ? -1 : s.getBytes(StandardCharsets.UTF_8).length;
} else {
return s.length();
}
} else if (obj instanceof byte[]) {
return ((byte[]) obj).length;
} else if (obj instanceof InputStreamInfo) {
return ((InputStreamInfo) obj).length();
} else {
return -1;
}
}
@Override @Override
public BeanProperty beanProperty() { public BeanProperty beanProperty() {
return this; return this;
@@ -0,0 +1,68 @@
package io.ebeaninternal.server.deploy;
import io.ebean.core.type.InputStreamInfo;
import java.nio.charset.StandardCharsets;
public interface BindMaxLength {
/**
* Return a UTF8 based implementation.
*/
static BindMaxLength ofUtf8() {
return new UTF8();
}
/**
* Return a standard implementation.
*/
static BindMaxLength ofStandard() {
return new Standard();
}
/**
* Return the length of the object.
*/
long length(int dbLength, Object obj);
final class UTF8 implements BindMaxLength {
@Override
public long length(int dbLength, Object obj) {
if (obj instanceof String) {
String s = (String) obj;
int stringLength = s.length();
if (stringLength > dbLength) {
return stringLength;
} else if (stringLength * 4 <= dbLength) {
return -1;
} else {
return s.getBytes(StandardCharsets.UTF_8).length;
}
} else if (obj instanceof byte[]) {
return ((byte[]) obj).length;
} else if (obj instanceof InputStreamInfo) {
return ((InputStreamInfo) obj).length();
} else {
return -1;
}
}
}
final class Standard implements BindMaxLength {
@Override
public long length(int dbLength, Object obj) {
if (obj instanceof String) {
return ((String) obj).length();
} else if (obj instanceof byte[]) {
return ((byte[]) obj).length;
} else if (obj instanceof InputStreamInfo) {
return ((InputStreamInfo) obj).length();
} else {
return -1;
}
}
}
}
@@ -139,6 +139,10 @@ public class DeployBeanDescriptor<T> {
this.beanType = beanType; this.beanType = beanType;
} }
public BindMaxLength bindMaxLength() {
return manager.bindMaxLength();
}
/** /**
* Set the IdClass to use. * Set the IdClass to use.
*/ */
@@ -1,14 +1,17 @@
package io.ebeaninternal.server.deploy.meta; package io.ebeaninternal.server.deploy.meta;
import io.avaje.lang.Nullable;
import io.ebean.annotation.*; import io.ebean.annotation.*;
import io.ebean.config.ScalarTypeConverter; import io.ebean.config.ScalarTypeConverter;
import io.ebean.config.dbplatform.DbDefaultValue; import io.ebean.config.dbplatform.DbDefaultValue;
import io.ebean.config.dbplatform.DbEncrypt; import io.ebean.config.dbplatform.DbEncrypt;
import io.ebean.config.dbplatform.DbEncryptFunction; import io.ebean.config.dbplatform.DbEncryptFunction;
import io.ebean.config.dbplatform.ExtraDbTypes;
import io.ebean.core.type.ScalarType; import io.ebean.core.type.ScalarType;
import io.ebean.util.AnnotationUtil; import io.ebean.util.AnnotationUtil;
import io.ebeaninternal.server.core.InternString; import io.ebeaninternal.server.core.InternString;
import io.ebeaninternal.server.deploy.BeanProperty; import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BindMaxLength;
import io.ebeaninternal.server.deploy.DbMigrationInfo; import io.ebeaninternal.server.deploy.DbMigrationInfo;
import io.ebeaninternal.server.deploy.DeployDocPropertyOptions; import io.ebeaninternal.server.deploy.DeployDocPropertyOptions;
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty; import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
@@ -1133,4 +1136,20 @@ public class DeployBeanProperty {
boolean isJsonType() { boolean isJsonType() {
return mutationDetection != null; return mutationDetection != null;
} }
@Nullable
public BindMaxLength bindMaxLength() {
if (dbLength == 0) {
return null;
}
switch (dbType) {
case Types.VARCHAR:
case Types.BLOB:
case ExtraDbTypes.JSON:
return desc.bindMaxLength();
default:
return null;
}
}
} }