Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions api/src/org/labkey/api/action/BaseApiAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.labkey.api.util.MimeMap;
import org.labkey.api.util.Pair;
import org.labkey.api.util.ResponseHelper;
import org.labkey.api.util.StringUtilsLabKey;
import org.labkey.api.view.BadRequestException;
import org.labkey.api.view.NotFoundException;
import org.labkey.api.view.UnauthorizedException;
Expand All @@ -52,6 +53,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.SocketTimeoutException;
import java.time.Duration;
Expand Down Expand Up @@ -511,12 +513,23 @@ protected long getMaximumJsonInputLength()

private @Nullable JSONObject getJsonObject() throws IOException
{
HttpServletRequest request = getViewContext().getRequest();
if (request == null)
return null;

String characterEncoding = request.getCharacterEncoding();
if (characterEncoding == null)
characterEncoding = StringUtilsLabKey.DEFAULT_CHARSET.name();

long maxLength = getMaximumJsonInputLength();
try (Reader r = getViewContext().getRequest().getReader();
Reader jsonReader = maxLength > 0 ? new StrictBoundedReader(r, maxLength) : r)

// Issue 53699: Use request.getInputStream() instead of request.getReader() to
// avoid BufferUnderflowException when processing multibyte character JSON payloads.
try (Reader streamReader = new InputStreamReader(request.getInputStream(), characterEncoding);
Reader jsonReader = maxLength > 0 ? new StrictBoundedReader(streamReader, maxLength) : streamReader)
{
JSONTokener tokener = new JSONTokener(r);
return tokener.more() ? new JSONObject(new JSONTokener(jsonReader)) : null;
JSONTokener tokener = new JSONTokener(jsonReader);
return tokener.more() ? new JSONObject(tokener) : null;
}
}

Expand Down
5 changes: 3 additions & 2 deletions api/src/org/labkey/api/data/validator/LengthValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public String _validate(int rowNum, Object value)
{
if (value instanceof String s)
{
if (s.length() > scale)
return "Value is too long for column '" + _columnName + "', a maximum length of " + scale + " is allowed. The supplied value, '" + StringUtils.abbreviateMiddle(s, "...", 50) + "', was " + s.length() + " characters long.";
int charLength = Character.codePointCount(s, 0, s.length());
if (charLength > scale)
return "Value is too long for column '" + _columnName + "', a maximum length of " + scale + " is allowed. The supplied value, '" + StringUtils.abbreviateMiddle(s, "...", 50) + "', was " + charLength + " characters long.";
}

return null;
Expand Down