This is an automated email from the ASF dual-hosted git repository.
bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new dd8ff6c130 Saving hop-config.json resets its permissions to 600, fixes
#9418 (#9419)
dd8ff6c130 is described below
commit dd8ff6c1300ff3241fdd0f8227aee1f65432ec71
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Fri Nov 13 16:40:41 2026 +0100
Saving hop-config.json resets its permissions to 600, fixes #9418 (#9419)
* Saving hop-config.json resets its permissions to 600, fixes #9418
* fix remarks
Set the permissions of the temporary file before writing the content, so a
600 configuration is never readable under the umask, not even after a save
that dies before the move.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../hop/core/config/ConfigFileSerializer.java | 25 ++++++++++++-
.../hop/core/config/ConfigFileSerializerTest.java | 43 ++++++++++++++++++++++
2 files changed, 67 insertions(+), 1 deletion(-)
diff --git
a/core/src/main/java/org/apache/hop/core/config/ConfigFileSerializer.java
b/core/src/main/java/org/apache/hop/core/config/ConfigFileSerializer.java
index 15987135a1..7c153ade1b 100644
--- a/core/src/main/java/org/apache/hop/core/config/ConfigFileSerializer.java
+++ b/core/src/main/java/org/apache/hop/core/config/ConfigFileSerializer.java
@@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
@@ -80,8 +81,14 @@ public class ConfigFileSerializer implements
IHopConfigSerializer {
Files.createDirectories(folder);
}
// In the same folder as the file itself: a move is only atomic within one
file store.
- Path temporary = Files.createTempFile(folder,
file.getFileName().toString() + ".", ".tmp");
+ // Not Files.createTempFile: that always creates the file as rw-------,
and the move would
+ // hand those permissions to the configuration, locking out every other
user sharing it.
+ Path temporary =
+ Files.createFile(folder.resolve(file.getFileName() + "." +
UUID.randomUUID() + ".tmp"));
try {
+ // Permissions before content: a 600 configuration must never sit
readable under the umask,
+ // not even for the moment between the write and the move, or after a
save that dies there.
+ keepPermissions(file, temporary);
Files.write(temporary, content);
try {
Files.move(
@@ -97,6 +104,22 @@ public class ConfigFileSerializer implements
IHopConfigSerializer {
}
}
+ /**
+ * Give the new file the permissions of the configuration it replaces, so a
file an administrator
+ * made group-writable for several users stays that way whoever saves it. A
new configuration
+ * keeps the permissions the umask gave it.
+ */
+ private static void keepPermissions(Path file, Path temporary) throws
IOException {
+ if (!Files.exists(file)) {
+ return;
+ }
+ try {
+ Files.setPosixFilePermissions(temporary,
Files.getPosixFilePermissions(file));
+ } catch (UnsupportedOperationException notPosix) {
+ // Windows and other non-POSIX file stores: nothing to carry over.
+ }
+ }
+
/** The same, for a configuration that does not live on this machine. */
private void writeThroughVfs(String filename, byte[] content) throws
Exception {
FileObject temporary = HopVfs.getFileObject(filename + "." +
UUID.randomUUID() + ".tmp");
diff --git
a/core/src/test/java/org/apache/hop/core/config/ConfigFileSerializerTest.java
b/core/src/test/java/org/apache/hop/core/config/ConfigFileSerializerTest.java
index 7cbfda289b..37a74d88ae 100644
---
a/core/src/test/java/org/apache/hop/core/config/ConfigFileSerializerTest.java
+++
b/core/src/test/java/org/apache/hop/core/config/ConfigFileSerializerTest.java
@@ -20,14 +20,19 @@ package org.apache.hop.core.config;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.attribute.PosixFileAttributeView;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@@ -165,6 +170,44 @@ class ConfigFileSerializerTest {
}
}
+ /**
+ * Several users sharing one configuration folder rely on group access to
hop-config.json. Saving
+ * used to leave it rw------- for whoever saved last, locking everyone else
out.
+ */
+ @Test
+ @DisplayName("saving keeps the permissions of the existing file")
+ void keepsThePermissionsOfTheExistingFile() throws Exception {
+ assumePosix();
+ Path file = folder.resolve("hop-config.json");
+ Files.writeString(file, "{}");
+ Set<PosixFilePermission> shared =
PosixFilePermissions.fromString("rw-rw-r--");
+ Files.setPosixFilePermissions(file, shared);
+
+ serializer.writeToFile(file.toString(), Map.of("answer", "42"));
+ serializer.writeToFile(file.toString(), Map.of("answer", "43"));
+
+ assertEquals(shared, Files.getPosixFilePermissions(file));
+ assertEquals("43", serializer.readFromFile(file.toString()).get("answer"));
+ }
+
+ @Test
+ @DisplayName("a new file gets the permissions of a normally created file,
not owner-only")
+ void createsANewFileLikeAnyOtherFile() throws Exception {
+ assumePosix();
+ Path reference = Files.createFile(folder.resolve("reference.json"));
+ Path file = folder.resolve("hop-config.json");
+
+ serializer.writeToFile(file.toString(), Map.of("answer", "42"));
+
+ assertEquals(Files.getPosixFilePermissions(reference),
Files.getPosixFilePermissions(file));
+ }
+
+ private void assumePosix() {
+ assumeTrue(
+ Files.getFileAttributeView(folder, PosixFileAttributeView.class) !=
null,
+ "the file store has no POSIX permissions");
+ }
+
private List<String> filesInFolder() throws IOException {
try (Stream<Path> files = Files.list(folder)) {
return files.map(path ->
path.getFileName().toString()).sorted().toList();