Skip to content
Open
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
6 changes: 6 additions & 0 deletions docs/changelog/158437.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
area: SQL
issues:
- 157664
pr: 158437
summary: Wrap runtime errors when decoding a SQL cursor
type: bug
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static BasicFormatter decodeFormatter(String base64) {
}
try (SqlStreamInput in = SqlStreamInput.fromString(base64, WRITEABLE_REGISTRY, VERSION)) {
return in.readOptionalWriteable(BasicFormatter::new);
} catch (IOException ex) {
} catch (IOException | RuntimeException ex) {
throw new SqlIllegalArgumentException("Unexpected failure reading cursor", ex);
}
}
Expand Down Expand Up @@ -151,7 +151,14 @@ private static Tuple<Cursor, ZoneId> internalDecodeFromStringWithZone(String bas
} else {
return internalDecodeFromStringWithZone(in.readString(), writeableRegistry);
}
} catch (IOException ex) {
} catch (SqlIllegalArgumentException ex) {
// already the right type and message, whether it came from the nested call above or
// from inside the stream itself, so it must not be wrapped in a second one
throw ex;
} catch (IOException | RuntimeException ex) {
// the cursor is whatever was in the request, so a broken one fails in ways that are
// not IOException: bad base64, a negative or oversized array length, an unknown
// writeable name. they all mean the same thing to the caller
throw new SqlIllegalArgumentException("Unexpected failure reading cursor", ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.proto.ColumnInfo;
import org.elasticsearch.xpack.sql.proto.StringUtils;
import org.elasticsearch.xpack.sql.proto.formatter.SimpleFormatter;
Expand Down Expand Up @@ -44,6 +45,14 @@ public static Cursor decodeFromString(String base64) {
return decodeFromStringWithZone(base64, WRITEABLE_REGISTRY).v1();
}

public void testDecodingGarbageCursorFails() {
// the cursor comes straight from the request, so anything can be in it
for (String garbage : List.of("not base64 at all!", "///", "AAAA", "AAAAAAAAAAAAAAAAAAAA")) {
SqlIllegalArgumentException e = expectThrows(SqlIllegalArgumentException.class, () -> decodeFromString(garbage));
assertEquals("Unexpected failure reading cursor", e.getMessage());
}
}

public void testAttachingFormatterToCursor() {
Cursor cursor = randomSearchHitCursor();
ZoneId zone = randomZone();
Expand Down