diff --git a/src/main/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializer.java b/src/main/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializer.java index 4f090a440b6..c5c38fd2506 100644 --- a/src/main/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializer.java +++ b/src/main/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializer.java @@ -51,8 +51,14 @@ public static LegReference decode(String legReference) { return null; } - var buf = Base64.getUrlDecoder().decode(legReference); - var input = new ByteArrayInputStream(buf); + byte[] serializedLegReference; + try { + serializedLegReference = Base64.getUrlDecoder().decode(legReference); + } catch (IllegalArgumentException e) { + LOG.info("Unable to decode leg reference (invalid base64 encoding): '{}'", legReference, e); + return null; + } + var input = new ByteArrayInputStream(serializedLegReference); try (var in = new ObjectInputStream(input)) { // The order must be the same in the encode and decode function @@ -60,7 +66,11 @@ public static LegReference decode(String legReference) { var type = readEnum(in, LegReferenceType.class); return type.getDeserializer().read(in); } catch (IOException e) { - LOG.error("Unable to decode leg reference: '" + legReference + "'", e); + LOG.info( + "Unable to decode leg reference (incompatible serialization format): '{}'", + legReference, + e + ); return null; } } diff --git a/src/test/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializerTest.java b/src/test/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializerTest.java index da295073e01..2d7df788ad3 100644 --- a/src/test/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializerTest.java +++ b/src/test/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializerTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import java.time.LocalDate; import org.junit.jupiter.api.Test; @@ -86,4 +87,19 @@ void testScheduledTransitLegReferenceLegacyV2Deserialize() { assertEquals(FROM_STOP_POS, ref.fromStopPositionInPattern()); assertEquals(TO_STOP_POS, ref.toStopPositionInPattern()); } + + @Test + void testNullSerializedLegReference() { + assertNull(LegReferenceSerializer.decode(null)); + } + + @Test + void testEmptySerializedLegReference() { + assertNull(LegReferenceSerializer.decode("")); + } + + @Test + void testIllegalBase64CharacterInSerializedLegReference() { + assertNull(LegReferenceSerializer.decode("RUT:Line:5")); + } }