diff options
| author | Jeroen Wijenbergh <jeroen.wijenbergh@geant.org> | 2025-08-01 15:00:58 +0200 |
|---|---|---|
| committer | Jeroen Wijenbergh <jeroen.wijenbergh@geant.org> | 2025-08-25 13:38:10 +0200 |
| commit | 08699e08d39675b76a9b813fd9fe41be6ab47a18 (patch) | |
| tree | 38267a48b5a3789dc42c85fb56d89d1e57d51880 /internal/atomicfile/atomicfile_test.go | |
| parent | 27b95b4911da055fe9b5fb37b5fb4a33eda6b989 (diff) | |
Atomicfile: Move outside of config package
Diffstat (limited to 'internal/atomicfile/atomicfile_test.go')
| -rw-r--r-- | internal/atomicfile/atomicfile_test.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/internal/atomicfile/atomicfile_test.go b/internal/atomicfile/atomicfile_test.go new file mode 100644 index 0000000..f14cb31 --- /dev/null +++ b/internal/atomicfile/atomicfile_test.go @@ -0,0 +1,47 @@ +// Copyright (c) Tailscale Inc & AUTHORS +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !js && !windows + +package atomicfile + +import ( + "net" + "os" + "path/filepath" + "runtime" + "strings" + "testing" +) + +func TestDoesNotOverwriteIrregularFiles(t *testing.T) { + // Per tailscale/tailscale#7658 as one example, almost any imagined use of + // atomicfile.Write should likely not attempt to overwrite an irregular file + // such as a device node, socket, or named pipe. + + const filename = "TestDoesNotOverwriteIrregularFiles" + var path string + // macOS private temp does not allow unix socket creation, but /tmp does. + if runtime.GOOS == "darwin" { + path = filepath.Join("/tmp", filename) + t.Cleanup(func() { os.Remove(path) }) //nolint:errcheck + } else { + path = filepath.Join(t.TempDir(), filename) + } + + // The least troublesome thing to make that is not a file is a unix socket. + // Making a null device sadly requires root. + l, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"}) + if err != nil { + t.Fatal(err) + } + defer l.Close() //nolint:errcheck + + err = WriteFile(path, []byte("hello"), 0o644) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), "is not a regular file") { + t.Fatalf("unexpected error: %v", err) + } +} |
