Skip to content

Commit d518e26

Browse files
committed
[JENKINS-73305] Create .ssh directory with owner only permissions
When the JGit implementation needs to create a `.ssh` directory, create it with permissions only allowing access to the directory owner. That is the common pattern used by the OpenSSH project and by POSIX systems to reduce access to the sensitive information stored in the directory. Testing done Ran the CredentialsTest in a debugger with a configured 'auth-data` directory and confirmed that the modified lines are executed on my RHEL 8 development computer. Confirmed that the resulting directory permissions were read, write, and execute for only the owner, with no other permissions.
1 parent f988d25 commit d518e26

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import java.nio.file.Files;
4343
import java.nio.file.Path;
4444
import java.nio.file.Paths;
45+
import java.nio.file.attribute.FileAttribute;
46+
import java.nio.file.attribute.PosixFilePermission;
47+
import java.nio.file.attribute.PosixFilePermissions;
4548
import java.security.GeneralSecurityException;
4649
import java.util.ArrayList;
4750
import java.util.Arrays;
@@ -201,10 +204,22 @@ public class JGitAPIImpl extends LegacyCompatibleGitAPIImpl {
201204
public SshdSessionFactory buildSshdSessionFactory(@NonNull final HostKeyVerifierFactory hostKeyVerifierFactory) {
202205
if (Files.notExists(hostKeyVerifierFactory.getKnownHostsFile().toPath())) {
203206
try {
204-
Files.createDirectories(hostKeyVerifierFactory
205-
.getKnownHostsFile()
206-
.getParentFile()
207-
.toPath());
207+
if (isWindows()) {
208+
Files.createDirectories(hostKeyVerifierFactory
209+
.getKnownHostsFile()
210+
.getParentFile()
211+
.toPath());
212+
} else {
213+
Set<PosixFilePermission> ownerOnly = PosixFilePermissions.fromString("rwx------");
214+
FileAttribute<Set<PosixFilePermission>> fileAttribute =
215+
PosixFilePermissions.asFileAttribute(ownerOnly);
216+
Files.createDirectories(
217+
hostKeyVerifierFactory
218+
.getKnownHostsFile()
219+
.getParentFile()
220+
.toPath(),
221+
fileAttribute);
222+
}
208223
Files.createFile(hostKeyVerifierFactory.getKnownHostsFile().toPath());
209224
} catch (IOException e) {
210225
LOGGER.log(Level.SEVERE, "could not create known hosts file", e);
@@ -3231,4 +3246,9 @@ public void close() {
32313246
}
32323247
}
32333248
}
3249+
3250+
/** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */
3251+
private static boolean isWindows() {
3252+
return File.pathSeparatorChar == ';';
3253+
}
32343254
}

0 commit comments

Comments
 (0)