From 511954171dc688436b65948fa27ef833fc99ca9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sat, 9 Mar 2024 16:14:20 +0100 Subject: [PATCH] Use en locale for uppercase/lowercase (fixes #156) --- .../src/main/java/org/shredzone/acme4j/Status.java | 3 ++- .../java/org/shredzone/acme4j/toolbox/AcmeUtils.java | 4 ++-- .../test/java/org/shredzone/acme4j/StatusTest.java | 7 ++++++- .../shredzone/acme4j/smime/wrapper/SignedMail.java | 11 ++++++----- .../shredzone/acme4j/smime/wrapper/SimpleMail.java | 3 ++- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java index 86101bde..1af60e07 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Status.java @@ -14,6 +14,7 @@ package org.shredzone.acme4j; import java.util.Arrays; +import java.util.Locale; /** * An enumeration of status codes of challenges and authorizations. @@ -84,7 +85,7 @@ public enum Status { * no match */ public static Status parse(String str) { - var check = str.toUpperCase(); + var check = str.toUpperCase(Locale.ENGLISH); return Arrays.stream(values()) .filter(s -> s.name().equals(check)) .findFirst() diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/toolbox/AcmeUtils.java b/acme4j-client/src/main/java/org/shredzone/acme4j/toolbox/AcmeUtils.java index fd10cbc9..ce523c13 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/toolbox/AcmeUtils.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/toolbox/AcmeUtils.java @@ -183,7 +183,7 @@ public static boolean isValidBase64Url(@Nullable String base64) { */ public static String toAce(String domain) { Objects.requireNonNull(domain, "domain"); - return IDN.toASCII(domain.trim()).toLowerCase(); + return IDN.toASCII(domain.trim()).toLowerCase(Locale.ENGLISH); } /** @@ -307,7 +307,7 @@ public static String getContentType(@Nullable String header) { if (charset != null && !"utf-8".equalsIgnoreCase(charset)) { throw new AcmeProtocolException("Unsupported charset " + charset); } - return m.group(1).trim().toLowerCase(); + return m.group(1).trim().toLowerCase(Locale.ENGLISH); } } return null; diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java index 72572dc8..7acf7819 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/StatusTest.java @@ -15,6 +15,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.Locale; + import org.junit.jupiter.api.Test; /** @@ -27,8 +29,11 @@ public class StatusTest { */ @Test public void testParse() { + // Would break toUpperCase() if English locale is not set, see #156. + Locale.setDefault(new Locale("tr")); + for (var s : Status.values()) { - var parsed = Status.parse(s.name().toLowerCase()); + var parsed = Status.parse(s.name().toLowerCase(Locale.ENGLISH)); assertThat(parsed).isEqualTo(s); } diff --git a/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SignedMail.java b/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SignedMail.java index 7f8e864f..d26beda6 100644 --- a/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SignedMail.java +++ b/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SignedMail.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.Enumeration; import java.util.List; +import java.util.Locale; import java.util.Optional; import java.util.Set; import java.util.TreeSet; @@ -74,7 +75,7 @@ public void importUntrustedHeaders(Enumeration
en) { while (en.hasMoreElements()) { var h = en.nextElement(); var name = h.getName(); - if (IGNORE_HEADERS.contains(name.toUpperCase())) { + if (IGNORE_HEADERS.contains(name.toUpperCase(Locale.ENGLISH))) { continue; } @@ -95,7 +96,7 @@ public void importTrustedHeaders(Enumeration
en) throws AcmeInvalidMessa while (en.hasMoreElements()) { var h = en.nextElement(); var name = h.getName(); - if (IGNORE_HEADERS.contains(name.toUpperCase())) { + if (IGNORE_HEADERS.contains(name.toUpperCase(Locale.ENGLISH))) { continue; } @@ -122,7 +123,7 @@ public void importTrustedHeadersRelaxed(Enumeration
en) { while (en.hasMoreElements()) { var h = en.nextElement(); var name = h.getName(); - if (IGNORE_HEADERS.contains(name.toUpperCase())) { + if (IGNORE_HEADERS.contains(name.toUpperCase(Locale.ENGLISH))) { continue; } @@ -252,7 +253,7 @@ public boolean isAutoSubmitted() { .filter(mh -> "AUTO-SUBMITTED".equalsIgnoreCase(mh.name)) .map(mh -> mh.value) .map(String::trim) - .map(String::toLowerCase) + .map(mh -> mh.toLowerCase(Locale.ENGLISH)) .anyMatch(h -> h.equals("auto-generated") || h.startsWith("auto-generated;")); } @@ -266,7 +267,7 @@ public Set getMissingSecuredHeaders() { headers.stream() .filter(mh -> mh.trusted) .map(mh -> mh.name) - .map(String::toUpperCase) + .map(mh -> mh.toUpperCase(Locale.ENGLISH)) .forEach(missing::remove); return missing; } diff --git a/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SimpleMail.java b/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SimpleMail.java index 995c54d0..7461c885 100644 --- a/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SimpleMail.java +++ b/acme4j-smime/src/main/java/org/shredzone/acme4j/smime/wrapper/SimpleMail.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Locale; import java.util.Optional; import jakarta.mail.Message; @@ -136,7 +137,7 @@ public boolean isAutoSubmitted() throws AcmeInvalidMessageException { } return Arrays.stream(autoSubmitted) .map(String::trim) - .map(String::toLowerCase) + .map(as -> as.toLowerCase(Locale.ENGLISH)) .anyMatch(h -> h.equals("auto-generated") || h.startsWith("auto-generated;")); } catch (MessagingException ex) { throw new AcmeInvalidMessageException("Could not read '" + HEADER_AUTO_SUBMITTED + "' header", ex);