This is an automated email from the ASF dual-hosted git repository. markt-asf pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit c4dcdb45cfafee4514e3a632887bd7a630f14bea Author: Mark Thomas <[email protected]> AuthorDate: Tue Dec 1 15:11:45 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 | 114 +++++++++++++++++++++ 2 files changed, 126 insertions(+), 5 deletions(-) diff --git a/java/org/apache/tomcat/buildutil/translate/Utils.java b/java/org/apache/tomcat/buildutil/translate/Utils.java index e851a81bba..7c1fcd99ea 100644 --- a/java/org/apache/tomcat/buildutil/translate/Utils.java +++ b/java/org/apache/tomcat/buildutil/translate/Utils.java @@ -147,13 +147,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 37122842ec..bbf1a93b63 100644 --- a/test/org/apache/tomcat/buildutil/translate/TestUtils.java +++ b/test/org/apache/tomcat/buildutil/translate/TestUtils.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; import org.junit.Assert; @@ -68,6 +70,118 @@ public class TestUtils { Assert.assertEquals("line1\\n\\\n\\tline2\\n\\\n\\tline3", Utils.formatValueExport("line1\n\tline2\n\tline3")); } + @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.formatValueExport("a\\b")); + 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.formatValueExport("\\uXXXX")); + 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.formatValueExport("a\\b\n\tc")); + Assert.assertEquals("\\ a\\\\", Utils.formatValueExport(" 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) { + doTestRoundTrip(value); + } + } + + @Test + public void testRoundTripExistingStrings() throws IOException { + // Every string in Tomcat's property files must survive being exported, loaded, imported and loaded again + File root = new File("."); + Map<String,Properties> translations = new HashMap<>(); + for (String dir : Constants.SEARCH_DIRS) { + Utils.processDirectory(root, new File(dir), translations); + } + Assert.assertFalse(translations.isEmpty()); + + File exportDir = temp.newFolder(); + for (Map.Entry<String,Properties> entry : translations.entrySet()) { + String language = entry.getKey(); + Properties original = entry.getValue(); + + Utils.export(language, original, exportDir); + Properties exported = + Utils.load(new File(exportDir, Constants.L10N_PREFIX + language + Constants.L10N_SUFFIX)); + + // What Tomcat's files would contain after import + File imported = temp.newFile(); + StringBuilder content = new StringBuilder(); + for (Object key : exported.keySet()) { + content.append(key).append('=').append(Utils.formatValueImport(exported.getProperty((String) key))) + .append('\n'); + } + Files.write(imported.toPath(), content.toString().getBytes(StandardCharsets.UTF_8)); + Properties reloaded = Utils.load(imported); + + Assert.assertEquals(language, original.keySet(), reloaded.keySet()); + for (Object key : original.keySet()) { + Assert.assertEquals(language + " : " + key, original.getProperty((String) key), + reloaded.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)); + } + + /* + * Follows a value from Tomcat to POEditor and back using the same code as the export and import tools. The value is + * written to the export file, the export file is loaded (as it is when importing), the value is formatted for + * writing to one of Tomcat's files and, finally, that file is loaded. + */ + private void doTestRoundTrip(String value) throws IOException { + Properties translation = new Properties(); + translation.setProperty("key", value); + + File exportDir = temp.newFolder(); + Utils.export("", translation, exportDir); + Properties exported = Utils.load(new File(exportDir, Constants.L10N_PREFIX + Constants.L10N_SUFFIX)); + + File imported = temp.newFile(); + String line = "key=" + Utils.formatValueImport(exported.getProperty("key")) + "\n"; + Files.write(imported.toPath(), line.getBytes(StandardCharsets.UTF_8)); + + Assert.assertEquals(value, Utils.load(imported).getProperty("key")); + } + @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]
