diff --git a/src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsService.kt b/src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsService.kt index e6a6d474304..0a828191840 100644 --- a/src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsService.kt +++ b/src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsService.kt @@ -14,6 +14,7 @@ package org.wfanet.measurement.kingdom.service.api.v2alpha +import com.google.protobuf.Descriptors.DescriptorValidationException import io.grpc.Status import io.grpc.StatusException import kotlin.math.min @@ -41,6 +42,7 @@ import org.wfanet.measurement.api.v2alpha.eventGroupMetadataDescriptor import org.wfanet.measurement.api.v2alpha.listEventGroupMetadataDescriptorsPageToken import org.wfanet.measurement.api.v2alpha.listEventGroupMetadataDescriptorsResponse import org.wfanet.measurement.api.v2alpha.principalFromCurrentContext +import org.wfanet.measurement.common.ProtoReflection import org.wfanet.measurement.common.api.ResourceKey import org.wfanet.measurement.common.base64UrlDecode import org.wfanet.measurement.common.base64UrlEncode @@ -135,6 +137,15 @@ class EventGroupMetadataDescriptorsService( } } + // Check if the FileDescriptorSet is valid by trying to build a list of Descriptors from it. + try { + ProtoReflection.buildDescriptors(listOf(request.eventGroupMetadataDescriptor.descriptorSet)) + } catch (e: DescriptorValidationException) { + throw Status.INVALID_ARGUMENT.withCause(e) + .withDescription("descriptor_set is invalid") + .asRuntimeException() + } + val createRequest = request.eventGroupMetadataDescriptor.toInternal( parentKey.dataProviderId, diff --git a/src/test/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsServiceTest.kt b/src/test/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsServiceTest.kt index 6a4dcb76af3..937df140dda 100644 --- a/src/test/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsServiceTest.kt +++ b/src/test/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha/EventGroupMetadataDescriptorsServiceTest.kt @@ -17,6 +17,7 @@ package org.wfanet.measurement.kingdom.service.api.v2alpha import com.google.common.truth.Truth.assertThat import com.google.common.truth.extensions.proto.ProtoTruth.assertThat import com.google.protobuf.DescriptorProtos.FileDescriptorSet +import com.google.protobuf.TextFormat import io.grpc.Status import io.grpc.StatusRuntimeException import kotlin.test.assertFailsWith @@ -234,6 +235,47 @@ class EventGroupMetadataDescriptorsServiceTest { assertThat(exception.status.code).isEqualTo(Status.Code.PERMISSION_DENIED) } + @Test + fun `createEventGroupMetadataDescriptor throws INVALID_ARGUMENT when descriptorSet invalid`() { + val badFileDescriptorSetText = + """ + file { + name: "" + package: "" + message_type { + name: "" + field { + name: "test" + number: 1 + label: LABEL_OPTIONAL + type: TYPE_STRING + } + } + } + """ + .trimIndent() + + val badFileDescriptorSetBuilder = FileDescriptorSet.newBuilder() + TextFormat.getParser().merge(badFileDescriptorSetText, badFileDescriptorSetBuilder) + + val request = createEventGroupMetadataDescriptorRequest { + parent = DATA_PROVIDER_NAME + eventGroupMetadataDescriptor = + EVENT_GROUP_METADATA_DESCRIPTOR.copy { + clearDescriptorSet() + descriptorSet = badFileDescriptorSetBuilder.build() + } + } + + val exception = + assertFailsWith { + withDataProviderPrincipal(DATA_PROVIDER_NAME) { + runBlocking { service.createEventGroupMetadataDescriptor(request) } + } + } + assertThat(exception.status.code).isEqualTo(Status.Code.INVALID_ARGUMENT) + } + @Test fun `createEventGroupMetadataDescriptor throws INVALID_ARGUMENT when parent is missing`() { val exception =