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

[JENKINS-73305] Create .ssh directory with owner only permissions #1150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

MarkEWaite
Copy link
Contributor

@MarkEWaite MarkEWaite commented Jun 15, 2024

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.

The coverage report on the ci.jenkins.io job also shows that the newly added statements are executed by automated tests.

Checklist

  • I have read the CONTRIBUTING doc
  • I have referenced the Jira issue related to my changes in one or more commit messages
  • I have added tests that verify my changes
  • Unit tests pass locally with my changes
  • I have added documentation as necessary
  • No Javadoc warnings were introduced with my changes
  • No spotbugs warnings were introduced with my changes
  • I have interactively tested my changes

Types of changes

What types of changes does your code introduce?

  • Bug fix (non-breaking change which fixes an issue)

@MarkEWaite MarkEWaite requested a review from a team as a code owner June 15, 2024 14:51
@MarkEWaite MarkEWaite added the bug Incorrect or flawed behavior label Jun 15, 2024
@MarkEWaite MarkEWaite requested a review from jtnord June 15, 2024 14:51
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.
@MarkEWaite MarkEWaite force-pushed the create-ssh-dir-with-better-perms branch from d518e26 to a09e86f Compare June 16, 2024 13:57
.getKnownHostsFile()
.getParentFile()
.toPath());
if (isWindows()) {
Copy link
Member

Choose a reason for hiding this comment

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

Here be dragons.

Don't assume that POSIX file systems == non windows systems.

You can be on Linux and have a non POSIX FS (and the inverse).

And there was (and iirc) still is a bug where when you ask for the filesystem for a path you get the default even if it should be different!

This the only reliable way to do this is to actually try, catch the exception and do a fallback unless that big is fixed.

Set<PosixFilePermission> ownerOnly = PosixFilePermissions.fromString("rwx------");
FileAttribute<Set<PosixFilePermission>> fileAttribute =
PosixFilePermissions.asFileAttribute(ownerOnly);
Files.createDirectories(
Copy link
Member

Choose a reason for hiding this comment

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

I think you want createDirectory here (and possibly a different call to create any missing parents of this directory)?
(At least you don't want all directories up to the parent to be created with these perms do you?)

@MarkEWaite MarkEWaite force-pushed the create-ssh-dir-with-better-perms branch from 5069b72 to a09e86f Compare October 29, 2024 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect or flawed behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants