|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 The Authors of CEL-Java |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.projectnessie.cel; |
| 17 | + |
| 18 | +import com.google.api.expr.v1alpha1.Expr; |
| 19 | +import java.util.ArrayDeque; |
| 20 | +import org.projectnessie.cel.OperationAbortedException.Phase; |
| 21 | +import org.projectnessie.cel.OperationAbortedException.Reason; |
| 22 | +import org.projectnessie.cel.OperationAbortedException.Resource; |
| 23 | +import org.projectnessie.cel.internal.OperationController; |
| 24 | + |
| 25 | +/** Iterative structural admission for controlled AST operations. */ |
| 26 | +final class AstAdmission { |
| 27 | + private AstAdmission() {} |
| 28 | + |
| 29 | + static void check(Ast ast, OperationController controller, Phase phase) { |
| 30 | + var limits = controller.limits(); |
| 31 | + if (limits.astNodes() >= 0 || limits.astDepth() >= 0) { |
| 32 | + checkTree(ast.getExpr(), controller, phase, limits.astNodes(), limits.astDepth()); |
| 33 | + } |
| 34 | + if (limits.astMetadataEntries() >= 0) { |
| 35 | + long entries = 0; |
| 36 | + var info = ast.getSourceInfo(); |
| 37 | + if (info != null) { |
| 38 | + entries = add(entries, info.getPositionsCount(), limits.astMetadataEntries(), phase); |
| 39 | + entries = add(entries, info.getLineOffsetsCount(), limits.astMetadataEntries(), phase); |
| 40 | + entries = add(entries, info.getMacroCallsCount(), limits.astMetadataEntries(), phase); |
| 41 | + for (Expr macro : info.getMacroCallsMap().values()) { |
| 42 | + entries = |
| 43 | + checkMetadataTree(macro, controller, phase, entries, limits.astMetadataEntries()); |
| 44 | + } |
| 45 | + } |
| 46 | + entries = add(entries, ast.refMap.size(), limits.astMetadataEntries(), phase); |
| 47 | + add(entries, ast.typeMap.size(), limits.astMetadataEntries(), phase); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static long checkTree( |
| 52 | + Expr root, OperationController controller, Phase phase, long nodeLimit, int depthLimit) { |
| 53 | + var pending = new ArrayDeque<NodeDepth>(); |
| 54 | + pending.push(new NodeDepth(root, 1)); |
| 55 | + long count = 0; |
| 56 | + while (!pending.isEmpty()) { |
| 57 | + controller.checkpoint(phase); |
| 58 | + var current = pending.pop(); |
| 59 | + count++; |
| 60 | + if (nodeLimit >= 0 && count > nodeLimit) { |
| 61 | + throw limit(Reason.AST_NODE_LIMIT, phase, Resource.AST_NODES, nodeLimit, count); |
| 62 | + } |
| 63 | + if (depthLimit >= 0 && current.depth > depthLimit) { |
| 64 | + throw limit(Reason.AST_DEPTH_LIMIT, phase, Resource.AST_DEPTH, depthLimit, current.depth); |
| 65 | + } |
| 66 | + pushChildren(current.expr, current.depth + 1, pending); |
| 67 | + } |
| 68 | + return count; |
| 69 | + } |
| 70 | + |
| 71 | + private static long checkMetadataTree( |
| 72 | + Expr root, |
| 73 | + OperationController controller, |
| 74 | + Phase phase, |
| 75 | + long initialCount, |
| 76 | + long metadataLimit) { |
| 77 | + var pending = new ArrayDeque<Expr>(); |
| 78 | + pending.push(root); |
| 79 | + long count = initialCount; |
| 80 | + while (!pending.isEmpty()) { |
| 81 | + controller.checkpoint(phase); |
| 82 | + var current = pending.pop(); |
| 83 | + count = add(count, 1, metadataLimit, phase); |
| 84 | + pushMetadataChildren(current, pending); |
| 85 | + } |
| 86 | + return count; |
| 87 | + } |
| 88 | + |
| 89 | + private static void pushChildren(Expr expr, int depth, ArrayDeque<NodeDepth> pending) { |
| 90 | + switch (expr.getExprKindCase()) { |
| 91 | + case SELECT_EXPR -> pending.push(new NodeDepth(expr.getSelectExpr().getOperand(), depth)); |
| 92 | + case CALL_EXPR -> { |
| 93 | + var call = expr.getCallExpr(); |
| 94 | + for (Expr arg : call.getArgsList()) { |
| 95 | + pending.push(new NodeDepth(arg, depth)); |
| 96 | + } |
| 97 | + if (call.hasTarget()) { |
| 98 | + pending.push(new NodeDepth(call.getTarget(), depth)); |
| 99 | + } |
| 100 | + } |
| 101 | + case LIST_EXPR -> { |
| 102 | + for (Expr element : expr.getListExpr().getElementsList()) { |
| 103 | + pending.push(new NodeDepth(element, depth)); |
| 104 | + } |
| 105 | + } |
| 106 | + case STRUCT_EXPR -> { |
| 107 | + for (var entry : expr.getStructExpr().getEntriesList()) { |
| 108 | + pending.push(new NodeDepth(entry.getValue(), depth)); |
| 109 | + if (entry.hasMapKey()) { |
| 110 | + pending.push(new NodeDepth(entry.getMapKey(), depth)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + case COMPREHENSION_EXPR -> { |
| 115 | + var comprehension = expr.getComprehensionExpr(); |
| 116 | + pending.push(new NodeDepth(comprehension.getIterRange(), depth)); |
| 117 | + pending.push(new NodeDepth(comprehension.getAccuInit(), depth)); |
| 118 | + pending.push(new NodeDepth(comprehension.getLoopCondition(), depth)); |
| 119 | + pending.push(new NodeDepth(comprehension.getLoopStep(), depth)); |
| 120 | + pending.push(new NodeDepth(comprehension.getResult(), depth)); |
| 121 | + } |
| 122 | + default -> { |
| 123 | + // Scalar expression. |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static void pushMetadataChildren(Expr expr, ArrayDeque<Expr> pending) { |
| 129 | + switch (expr.getExprKindCase()) { |
| 130 | + case SELECT_EXPR -> pending.push(expr.getSelectExpr().getOperand()); |
| 131 | + case CALL_EXPR -> { |
| 132 | + var call = expr.getCallExpr(); |
| 133 | + pending.addAll(call.getArgsList()); |
| 134 | + if (call.hasTarget()) { |
| 135 | + pending.push(call.getTarget()); |
| 136 | + } |
| 137 | + } |
| 138 | + case LIST_EXPR -> pending.addAll(expr.getListExpr().getElementsList()); |
| 139 | + case STRUCT_EXPR -> { |
| 140 | + for (var entry : expr.getStructExpr().getEntriesList()) { |
| 141 | + pending.push(entry.getValue()); |
| 142 | + if (entry.hasMapKey()) { |
| 143 | + pending.push(entry.getMapKey()); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + case COMPREHENSION_EXPR -> { |
| 148 | + var comprehension = expr.getComprehensionExpr(); |
| 149 | + pending.push(comprehension.getIterRange()); |
| 150 | + pending.push(comprehension.getAccuInit()); |
| 151 | + pending.push(comprehension.getLoopCondition()); |
| 152 | + pending.push(comprehension.getLoopStep()); |
| 153 | + pending.push(comprehension.getResult()); |
| 154 | + } |
| 155 | + default -> { |
| 156 | + // Scalar expression. |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private static long add(long value, long increment, long limit, Phase phase) { |
| 162 | + var observed = value + increment; |
| 163 | + if (observed < value) { |
| 164 | + observed = Long.MAX_VALUE; |
| 165 | + } |
| 166 | + if (observed > limit) { |
| 167 | + throw limit(Reason.AST_METADATA_LIMIT, phase, Resource.AST_METADATA_ENTRIES, limit, observed); |
| 168 | + } |
| 169 | + return observed; |
| 170 | + } |
| 171 | + |
| 172 | + private static OperationAbortedException limit( |
| 173 | + Reason reason, Phase phase, Resource resource, long limit, long observed) { |
| 174 | + return new OperationAbortedException(reason, phase, resource, limit, observed); |
| 175 | + } |
| 176 | + |
| 177 | + private record NodeDepth(Expr expr, int depth) {} |
| 178 | +} |
0 commit comments