Override `bytes_to_string` by srikrsna-buf · Pull Request #304 · bufbuild/protovalidate-java · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions conformance/expected-failures.yaml
23 changes: 23 additions & 0 deletions src/main/java/build/buf/protovalidate/CustomOverload.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import dev.cel.common.CelErrorCode;
import dev.cel.common.CelRuntimeException;
import dev.cel.common.types.CelType;
import dev.cel.common.types.SimpleType;
import dev.cel.runtime.CelEvaluationException;
Expand Down Expand Up @@ -47,6 +49,7 @@ static List<CelFunctionBinding> create() {
ArrayList<CelFunctionBinding> bindings = new ArrayList<>();
bindings.addAll(
Arrays.asList(
celBytesToString(),
celGetField(),
celFormat(),
celStartsWithBytes(),
Expand All @@ -70,6 +73,26 @@ static List<CelFunctionBinding> create() {
return Collections.unmodifiableList(bindings);
}

/**
* This implementes that standard {@code bytes_to_string} function. We override it because the CEL
* library doesn't validate that the bytes are valid utf-8.
*
* <p>Workround until https://github.com/google/cel-java/pull/717 lands.
*/
private static CelFunctionBinding celBytesToString() {
return CelFunctionBinding.from(
"bytes_to_string",
ByteString.class,
v -> {
if (!v.isValidUtf8()) {
throw new CelRuntimeException(
new IllegalArgumentException("invalid UTF-8 in bytes, cannot convert to string"),
CelErrorCode.BAD_FORMAT);
}
return v.toStringUtf8();
});
}

/**
* Creates a custom function overload for the "getField" operation.
*
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/build/buf/protovalidate/ValidateLibrary.java