From 7fc7687e747abda9c5a0e71faf90b9c75234f721 Mon Sep 17 00:00:00 2001 From: Vincent Paturet Date: Thu, 21 Mar 2024 07:55:22 +0100 Subject: [PATCH] Improve handling of invalid leg reference --- .../legreference/LegReferenceSerializer.java | 10 ++++++++-- .../legreference/LegReferenceSerializerTest.java | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) 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..fb69ca71f73 100644 --- a/src/main/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializer.java +++ b/src/main/java/org/opentripplanner/model/plan/legreference/LegReferenceSerializer.java @@ -51,7 +51,13 @@ public static LegReference decode(String legReference) { return null; } - var buf = Base64.getUrlDecoder().decode(legReference); + byte[] buf; + try { + buf = Base64.getUrlDecoder().decode(legReference); + } catch (IllegalArgumentException e) { + LOG.warn("Unable to decode leg reference (invalid base64 encoding): '{}'", legReference, e); + return null; + } var input = new ByteArrayInputStream(buf); try (var in = new ObjectInputStream(input)) { @@ -60,7 +66,7 @@ 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.warn("Unable to decode leg reference: '{}'", 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")); + } }