You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2026/08/28 06:32:26 UTC
(directory-ldap-api) branch master updated: Fixed a potential exponential regexp backtracking Also removed a useless synchronized section
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git
The following commit(s) were added to refs/heads/master by this push:
new 835ce23b7 Fixed a potential exponential regexp backtracking Also removed a useless synchronized section
835ce23b7 is described below
commit 835ce23b7c2130c85dc92418c78e6753cba1af4c
Author: emmanuel lecharny <el...@apache.org>
AuthorDate: Fri Aug 28 08:32:21 2026 +0200
Fixed a potential exponential regexp backtracking
Also removed a useless synchronized section
---
.../FacsimileTelephoneNumberSyntaxChecker.java | 46 +++++++++++++++------
.../TelephoneNumberSyntaxChecker.java | 34 +++++++++++-----
.../FacsimileTelephoneNumberSyntaxCheckerTest.java | 47 ++++++++++++++++++++++
.../syntaxes/TelephoneNumberSyntaxCheckerTest.java | 43 ++++++++++++++++++++
4 files changed, 148 insertions(+), 22 deletions(-)
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/FacsimileTelephoneNumberSyntaxChecker.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/FacsimileTelephoneNumberSyntaxChecker.java
index 5871b18f0..82e1a1bae 100644
--- a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/FacsimileTelephoneNumberSyntaxChecker.java
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/FacsimileTelephoneNumberSyntaxChecker.java
@@ -56,8 +56,25 @@ import org.apache.directory.api.util.Strings;
@SuppressWarnings("serial")
public final class FacsimileTelephoneNumberSyntaxChecker extends SyntaxChecker
{
- /** The default pattern used to check a TelephoneNumber */
- private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*]+)+$";
+ /**
+ * The default pattern used to check a TelephoneNumber.
+ * <p>
+ * Note : the unparenthesized alternative matches a single character (not a
+ * '+' quantified run) : a quantifier nested inside the outer '(...)+' is the
+ * classic catastrophic backtracking (ReDoS) shape, and syntax checkers run
+ * on attacker supplied values. Both forms accept exactly the same language,
+ * but this one is matched in linear time.
+ */
+ //private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*]+)+$";
+ private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*])+$";
+
+ /**
+ * The maximum length accepted for a value. A facsimile telephone number is
+ * a few tens of characters at most, plus a handful of fax parameters :
+ * refusing longer values up front bounds both the matching work and the
+ * regexp engine's recursion depth on attacker supplied values.
+ */
+ protected static final int MAX_VALUE_LENGTH = 255;
/** The compiled default pattern */
private String defaultRegexp;
@@ -238,7 +255,7 @@ public final class FacsimileTelephoneNumberSyntaxChecker extends SyntaxChecker
strValue = value.toString();
}
- if ( strValue.length() == 0 )
+ if ( ( strValue.length() == 0 ) || ( strValue.length() > MAX_VALUE_LENGTH ) )
{
if ( LOG.isDebugEnabled() )
{
@@ -275,19 +292,24 @@ public final class FacsimileTelephoneNumberSyntaxChecker extends SyntaxChecker
// First check the telephone number if the '$' is not at the first position
if ( dollarPos > 0 )
{
- boolean result = defaultPattern.matcher( strValue.substring( 0, dollarPos - 1 ) ).matches();
+ // The telephone number part is everything up to (excluding) the first '$'
+ // (it previously dropped the character before the '$' too)
+ boolean result = defaultPattern.matcher( strValue.substring( 0, dollarPos ) ).matches();
- if ( LOG.isDebugEnabled() )
+ if ( !result )
{
- if ( result )
+ if ( LOG.isDebugEnabled() )
{
- LOG.debug( I18n.err( I18n.MSG_13701_SYNTAX_VALID, value ) );
- }
- else
- {
LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
-
- return false;
+ }
+
+ return false;
+ }
+ else
+ {
+ if ( LOG.isDebugEnabled() )
+ {
+ LOG.debug( I18n.err( I18n.MSG_13701_SYNTAX_VALID, value ) );
}
}
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/TelephoneNumberSyntaxChecker.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/TelephoneNumberSyntaxChecker.java
index 60cc14326..99a59221f 100644
--- a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/TelephoneNumberSyntaxChecker.java
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/TelephoneNumberSyntaxChecker.java
@@ -47,8 +47,25 @@ import org.apache.directory.api.util.Strings;
@SuppressWarnings("serial")
public final class TelephoneNumberSyntaxChecker extends SyntaxChecker
{
- /** The default pattern used to check a TelephoneNumber */
- private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*]+)+$";
+ /**
+ * The default pattern used to check a TelephoneNumber.
+ * <p>
+ * Note : the unparenthesized alternative matches a single character (not a
+ * '+' quantified run) : a quantifier nested inside the outer '(...)+' is the
+ * classic catastrophic backtracking (ReDoS) shape, and syntax checkers run
+ * on attacker supplied values. Both forms accept exactly the same language,
+ * but this one is matched in linear time.
+ */
+ //private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*]+)+$";
+ private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*])+$";
+
+ /**
+ * The maximum length accepted for a value. An E.123 telephone number is a
+ * few tens of characters at most : refusing longer values up front bounds
+ * both the matching work and the regexp engine's recursion depth on
+ * attacker supplied values.
+ */
+ protected static final int MAX_VALUE_LENGTH = 255;
/** The default pattern */
private final String defaultRegexp;
@@ -197,7 +214,7 @@ public final class TelephoneNumberSyntaxChecker extends SyntaxChecker
strValue = value.toString();
}
- if ( strValue.length() == 0 )
+ if ( ( strValue.length() == 0 ) || ( strValue.length() > MAX_VALUE_LENGTH ) )
{
if ( LOG.isDebugEnabled() )
{
@@ -208,13 +225,10 @@ public final class TelephoneNumberSyntaxChecker extends SyntaxChecker
}
// We will use a regexp to check the TelephoneNumber.
- boolean result;
-
- // Not sure this is 100% necessary...
- synchronized ( defaultPattern )
- {
- result = defaultPattern.matcher( strValue ).matches();
- }
+ // Pattern is thread safe and each matcher is local to this call, so no
+ // synchronization is needed : a synchronized block here would serialize
+ // every validation done on the shared INSTANCE across all threads.
+ boolean result = defaultPattern.matcher( strValue ).matches();
if ( LOG.isDebugEnabled() )
{
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/FacsimileTelephoneNumberSyntaxCheckerTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/FacsimileTelephoneNumberSyntaxCheckerTest.java
index e6edd8cb5..8856ff8ee 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/FacsimileTelephoneNumberSyntaxCheckerTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/FacsimileTelephoneNumberSyntaxCheckerTest.java
@@ -22,6 +22,9 @@ package org.apache.directory.api.ldap.model.schema.syntaxes;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+
+import java.time.Duration;
import org.apache.directory.api.ldap.model.schema.syntaxCheckers.FacsimileTelephoneNumberSyntaxChecker;
import org.junit.jupiter.api.Test;
@@ -137,4 +140,48 @@ public class FacsimileTelephoneNumberSyntaxCheckerTest
assertFalse( checker.isValidSyntax( "+ 33 1 (456) 7891 12345$twoDimensional$twoDimensional" ) );
assertFalse( checker.isValidSyntax( "+ 33 1 (456) 7891 12345$b4Width$ $a3Width" ) );
}
+
+
+ @Test
+ public void testNoCatastrophicBacktracking()
+ {
+ // A long digit run followed by one non matching character used to send
+ // the previous default regexp into exponential backtracking (ReDoS) :
+ // the match must stay linear in the value length
+ StringBuilder sb = new StringBuilder();
+
+ for ( int i = 0; i < 200; i++ )
+ {
+ sb.append( '1' );
+ }
+
+ sb.append( 'A' );
+
+ String attack = sb.toString();
+
+ assertTimeoutPreemptively( Duration.ofSeconds( 5 ),
+ () -> assertFalse( checker.isValidSyntax( attack ) ) );
+
+ assertTimeoutPreemptively( Duration.ofSeconds( 5 ),
+ () -> assertFalse( checker.isValidSyntax( attack + "$twoDimensional" ) ) );
+
+ // An invalid telephone number part must be rejected whatever the logging
+ // level (the rejection used to be guarded by LOG.isDebugEnabled()), and
+ // the character right before the '$' is part of the number
+ assertFalse( checker.isValidSyntax( "12A$twoDimensional" ) );
+
+ // Values above the length cap are rejected up front, bounding both the
+ // matching work and the regexp engine's recursion depth
+ StringBuilder big = new StringBuilder();
+
+ for ( int i = 0; i < 100000; i++ )
+ {
+ big.append( '1' );
+ }
+
+ String bigValue = big.toString();
+
+ assertTimeoutPreemptively( Duration.ofSeconds( 5 ),
+ () -> assertFalse( checker.isValidSyntax( bigValue ) ) );
+ }
}
diff --git a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelephoneNumberSyntaxCheckerTest.java b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelephoneNumberSyntaxCheckerTest.java
index 58d507e1d..50ecb7cff 100644
--- a/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelephoneNumberSyntaxCheckerTest.java
+++ b/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/syntaxes/TelephoneNumberSyntaxCheckerTest.java
@@ -22,8 +22,11 @@ package org.apache.directory.api.ldap.model.schema.syntaxes;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.time.Duration;
+
import org.apache.directory.api.ldap.model.schema.syntaxCheckers.TelephoneNumberSyntaxChecker;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
@@ -111,4 +114,44 @@ public class TelephoneNumberSyntaxCheckerTest
checker = TelephoneNumberSyntaxChecker.builder().build();
assertTrue( checker.isValidSyntax( "1" ) );
}
+
+
+ @Test
+ public void testNoCatastrophicBacktracking()
+ {
+ // A long digit run followed by one non matching character used to send
+ // the previous default regexp into exponential backtracking (ReDoS) :
+ // the match must stay linear in the value length
+ StringBuilder sb = new StringBuilder();
+
+ for ( int i = 0; i < 200; i++ )
+ {
+ sb.append( '1' );
+ }
+
+ sb.append( 'A' );
+
+ String attack = sb.toString();
+
+ assertTimeoutPreemptively( Duration.ofSeconds( 5 ),
+ () -> assertFalse( checker.isValidSyntax( attack ) ) );
+
+ // And the equivalent valid value still matches
+ assertTimeoutPreemptively( Duration.ofSeconds( 5 ),
+ () -> assertTrue( checker.isValidSyntax( attack.substring( 0, attack.length() - 1 ) ) ) );
+
+ // Values above the length cap are rejected up front, bounding both the
+ // matching work and the regexp engine's recursion depth
+ StringBuilder big = new StringBuilder();
+
+ for ( int i = 0; i < 100000; i++ )
+ {
+ big.append( '1' );
+ }
+
+ String bigValue = big.toString();
+
+ assertTimeoutPreemptively( Duration.ofSeconds( 5 ),
+ () -> assertFalse( checker.isValidSyntax( bigValue ) ) );
+ }
}