Skip to content

Commit 837919a

Browse files
authored
fscopy: derive copy direction from the source argument (gopasspw#3462)
binaryCopy rejected a filesystem-to-store copy whenever the destination secret name matched a same-named file in the working directory, e.g. "gopass fscopy test test" failed with "ambiguity detected. Only from or to can be a file". The over-eager guard keyed off both arguments looking like file paths, so a legitimate copy into a store entry that shares a basename with the source file was wrongly refused. Determine the direction from the source instead: if "from" is a real file on disk we copy into the store and treat "to" as a secret name, even when a same-named file exists in the current directory. fscopy does not support file-to-file copies, so once the source is known the destination side is unambiguous. The genuine guards are kept: copying between two existing secrets still tells the user to use cp, a source that lives inside the store still warns and requires --force, and an unknown source now returns a clear error instead of the misleading "unhandled case" message. Fixes gopasspw#3340 Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
1 parent ae9e8e2 commit 837919a

2 files changed

Lines changed: 96 additions & 9 deletions

File tree

‎internal/action/binary.go‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ func (s *binaryHandler) binaryCopy(ctx context.Context, cmd *cli.Command, from,
194194
return fmt.Errorf("usage: %s fs%s from to", cmd.Root().Name, op)
195195
}
196196

197+
// The direction is determined by the source: if "from" is a real file on
198+
// disk we copy from the filesystem into the store, otherwise we copy from
199+
// the store out to the filesystem. We deliberately key off "from" so that a
200+
// destination which happens to share a name with a file in the current
201+
// directory (e.g. "gopass fscopy test test") is still treated as a store
202+
// entry. fscopy does not support copying between two files, so once the
203+
// source is known the destination side is unambiguous. See #3340.
197204
switch {
198-
case isFilePath(from) && isFilePath(to):
199-
// copying from on file to another file is not supported.
200-
return fmt.Errorf("ambiguity detected. Only from or to can be a file. Use cp to copy between files")
201-
case s.Store.Exists(ctx, from) && s.Store.Exists(ctx, to):
202-
// copying from one secret to another secret is not supported.
203-
return fmt.Errorf("ambiguity detected. Either from or to must be a file. Use gopass cp to copy between secrets")
204-
case isFilePath(from) && !isFilePath(to):
205+
case isFilePath(from):
206+
// the source is a file on disk, so the destination is a store entry,
207+
// even if a same-named file happens to exist in the working directory.
205208
if s.isInStore(from) {
206209
out.Warningf(ctx, "Ambiguity detected. Source %q is in the store. Use --force if intended", from)
207210
if !cmd.Bool("force") {
@@ -210,7 +213,13 @@ func (s *binaryHandler) binaryCopy(ctx context.Context, cmd *cli.Command, from,
210213
}
211214

212215
return s.binaryCopyFromFileToStore(ctx, from, to, deleteSource)
213-
case !isFilePath(from):
216+
case s.Store.Exists(ctx, from) && s.Store.Exists(ctx, to):
217+
// the source is not a file but both names resolve to secrets, so we
218+
// cannot tell which one is meant to be the file. Copying from one
219+
// secret to another is not supported here; use cp instead.
220+
return fmt.Errorf("ambiguity detected. Either from or to must be a file. Use gopass cp to copy between secrets")
221+
case s.Store.Exists(ctx, from):
222+
// the source is a store entry, so the destination is a file on disk.
214223
if s.isInStore(to) {
215224
out.Warningf(ctx, "Ambiguity detected. Destination %q is in the store. Use --force if intended", to)
216225
if !cmd.Bool("force") {
@@ -220,7 +229,8 @@ func (s *binaryHandler) binaryCopy(ctx context.Context, cmd *cli.Command, from,
220229

221230
return s.binaryCopyFromStoreToFile(ctx, from, to, deleteSource)
222231
default:
223-
return fmt.Errorf("ambiguity detected. Unhandled case. Please report a bug")
232+
// the source is neither a file on disk nor an existing secret.
233+
return fmt.Errorf("%q is neither a file nor a secret in the store", from)
224234
}
225235
}
226236

‎internal/action/binary_test.go‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gopasspw/gopass/internal/config"
1212
"github.com/gopasspw/gopass/internal/out"
1313
"github.com/gopasspw/gopass/pkg/ctxutil"
14+
"github.com/gopasspw/gopass/pkg/gopass/secrets"
1415
"github.com/gopasspw/gopass/tests/gptest"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
@@ -211,6 +212,82 @@ func TestBinaryCopy(t *testing.T) {
211212
})
212213
}
213214

215+
// TestBinaryCopyNameAmbiguity covers https://github.com/gopasspw/gopass/issues/3340:
216+
// copying a file into the store must work even when the destination secret name
217+
// matches the basename of an existing file in the current directory, while the
218+
// genuine "both arguments are secrets" ambiguity is still rejected.
219+
func TestBinaryCopyNameAmbiguity(t *testing.T) {
220+
u := gptest.NewUnitTester(t)
221+
222+
ctx := config.NewContextInMemory()
223+
ctx = ctxutil.WithAlwaysYes(ctx, true)
224+
ctx = ctxutil.WithHidden(ctx, true)
225+
226+
buf := &bytes.Buffer{}
227+
out.Stdout = buf
228+
defer func() {
229+
out.Stdout = os.Stdout
230+
}()
231+
232+
act, err := newMock(ctx, u.StoreDir(""))
233+
require.NoError(t, err)
234+
require.NotNil(t, act)
235+
ctx = act.cfg.WithConfig(ctx)
236+
237+
// Operate from a directory that holds a file whose name collides with the
238+
// destination secret name, reproducing the exact scenario from the issue.
239+
workdir := t.TempDir()
240+
old, err := os.Getwd()
241+
require.NoError(t, err)
242+
require.NoError(t, os.Chdir(workdir))
243+
defer func() { require.NoError(t, os.Chdir(old)) }()
244+
245+
require.NoError(t, os.WriteFile(filepath.Join(workdir, "test"), []byte("0xDEADBEEF\n"), 0o644))
246+
247+
t.Run("file into same-named store entry succeeds", func(t *testing.T) {
248+
defer buf.Reset()
249+
// "gopass fscopy test test": source is a real file, destination is the
250+
// store entry "test". This used to fail with an ambiguity error.
251+
require.NoError(t, act.BinaryCopy(ctx, gptest.CliCtx(ctx, t, "test", "test")))
252+
require.True(t, act.Store.Exists(ctx, "test"))
253+
})
254+
255+
t.Run("file into nested same-basename store entry succeeds", func(t *testing.T) {
256+
defer buf.Reset()
257+
// "gopass fscopy test sub/test": the destination shares the basename of
258+
// the on-disk file but is a nested store path. This used to fail with
259+
// the ambiguity error too.
260+
require.NoError(t, act.BinaryCopy(ctx, gptest.CliCtx(ctx, t, "test", "sub/test")))
261+
require.True(t, act.Store.Exists(ctx, "sub/test"))
262+
})
263+
264+
t.Run("file into rooted same-named path no longer reports ambiguity", func(t *testing.T) {
265+
defer buf.Reset()
266+
// "gopass fscopy test /test": the source is a file so this is routed to
267+
// a filesystem-to-store copy. The store rejects a leading-slash secret
268+
// name, but the user now gets that clear validation error instead of the
269+
// misleading "ambiguity detected" message from #3340.
270+
err := act.BinaryCopy(ctx, gptest.CliCtx(ctx, t, "test", "/test"))
271+
require.Error(t, err)
272+
require.NotContains(t, err.Error(), "ambiguity")
273+
})
274+
275+
t.Run("genuine secret-to-secret ambiguity still errors", func(t *testing.T) {
276+
defer buf.Reset()
277+
// Set up two secrets that have no matching file on disk, then try to
278+
// fscopy between them: this must keep erroring and point at cp.
279+
require.NoError(t, act.Store.Set(ctx, "src-secret", secrets.NewAKV()))
280+
require.NoError(t, act.Store.Set(ctx, "dst-secret", secrets.NewAKV()))
281+
require.Error(t, act.BinaryCopy(ctx, gptest.CliCtx(ctx, t, "src-secret", "dst-secret")))
282+
})
283+
284+
t.Run("unknown source errors", func(t *testing.T) {
285+
defer buf.Reset()
286+
// Neither a file on disk nor a secret in the store.
287+
require.Error(t, act.BinaryCopy(ctx, gptest.CliCtx(ctx, t, "does-not-exist", "dest")))
288+
})
289+
}
290+
214291
func TestBinarySum(t *testing.T) {
215292
u := gptest.NewUnitTester(t)
216293

0 commit comments

Comments
 (0)
⌂ 换网址