This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-jspf.git
commit 626e920128da6b533a81c7527afe376f111cfb7d Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 18:04:16 2026 +0100 [FIX] SPF1Utils: check an FQDN without a regex The repeated group of the regex recursed once per label: a name of many labels, as from a PTR answer, threw a StackOverflowError. A loop checks the same language. Fixes https://github.com/linagora/james-jspf/issues/26 --- .../java/org/apache/james/jspf/core/SPF1Utils.java | 32 ++++++++++++++-- .../org/apache/james/jspf/core/SPF1UtilsTest.java | 44 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/core/SPF1Utils.java b/resolver/src/main/java/org/apache/james/jspf/core/SPF1Utils.java index 5564b78..a07452b 100644 --- a/resolver/src/main/java/org/apache/james/jspf/core/SPF1Utils.java +++ b/resolver/src/main/java/org/apache/james/jspf/core/SPF1Utils.java @@ -65,11 +65,35 @@ public class SPF1Utils { * @return false or true */ public static boolean checkFQDN(String host) { - String regex = "(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z]+)$"; - if (host.matches(regex)) { - return true; - } else { + // the language of (([a-zA-Z0-9\\-])+\\.)+([a-zA-Z]+), checked without a regex: a repeated group + // recurses once per label and overflows the stack on a long name + int lastDot = host.lastIndexOf('.'); + if (lastDot < 0 || lastDot == host.length() - 1) { return false; } + for (int i = lastDot + 1; i < host.length(); i++) { + if (!isAsciiLetter(host.charAt(i))) { + return false; + } + } + int labelLength = 0; + for (int i = 0; i <= lastDot; i++) { + char c = host.charAt(i); + if (c == '.') { + if (labelLength == 0) { + return false; + } + labelLength = 0; + } else if (isAsciiLetter(c) || (c >= '0' && c <= '9') || c == '-') { + labelLength++; + } else { + return false; + } + } + return true; + } + + private static boolean isAsciiLetter(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } } diff --git a/resolver/src/test/java/org/apache/james/jspf/core/SPF1UtilsTest.java b/resolver/src/test/java/org/apache/james/jspf/core/SPF1UtilsTest.java new file mode 100644 index 0000000..f50bd43 --- /dev/null +++ b/resolver/src/test/java/org/apache/james/jspf/core/SPF1UtilsTest.java @@ -0,0 +1,44 @@ +/**************************************************************** + * 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 org.apache.james.jspf.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class SPF1UtilsTest { + + private static final String FORMER_REGEX = "(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z]+)$"; + + @Test + public void checkFQDNShouldAcceptTheLanguageOfTheFormerRegex() { + String[] hosts = {"", ".", "com", "example.com", "a.b.example.com", "-.x", "a-1.b2.COM", "example.com.", + ".example.com", "a..com", "example.c0m", "example.com\n", "exa_mple.com", "1.2.3.4", "é.com", "a.b-"}; + for (String host : hosts) { + assertEquals(host, host.matches(FORMER_REGEX), SPF1Utils.checkFQDN(host)); + } + } + + @Test + public void checkFQDNShouldNotOverflowTheStackOnManyLabels() { + assertTrue(SPF1Utils.checkFQDN("a.".repeat(100_000) + "com")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
