This is an automated email from the ASF dual-hosted git repository.
bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new f96cf92d53 Fix#9196 Mail validator A-record fallback when domain has
no MX (#9261)
f96cf92d53 is described below
commit f96cf92d53102eff7ba597fd105607f2b558f1cc
Author: Lance <[email protected]>
AuthorDate: Thu Nov 5 14:08:32 2026 +0800
Fix#9196 Mail validator A-record fallback when domain has no MX (#9261)
* Fix Mail validator A-record fallback when domain has no MX
Signed-off-by: leehaut <[email protected]>
* Fix Mail validator A-record fallback when domain has no MX
Signed-off-by: leehaut <[email protected]>
---------
Signed-off-by: leehaut <[email protected]>
---
.../ROOT/pages/workflow/actions/mailvalidator.adoc | 4 +-
.../actions/mailvalidator/MailValidation.java | 53 ++++++++++++++++++----
.../actions/mailvalidator/MailValidationTest.java | 29 ++++++++++++
3 files changed, 74 insertions(+), 12 deletions(-)
diff --git
a/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/mailvalidator.adoc
b/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/mailvalidator.adoc
index d718fbc57d..0bc80d0795 100644
---
a/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/mailvalidator.adoc
+++
b/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/mailvalidator.adoc
@@ -55,11 +55,11 @@ What this action passes on to the next action. See
xref:workflow/action-results.
|===
|Result|Value
|True|Every address in *Email address* is valid, with 0 errors. With *SMTP
check*, a mail server also accepted *Email sender* and each address.
-|False|The number of errors is 1. *Email address* is empty, an address is not
a valid email address, *SMTP check* is selected while *Email sender* is empty,
or with *SMTP check* the mail server refuses *Email sender* or an address, the
domain doesn't exist or has no MX record, or the server can't be reached. The
addresses are checked one after the other, and the first one that is not valid
ends the check.
+|False|The number of errors is 1. *Email address* is empty, an address is not
a valid email address, *SMTP check* is selected while *Email sender* is empty,
or with *SMTP check* the mail server refuses *Email sender* or an address, the
domain doesn't exist or has no MX or A record, or the server can't be reached.
The addresses are checked one after the other, and the first one that is not
valid ends the check.
|Result files|Passed on unchanged.
|Result rows|Passed on unchanged.
|===
NOTE: The addresses must be separated by exactly one space: two spaces, or a
comma, make the check fail.
-NOTE: *SMTP check* connects to port 25 of the mail servers of the domain (its
MX records), or of *Default SMTP server* when it is set, and asks whether it
accepts *Email sender* and the address. Without *Default SMTP server*, an
address of a domain that has no MX record always fails, even when the domain
has an A record: the check fails with "Error getting machines : Index 1 out of
bounds for length 1".
+NOTE: *SMTP check* connects to port 25 of the mail servers of the domain (its
MX records, or its A record when it has no MX record), or of *Default SMTP
server* when it is set, and asks whether it accepts *Email sender* and the
address.
diff --git
a/plugins/misc/mail/src/main/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidation.java
b/plugins/misc/mail/src/main/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidation.java
index d62100791b..cb1d6410a5 100644
---
a/plugins/misc/mail/src/main/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidation.java
+++
b/plugins/misc/mail/src/main/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidation.java
@@ -40,8 +40,8 @@ public class MailValidation {
private static final Class<?> PKG = ActionMailValidator.class;
- public static boolean isRegExValid(String emailAdress) {
- return GenericValidator.isEmail(emailAdress);
+ public static boolean isRegExValid(String emailAddress) {
+ return GenericValidator.isEmail(emailAddress);
}
/**
@@ -86,8 +86,6 @@ public class MailValidation {
private static void say(BufferedWriter wr, String text) throws IOException {
wr.write(text + "\r\n");
wr.flush();
-
- return;
}
private static ArrayList<String> getMX(String hostName) throws
NamingException {
@@ -111,22 +109,57 @@ public class MailValidation {
// Huzzah! we have machines to try. Return them as an array list
// NOTE: We SHOULD take the preference into account to be absolutely
// correct. This is left as an exercise for anyone who cares.
+ //
+ // MX values look like "10 mail.example.com." while A values are a bare
+ // address ("34.223.124.45"). Parse both formats so the A-record fallback
+ // above actually reaches SMTP on port 25.
ArrayList<String> res = new ArrayList<>();
NamingEnumeration<?> en = attr.getAll();
while (en.hasMore()) {
- String x = (String) en.next();
- String[] f = x.split(" ");
- if (f[1].endsWith(".")) {
- f[1] = f[1].substring(0, (f[1].length() - 1));
+ String host = extractMailHost((String) en.next());
+ if (host != null) {
+ res.add(host);
}
- res.add(f[1]);
}
return res;
}
/**
- * Validate an email address This code is from :
http://www.rgagnon.com/javadetails/java-0452.html
+ * Extract a mail host from a DNS attribute value.
+ *
+ * <p>MX records are stored as {@code preference host} (e.g. {@code 40
+ * alt4.gmail-smtp-in.l.google.com.}). A records are a single address (e.g.
{@code
+ * 34.223.124.45}).
+ *
+ * @param dnsAttributeValue raw MX or A attribute value from JNDI DNS
+ * @return host or IP to contact, or {@code null} if the value is empty
+ */
+ static String extractMailHost(String dnsAttributeValue) {
+ if (dnsAttributeValue == null) {
+ return null;
+ }
+ String[] parts = dnsAttributeValue.trim().split("\\s+");
+ if (parts.length == 0 || Utils.isEmpty(parts[0])) {
+ return null;
+ }
+ String host;
+ if (parts.length >= 2) {
+ // MX: preference + host
+ host = parts[1];
+ } else {
+ // A: bare address / hostname
+ host = parts[0];
+ }
+ if (host.endsWith(".")) {
+ host = host.substring(0, host.length() - 1);
+ }
+ return Utils.isEmpty(host) ? null : host;
+ }
+
+ /**
+ * Validate an email address This code is from : <a
+ * href="http://www.rgagnon.com/javadetails/java-0452.html">java details</a>
*/
public static MailValidationResult isAddressValid(
ILogChannel log,
diff --git
a/plugins/misc/mail/src/test/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidationTest.java
b/plugins/misc/mail/src/test/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidationTest.java
index 1252019f82..6eef39b373 100644
---
a/plugins/misc/mail/src/test/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidationTest.java
+++
b/plugins/misc/mail/src/test/java/org/apache/hop/mail/workflow/actions/mailvalidator/MailValidationTest.java
@@ -16,11 +16,14 @@
*/
package org.apache.hop.mail.workflow.actions.mailvalidator;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
+/** Unit test for {@link MailValidation} */
class MailValidationTest {
@Test
@@ -39,4 +42,30 @@ class MailValidationTest {
assertFalse(MailValidation.isRegExValid("user@"));
assertFalse(MailValidation.isRegExValid("user@@double.example.com"));
}
+
+ @Test
+ void extractMailHostParsesMxRecord() {
+ // JNDI MX values are "preference host", often with a trailing DNS root dot
+ assertEquals(
+ "alt4.gmail-smtp-in.l.google.com",
+ MailValidation.extractMailHost("40 alt4.gmail-smtp-in.l.google.com."));
+ assertEquals("mail.example.com", MailValidation.extractMailHost("10
mail.example.com"));
+ assertEquals("mail.example.com", MailValidation.extractMailHost(" 5
mail.example.com. "));
+ }
+
+ @Test
+ void extractMailHostParsesARecord() {
+ // Domains without MX fall back to A; the value is a bare address
(HOP-9196).
+ // Previously this path always threw ArrayIndexOutOfBoundsException on
f[1].
+ assertEquals("34.223.124.45",
MailValidation.extractMailHost("34.223.124.45"));
+ assertEquals("34.223.124.45", MailValidation.extractMailHost("
34.223.124.45 "));
+ assertEquals("mail.example.com",
MailValidation.extractMailHost("mail.example.com."));
+ }
+
+ @Test
+ void extractMailHostRejectsEmpty() {
+ assertNull(MailValidation.extractMailHost(null));
+ assertNull(MailValidation.extractMailHost(""));
+ assertNull(MailValidation.extractMailHost(" "));
+ }
}