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-jdkim.git

commit 7c586953e40b4e9e9fb479f990c4d30cbe2beb67
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Nov 13 15:18:54 2026 +0100

    [FIX] TagValue: accept two folds around an empty value
    
    checkSingleFWSAround splits a segment into a leading and a trailing
    whitespace run around its content and allows one FWS in each. With an
    empty value both [FWS] of "[FWS] tag-value [FWS]" fall into the leading
    run, so "b=\r\n\t\r\n\t", two folds valid per RFC 6376 section 3.2, was
    rejected and the signature or key record got PERMFAIL.
    
    A segment with no content now accepts up to two FWS. A blank line
    ("\r\n\r\n ") is still rejected.
---
 .../java/org/apache/james/jdkim/tagvalue/TagValue.java    | 11 +++++++++++
 .../org/apache/james/jdkim/tagvalue/TagValueTest.java     | 15 +++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/main/src/main/java/org/apache/james/jdkim/tagvalue/TagValue.java 
b/main/src/main/java/org/apache/james/jdkim/tagvalue/TagValue.java
index 4e47f5e..46d4079 100644
--- a/main/src/main/java/org/apache/james/jdkim/tagvalue/TagValue.java
+++ b/main/src/main/java/org/apache/james/jdkim/tagvalue/TagValue.java
@@ -65,6 +65,8 @@ public class TagValue {
     private static final Pattern fwsPattern = Pattern.compile("[ \t\r\n]");
     // FWS = [*WSP CRLF] 1*WSP, or nothing
     private static final Pattern trailingFWSPattern = 
Pattern.compile("^(?:(?:[ \t]*\r\n)?[ \t]+)?$");
+    // [FWS] tag-value [FWS] with an empty tag-value: up to two FWS, 
possessive to stay linear
+    private static final Pattern twoFWSPattern = Pattern.compile("^(?:(?:[ 
\t]*+\r\n)?+[ \t]++){0,2}+$");
 
     // we may use a TreeMap because we may need to know original order.
     private final Map<String, CharSequence> tagValues;
@@ -214,6 +216,7 @@ public class TagValue {
     /**
      * Checks that the whitespace that starts and ends data[start, end) is at 
most one FWS on each side:
      * FWS = [*WSP CRLF] 1*WSP holds a single CRLF, a line holding only WSP is 
obsolete syntax.
+     * Whitespace alone may be two FWS, the one before and the one after an 
empty value.
      */
     private static void checkSingleFWSAround(String data, int start, int end) {
         int contentStart = start;
@@ -226,6 +229,14 @@ public class TagValue {
         }
         String leading = data.substring(start, contentStart);
         String trailing = data.substring(contentEnd, end);
+        if (contentStart == end) {
+            // no content: the run holds the FWS before and the FWS after an 
empty value
+            if (!twoFWSPattern.matcher(leading).matches()) {
+                throw new IllegalStateException("Syntax error: whitespace that 
is not FWS in ["
+                        + leading + "]");
+            }
+            return;
+        }
         if (!trailingFWSPattern.matcher(leading).matches() || 
!trailingFWSPattern.matcher(trailing).matches()) {
             throw new IllegalStateException("Syntax error: whitespace that is 
not FWS in ["
                     + data.substring(start, end) + "]");
diff --git 
a/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java 
b/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java
index 50eac49..e0cc968 100644
--- a/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java
+++ b/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java
@@ -236,4 +236,19 @@ public class TagValueTest {
         assertEquals("b", new TagValue("a\r\n 
=b;\r\n\tc=d").getValue("a").toString());
         assertEquals("", new TagValue("v= 
\r\n\t;f=1").getValue("v").toString());
     }
+
+    @Test
+    public void twoFoldsAroundAnEmptyValueShouldBeAccepted() {
+        // [FWS] tag-value [FWS] with an empty tag-value: two FWS, one fold 
each
+        assertEquals("", new 
TagValue("b\r\n\t=\r\n\t\r\n\t").getValue("b").toString());
+        assertEquals("", new TagValue("v=\r\n \r\n 
;f=1").getValue("v").toString());
+        assertEquals("", new TagValue("v= \t\r\n \t\r\n\t 
;f=1").getValue("v").toString());
+    }
+
+    @Test
+    public void moreThanTwoFoldsAroundAnEmptyValueShouldBeRejected() {
+        assertThatThrownBy(() -> new TagValue("v=\r\n \r\n \r\n 
;f=1")).isInstanceOf(IllegalStateException.class);
+        assertThatThrownBy(() -> new TagValue("v=\r\n\r\n 
;f=1")).isInstanceOf(IllegalStateException.class);
+        assertThatThrownBy(() -> new TagValue("v= 
\r\n")).isInstanceOf(IllegalStateException.class);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to