Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1453] Canonicalize properties files #77

Closed
wants to merge 15 commits into from
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
<version>${slf4jVersion}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.12.0</version>
</dependency>

<!--
Plexus dependencies
-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
*/
package org.apache.maven.shared.archiver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
Expand Down Expand Up @@ -61,36 +60,51 @@ private boolean sameContents(Properties props, Path file) throws IOException {
return fileProps.equals(props);
}

private void createPropertiesFile(Properties properties, Path outputFile, boolean forceCreation)
private void createPropertiesFile(Properties unsortedProperties, Path outputFile, boolean forceCreation)
throws IOException {
Path outputDir = outputFile.getParent();
if (outputDir != null && !Files.isDirectory(outputDir)) {
Files.createDirectories(outputDir);
}
if (!forceCreation && sameContents(properties, outputFile)) {
if (!forceCreation && sameContents(unsortedProperties, outputFile)) {
return;
}

try (PrintWriter pw = new PrintWriter(outputFile.toFile(), StandardCharsets.ISO_8859_1.name());
StringWriter sw = new StringWriter()) {

properties.store(sw, null);

List<String> lines = new ArrayList<>();
try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) {
String line;
while ((line = r.readLine()) != null) {
if (!line.startsWith("#")) {
lines.add(line);
}
}
// For reproducible builds, sort the properties and drop comments.
// The java.util.Properties class doesn't guarantee order so we have
// to write the file using a Writer.
Set<String> propertyNames = unsortedProperties.stringPropertyNames();
List<String> sortedPropertyNames = new ArrayList<>(propertyNames);
Collections.sort(sortedPropertyNames);

try (Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) {
for (String key : sortedPropertyNames) {
out.write(escape(key));
out.write("=");
out.write(escape(unsortedProperties.getProperty(key)));
out.write('\n');
}
}
}

Collections.sort(lines);
for (String l : lines) {
pw.println(l);
private static String escape(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (char c : s.toCharArray()) {
if (Character.isWhitespace(c) || c == '#' || c == '!' || c == '=' || c == ':') { // backslash escape
sb.append('\\');
sb.append(c);
} else if (c < 256) { // 8859-1
Copy link
Contributor

@gnodet gnodet Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,. this one I don't see. What's wrong here?

Copy link
Contributor

@gnodet gnodet Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following test succeeds:

        Properties p = new Properties();
        p.put("foo", "aéüb");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        p.store(baos, null);
        String s = baos.toString();
        assertTrue(s.contains("foo=a\\u00E9\\u00FCb"));

So non plain ascii chars should be unicode encoded.

sb.append(c);
} else {
sb.append("\\u");
String hexString = Integer.toHexString(c).toUpperCase(Locale.ENGLISH);
while (hexString.length() < 4) {
hexString = '0' + hexString;
}
sb.append(hexString);
}
}
return sb.toString();
}

/**
Expand Down Expand Up @@ -129,6 +143,7 @@ public void createPomProperties(

// CHECKSTYLE_OFF: ParameterNumber
public void createPomProperties(
// TODO the session isn't needed here
Session session,
String groupId,
String artifactId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.maven.shared.archiver;

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;

import org.apache.maven.api.Session;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class PomPropertiesUtilTest {

private PomPropertiesUtil util = new PomPropertiesUtil();

@TempDir
Path tempDirectory;

@Test
void testCreatePomProperties() throws IOException {
Path pomPropertiesFile = tempDirectory.resolve("bar.properties");
util.createPomProperties(
(Session) null, "org.foo", "bar", "2.1.5", new JarArchiver(), null, pomPropertiesFile, true);

assertThat(pomPropertiesFile).exists();
Properties actual = new Properties();
actual.load(Files.newInputStream(pomPropertiesFile));
assertEquals("org.foo", actual.getProperty("groupId"));
assertEquals("bar", actual.getProperty("artifactId"));
assertEquals("2.1.5", actual.getProperty("version"));

// Now read the file directly to check for alphabetical order
List<String> contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1);
assertEquals("artifactId=bar", contents.get(0));
assertEquals("groupId=org.foo", contents.get(1));
assertEquals("version=2.1.5", contents.get(2));
assertEquals(3, contents.size());
}

@Test
void testUnicodeEscape() throws IOException {
Path pomPropertiesFile = tempDirectory.resolve("bar.properties");
util.createPomProperties(
(Session) null, "org.foo", "こんにちは", "2.1.5", new JarArchiver(), null, pomPropertiesFile, true);

assertThat(pomPropertiesFile).exists();
Properties actual = new Properties();
actual.load(Files.newInputStream(pomPropertiesFile));
assertEquals("org.foo", actual.getProperty("groupId"));
assertEquals("こんにちは", actual.getProperty("artifactId"));
assertEquals("2.1.5", actual.getProperty("version"));

// Now read the file directly to check for alphabetical order and encoding
List<String> contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1);
assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(0));
assertEquals("groupId=org.foo", contents.get(1));
assertEquals("version=2.1.5", contents.get(2));
assertEquals(3, contents.size());
}

@Test
void testWhitespaceEscape() throws IOException {
Path pomPropertiesFile = tempDirectory.resolve("bar.properties");
Path customPomPropertiesFile = tempDirectory.resolve("custom.properties");
try (Writer out = Files.newBufferedWriter(customPomPropertiesFile, StandardCharsets.ISO_8859_1)) {
out.write("a\\u0020key\\u0020with\\u0009whitespace=value\\u0020with\\u0009whitespace\n");
}

util.createPomProperties(
(Session) null,
"org.foo",
"こんにちは",
"2.1.5",
new JarArchiver(),
customPomPropertiesFile,
pomPropertiesFile,
true);
assertThat(pomPropertiesFile).exists();

Properties actual = new Properties();
actual.load(Files.newInputStream(pomPropertiesFile));
assertEquals("value with\twhitespace", actual.getProperty("a key with\twhitespace"));

// Now read the file directly to check for alphabetical order and encoding
List<String> contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1);
assertEquals(4, contents.size());
assertEquals("a\\ key\\ with\\\twhitespace=value\\ with\\\twhitespace", contents.get(0));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test is wrong afaik. The mechanism is different for keys and values

Then every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(1));
assertEquals("groupId=org.foo", contents.get(2));
assertEquals("version=2.1.5", contents.get(3));
}
}
Loading