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
31 changes: 16 additions & 15 deletions verifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ properties about your expressions.
runtime partial evaluation.

```java
CelVerifier verifier = CelVerifierFactory.newVerifier()
CelVerifier verifier = CelVerifierFactory.newVerifier(cel)
.addUnknownIdentifier("request.headers") // Exclude unknown fields from failure paths
.build();
```
Expand Down Expand Up @@ -85,10 +85,10 @@ The following example demonstrates how to verify if two CEL expressions
are logically equivalent.

```java
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelFactory;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.verifier.CelVerificationException;
import dev.cel.verifier.CelVerificationResult;
import dev.cel.verifier.CelVerificationResult.VerificationStatus;
Expand All @@ -98,16 +98,16 @@ import java.time.Duration;

public class VerifierExample {
public static void main(String[] args) throws Exception {
CelCompiler compiler = CelCompilerFactory.standardCelCompilerBuilder()
Cel cel = CelFactory.plannerCelBuilder()
.addVar("x", SimpleType.INT)
.build();

// Compile two logically equivalent expressions
CelAbstractSyntaxTree astA = compiler.compile("x > 10").getAst();
CelAbstractSyntaxTree astB = compiler.compile("10 < x").getAst();
CelAbstractSyntaxTree astA = cel.compile("x > 10").getAst();
CelAbstractSyntaxTree astB = cel.compile("10 < x").getAst();

// Create and configure the verifier
CelVerifier verifier = CelVerifierFactory.newVerifier()
CelVerifier verifier = CelVerifierFactory.newVerifier(cel)
.setTimeout(Duration.ofSeconds(2))
.build();

Expand Down Expand Up @@ -142,7 +142,8 @@ public class VerifierExample {

### 2. Policy Equivalence Verification

You can also verify the equivalence of two structured CEL Policies.
You can also verify the equivalence of two structured CEL Policies. Build the
policy compiler and the AST verifier from the same `Cel` environment.

```java
import dev.cel.bundle.Cel;
Expand Down Expand Up @@ -176,7 +177,7 @@ public class PolicyVerifierExample {
CelPolicyCompilerFactory.newPolicyCompiler(CEL).build();

// Verifiers are immutable and thread-safe, making them safe to store as static constants
private static final CelVerifier AST_VERIFIER = CelVerifierFactory.newVerifier().build();
private static final CelVerifier AST_VERIFIER = CelVerifierFactory.newVerifier(CEL).build();

private static final CelPolicyVerifier POLICY_VERIFIER =
CelPolicyVerifierFactory.newVerifier(POLICY_COMPILER, AST_VERIFIER).build();
Expand Down Expand Up @@ -245,27 +246,27 @@ provides concrete satisfying model assignments (witness/test case generation),
and `result.witness()` exposes them as CEL runtime values.

```java
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelFactory;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.verifier.CelVerificationResult;
import dev.cel.verifier.CelVerificationResult.VerificationStatus;
import dev.cel.verifier.CelVerifier;
import dev.cel.verifier.CelVerifierFactory;

public class SatisfiabilityExample {
public static void main(String[] args) throws Exception {
CelCompiler compiler = CelCompilerFactory.standardCelCompilerBuilder()
Cel cel = CelFactory.plannerCelBuilder()
.addVar("role", SimpleType.STRING)
.addVar("port", SimpleType.INT)
.build();

// Check if an authorization condition can ever be met
CelAbstractSyntaxTree ast =
compiler.compile("role == 'editor' && port > 1024 && port < 65535").getAst();
cel.compile("role == 'editor' && port > 1024 && port < 65535").getAst();

CelVerifier verifier = CelVerifierFactory.newVerifier().build();
CelVerifier verifier = CelVerifierFactory.newVerifier(cel).build();
CelVerificationResult result = verifier.isSatisfiable(ast);

switch (result.status()) {
Expand Down Expand Up @@ -322,7 +323,7 @@ public class InvariantsExample {
private static final CelPolicyCompiler POLICY_COMPILER =
CelPolicyCompilerFactory.newPolicyCompiler(CEL).build();

private static final CelVerifier AST_VERIFIER = CelVerifierFactory.newVerifier().build();
private static final CelVerifier AST_VERIFIER = CelVerifierFactory.newVerifier(CEL).build();

private static final CelPolicyVerifier POLICY_VERIFIER =
CelPolicyVerifierFactory.newVerifier(POLICY_COMPILER, AST_VERIFIER).build();
Expand Down
4 changes: 0 additions & 4 deletions verifier/src/main/java/dev/cel/verifier/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ java_library(
deps = [
"//:auto_value",
"//common:cel_ast",
"//common/types:type_providers",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand Down Expand Up @@ -190,10 +189,8 @@ java_library(
"//common/ast",
"//common/ast:cel_block",
"//common/exceptions:runtime_exception",
"//common/internal:cel_descriptor_pools",
"//common/internal:map_keys",
"//common/internal:proto_time_utils",
"//common/internal:well_known_proto",
"//common/types",
"//common/types:cel_types",
"//common/types:type_providers",
Expand All @@ -209,7 +206,6 @@ java_library(
"//verifier/axioms",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:org_jspecify_jspecify",
"@maven//:tools_aqua_z3_turnkey",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
/** Factory class for producing policy verifiers. */
public final class CelPolicyVerifierFactory {

/** Create a new {@link CelPolicyVerifierBuilder} instance. */
/**
* Create a new {@link CelPolicyVerifierBuilder} instance. {@code astVerifier} must be built from
* the same {@code Cel} environment as {@code compiler}.
*/
public static CelPolicyVerifierBuilder newVerifier(
CelPolicyCompiler compiler, CelVerifier astVerifier) {
return CelPolicyVerifierImpl.newBuilder(compiler, astVerifier);
Expand Down
11 changes: 0 additions & 11 deletions verifier/src/main/java/dev/cel/verifier/CelVerifierBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package dev.cel.verifier;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import dev.cel.common.types.CelTypeProvider;
import java.time.Duration;

/** Interface for building an instance of CelVerifier. */
Expand All @@ -41,16 +40,6 @@ public interface CelVerifierBuilder {
@CanIgnoreReturnValue
CelVerifierBuilder addUnknownIdentifier(String identifier);

/**
* Sets the type provider for looking up {@code CelType} definitions by name.
*
* <p>The verifier uses this to resolve structural type definitions (such as protocol buffer
* messages) during verification. If not set, the verifier will fall back to type information
* present in the AST, which may lose details such as wrapper types.
*/
@CanIgnoreReturnValue
CelVerifierBuilder setTypeProvider(CelTypeProvider typeProvider);

/**
* Sets the unroll limit for comprehensions (such as {@code map}, {@code filter}, {@code all},
* {@code exists}).
Expand Down
12 changes: 2 additions & 10 deletions verifier/src/main/java/dev/cel/verifier/CelVerifierFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@
public final class CelVerifierFactory {

/**
* Create a builder for configuring a {@link CelVerifier}.
*
* @deprecated Prefer passing a {@link Cel} environment using {@link #newVerifier(Cel)} to enable
* canonicalization and expression re-typechecking during verification.
* Create a builder for configuring a {@link CelVerifier} with a CEL environment. The ASTs to
* verify must be compiled in {@code cel}.
*/
@Deprecated
public static CelVerifierBuilder newVerifier() {
return CelVerifierZ3Impl.newBuilder();
}

/** Create a builder for configuring a {@link CelVerifier} with a CEL environment. */
public static CelVerifierBuilder newVerifier(Cel cel) {
return CelVerifierZ3Impl.newBuilder(cel);
}
Expand Down
62 changes: 1 addition & 61 deletions verifier/src/main/java/dev/cel/verifier/CelVerifierZ3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,21 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.Descriptors.Descriptor;
import com.microsoft.z3.BoolExpr;
import com.microsoft.z3.Context;
import com.microsoft.z3.Model;
import com.microsoft.z3.Params;
import com.microsoft.z3.Solver;
import com.microsoft.z3.Status;
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelFactory;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.internal.DefaultDescriptorPool;
import dev.cel.common.internal.WellKnownProto;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.optimizer.CelOptimizationException;
import dev.cel.optimizer.CelOptimizer;
import dev.cel.optimizer.CelOptimizerFactory;
Expand All @@ -56,55 +48,15 @@
@Immutable
final class CelVerifierZ3Impl implements CelVerifier {

@VisibleForTesting
static final CelTypeProvider EMPTY_TYPE_PROVIDER =
new CelTypeProvider() {
@Override
public ImmutableList<CelType> types() {
return ImmutableList.of();
}

@Override
public Optional<CelType> findType(String typeName) {
return Optional.empty();
}
};

private static final ImmutableList<Descriptor> WELL_KNOWN_DESCRIPTORS =
ImmutableList.of(
WellKnownProto.DURATION,
WellKnownProto.JSON_LIST_VALUE,
WellKnownProto.JSON_STRUCT_VALUE,
WellKnownProto.JSON_VALUE,
WellKnownProto.TIMESTAMP,
WellKnownProto.FLOAT_VALUE,
WellKnownProto.INT32_VALUE,
WellKnownProto.INT64_VALUE,
WellKnownProto.STRING_VALUE,
WellKnownProto.BOOL_VALUE,
WellKnownProto.BYTES_VALUE,
WellKnownProto.DOUBLE_VALUE,
WellKnownProto.UINT32_VALUE,
WellKnownProto.UINT64_VALUE)
.stream()
.map(DefaultDescriptorPool::getWellKnownProtoDescriptor)
.collect(toImmutableList());

private final Duration timeout;
private final int comprehensionUnrollLimit;
private final ImmutableSet<String> unknownIdentifiers;
private final CelZ3FunctionRegistry functionRegistry;
private final CelTypeProvider typeProvider;
private final boolean enableCegar;

@SuppressWarnings("Immutable") // Cel environment is immutable, just not marked as such
private final Cel cel;

static Builder newBuilder() {
return new Builder(
CelFactory.plannerCelBuilder().addMessageTypes(WELL_KNOWN_DESCRIPTORS).build());
}

static Builder newBuilder(Cel cel) {
return new Builder(checkNotNull(cel));
}
Expand All @@ -115,7 +67,6 @@ static final class Builder implements CelVerifierBuilder {
private final ImmutableSet.Builder<String> unknownIdentifiers;
private final ImmutableList.Builder<CelZ3FunctionAxiom> functionAxioms;
private final Cel cel;
private CelTypeProvider typeProvider;
private boolean enableCegar;

@Override
Expand All @@ -135,13 +86,6 @@ public CelVerifierBuilder addUnknownIdentifier(String identifier) {
return this;
}

@Override
@CanIgnoreReturnValue
public CelVerifierBuilder setTypeProvider(CelTypeProvider typeProvider) {
this.typeProvider = checkNotNull(typeProvider);
return this;
}

@Override
@CanIgnoreReturnValue
public CelVerifierBuilder setComprehensionUnrollLimit(int unrollLimit) {
Expand Down Expand Up @@ -182,7 +126,6 @@ public CelVerifier build() {
comprehensionUnrollLimit,
unknownIdentifiers.build(),
registry,
typeProvider,
enableCegar,
cel);
}
Expand All @@ -192,7 +135,6 @@ private Builder(Cel cel) {
this.comprehensionUnrollLimit = 5;
this.unknownIdentifiers = ImmutableSet.builder();
this.functionAxioms = ImmutableList.builder();
this.typeProvider = cel.getTypeProvider();
this.cel = cel;
}
}
Expand Down Expand Up @@ -538,7 +480,7 @@ private CelAstToZ3Translator newTranslator(Context ctx, boolean parameterizeUnkn
comprehensionUnrollLimit,
unknownIdentifiers,
functionRegistry,
typeProvider,
cel.getTypeProvider(),
parameterizeUnknowns);
}

Expand All @@ -565,14 +507,12 @@ private CelVerifierZ3Impl(
int comprehensionUnrollLimit,
ImmutableSet<String> unknownIdentifiers,
CelZ3FunctionRegistry functionRegistry,
CelTypeProvider typeProvider,
boolean enableCegar,
Cel cel) {
this.timeout = timeout;
this.comprehensionUnrollLimit = comprehensionUnrollLimit;
this.unknownIdentifiers = unknownIdentifiers;
this.functionRegistry = functionRegistry;
this.typeProvider = typeProvider;
this.enableCegar = enableCegar;
this.cel = cel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ public void verifyInvariants_secureResourceAccess_verified() throws Exception {
.build();
CelPolicyVerifier verifier =
CelPolicyVerifierFactory.newVerifier(
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(), AST_VERIFIER)
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(),
CelVerifierFactory.newVerifier(extendedCel).build())
.build();

CelPolicy policy =
Expand Down Expand Up @@ -448,7 +449,8 @@ public void verifyInvariants_restrictedDestinationsPolicy_verified() throws Exce
.build();
CelPolicyVerifier verifier =
CelPolicyVerifierFactory.newVerifier(
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(), AST_VERIFIER)
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(),
CelVerifierFactory.newVerifier(extendedCel).build())
.build();

CelPolicy policy =
Expand Down Expand Up @@ -667,7 +669,8 @@ public void verifyInvariants_workloadAdmissionFlawed_violationsDetected() throws
.build();
CelPolicyVerifier verifier =
CelPolicyVerifierFactory.newVerifier(
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(), AST_VERIFIER)
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(),
CelVerifierFactory.newVerifier(extendedCel).build())
.build();

CelPolicy policy =
Expand Down Expand Up @@ -703,7 +706,8 @@ public void verifyInvariants_workloadAdmissionFixed_verified() throws Exception
.build();
CelPolicyVerifier verifier =
CelPolicyVerifierFactory.newVerifier(
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(), AST_VERIFIER)
CelPolicyCompilerFactory.newPolicyCompiler(extendedCel).build(),
CelVerifierFactory.newVerifier(extendedCel).build())
.build();

CelPolicy policy =
Expand Down
Loading
Loading