Compare commits
2
Commits
736d14d621
...
7aedb10eed
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7aedb10eed | ||
|
|
aeac897805 |
No files matched your search
+47
-6
@@ -30,6 +30,12 @@ var (
|
|||||||
"storage URL must not carry credentials; " +
|
"storage URL must not carry credentials; " +
|
||||||
"set s3.access_key_id and s3.secret_access_key in the config instead")
|
"set s3.access_key_id and s3.secret_access_key in the config instead")
|
||||||
ErrURLUnknownParam = errors.New("unknown query parameter in storage URL")
|
ErrURLUnknownParam = errors.New("unknown query parameter in storage URL")
|
||||||
|
ErrURLBadQuery = errors.New(
|
||||||
|
"storage URL query cannot be decoded; " +
|
||||||
|
"check it for a ; or a % not followed by two hex digits")
|
||||||
|
ErrRcloneRemoteParams = errors.New(
|
||||||
|
"rclone URL must not carry parameters in the remote name; " +
|
||||||
|
"set them on the remote with rclone config instead")
|
||||||
)
|
)
|
||||||
|
|
||||||
// URL represents a parsed storage URL.
|
// URL represents a parsed storage URL.
|
||||||
@@ -90,8 +96,9 @@ func parseFileURL(rawPath string) (*URL, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseS3URL parses an s3://bucket/prefix URL. It rejects credentials in
|
// parseS3URL parses an s3://bucket/prefix URL. It rejects credentials in
|
||||||
// the userinfo and any query parameter other than endpoint, region and
|
// the userinfo or in the endpoint parameter's userinfo, a query that
|
||||||
// ssl, so a credential-bearing URL is never stored or echoed.
|
// cannot be decoded, and any query parameter other than endpoint, region
|
||||||
|
// and ssl, so a credential-bearing URL is never stored or echoed.
|
||||||
func parseS3URL(rawURL string) (*URL, error) {
|
func parseS3URL(rawURL string) (*URL, error) {
|
||||||
u, err := url.Parse(rawURL)
|
u, err := url.Parse(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -107,13 +114,26 @@ func parseS3URL(rawURL string) (*URL, error) {
|
|||||||
return nil, ErrMissingBucket
|
return nil, ErrMissingBucket
|
||||||
}
|
}
|
||||||
|
|
||||||
query := u.Query()
|
// u.Query() would silently drop a part it cannot decode, endpoint
|
||||||
|
// included. The decode error is not wrapped: it can quote the value.
|
||||||
|
query, err := url.ParseQuery(u.RawQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrURLBadQuery
|
||||||
|
}
|
||||||
|
|
||||||
err = rejectUnknownParams(query, "endpoint", "region", "ssl")
|
err = rejectUnknownParams(query, "endpoint", "region", "ssl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A host name never contains "@", so one in an endpoint, with or
|
||||||
|
// without a scheme, can only introduce userinfo.
|
||||||
|
for _, endpoint := range query["endpoint"] {
|
||||||
|
if strings.Contains(endpoint, "@") {
|
||||||
|
return nil, ErrURLCredentials
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &URL{
|
return &URL{
|
||||||
Scheme: schemeS3,
|
Scheme: schemeS3,
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
@@ -125,9 +145,23 @@ func parseS3URL(rawURL string) (*URL, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseRcloneURL parses an rclone://remote/path URL. rclone:// takes no
|
// parseRcloneURL parses an rclone://remote/path URL. rclone:// takes no
|
||||||
// query parameters, so credentials in the userinfo and any parameter at
|
// query parameters, so credentials in the userinfo, parameters in the
|
||||||
|
// remote name, a query that cannot be decoded and any query parameter at
|
||||||
// all are rejected rather than silently ignored.
|
// all are rejected rather than silently ignored.
|
||||||
func parseRcloneURL(rawURL string) (*URL, error) {
|
func parseRcloneURL(rawURL string) (*URL, error) {
|
||||||
|
// The remote name runs up to the first "/", "?" or "#". It never
|
||||||
|
// contains a comma; one starts rclone's remote,param=value form,
|
||||||
|
// whose values can be credentials. This is checked before url.Parse,
|
||||||
|
// whose error for such a name can print part of it.
|
||||||
|
remote := strings.TrimPrefix(rawURL, "rclone://")
|
||||||
|
if end := strings.IndexAny(remote, "/?#"); end >= 0 {
|
||||||
|
remote = remote[:end]
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(remote, ",") {
|
||||||
|
return nil, ErrRcloneRemoteParams
|
||||||
|
}
|
||||||
|
|
||||||
u, err := url.Parse(rawURL)
|
u, err := url.Parse(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, wrapParseError(err)
|
return nil, wrapParseError(err)
|
||||||
@@ -137,12 +171,19 @@ func parseRcloneURL(rawURL string) (*URL, error) {
|
|||||||
return nil, ErrURLCredentials
|
return nil, ErrURLCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
remote := u.Host
|
remote = u.Host
|
||||||
if remote == "" {
|
if remote == "" {
|
||||||
return nil, ErrMissingRemote
|
return nil, ErrMissingRemote
|
||||||
}
|
}
|
||||||
|
|
||||||
err = rejectUnknownParams(u.Query())
|
// As in parseS3URL: u.Query() would drop a part it cannot decode, and
|
||||||
|
// the decode error can quote the value.
|
||||||
|
query, err := url.ParseQuery(u.RawQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ErrURLBadQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rejectUnknownParams(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,6 +150,19 @@ func TestParseStorageURLRejectsCredentials(t *testing.T) {
|
|||||||
wantErr: storage.ErrURLCredentials,
|
wantErr: storage.ErrURLCredentials,
|
||||||
secrets: []string{key, secret, "mybucket"},
|
secrets: []string{key, secret, "mybucket"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "s3 userinfo in endpoint",
|
||||||
|
raw: "s3://mybucket?endpoint=https://" +
|
||||||
|
key + ":" + secret + "@minio.example.com",
|
||||||
|
wantErr: storage.ErrURLCredentials,
|
||||||
|
secrets: []string{key, secret},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "s3 userinfo in endpoint without scheme",
|
||||||
|
raw: "s3://mybucket?endpoint=" + key + ":" + secret + "@minio.example.com",
|
||||||
|
wantErr: storage.ErrURLCredentials,
|
||||||
|
secrets: []string{key, secret},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "s3 unknown query param",
|
name: "s3 unknown query param",
|
||||||
raw: "s3://mybucket?access_key=" + key + "&secret=" + secret,
|
raw: "s3://mybucket?access_key=" + key + "&secret=" + secret,
|
||||||
@@ -203,6 +216,112 @@ func TestParseStorageURLRejectsCredentials(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestParseStorageURLRejectsRcloneRemoteParams checks that an rclone
|
||||||
|
// remote name carrying parameters (rclone's "remote,param=value" form,
|
||||||
|
// which can hold a credential) is rejected without echoing the value,
|
||||||
|
// including the URLs that url.Parse itself refuses.
|
||||||
|
func TestParseStorageURLRejectsRcloneRemoteParams(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
raw string
|
||||||
|
secrets []string // must not appear in the error message
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "configured remote",
|
||||||
|
raw: "rclone://gdrive,client_secret=topsecret/backups",
|
||||||
|
secrets: []string{"topsecret"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "remote defined inline",
|
||||||
|
raw: "rclone://:s3,access_key_id=KEY,secret_access_key=SECRET/bucket",
|
||||||
|
secrets: []string{"KEY", "SECRET"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "colon in a parameter value",
|
||||||
|
raw: "rclone://gdrive,client_secret=ab:cd/backups",
|
||||||
|
secrets: []string{"cd"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
_, err := storage.ParseStorageURL(tc.raw)
|
||||||
|
if !errors.Is(err, storage.ErrRcloneRemoteParams) {
|
||||||
|
t.Fatalf("ParseStorageURL(%q) error = %v, want %v",
|
||||||
|
tc.raw, err, storage.ErrRcloneRemoteParams)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, secret := range tc.secrets {
|
||||||
|
if strings.Contains(err.Error(), secret) {
|
||||||
|
t.Errorf("error message leaked %q: %v", secret, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestParseStorageURLRejectsUndecodableQuery checks that an s3 or rclone
|
||||||
|
// URL whose query cannot be decoded is rejected, not read with that part
|
||||||
|
// of the query left out, and that the error does not echo it.
|
||||||
|
func TestParseStorageURLRejectsUndecodableQuery(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const (
|
||||||
|
key = "AKIAKEY"
|
||||||
|
secret = "topsecret"
|
||||||
|
)
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
raw string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "semicolon",
|
||||||
|
raw: "s3://mybucket/prefix?endpoint=https://" + key + ":" + secret +
|
||||||
|
"@minio.example.com:9000;region=us-east-1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad percent escape",
|
||||||
|
raw: "s3://mybucket/prefix?endpoint=https://" + key + ":" + secret +
|
||||||
|
"%zz@minio.example.com:9000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rclone semicolon",
|
||||||
|
raw: "rclone://gdrive/backups?token=" + secret + ";x=1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rclone bad percent escape",
|
||||||
|
raw: "rclone://gdrive/backups?token=" + secret + "%zz",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
_, err := storage.ParseStorageURL(tc.raw)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("ParseStorageURL(%q) returned no error", tc.raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !errors.Is(err, storage.ErrURLBadQuery) {
|
||||||
|
t.Errorf("ParseStorageURL(%q) error = %v, want %v",
|
||||||
|
tc.raw, err, storage.ErrURLBadQuery)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range []string{key, secret, "minio.example.com"} {
|
||||||
|
if strings.Contains(err.Error(), s) {
|
||||||
|
t.Errorf("error message leaked %q: %v", s, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestParseStorageURLParseFailureHidesURL checks that when url.Parse
|
// TestParseStorageURLParseFailureHidesURL checks that when url.Parse
|
||||||
// itself fails, the wrapped error carries only the inner cause, not the
|
// itself fails, the wrapped error carries only the inner cause, not the
|
||||||
// *url.Error whose text embeds the raw (possibly credential-bearing) URL.
|
// *url.Error whose text embeds the raw (possibly credential-bearing) URL.
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
@@ -219,9 +220,7 @@ func (v *Vaultik) finishRestore(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if os.Geteuid() != 0 {
|
if os.Geteuid() != 0 {
|
||||||
v.UI.Warningf("Restore did not preserve file ownership: chown(2) " +
|
v.warnIfOwnershipNotPreserved(files, opts.TargetDir)
|
||||||
"requires root. Re-run as root (e.g. with sudo) if you need " +
|
|
||||||
"original UID/GID preserved.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.FilesFailed > 0 {
|
if result.FilesFailed > 0 {
|
||||||
@@ -247,6 +246,43 @@ func (v *Vaultik) finishRestore(
|
|||||||
return unmatchedErr
|
return unmatchedErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// warnIfOwnershipNotPreserved warns once if some entry restored under
|
||||||
|
// targetDir has another owner or group on disk than the snapshot
|
||||||
|
// records. It is meant for a restore by a normal user, which changes no
|
||||||
|
// owner or group: a new entry is owned by that user, and its group is
|
||||||
|
// that user's, or the group of the directory it is created in on macOS
|
||||||
|
// and in a setgid directory on Linux. An entry that is not on disk, or
|
||||||
|
// whose filesystem reports no owner, is skipped.
|
||||||
|
func (v *Vaultik) warnIfOwnershipNotPreserved(
|
||||||
|
files []*database.File, targetDir string,
|
||||||
|
) {
|
||||||
|
for _, file := range files {
|
||||||
|
targetPath, err := containedRestorePath(
|
||||||
|
v.Fs, targetDir, file.Path.String())
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := lstatIfPossible(v.Fs, targetPath)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
owner, ok := info.Sys().(*syscall.Stat_t)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if owner.Uid != file.UID || owner.Gid != file.GID {
|
||||||
|
v.UI.Warningf("Restore did not preserve file ownership: chown(2) " +
|
||||||
|
"requires root. Re-run as root (e.g. with sudo) if you need " +
|
||||||
|
"original UID/GID preserved.")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// reportUnmatchedPaths warns, by name, about each requested path that
|
// reportUnmatchedPaths warns, by name, about each requested path that
|
||||||
// matched nothing in the snapshot, and returns an error counting them,
|
// matched nothing in the snapshot, and returns an error counting them,
|
||||||
// or nil when there are none.
|
// or nil when there are none.
|
||||||
@@ -965,8 +1001,9 @@ type restoreSession struct {
|
|||||||
// root can chown a file to an arbitrary UID/GID — non-root chown
|
// root can chown a file to an arbitrary UID/GID — non-root chown
|
||||||
// always fails with EPERM. Attempting it anyway produces N
|
// always fails with EPERM. Attempting it anyway produces N
|
||||||
// guaranteed-failed syscalls + N noisy debug lines, so we skip
|
// guaranteed-failed syscalls + N noisy debug lines, so we skip
|
||||||
// the call entirely as non-root and emit one warning at the end
|
// the call entirely as non-root; warnIfOwnershipNotPreserved warns
|
||||||
// of the restore explaining that ownership was not preserved.
|
// once at the end of the restore if that left any owner or group
|
||||||
|
// other than recorded.
|
||||||
runningAsRoot bool
|
runningAsRoot bool
|
||||||
// directories holds every restored directory, for
|
// directories holds every restored directory, for
|
||||||
// applyDirectoryMetadata to finish after the restore loop.
|
// applyDirectoryMetadata to finish after the restore loop.
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
package vaultik //nolint:testpackage // calls the unexported ownership check
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/vaultik/internal/database"
|
||||||
|
"sneak.berlin/go/vaultik/internal/log"
|
||||||
|
"sneak.berlin/go/vaultik/internal/ui"
|
||||||
|
)
|
||||||
|
|
||||||
|
// These tests cover https://git.eeqj.de/sneak/vaultik/issues/316: a
|
||||||
|
// restore by a normal user warns that ownership was not preserved only
|
||||||
|
// when some restored entry has another owner or group on disk than the
|
||||||
|
// snapshot records.
|
||||||
|
|
||||||
|
const ownershipWarning = "did not preserve file ownership"
|
||||||
|
|
||||||
|
// TestOwnershipWarningSilentWhenEntriesMatch checks that a restore whose
|
||||||
|
// entries all end up with their recorded owner and group prints no
|
||||||
|
// warning.
|
||||||
|
func TestOwnershipWarningSilentWhenEntriesMatch(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
targetDir := t.TempDir()
|
||||||
|
owner := ownerOf(t, targetDir)
|
||||||
|
|
||||||
|
out := restoreAsNormalUser(t, targetDir, []*database.File{
|
||||||
|
{Path: "/a", Mode: plainFileMode, UID: owner.Uid, GID: owner.Gid},
|
||||||
|
{Path: "/b", Mode: plainFileMode, UID: owner.Uid, GID: owner.Gid},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Empty(t, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOwnershipWarningWhenDirectoryGivesItsGroup checks that the warning
|
||||||
|
// is printed, once, when entries recorded with the restoring user's
|
||||||
|
// owner and group take another group from the setgid directory they are
|
||||||
|
// restored into, as every new entry takes its directory's group on
|
||||||
|
// macOS.
|
||||||
|
func TestOwnershipWarningWhenDirectoryGivesItsGroup(t *testing.T) {
|
||||||
|
log.Initialize(log.Config{})
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
t.Skip("giving a directory to another group needs root")
|
||||||
|
}
|
||||||
|
|
||||||
|
targetDir := t.TempDir()
|
||||||
|
user := ownerOf(t, targetDir)
|
||||||
|
|
||||||
|
require.NoError(t, os.Chown(targetDir, -1, int(otherOwnerID)))
|
||||||
|
require.NoError(t, os.Chmod(targetDir, os.ModeSetgid|writableTestMode))
|
||||||
|
|
||||||
|
out := restoreAsNormalUser(t, targetDir, []*database.File{
|
||||||
|
{Path: "/a", Mode: plainFileMode, UID: user.Uid, GID: user.Gid},
|
||||||
|
{Path: "/b", Mode: plainFileMode, UID: user.Uid, GID: user.Gid},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, 1, strings.Count(out, ownershipWarning), out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ownerOf returns the owner and group path has on disk.
|
||||||
|
func ownerOf(t *testing.T, path string) *syscall.Stat_t {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
info, err := os.Lstat(path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
owner, ok := info.Sys().(*syscall.Stat_t)
|
||||||
|
require.True(t, ok)
|
||||||
|
|
||||||
|
return owner
|
||||||
|
}
|
||||||
|
|
||||||
|
// restoreAsNormalUser restores rows, all empty regular files, into
|
||||||
|
// targetDir without changing any owner or group, as restore does for a
|
||||||
|
// normal user, and returns what warnIfOwnershipNotPreserved prints.
|
||||||
|
func restoreAsNormalUser(
|
||||||
|
t *testing.T, targetDir string, rows []*database.File,
|
||||||
|
) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
rows, repos := makeFiles(ctx, t, rows)
|
||||||
|
|
||||||
|
var out bytes.Buffer
|
||||||
|
|
||||||
|
v := newContainmentVaultik(ctx, afero.NewOsFs())
|
||||||
|
v.UI = ui.NewWithColor(&out, false)
|
||||||
|
|
||||||
|
session := &restoreSession{
|
||||||
|
v: v,
|
||||||
|
ctx: ctx,
|
||||||
|
repos: repos,
|
||||||
|
opts: &RestoreOptions{TargetDir: targetDir},
|
||||||
|
result: &RestoreResult{},
|
||||||
|
}
|
||||||
|
for _, row := range rows {
|
||||||
|
require.NoError(t, session.restoreFile(row))
|
||||||
|
}
|
||||||
|
|
||||||
|
v.warnIfOwnershipNotPreserved(rows, targetDir)
|
||||||
|
|
||||||
|
return out.String()
|
||||||
|
}
|
||||||
Reference in new issue
Block a user