From d8bc71383ce83e7698935fff92da25a614c4bb40 Mon Sep 17 00:00:00 2001 From: Muhammad Askri Date: Thu, 3 Dec 2026 17:53:40 -0800 Subject: [PATCH] Reject disabling enableTimestampEpoch in CelOptions. `CelOptions.current().enableTimestampEpoch(false)` previously disabled `timestamp(int)` (`int64_to_timestamp`) in the type-checker and legacy runtime, and now throws `IllegalArgumentException` in favor of subsetting `CelStandardDeclarations` and `CelStandardFunctions`. `enableTimestampEpoch()` now always returns `true`, while `enableTimestampEpoch(true)` remains a deprecated no-op so existing callers keep working without migration breakage. PiperOrigin-RevId: 1035881472 --- .../src/main/java/dev/cel/checker/Env.java | 8 ----- .../checker/CelStandardDeclarationsTest.java | 9 +++-- .../main/java/dev/cel/common/CelOptions.java | 34 +++++++++++-------- .../java/dev/cel/common/CelOptionsTest.java | 21 ++++++++++++ .../java/dev/cel/runtime/CelRuntimeImpl.java | 6 ---- .../dev/cel/runtime/CelRuntimeLegacyImpl.java | 8 ----- .../src/test/java/dev/cel/runtime/BUILD.bazel | 1 + .../cel/runtime/CelStandardFunctionsTest.java | 10 ++++-- 8 files changed, 57 insertions(+), 40 deletions(-) diff --git a/checker/src/main/java/dev/cel/checker/Env.java b/checker/src/main/java/dev/cel/checker/Env.java index 19acb0957..77591a5bf 100644 --- a/checker/src/main/java/dev/cel/checker/Env.java +++ b/checker/src/main/java/dev/cel/checker/Env.java @@ -233,14 +233,6 @@ private static CelStandardDeclarations newStandardDeclarations(CelOptions celOpt return false; } break; - case TIMESTAMP: - // TODO: Remove this flag guard once the feature has been - // auto-enabled. - if (!celOptions.enableTimestampEpoch() - && overload.equals(Conversions.INT64_TO_TIMESTAMP)) { - return false; - } - break; default: // Heterogeneous numeric comparisons require UnsignedLong at runtime so uint // values do not share Long with int and dispatch to signed int64 overloads. diff --git a/checker/src/test/java/dev/cel/checker/CelStandardDeclarationsTest.java b/checker/src/test/java/dev/cel/checker/CelStandardDeclarationsTest.java index 8c612b8e3..8aabc15af 100644 --- a/checker/src/test/java/dev/cel/checker/CelStandardDeclarationsTest.java +++ b/checker/src/test/java/dev/cel/checker/CelStandardDeclarationsTest.java @@ -23,6 +23,7 @@ import dev.cel.checker.CelStandardDeclarations.StandardFunction; import dev.cel.checker.CelStandardDeclarations.StandardFunction.Overload.Arithmetic; import dev.cel.checker.CelStandardDeclarations.StandardFunction.Overload.Comparison; +import dev.cel.checker.CelStandardDeclarations.StandardFunction.Overload.Conversions; import dev.cel.checker.CelStandardDeclarations.StandardIdentifier; import dev.cel.common.CelOptions; import dev.cel.common.CelValidationException; @@ -374,10 +375,14 @@ public void unsignedLongsDisabled_int64Identity_throws() { } @Test - public void timestampEpochDisabled_int64Identity_throws() { + public void standardEnvironment_excludeInt64ToTimestamp_throws() { CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder() - .setOptions(CelOptions.current().enableTimestampEpoch(false).build()) + .setStandardDeclarations( + CelStandardDeclarations.newBuilder() + .filterFunctions( + (function, overload) -> !overload.equals(Conversions.INT64_TO_TIMESTAMP)) + .build()) .build(); CelValidationException e = diff --git a/common/src/main/java/dev/cel/common/CelOptions.java b/common/src/main/java/dev/cel/common/CelOptions.java index 31e05de2f..68d57fa38 100644 --- a/common/src/main/java/dev/cel/common/CelOptions.java +++ b/common/src/main/java/dev/cel/common/CelOptions.java @@ -79,7 +79,13 @@ public enum ProtoUnsetFieldOptions { public abstract boolean enableHomogeneousLiterals(); - public abstract boolean enableTimestampEpoch(); + /** + * @deprecated Timestamp from Unix epoch seconds ({@code timestamp(int)}) is always enabled. + */ + @Deprecated + public boolean enableTimestampEpoch() { + return true; + } /** * @deprecated Cross-type numeric comparisons are always enabled. @@ -154,7 +160,6 @@ public static Builder newBuilder() { // Type-Checker options .enableCompileTimeOverloadResolution(false) .enableHomogeneousLiterals(false) - .enableTimestampEpoch(false) .enableNamespacedDeclarations(true) .enableJsonFieldNames(false) // Evaluation options @@ -188,7 +193,6 @@ public static Builder current() { .enableUnsignedComparisonAndArithmeticIsUnsigned(true) .enableUnsignedLongs(true) .enableRegexPartialMatch(true) - .enableTimestampEpoch(true) .errorOnDuplicateMapKeys(true) .evaluateCanonicalTypesToNativeValues(true) .errorOnIntWrap(true) @@ -309,20 +313,22 @@ public abstract static class Builder { public abstract Builder enableHomogeneousLiterals(boolean value); /** - * Enable the {@code int64_to_timestamp} overload which creates a timestamp from Unix epoch - * seconds. - * - *

