Skip to content

Commit 9076980

Browse files
TMHSDigitalclaude
andcommitted
fix(output): new result files honour the umask; -o symlinks are kept
CreateFile made its temp file with os.CreateTemp (0600) and then chmod'ed it to a fixed 0644, bypassing the umask, so results were world-readable under umask 077. New files are now created 0666 and the umask applies; an existing file keeps its mode. A symlinked -o path is resolved first, so the link survives and its target receives the results. Fixes #119 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e5bec69 commit 9076980

3 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- The minimum Go version is now 1.26 (`go 1.26.0` in go.mod), required by current `golang.org/x/sys` and `golang.org/x/text`; both were bumped and `govulncheck -show verbose` now reports no vulnerable modules. Dependabot also updates indirect modules, and CI lints with errorlint, nilerr, contextcheck, noctx, copyloopvar, usestdlibvars and revive, with errcheck exemptions narrowed to stdout prints (#83).
3131

3232
### Fixed
33+
- New `-o` and `-stats` files get 0666 less the umask, as `os.Create` would, instead of a fixed 0644, so results stay private under `umask 077`; an `-o` path that is a symlink keeps the link and replaces the file it points at, instead of turning into a regular file (#119).
3334
- Takeover hints resolve each CNAME target once per scan instead of once per result, so hundreds of names aliased to the same CDN or SaaS host no longer spend a query and a `-rate` slot each; concurrent results for one target share the lookup, and an inconclusive one is retried by the next name (#121).
3435
- DoT connection reuse is accurate: a pooled connection the server already closed is detected before reuse instead of failing a query that had already been counted and paced (a server closing after each answer cost 19 dials for 10 lookups; now 10). Up to 128 connections are kept, matching the default `-t`, and a scan closes its idle DoT and DoH connections when it ends, so a program that scans repeatedly through `pkg/subenum` does not hold them open (#113).
3536
- DNS over HTTPS never follows a redirect, so a misconfigured endpoint or proxy cannot move the query elsewhere or downgrade it to cleartext `http://`; HTTP 429 and 503 count as `refused` (the rate-limit signal) instead of `other` (#114).

‎internal/output/file.go‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package output
22

33
import (
44
"bufio"
5+
"errors"
6+
"fmt"
7+
"io/fs"
8+
"math/rand/v2"
59
"os"
610
"path/filepath"
11+
"strconv"
712
)
813

914
// File is an -o results file that is replaced atomically. Output goes to a
@@ -21,9 +26,14 @@ type File struct {
2126
}
2227

2328
// CreateFile opens path for writing results. An existing regular file keeps
24-
// its permissions; a new one is created 0644.
29+
// its permissions; a new one gets 0666 less the process umask, as os.Create
30+
// would, so a umask of 077 keeps results private (#119). A path that is a
31+
// symlink is followed: the file it points at is replaced and the link kept.
2532
func CreateFile(path string) (*File, error) {
26-
mode := os.FileMode(0o644)
33+
if real, err := filepath.EvalSymlinks(path); err == nil {
34+
path = real
35+
}
36+
keep := os.FileMode(0)
2737
if fi, err := os.Stat(path); err == nil {
2838
if !fi.Mode().IsRegular() {
2939
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0)
@@ -32,16 +42,31 @@ func CreateFile(path string) (*File, error) {
3242
}
3343
return &File{Writer: bufio.NewWriter(f), path: path, f: f}, nil
3444
}
35-
mode = fi.Mode().Perm()
45+
keep = fi.Mode().Perm()
3646
}
37-
f, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*.tmp")
47+
f, err := createTemp(filepath.Dir(path), "."+filepath.Base(path)+".")
3848
if err != nil {
3949
return nil, err
4050
}
41-
_ = f.Chmod(mode) // best effort; Windows only honors the read-only bit
51+
if keep != 0 {
52+
_ = f.Chmod(keep) // best effort; Windows only honors the read-only bit
53+
}
4254
return &File{Writer: bufio.NewWriter(f), path: path, f: f, tmp: true}, nil
4355
}
4456

57+
// createTemp is os.CreateTemp with mode 0666, so the umask applies the way
58+
// it does to os.Create; os.CreateTemp always creates 0600.
59+
func createTemp(dir, prefix string) (*os.File, error) {
60+
for range 100 {
61+
name := filepath.Join(dir, prefix+strconv.FormatUint(rand.Uint64(), 36)+".tmp")
62+
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o666) //nolint:gosec // the user names the output path
63+
if !errors.Is(err, fs.ErrExist) {
64+
return f, err
65+
}
66+
}
67+
return nil, fmt.Errorf("creating a temporary file in %s: too many collisions", dir)
68+
}
69+
4570
// Close flushes and closes the file. With commit set, the temporary file
4671
// replaces the target; otherwise it is discarded and the target is left as it
4772
// was. A failed flush or close also discards it rather than replacing a good

‎internal/output/file_unix_test.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//go:build unix
2+
3+
package output
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"syscall"
9+
"testing"
10+
)
11+
12+
// TestCreateFileHonorsUmask covers #119: a new results file gets 0666 less
13+
// the umask, as os.Create would, instead of a fixed 0644.
14+
func TestCreateFileHonorsUmask(t *testing.T) {
15+
old := syscall.Umask(0o077)
16+
defer syscall.Umask(old)
17+
path := filepath.Join(t.TempDir(), "results.txt")
18+
f, err := CreateFile(path)
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
if _, err := f.WriteString("www.example.com\n"); err != nil {
23+
t.Fatal(err)
24+
}
25+
if err := f.Close(true); err != nil {
26+
t.Fatal(err)
27+
}
28+
fi, err := os.Stat(path)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
if fi.Mode().Perm() != 0o600 {
33+
t.Errorf("mode %v under umask 077, want 0600", fi.Mode().Perm())
34+
}
35+
}
36+
37+
// TestCreateFileFollowsSymlink covers #119: an -o path that is a symlink
38+
// stays a symlink, and the file it points at receives the results.
39+
func TestCreateFileFollowsSymlink(t *testing.T) {
40+
dir := t.TempDir()
41+
target := filepath.Join(dir, "data", "results.txt")
42+
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
43+
t.Fatal(err)
44+
}
45+
if err := os.WriteFile(target, []byte("old\n"), 0o640); err != nil {
46+
t.Fatal(err)
47+
}
48+
link := filepath.Join(dir, "results.txt")
49+
if err := os.Symlink(target, link); err != nil {
50+
t.Fatal(err)
51+
}
52+
f, err := CreateFile(link)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
if _, err := f.WriteString("new\n"); err != nil {
57+
t.Fatal(err)
58+
}
59+
if err := f.Close(true); err != nil {
60+
t.Fatal(err)
61+
}
62+
if fi, err := os.Lstat(link); err != nil || fi.Mode()&os.ModeSymlink == 0 {
63+
t.Fatalf("-o symlink was replaced (mode %v, err %v)", fi.Mode(), err)
64+
}
65+
data, err := os.ReadFile(target)
66+
if err != nil || string(data) != "new\n" {
67+
t.Fatalf("target holds %q (err %v), want the new results", data, err)
68+
}
69+
if fi, _ := os.Stat(target); fi.Mode().Perm() != 0o640 {
70+
t.Errorf("target mode %v, want its original 0640", fi.Mode().Perm())
71+
}
72+
}

0 commit comments

Comments
 (0)