Skip to content

Commit

Permalink
Consider the protobuf.Any invalid if typeUrl.split("/") returns an em…
Browse files Browse the repository at this point in the history
…pty array.

Currently this corner case (discovered by fuzzing) is not considered. The code throws `ArrayIndexOutOfBoundsException` which can escape `protobuf.toString()` method.

PiperOrigin-RevId: 550514062
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 24, 2023
1 parent 4f6fc33 commit 004f54a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public final Descriptor getDescriptorForTypeUrl(String typeUrl)

private static String getTypeName(String typeUrl) throws InvalidProtocolBufferException {
String[] parts = typeUrl.split("/");
if (parts.length == 1) {
if (parts.length <= 1) {
throw new InvalidProtocolBufferException("Invalid type url found: " + typeUrl);
}
return parts[parts.length - 1];
Expand Down
11 changes: 11 additions & 0 deletions java/core/src/test/java/com/google/protobuf/TypeRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
package com.google.protobuf;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.protobuf.Descriptors.Descriptor;
import protobuf_unittest.UnittestProto;
Expand All @@ -41,6 +42,16 @@
@RunWith(JUnit4.class)
public final class TypeRegistryTest {

@Test
public void getDescriptorForTypeUrl_throwsExceptionForUnknownTypes() throws Exception {
assertThrows(
InvalidProtocolBufferException.class,
() -> TypeRegistry.getEmptyTypeRegistry().getDescriptorForTypeUrl("UnknownType"));
assertThrows(
InvalidProtocolBufferException.class,
() -> TypeRegistry.getEmptyTypeRegistry().getDescriptorForTypeUrl("///"));
}

@Test
public void findDescriptorByFullName() throws Exception {
Descriptor descriptor = UnittestProto.TestAllTypes.getDescriptor();
Expand Down

0 comments on commit 004f54a

Please sign in to comment.