This option is enabled by default in {@link CelOptions#current}. - * - *

TODO: Remove this feature once it has been auto-enabled. - * - * @deprecated This option is now enabled by default. If you are passing {@code true}, simply - * remove this method call. If you are passing {@code false} to disable this feature, subset - * the environment instead using {@code dev.cel.checker.CelStandardDeclarations} and {@code + * @deprecated Timestamp from Unix epoch seconds ({@code timestamp(int)}) is always enabled + * ({@code false} throws {@link IllegalArgumentException}). Remove calls passing {@code + * true}, or subset using {@code dev.cel.checker.CelStandardDeclarations} and {@code * dev.cel.runtime.CelStandardFunctions}. */ @Deprecated - public abstract Builder enableTimestampEpoch(boolean value); + @CanIgnoreReturnValue + public Builder enableTimestampEpoch(boolean value) { + if (!value) { + throw new IllegalArgumentException( + "Misconfigured CelOptions: enableTimestampEpoch cannot be disabled." + + " Subset the environment instead using CelStandardDeclarations and" + + " CelStandardFunctions."); + } + return this; + } /** * @deprecated Cross-type numeric comparisons are always enabled ({@code false} throws {@link diff --git a/common/src/test/java/dev/cel/common/CelOptionsTest.java b/common/src/test/java/dev/cel/common/CelOptionsTest.java index 456e3dd56..02f08d15f 100644 --- a/common/src/test/java/dev/cel/common/CelOptionsTest.java +++ b/common/src/test/java/dev/cel/common/CelOptionsTest.java @@ -63,4 +63,25 @@ public void enableHeterogeneousNumericComparisons_false_throws() { .hasMessageThat() .contains("enableHeterogeneousNumericComparisons cannot be disabled"); } + + @Test + @SuppressWarnings("deprecation") // Testing deprecated compatibility methods + public void enableTimestampEpoch_true_isNoOp() { + CelOptions.Builder builder = CelOptions.newBuilder(); + + assertThat(builder.enableTimestampEpoch(true)).isSameInstanceAs(builder); + assertThat(CelOptions.DEFAULT.enableTimestampEpoch()).isTrue(); + assertThat(CelOptions.LEGACY.enableTimestampEpoch()).isTrue(); + } + + @Test + @SuppressWarnings("deprecation") // Testing deprecated compatibility methods + public void enableTimestampEpoch_false_throws() { + CelOptions.Builder builder = CelOptions.DEFAULT.toBuilder(); + + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> builder.enableTimestampEpoch(false)); + + assertThat(e).hasMessageThat().contains("enableTimestampEpoch cannot be disabled"); + } } diff --git a/runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java b/runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java index 7a9d2506b..a8dc9b073 100644 --- a/runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java +++ b/runtime/src/main/java/dev/cel/runtime/CelRuntimeImpl.java @@ -446,12 +446,6 @@ private static void assertAllowedCelOptions(CelOptions celOptions) { throw new IllegalArgumentException( prefix + "unwrapWellKnownTypesOnFunctionDispatch cannot be disabled."); } - if (!celOptions.enableTimestampEpoch()) { - throw new IllegalArgumentException( - prefix - + "enableTimestampEpoch cannot be disabled. Subset the environment instead using" - + " setStandardFunctions method."); - } if (!celOptions.evaluateCanonicalTypesToNativeValues()) { throw new IllegalArgumentException( prefix + "evaluateCanonicalTypesToNativeValues cannot be disabled."); diff --git a/runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java b/runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java index ea30dd942..027bc2ba4 100644 --- a/runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java +++ b/runtime/src/main/java/dev/cel/runtime/CelRuntimeLegacyImpl.java @@ -46,7 +46,6 @@ import dev.cel.common.values.CelValueConverter; import dev.cel.common.values.CelValueProvider; import dev.cel.runtime.standard.IntFunction.IntOverload; -import dev.cel.runtime.standard.TimestampFunction.TimestampOverload; import java.util.Arrays; import java.util.HashMap; import java.util.Optional; @@ -401,13 +400,6 @@ private ImmutableSet newStandardFunctionBindings( return options.enableUnsignedLongs(); } break; - case TIMESTAMP: - // TODO: Remove this flag guard once the feature has been - // auto-enabled. - if (standardOverload.equals(TimestampOverload.INT64_TO_TIMESTAMP)) { - return options.enableTimestampEpoch(); - } - break; default: if (!options.enableUnsignedLongs()) { return !CelStandardFunctions.isHeterogeneousComparison( diff --git a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel index fb2278015..3fc75c8f0 100644 --- a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel @@ -108,6 +108,7 @@ java_library( "//runtime/standard:not_strictly_false", "//runtime/standard:standard_overload", "//runtime/standard:subtract", + "//runtime/standard:timestamp", "//runtime/standard:type", "//testing:cel_runtime_flavor", "//testing/protos:message_with_enum_cel_java_proto", diff --git a/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java b/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java index a18d6d110..aca3ff8b2 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java @@ -27,6 +27,7 @@ import dev.cel.runtime.standard.AddOperator.AddOverload; import dev.cel.runtime.standard.CelStandardOverload; import dev.cel.runtime.standard.SubtractOperator.SubtractOverload; +import dev.cel.runtime.standard.TimestampFunction.TimestampOverload; import org.junit.Test; import org.junit.runner.RunWith; @@ -222,14 +223,19 @@ public void unsignedLongsDisabled_int64Identity_throws() { } @Test - public void timestampEpochDisabled_int64Identity_throws() { + public void standardEnvironment_excludeInt64ToTimestamp_throws() throws Exception { CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder() .setOptions(CelOptions.current().build()) .build(); CelRuntime celRuntime = CelRuntimeFactory.legacyCelRuntimeBuilder() - .setOptions(CelOptions.current().enableTimestampEpoch(false).build()) + .setStandardEnvironmentEnabled(false) + .setStandardFunctions( + CelStandardFunctions.newBuilder() + .filterFunctions((function, overload) -> + !overload.equals(TimestampOverload.INT64_TO_TIMESTAMP)) + .build()) .build(); CelEvaluationException e =