This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-jspf.git
The following commit(s) were added to refs/heads/master by this push: new 13711f0 JSPF-110 Prevent infinite loop in error handling 13711f0 is described below commit 13711f00e83b6422cb8f9253ad1fc997a08d1283 Author: Benoit TELLIER <btell...@linagora.com> AuthorDate: Fri May 31 17:23:07 2024 +0200 JSPF-110 Prevent infinite loop in error handling --- .../java/org/apache/james/jspf/core/SPFSession.java | 17 +++++++++++++++++ .../james/jspf/executor/AsynchronousSPFExecutor.java | 8 ++++---- .../james/jspf/executor/StagedMultipleSPFExecutor.java | 8 ++++---- .../james/jspf/executor/SynchronousSPFExecutor.java | 8 ++++---- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java b/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java index deb2ad3..90651a7 100644 --- a/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java +++ b/resolver/src/main/java/org/apache/james/jspf/core/SPFSession.java @@ -25,6 +25,7 @@ import org.apache.james.jspf.core.exceptions.PermErrorException; import java.util.HashMap; import java.util.Map; import java.util.Stack; +import java.util.function.Predicate; /** * @@ -369,6 +370,22 @@ public class SPFSession implements MacroData { } } + public SPFChecker popChecker(Predicate<SPFChecker> predicate) { + SPFChecker result = null; + while (result == null) { + if (checkers.isEmpty()) { + return null; + } + SPFChecker checker = checkers.pop(); + if (predicate.test(checker)) { + result = checker; + } + } + return result; + } + + + /** * @param result */ diff --git a/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java b/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java index 0b950b3..4e8d971 100644 --- a/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java +++ b/resolver/src/main/java/org/apache/james/jspf/executor/AsynchronousSPFExecutor.java @@ -103,16 +103,16 @@ public class AsynchronousSPFExecutor implements SPFExecutor { private void handleError(SPFSession session, SPFChecker checker, Exception e) { while (e != null) { - while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) { - checker = session.popChecker(); + checker = session.popChecker(c -> c instanceof SPFCheckerExceptionCatcher); + if (checker == null) { + // Error case not handled by JSPF. Throw to avoid infinite loop. See JSPF-110. + throw new RuntimeException(e); } try { ((SPFCheckerExceptionCatcher) checker).onException(e, session); e = null; } catch (SPFResultException ex) { e = ex; - } finally { - checker = null; } } } 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 f1ee767..47f597d 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 @@ -128,16 +128,16 @@ public class StagedMultipleSPFExecutor implements SPFExecutor, Runnable { } } catch (Exception e) { while (e != null) { - while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) { - checker = session.popChecker(); + checker = session.popChecker(c -> c instanceof SPFCheckerExceptionCatcher); + if (checker == null) { + // Error case not handled by JSPF. Throw to avoid infinite loop. See JSPF-110. + throw new RuntimeException(e); } try { ((SPFCheckerExceptionCatcher) checker).onException(e, session); e = null; } catch (SPFResultException ex) { e = ex; - } finally { - checker = null; } } } diff --git a/resolver/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java b/resolver/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java index 882a6dc..db614dc 100644 --- a/resolver/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java +++ b/resolver/src/main/java/org/apache/james/jspf/executor/SynchronousSPFExecutor.java @@ -65,16 +65,16 @@ public class SynchronousSPFExecutor implements SPFExecutor { } } catch (Exception e) { while (e != null) { - while (checker == null || !(checker instanceof SPFCheckerExceptionCatcher)) { - checker = session.popChecker(); + checker = session.popChecker(c -> c instanceof SPFCheckerExceptionCatcher); + if (checker == null) { + // Error case not handled by JSPF. Throw to avoid infinite loop. See JSPF-110. + throw new RuntimeException(e); } try { ((SPFCheckerExceptionCatcher) checker).onException(e, session); e = null; } catch (SPFResultException ex) { e = ex; - } finally { - checker = null; } } } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org