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 24bd0893ddb3a4fb69608141390715f5ce809853 Author: Benoit TELLIER <[email protected]> AuthorDate: Wed Nov 25 09:03:13 2026 +0100 [FIX] StagedMultipleSPFExecutor: do not reuse the key of a pending lookup The 16 bit static counter wrapped after 65536 lookups, so a lookup still pending could have its key taken by a newer one and its answer was lost or given to the other check. Use an instance int and skip the keys in use. --- .../jspf/executor/StagedMultipleSPFExecutor.java | 17 +++++------- .../StagedMultipleSPFExecutorIntegrationTest.java | 31 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java b/resolver/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java index 87fd221..f8a2d3d 100644 --- a/resolver/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java +++ b/resolver/src/main/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutor.java @@ -77,17 +77,14 @@ public class StagedMultipleSPFExecutor implements SPFExecutor, Runnable { } - // Use short as id because the id header is limited to 16 bit - // From RFC1035 4.1.1. Header section format : - // - // ID A 16 bit identifier assigned by the program that - // generates any kind of query. This identifier is copied - // the corresponding reply and can be used by the requester - // to match up replies to outstanding queries. - // - private static short id; - + // The key of a pending lookup in sessions and results. It is not a DNS message id: the lookup + // service keeps its own. An int wraps only after 2^32 lookups, and nextId() skips a key still in use. + private int id; + private synchronized int nextId() { + while (sessions.containsKey(id)) { + id++; + } return id++; } diff --git a/resolver/src/test/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutorIntegrationTest.java b/resolver/src/test/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutorIntegrationTest.java index 372d5b3..4b9df18 100644 --- a/resolver/src/test/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutorIntegrationTest.java +++ b/resolver/src/test/java/org/apache/james/jspf/executor/StagedMultipleSPFExecutorIntegrationTest.java @@ -28,6 +28,9 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import org.apache.james.jspf.core.DNSRequest; import org.apache.james.jspf.core.DNSService; @@ -40,6 +43,7 @@ public class StagedMultipleSPFExecutorIntegrationTest { private static final Map<String, String> RECORDS = Map.of( "example.com", "v=spf1 +all", + "held.example.com", "v=spf1 -all", "include.example.com", "v=spf1 include:broken.example.com -all", "redirect.example.com", "v=spf1 redirect=broken.example.com"); @@ -137,6 +141,33 @@ public class StagedMultipleSPFExecutorIntegrationTest { assertEquals(SPFErrorConstants.PASS_CONV, spf.checkSPF("1.2.3.4", "[email protected]", "example.com").getResult()); } + @Test(timeout = 60000) + public void aPendingLookupShouldGetItsOwnAnswerAfterManyOtherLookups() { + AtomicReference<Runnable> heldAnswer = new AtomicReference<>(); + AtomicInteger lookups = new AtomicInteger(); + // answers at once, except the lookup of held.example.com, answered on demand + DNSAsynchLookupService holding = (request, id, responseQueue) -> { + lookups.incrementAndGet(); + Runnable answer = () -> responseQueue.insertResponse(new IResponseImpl(id, RECORDS.get(request.getHostname()) == null + ? null : new ArrayList<>(List.of(RECORDS.get(request.getHostname()))))); + if (request.getHostname().equals("held.example.com")) { + heldAnswer.set(answer); + } else { + answer.run(); + } + }; + SPF spf = new SPF(DNS, new StagedMultipleSPFExecutor(holding)); + + FutureSPFResult held = (FutureSPFResult) spf.checkSPF("1.2.3.4", "[email protected]", "held.example.com"); + // more lookups than a 16 bit id can tell apart while the lookup of held.example.com is pending + while (lookups.get() < 70000) { + assertEquals(SPFErrorConstants.PASS_CONV, spf.checkSPF("1.2.3.4", "[email protected]", "example.com").getResult()); + } + heldAnswer.get().run(); + + assertEquals(SPFErrorConstants.FAIL_CONV, held.getResult(10, TimeUnit.SECONDS)); + } + @Test(timeout = 10000) public void anInterruptedWaitForAResponseShouldKeepTheInterrupt() { Thread.currentThread().interrupt(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
