This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit d84b3a7f65f04ea60653f519750da90a7657eaa7
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Dec 1 15:13:55 2026 +0000

    Escape backslashes when exporting and importing translations
    
    A backslash in a property value was written out unchanged. When the value
    was read back, the backslash was treated as the start of an escape sequence.
    A value containing the text backslash-u-X-X-X-X, such as
    lockOutRealm.invalidEscape, could not be loaded at all so an export followed
    by an import failed.
    
    Escape backslashes, before any other escaping is added, when formatting a
    value for a properties file. Add tests that follow values, including every
    value in Tomcat's property files, through an export and an import.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 .../apache/tomcat/buildutil/translate/Utils.java   | 17 ++--
 .../tomcat/buildutil/translate/TestUtils.java      | 95 ++++++++++++++++++++++
 2 files changed, 107 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/tomcat/buildutil/translate/Utils.java 
b/java/org/apache/tomcat/buildutil/translate/Utils.java
index b2b2c47f4e..836c4e2912 100644
--- a/java/org/apache/tomcat/buildutil/translate/Utils.java
+++ b/java/org/apache/tomcat/buildutil/translate/Utils.java
@@ -121,13 +121,20 @@ public class Utils {
 
 
     /*
-     * Common formatting to convert a String value for storage as a value in a 
property file. Values that contain
-     * line-breaks need the line-break in the value to be replaced with the 
correct representation of a line-break in a
-     * property file. Leading space needs to be escaped with a '\' and 
horizontal tabs need to be converted to "\t".
-     * Backslashes are passed through unchanged.
+     * Common formatting to convert a String value for storage as a value in a 
property file. Backslashes need to be
+     * escaped with a '\' else they are read as the start of an escape 
sequence when the file is loaded. For example, a
+     * value that contains a backslash followed by 'u' and four characters 
that are not hex digits could not be loaded
+     * at all, and a backslash followed by 'n' would be read as a line-break. 
Values that contain line-breaks need the
+     * line-break in the value to be replaced with the correct representation 
of a line-break in a property file.
+     * Leading space needs to be escaped with a '\' and horizontal tabs need 
to be converted to "\t".
      */
     static String formatValueCommon(String in) {
-        String result = in.replace("\n", "\\n\\\n");
+        // Escape the backslashes that are part of the value. This must be 
done before the replacements below, which add
+        // backslashes that are part of the file syntax (\n, \t and the escape 
for a leading space) and must not be
+        // escaped.
+        String result = in.replace("\\", "\\\\");
+
+        result = result.replace("\n", "\\n\\\n");
         if (result.endsWith("\\n\\\n")) {
             result = result.substring(0, result.length() - 2);
         }
diff --git a/test/org/apache/tomcat/buildutil/translate/TestUtils.java 
b/test/org/apache/tomcat/buildutil/translate/TestUtils.java
index 275320728a..83116d8d96 100644
--- a/test/org/apache/tomcat/buildutil/translate/TestUtils.java
+++ b/test/org/apache/tomcat/buildutil/translate/TestUtils.java
@@ -20,7 +20,9 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Properties;
+import java.util.stream.Stream;
 
 import org.junit.Assert;
 import org.junit.Rule;
@@ -51,6 +53,99 @@ public class TestUtils {
     }
 
 
+    @Test
+    public void testFormatValueBackslash() {
+        // A backslash in a value must be escaped else it is read as the start 
of an escape sequence
+        Assert.assertEquals("a\\\\b", Utils.formatValueImport("a\\b"));
+    }
+
+    @Test
+    public void testFormatValueBackslashU() {
+        // A backslash followed by 'u' and four characters that are not hex 
digits is not a valid unicode escape. It
+        // must be escaped so it is read back as that text.
+        Assert.assertEquals("\\\\uXXXX", Utils.formatValueImport("\\uXXXX"));
+    }
+
+    @Test
+    public void testFormatValueBackslashAndOtherEscaping() {
+        // The backslashes added when escaping line-breaks, tabs and leading 
spaces must not be escaped again
+        Assert.assertEquals("a\\\\b\\n\\\n\\tc", 
Utils.formatValueImport("a\\b\n\tc"));
+        Assert.assertEquals("\\ a\\\\", Utils.formatValueImport(" a\\"));
+    }
+
+    @Test
+    public void testRoundTripValues() throws IOException {
+        String[] values = new String[] {
+                "plain", "\\", "\\\\", "\\uXXXX", "\\u0041", "\\u'{'X...X'}'",
+                "The supported escape sequences are \\\\, \\uXXXX and 
\\u'{'X...X'}'",
+                "C:\\temp\\new", "trailing\\", "\\\nafter", "back\\\nslash 
then newline",
+                " leading space", "\tleading tab", "line1\nline2", "\nstarts 
with newline",
+                "ends with newline\n", "mix \\ \n \t \\n \\t", "caf\u00e9 
\u4e2d\u6587" };
+        for (String value : values) {
+            // The value as POEditor provides it, formatted for one of 
Tomcat's files, must load as the same value
+            File f = temp.newFile();
+            Files.write(f.toPath(), ("key=" + Utils.formatValueImport(value) + 
"\n").getBytes(StandardCharsets.UTF_8));
+            Assert.assertEquals(value, Utils.load(f).getProperty("key"));
+        }
+    }
+
+    @Test
+    public void testRoundTripExistingStrings() throws IOException {
+        // Every string in Tomcat's property files must survive being 
formatted for a property file and loaded again
+        int files = 0;
+        for (String dir : new String[] { "java", "webapps" }) {
+            try (Stream<Path> paths = Files.walk(new File(dir).toPath())) {
+                for (Path path : (Iterable<Path>) 
paths.filter(Files::isRegularFile)::iterator) {
+                    String name = path.getFileName().toString();
+                    if (name.startsWith(Constants.L10N_PREFIX) && 
name.endsWith(Constants.L10N_SUFFIX)) {
+                        doTestRoundTripFile(path.toFile());
+                        files++;
+                    }
+                }
+            }
+        }
+        Assert.assertTrue(files > 0);
+    }
+
+    private void doTestRoundTripFile(File original) throws IOException {
+        Properties originalProps = Utils.load(original);
+
+        StringBuilder content = new StringBuilder();
+        for (Object key : originalProps.keySet()) {
+            
content.append(key).append('=').append(Utils.formatValueImport(originalProps.getProperty((String)
 key)))
+                    .append('\n');
+        }
+        File formatted = temp.newFile();
+        Files.write(formatted.toPath(), 
content.toString().getBytes(StandardCharsets.UTF_8));
+        Properties formattedProps = Utils.load(formatted);
+
+        Assert.assertEquals(original.getPath(), originalProps.keySet(), 
formattedProps.keySet());
+        for (Object key : originalProps.keySet()) {
+            Assert.assertEquals(original.getPath() + " : " + key, 
originalProps.getProperty((String) key),
+                    formattedProps.getProperty((String) key));
+        }
+    }
+
+    @Test
+    public void testImportReproducesExistingEscapedBackslashes() throws 
IOException {
+        // The value contains backslashes. Importing it must give the text 
that is in the file so that exporting and
+        // importing does not change the file.
+        File f = new 
File("java/org/apache/catalina/realm/LocalStrings.properties");
+        String key = "lockOutRealm.invalidEscape";
+        String prefix = key + "=";
+        String line = null;
+        for (String candidate : Files.readAllLines(f.toPath(), 
StandardCharsets.UTF_8)) {
+            if (candidate.startsWith(prefix)) {
+                line = candidate;
+            }
+        }
+        Assert.assertNotNull(line);
+
+        String value = Utils.load(f).getProperty(key);
+        Assert.assertTrue(value, value.contains("\\"));
+        Assert.assertEquals(line.substring(prefix.length()), 
Utils.formatValueImport(value));
+    }
+
     @Test
     public void testLoadValid() throws IOException {
         File f = temp.newFile("valid.properties");


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

Reply via email to