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 7f0492390c92c3e21b55f516312b3134d07dced8 Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 19 06:02:45 2026 +0100 [PERF] DKIMCommon: canonicalize relaxed header values in one pass Relaxed header canonicalization ran three String#replaceAll per signed field, each compiling its pattern and copying the value, then scanned the result again in octets(). On a large To: field this was most of the header verification time and of its allocations. A single loop now unfolds, collapses WSP runs and deletes the WSP around the value while writing the octets, and still rejects a char above U+00FF. The ":" and CRLF octets are static arrays. One difference: the former "$" also matched before a final line terminator, so "x \r\n" lost its space. RFC 6376 section 3.4.2 only deletes WSP at the end of the value, which the loop now does. --- .../java/org/apache/james/jdkim/DKIMCommon.java | 60 +++++++++++--- .../jdkim/RelaxedHeaderCanonicalizationTest.java | 91 ++++++++++++++++++++++ 2 files changed, 142 insertions(+), 9 deletions(-) diff --git a/main/src/main/java/org/apache/james/jdkim/DKIMCommon.java b/main/src/main/java/org/apache/james/jdkim/DKIMCommon.java index 124b0b4..83c91a5 100644 --- a/main/src/main/java/org/apache/james/jdkim/DKIMCommon.java +++ b/main/src/main/java/org/apache/james/jdkim/DKIMCommon.java @@ -32,6 +32,7 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.security.Signature; import java.security.SignatureException; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -72,6 +73,9 @@ public abstract class DKIMCommon { return s.getBytes(StandardCharsets.ISO_8859_1); } + private static final byte[] COLON = {':'}; + private static final byte[] CRLF = {'\r', '\n'}; + protected static void updateSignature(Signature signature, boolean relaxed, CharSequence header, @@ -81,15 +85,11 @@ public abstract class DKIMCommon { System.out .println("#" + Headers.lowerCaseFieldName(header) + ":-"); signature.update(octets(Headers.lowerCaseFieldName(header))); - signature.update(octets(":")); - String headerValue = fv.substring(fv.indexOf(':') + 1); - headerValue = headerValue.replaceAll("\r\n[\t ]", " "); - headerValue = headerValue.replaceAll("[\t ]+", " "); - // RFC 6376 section 3.4.2 only deletes WSP: String#trim would also drop control characters - headerValue = headerValue.replaceAll("^ | $", ""); - signature.update(octets(headerValue)); + signature.update(COLON); + byte[] headerValue = relaxedValue(fv); + signature.update(headerValue); if (DEEP_DEBUG) - System.out.println("#" + headerValue + "#"); + System.out.println("#" + new String(headerValue, StandardCharsets.ISO_8859_1) + "#"); } else { signature.update(octets(fv)); if (DEEP_DEBUG) @@ -97,6 +97,48 @@ public abstract class DKIMCommon { } } + /** + * The relaxed canonicalization of the value of a field (RFC 6376 section 3.4.2), in one pass: a CRLF + * followed by WSP is unfolded, each run of WSP becomes a single SP, and the WSP at the start and at the end + * of the value is deleted. Only WSP is deleted, never control characters, and a CRLF that is not followed + * by WSP is kept as is. + * + * @param fv the field, name included: the value is what follows the first colon + * @throws IllegalArgumentException on a char above U+00FF, see {@link #octets(String)} + */ + static byte[] relaxedValue(String fv) { + int n = fv.length(); + int start = fv.indexOf(':') + 1; + byte[] out = new byte[n - start]; + int o = 0; + boolean pendingWsp = false; + for (int i = start; i < n; i++) { + char c = fv.charAt(i); + if (c == ' ' || c == '\t') { + pendingWsp = true; + continue; + } + if (c == '\r' && i + 2 < n && fv.charAt(i + 1) == '\n' + && (fv.charAt(i + 2) == ' ' || fv.charAt(i + 2) == '\t')) { + // unfolding: the CRLF goes, the WSP after it is handled by the next iteration + pendingWsp = true; + i++; + continue; + } + if (c > 0xFF) { + throw new IllegalArgumentException("Header field holds a char above U+00FF at " + i + + ": header fields must carry one octet per char"); + } + if (pendingWsp && o > 0) { + out[o++] = ' '; + } + pendingWsp = false; + out[o++] = (byte) c; + } + // WSP still pending ends the value: it is deleted + return o == out.length ? out : Arrays.copyOf(out, o); + } + protected static void signatureCheck(Headers h, SignatureRecord sign, List<CharSequence> headers, Signature signature) throws SignatureException, PermFailException { @@ -149,7 +191,7 @@ public abstract class DKIMCommon { if (doneHeaders <= hl.size()) { String fv = hl.get(hl.size() - doneHeaders); updateSignature(signature, relaxedHeaders, header, fv); - signature.update(octets("\r\n")); + signature.update(CRLF); processedHeader.put(lowerCaseHeader, doneHeaders); } } diff --git a/main/src/test/java/org/apache/james/jdkim/RelaxedHeaderCanonicalizationTest.java b/main/src/test/java/org/apache/james/jdkim/RelaxedHeaderCanonicalizationTest.java new file mode 100644 index 0000000..70a1f86 --- /dev/null +++ b/main/src/test/java/org/apache/james/jdkim/RelaxedHeaderCanonicalizationTest.java @@ -0,0 +1,91 @@ +/****************************************************************************** + * 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.jdkim; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.charset.StandardCharsets; +import java.util.Random; + +import org.junit.Test; + +public class RelaxedHeaderCanonicalizationTest { + + /** + * The former implementation, with "\\z" where it had "$": "$" also matches before a final line terminator, + * so it deleted a space that RFC 6376 section 3.4.2 keeps ("a \r\n" ends with CRLF, not with WSP). + */ + private static String reference(String fv) { + String headerValue = fv.substring(fv.indexOf(':') + 1); + headerValue = headerValue.replaceAll("\r\n[\t ]", " "); + headerValue = headerValue.replaceAll("[\t ]+", " "); + return headerValue.replaceAll("^ | \\z", ""); + } + + private static String canonical(String fv) { + return new String(DKIMCommon.relaxedValue(fv), StandardCharsets.ISO_8859_1); + } + + @Test + public void shouldGiveTheRfc6376Examples() { + assertThat(canonical("A: X")).isEqualTo("X"); + assertThat(canonical("B : Y\t\r\n\tZ ")).isEqualTo("Y Z"); + assertThat(canonical("c:")).isEqualTo(""); + assertThat(canonical("d: \t ")).isEqualTo(""); + } + + @Test + public void shouldKeepControlCharactersAroundTheValue() { + assertThat(canonical("a:\u0001 x \u0001")).isEqualTo("\u0001 x \u0001"); + assertThat(canonical("a: \r\nx")).isEqualTo("\r\nx"); + assertThat(canonical("a:x \r\n")).isEqualTo("x \r\n"); + } + + @Test + public void shouldUnfoldOnlyCrlfFollowedByWsp() { + assertThat(canonical("a: x\r\n\r\n y")).isEqualTo("x\r\n y"); + assertThat(canonical("a: x\r\n\t\r\n y")).isEqualTo("x y"); + assertThat(canonical("a: x\ry\nz")).isEqualTo("x\ry\nz"); + } + + @Test + public void shouldRejectCharsAboveU00ff() { + String fv = "a: x" + (char) 0x100; + assertThatThrownBy(() -> DKIMCommon.relaxedValue(fv)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("above U+00FF"); + } + + @Test + public void shouldMatchTheRegexImplementationOnRandomFields() { + char[] alphabet = {' ', '\t', '\r', '\n', 'a', ':', (char) 0x01, (char) 0xe9, (char) 0x85}; + Random random = new Random(6376); + for (int run = 0; run < 200000; run++) { + StringBuilder sb = new StringBuilder(); + int length = random.nextInt(12); + for (int i = 0; i < length; i++) { + sb.append(alphabet[random.nextInt(alphabet.length)]); + } + String fv = "x:" + sb; + assertThat(canonical(fv)).as("field [%s]", fv).isEqualTo(reference(fv)); + } + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
