Copilot commented on code in PR #2668:
URL: https://github.com/apache/groovy/pull/2668#discussion_r3642189047


##########
src/main/java/groovy/transform/SafeRegex.java:
##########
@@ -0,0 +1,80 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.transform;
+
+import org.apache.groovy.lang.annotation.Incubating;
+import org.codehaus.groovy.transform.GroovyASTTransformationClass;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Guards the regex operators within the annotated scope against Regular
+ * Expression Denial of Service (ReDoS). Match ({@code ==~}) and find
+ * ({@code =~}) expressions are rewritten at compile time to deadline-guarded
+ * {@link groovy.util.regex.RegexGuard} calls which throw
+ * {@link groovy.util.regex.RegexTimeoutException} if evaluation, e.g. due to
+ * catastrophic backtracking on adversarial input, exceeds the configured
+ * timeout. Matching semantics are otherwise unchanged.
+ * <pre>
+ * &#64;SafeRegex(millis = 200)
+ * class Handler {
+ *     boolean check(String input) {
+ *         input ==~ /(a+)+$/    // rewritten to RegexGuard.matches(/(a+)+$/, 
input, 200)

Review Comment:
   The Javadoc example says `==~` is rewritten to `RegexGuard.matches(pattern, 
input, millis)`, but the AST transform actually rewrites to the operator-order 
entry point `RegexGuard.matchRegex(input, pattern, millis)` to preserve 
left-to-right operand evaluation. The example should match the generated form 
to avoid misleading users.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to