Skip to content

sftp: Use mode 0700 for repository directories#21817

Merged
MichaelEischer merged 1 commit into
restic:masterfrom
eyupcanakman:fix/sftp-dir-permissions
May 31, 2026
Merged

sftp: Use mode 0700 for repository directories#21817
MichaelEischer merged 1 commit into
restic:masterfrom
eyupcanakman:fix/sftp-dir-permissions

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

What does this PR change? What problem does it solve?

When a repository is initialized over SFTP, its directories were created with the SFTP server's default permissions, often 0755, instead of the 0700 used for local repositories.
The SFTP backend creates them with pkg/sftp's Mkdir and MkdirAll, which take no mode argument.
It now sets each created directory to 0700.

Was the change previously discussed in an issue or on the forum?

Closes #4447

Checklist

  • I have added tests for all code changes, see writing tests
  • I have added documentation for relevant changes (in the manual).
  • There's a new file in changelog/unreleased/ that describes the changes for our users (see template).
  • I'm done! This pull request is ready for review.

@eyupcanakman eyupcanakman force-pushed the fix/sftp-dir-permissions branch from a266449 to 33bea4a Compare May 20, 2026 18:39

@MichaelEischer MichaelEischer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR, I have two minor remarks. Besides that LGTM.

Comment thread internal/backend/sftp/sftp_test.go Outdated

// Remove a data subdirectory so that Save has to recreate it.
subdir := filepath.Join(cfg.Path, "data", "01")
rtest.OK(t, os.RemoveAll(subdir))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The repository is empty at that point such that os.Remove(...) should also be sufficient.

Comment thread internal/backend/sftp/sftp_test.go Outdated
t.Skip("sftp server binary not found")
}

cfg := sftp.Config{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please deduplicate the sftp.Config generation in the new and existing tests.

@MichaelEischer MichaelEischer self-assigned this May 29, 2026
The SFTP backend created repository directories with pkg/sftp's Mkdir
and MkdirAll, which take no mode argument, so the directories inherited
the SFTP server's umask instead of the 0700 used for local
repositories. Set the mode of each directory the backend creates.
@eyupcanakman eyupcanakman force-pushed the fix/sftp-dir-permissions branch from 33bea4a to 62cf574 Compare May 31, 2026 09:50
@eyupcanakman

Copy link
Copy Markdown
Contributor Author

Done, switched to os.Remove and pulled the shared config into a testConfig helper.

@jtru

jtru commented May 31, 2026

Copy link
Copy Markdown
Contributor

I'm not convinced that changing this default and set 0700 permissions "in stone" is a good idea. (Although I have to admit I was not aware of the 0700 default for local repos either, and don't think that to be a great choice as well ;))

Taking away the opportunity to have system-wide and user-set defaults via umask affect a number of use cases that might not be immediately obvious, but still worthwhile - such as having a dedicated user account with read-only access to all repo data be able to rsync/copy all repo data over to another host.

Since restic's threat model does allow for unauthorized parties to read repository content (because the attacker cannot learn anything about snapshot contents that way), I don't think the potential gain (keeping ciphertext inaccessible via DAC) is necessarily worth any potential breakage.

@MichaelEischer

Copy link
Copy Markdown
Member

I'm not convinced that changing this default and set 0700 permissions "in stone" is a good idea. (Although I have to admit I was not aware of the 0700 default for local repos either, and don't think that to be a great choice as well ;))

The sftp backend already enforces specific permissions on files, thus extending this to directories does not introduce new problems. Besides, there's a mechanism for group-accessible repositories for the local and sftp backend: https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#group-accessible-repositories

@MichaelEischer MichaelEischer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks!

@MichaelEischer MichaelEischer enabled auto-merge May 31, 2026 14:30
@MichaelEischer MichaelEischer merged commit 474c094 into restic:master May 31, 2026
21 of 23 checks passed
@jtru

jtru commented May 31, 2026

Copy link
Copy Markdown
Contributor

I'm not convinced that changing this default and set 0700 permissions "in stone" is a good idea. (Although I have to admit I was not aware of the 0700 default for local repos either, and don't think that to be a great choice as well ;))

The sftp backend already enforces specific permissions on files, thus extending this to directories does not introduce new problems. Besides, there's a mechanism for group-accessible repositories for the local and sftp backend: https://restic.readthedocs.io/en/stable/030_preparing_a_new_repo.html#group-accessible-repositories

Ah, thanks for the pointer - I was not aware of this feature/subtlety until now. After having learned these facts, I wholeheartedly support the suggested change ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect directory permissions after init via SFTP

3 participants