Fail if an unknown field is found in the stored replication task The Typeadapter that reads the stored replication task could end up in an endless loop, if an unknown field is present in the stored json string. Bug: Issue 15922 Change-Id: Ib518a8ac10eb178ea2dd50767377f57080fc8411
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java index c7765a3..ce7fe7b 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorage.java
@@ -283,26 +283,22 @@ if ("project".equals(fieldname)) { project = in.nextString(); - } - - if ("refs".equals(fieldname)) { + } else if ("refs".equals(fieldname)) { in.beginArray(); while (in.hasNext()) { refs.add(in.nextString()); } in.endArray(); - } - - if ("uri".equals(fieldname)) { + } else if ("uri".equals(fieldname)) { try { uri = new URIish(in.nextString()); } catch (URISyntaxException e) { throw new IOException("Unable to parse remote URI", e); } - } - - if ("remote".equals(fieldname)) { + } else if ("remote".equals(fieldname)) { remote = in.nextString(); + } else { + throw new IOException(String.format("Unknown field in stored task: %s", fieldname)); } }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTaskTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTaskTest.java index 7c3b847..3b7aec7 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTaskTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTaskTest.java
@@ -14,6 +14,7 @@ package com.googlesource.gerrit.plugins.replication; +import static com.google.gerrit.testing.GerritJUnit.assertThrows; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -23,6 +24,7 @@ import com.google.common.jimfs.Jimfs; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; import com.googlesource.gerrit.plugins.replication.ReplicationTasksStorage.ReplicateRefUpdate; import com.googlesource.gerrit.plugins.replication.ReplicationTasksStorage.ReplicateRefUpdateTypeAdapterFactory; import com.googlesource.gerrit.plugins.replication.ReplicationTasksStorage.Task; @@ -415,6 +417,17 @@ update2); } + @Test + public void ReplicateRefUpdateTypeAdapter_FailWithUnknownField() throws Exception { + Gson gson = + new GsonBuilder() + .registerTypeAdapterFactory(new ReplicateRefUpdateTypeAdapterFactory()) + .create(); + assertThrows( + JsonSyntaxException.class, + () -> gson.fromJson("{\"unknownKey\":\"someValue\"}", ReplicateRefUpdate.class)); + } + protected static void assertIsWaiting(Task task) { assertTrue(task.isWaiting()); }