Skip to content

Commit

Permalink
#3341 String length validation with Postgres PGobject (JSON/JSONB)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Mar 1, 2024
1 parent 6babb2e commit 5fa09a6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,29 @@ final class UTF8 implements BindMaxLength {
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;
}

return utf8String(dbLength, s);
} else if (obj instanceof byte[]) {
return ((byte[]) obj).length;
} else if (obj instanceof InputStreamInfo) {
return ((InputStreamInfo) obj).length();
} else if ("org.postgresql.util.PGobject".equals(obj.getClass().getCanonicalName())) {
String value = ((org.postgresql.util.PGobject) obj).getValue();
return value == null ? -1 : utf8String(dbLength, value);
} else {
return -1;
}
}

private static int utf8String(int dbLength, String s) {
int stringLength = s.length();
if (stringLength > dbLength) {
return stringLength;
} else if (stringLength * 4 <= dbLength) {
return -1;
} else {
return s.getBytes(StandardCharsets.UTF_8).length;
}
}
}

/**
Expand All @@ -69,6 +75,9 @@ public long length(int dbLength, Object obj) {
return ((byte[]) obj).length;
} else if (obj instanceof InputStreamInfo) {
return ((InputStreamInfo) obj).length();
} else if ("org.postgresql.util.PGobject".equals(obj.getClass().getCanonicalName())) {
String value = ((org.postgresql.util.PGobject) obj).getValue();
return value == null ? -1 : value.length();
} else {
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions ebean-test/src/test/resources/ebean.properties
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ datasource.oracle.url=jdbc:oracle:thin:@localhost:1521:XE
## set this to use timestamp rather than timestamptz
#ebean.postgres.timestamp=timestamp

ebean.pg.lengthCheck=on
datasource.pg.username=unit
datasource.pg.password=test
datasource.pg.url=jdbc:postgresql://127.0.0.1:6432/unit
Expand Down
2 changes: 1 addition & 1 deletion ebean-test/testconfig/ebean-cockroach.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ebean.test.platform=cockroach
ebean.test.dbName=unit
datasource.default=cockroach
#ebean.test.cockroach.version=v21.2.6

ebean.lengthCheck=on
2 changes: 1 addition & 1 deletion ebean-test/testconfig/ebean-postgres.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ebean.test.platform=postgres
datasource.default=postgres
ebean.test.dbName=unit
ebean.test.postgres.version=15

ebean.lengthCheck=on
2 changes: 1 addition & 1 deletion ebean-test/testconfig/ebean-yugabyte.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ebean.test.platform=yugabyte
datasource.default=yugabyte
ebean.test.dbName=unit
ebean.test.yugabyte.version=2.18.0.0-b65

ebean.lengthCheck=on

0 comments on commit 5fa09a6

Please sign in to comment.