Default managed SOCKS hostname DNS to verified HTTPS with literal-IP bootstrap and no plaintext fallback. Supervise the loopback forwarder with private anchored readiness and preserve foreign markers. Include configuration, deployment, documentation and offline checks. Co-authored-by: GPT-6 <gpt-6@openai.com>
296 lines
8.7 KiB
Go
296 lines
8.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func dnsReadyAnchorsForTest(t *testing.T) (string, *os.File, *os.File, func()) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
if err := os.Chmod(dir, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(dir, "dns-ready")
|
|
cleanup, err := createDNSReady(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(cleanup)
|
|
parent, err := os.Open(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { parent.Close() })
|
|
anchor, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { anchor.Close() })
|
|
return path, parent, anchor, cleanup
|
|
}
|
|
|
|
func TestDNSReadinessAnchorActions(t *testing.T) {
|
|
path, parent, anchor, childCleanup := dnsReadyAnchorsForTest(t)
|
|
pid := os.Getpid()
|
|
if _, err := anchor.Seek(1, io.SeekStart); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dnsReadinessAction(parent, anchor, pid, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if offset, _ := anchor.Seek(0, io.SeekCurrent); offset != 1 {
|
|
t.Fatal("shared anchor offset changed", offset)
|
|
}
|
|
if err := dnsReadinessAction(parent, anchor, pid+1, false); err == nil {
|
|
t.Fatal("wrong PID accepted")
|
|
}
|
|
if err := dnsReadinessAction(parent, nil, pid, false); err == nil {
|
|
t.Fatal("missing file anchor accepted")
|
|
}
|
|
if err := dnsReadinessAction(parent, nil, pid, true); err == nil {
|
|
t.Fatal("unanchored entry removed")
|
|
}
|
|
if _, err := os.Stat(path); err != nil {
|
|
t.Fatal("unanchored entry not preserved", err)
|
|
}
|
|
if err := dnsReadinessAction(parent, anchor, pid, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatal("owned entry not removed", err)
|
|
}
|
|
if err := dnsReadinessAction(parent, nil, pid, true); err != nil {
|
|
t.Fatal("missing entry cleanup not idempotent", err)
|
|
}
|
|
if err := dnsReadinessAction(parent, anchor, pid, false); err == nil {
|
|
t.Fatal("unlinked entry reported ready")
|
|
}
|
|
childCleanup() // Graceful child cleanup after parent cleanup also remains safe.
|
|
}
|
|
|
|
func TestDNSReadinessForeignReplacement(t *testing.T) {
|
|
for _, kind := range []string{"same-PID-copy", "symlink", "directory", "FIFO"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
path, parent, anchor, childCleanup := dnsReadyAnchorsForTest(t)
|
|
os.Remove(path)
|
|
switch kind {
|
|
case "same-PID-copy":
|
|
if err := os.WriteFile(path, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case "symlink":
|
|
if err := os.Symlink("missing", path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case "directory":
|
|
if err := os.Mkdir(path, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case "FIFO":
|
|
if err := syscall.Mkfifo(path, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
foreign, err := os.Lstat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, cleanup := range []bool{false, true} {
|
|
if err := dnsReadinessAction(parent, anchor, os.Getpid(), cleanup); err == nil {
|
|
t.Fatal("foreign entry accepted", cleanup)
|
|
}
|
|
}
|
|
childCleanup()
|
|
current, err := os.Lstat(path)
|
|
if err != nil || !os.SameFile(foreign, current) {
|
|
t.Fatal("foreign entry deleted/replaced", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDNSReadinessRejectsUnsafeAnchors(t *testing.T) {
|
|
path, parent, anchor, _ := dnsReadyAnchorsForTest(t)
|
|
pid := os.Getpid()
|
|
if err := dnsReadinessAction(nil, anchor, pid, true); err == nil {
|
|
t.Fatal("missing directory anchor accepted")
|
|
}
|
|
if err := dnsReadinessAction(anchor, anchor, pid, true); err == nil {
|
|
t.Fatal("regular file used as directory anchor")
|
|
}
|
|
for _, body := range []string{"", strconv.Itoa(pid), strconv.Itoa(pid) + "\nextra", "secret-content\n", strings.Repeat("x", 1024)} {
|
|
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dnsReadinessAction(parent, anchor, pid, true); err == nil {
|
|
t.Fatal("malformed PID body accepted")
|
|
}
|
|
}
|
|
os.WriteFile(path, []byte(fmt.Sprintf("%d\n", pid)), 0600)
|
|
os.Chmod(path, 0644)
|
|
if err := dnsReadinessAction(parent, anchor, pid, true); err == nil {
|
|
t.Fatal("public file accepted")
|
|
}
|
|
os.Chmod(path, 0600)
|
|
os.Chmod(filepath.Dir(path), 0755)
|
|
if err := dnsReadinessAction(parent, anchor, pid, true); err == nil {
|
|
t.Fatal("public directory accepted")
|
|
}
|
|
os.Chmod(filepath.Dir(path), 0700)
|
|
}
|
|
|
|
// Subprocess helper exercises the actual inherited FD 8/9 CLI contract.
|
|
func TestDNSReadinessCLIHelper(t *testing.T) {
|
|
if os.Getenv("DNS_TEST_READINESS_CLI") != "1" {
|
|
return
|
|
}
|
|
for i, arg := range os.Args {
|
|
if arg == "--" {
|
|
os.Exit(run(os.Args[i+1:], os.Stdout, os.Stderr))
|
|
}
|
|
}
|
|
os.Exit(99)
|
|
}
|
|
|
|
func dnsReadinessSubprocess(t *testing.T, parent, anchor *os.File, command, pid string) (int, string) {
|
|
t.Helper()
|
|
binary, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, binary, "-test.run=^TestDNSReadinessCLIHelper$", "--", "dns-forwarder", command, pid)
|
|
cmd.Env = append(os.Environ(), "DNS_TEST_READINESS_CLI=1", "ZAPRET_AUTOTUNE_DNS_MODE=doh", "ZAPRET_AUTOTUNE_DOH_URL="+defaultDoHURL, "ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS=", "ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC=5")
|
|
padding, err := os.Open(os.DevNull)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer padding.Close()
|
|
cmd.ExtraFiles = []*os.File{padding, padding, padding, padding, padding, parent}
|
|
if anchor != nil {
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, anchor)
|
|
}
|
|
output, err := cmd.CombinedOutput()
|
|
if ctx.Err() != nil {
|
|
t.Fatal("readiness helper timed out")
|
|
}
|
|
if err == nil {
|
|
return 0, string(output)
|
|
}
|
|
if exit, ok := err.(*exec.ExitError); ok {
|
|
return exit.ExitCode(), string(output)
|
|
}
|
|
t.Fatal(err)
|
|
return -1, ""
|
|
}
|
|
|
|
func TestDNSReadinessInheritedCLI(t *testing.T) {
|
|
path, parent, anchor, _ := dnsReadyAnchorsForTest(t)
|
|
pid := strconv.Itoa(os.Getpid())
|
|
for _, bad := range []string{"0", "1", "-2", "+2", "02", "2\n", "2147483648", "secret-token"} {
|
|
code, out := dnsReadinessSubprocess(t, parent, anchor, "readiness-check", bad)
|
|
if code != 64 || out != "configerror: invalid DNS readiness command\n" {
|
|
t.Fatal(code, out)
|
|
}
|
|
}
|
|
if code, out := dnsReadinessSubprocess(t, parent, anchor, "readiness-check", pid); code != 0 || out != "" {
|
|
t.Fatal(code, out)
|
|
}
|
|
if code, out := dnsReadinessSubprocess(t, nil, anchor, "readiness-cleanup", pid); code != 78 || out != "error: DNS readiness ownership unavailable\n" {
|
|
t.Fatal("missing directory anchor accepted", code, out)
|
|
}
|
|
if offset, _ := anchor.Seek(0, io.SeekCurrent); offset != 0 {
|
|
t.Fatal("CLI changed parent's offset", offset)
|
|
}
|
|
if code, out := dnsReadinessSubprocess(t, parent, nil, "readiness-cleanup", pid); code != 78 || out != "error: DNS readiness ownership unavailable\n" {
|
|
t.Fatal(code, out)
|
|
}
|
|
if code, out := dnsReadinessSubprocess(t, parent, anchor, "readiness-cleanup", pid); code != 0 || out != "" {
|
|
t.Fatal(code, out)
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatal(err)
|
|
}
|
|
if code, out := dnsReadinessSubprocess(t, parent, nil, "readiness-cleanup", pid); code != 0 || out != "" {
|
|
t.Fatal(code, out)
|
|
}
|
|
}
|
|
|
|
func TestDNSReadinessPublisherHelper(t *testing.T) {
|
|
dir := os.Getenv("DNS_TEST_READINESS_PUBLISH_DIR")
|
|
if dir == "" {
|
|
return
|
|
}
|
|
cleanup, err := createDNSReady(filepath.Join(dir, "dns-ready"))
|
|
if err != nil {
|
|
os.Exit(99)
|
|
}
|
|
defer cleanup()
|
|
fmt.Fprintln(os.Stdout, "published")
|
|
time.Sleep(time.Hour)
|
|
}
|
|
|
|
func TestDNSReadinessAnchorsSurviveSIGKILL(t *testing.T) {
|
|
dir := t.TempDir()
|
|
os.Chmod(dir, 0700)
|
|
binary, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, binary, "-test.run=^TestDNSReadinessPublisherHelper$")
|
|
cmd.Env = append(os.Environ(), "DNS_TEST_READINESS_PUBLISH_DIR="+dir)
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var diagnostic bytes.Buffer
|
|
cmd.Stderr = &diagnostic
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cmd.Process.Kill()
|
|
if line, err := bufio.NewReader(stdout).ReadString('\n'); err != nil || line != "published\n" {
|
|
t.Fatal("publisher failed", line, err)
|
|
}
|
|
parent, err := os.Open(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer parent.Close()
|
|
path := filepath.Join(dir, "dns-ready")
|
|
anchor, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer anchor.Close()
|
|
pid := cmd.Process.Pid
|
|
if err := dnsReadinessAction(parent, anchor, pid, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cmd.Process.Kill(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cmd.Wait()
|
|
if err := dnsReadinessAction(parent, anchor, pid, true); err != nil {
|
|
t.Fatal("anchored SIGKILL cleanup failed", err)
|
|
}
|
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
|
t.Fatal("SIGKILL left owned readiness entry", err)
|
|
}
|
|
}
|