diff --git a/internal/fs/archive.go b/internal/fs/archive.go index daf7215..9f45744 100644 --- a/internal/fs/archive.go +++ b/internal/fs/archive.go @@ -9,6 +9,7 @@ import ( "io" "os" "path/filepath" + "strconv" "strings" "time" ) @@ -341,7 +342,9 @@ func CreateArchive(ctx context.Context, sources []string, archivePath string, pr return fmt.Errorf("unsupported archive format: %s", filepath.Ext(archivePath)) } - tmp, err := os.CreateTemp(filepath.Dir(archivePath), ".vcom-tmp-*-"+filepath.Base(archivePath)) + // Short temp name: a long but valid archive name plus a prefix would + // exceed NAME_MAX (QA-7). CreateTemp makes it 0600 while it is written. + tmp, err := os.CreateTemp(filepath.Dir(archivePath), ".vcom-tmp-*") if err != nil { return fmt.Errorf("create %s: %w", archivePath, err) } @@ -351,7 +354,14 @@ func CreateArchive(ctx context.Context, sources []string, archivePath string, pr os.Remove(tmpPath) return err } - if err := os.Chmod(tmpPath, 0o644); err != nil { + // A replaced archive keeps its permissions (a private 0600 archive must + // not become world-readable, QA-6); a new one gets 0666 minus umask, + // as os.Create would. + mode := 0o666 &^ processUmask() + if st, err := os.Stat(archivePath); err == nil { + mode = st.Mode().Perm() + } + if err := os.Chmod(tmpPath, mode); err != nil { os.Remove(tmpPath) return err } @@ -726,3 +736,19 @@ func commonBaseDir(paths []string) string { } return base } + +// processUmask reads the umask without changing it (syscall.Umask would +// briefly change it for the whole process). Falls back to 022. +func processUmask() os.FileMode { + data, err := os.ReadFile("/proc/self/status") + if err == nil { + for _, line := range strings.Split(string(data), "\n") { + if v, ok := strings.CutPrefix(line, "Umask:"); ok { + if n, err := strconv.ParseUint(strings.TrimSpace(v), 8, 32); err == nil { + return os.FileMode(n) & 0o777 + } + } + } + } + return 0o022 +} diff --git a/internal/fs/qa_archive_trash_test.go b/internal/fs/qa_archive_trash_test.go index 4836058..9420f64 100644 --- a/internal/fs/qa_archive_trash_test.go +++ b/internal/fs/qa_archive_trash_test.go @@ -132,7 +132,6 @@ func qaListArchive(t *testing.T, p string) []string { // Overwriting an existing archive after confirmation must not widen its // permissions: a private 0600 archive became world-readable 0644. func TestQAArchiveOverwriteKeepsPermissions(t *testing.T) { - t.Skip("BUG QA-6: CreateArchive chmods the new archive to 0644, an existing 0600 archive becomes world-readable (archive.go:354)") d, out := t.TempDir(), t.TempDir() qaWrite(t, filepath.Join(d, "secret.txt"), "pin 1234", 0o600) arc := filepath.Join(out, "secret.zip") @@ -152,7 +151,6 @@ func TestQAArchiveOverwriteKeepsPermissions(t *testing.T) { // A long but valid file name (NAME_MAX is 255 bytes) can no longer be // archived: the temp name ".vcom-tmp--" exceeds NAME_MAX. func TestQAArchiveLongName(t *testing.T) { - t.Skip("BUG QA-7: archive temp file name adds ~20 bytes to the target name; names near NAME_MAX fail with ENAMETOOLONG (archive.go:344)") d, out := t.TempDir(), t.TempDir() base := strings.Repeat("n", 240) src := filepath.Join(d, base+".txt") diff --git a/internal/fs/qa_fixes_test.go b/internal/fs/qa_fixes_test.go new file mode 100644 index 0000000..6ec863e --- /dev/null +++ b/internal/fs/qa_fixes_test.go @@ -0,0 +1,26 @@ +package vfs + +import ( + "context" + "os" + "path/filepath" + "testing" +) + +// QA-6: a new archive gets 0666 minus umask (as os.Create would), not the +// 0600 of the temp file and not a fixed 0644. +func TestNewArchiveModeFollowsUmask(t *testing.T) { + d, out := t.TempDir(), t.TempDir() + qaWrite(t, filepath.Join(d, "a.txt"), "x", 0o644) + arc := filepath.Join(out, "a.tar") + if err := CreateArchive(context.Background(), []string{filepath.Join(d, "a.txt")}, arc, nil); err != nil { + t.Fatal(err) + } + st, err := os.Stat(arc) + if err != nil { + t.Fatal(err) + } + if want := 0o666 &^ processUmask(); st.Mode().Perm() != want { + t.Fatalf("new archive mode %o, want %o", st.Mode().Perm(), want) + } +}