feat: add declarative HTTPS resource checks

Add strict check-only resource configuration, owned DNS resolution and pinned HTTPS probes with bounded private output. Document the schema limits and pending remote acceptance; leave generic controller and strategy migration out of scope.

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-13 18:38:46 +03:00
1 parent 0623fa3323
commit 4d7c4b33c3
9 files changed
+1840 -6

No files matched your search

+51
View File
@@ -11,6 +11,57 @@ Clients must enable SOCKS5 remote DNS (e.g. browser remote DNS on the chosen
loopback SOCKS port). See [DNS configuration and limits](deploy/managed-target/README.md). loopback SOCKS port). See [DNS configuration and limits](deploy/managed-target/README.md).
This new integration is not covered by the earlier clean-run acceptance. This new integration is not covered by the earlier clean-run acceptance.
## Configured HTTPS checks (checks only)
Inside the owned managed target, with its configured DNS/SOCKS environment, run:
```sh
zapret-autotune check --config /path/to/resource-check.example.json
```
See [the public example](resource-check.example.json). This does **not** select
strategies, migrate candidates/transactions, implement metrics, or declare READY;
`check youtube` and the YouTube qualification contract remain unchanged.
Version 1 supports `kind: "http"`, GET/HEAD, exact HTTPS DNS-hostname origins
(port 443 only), `tls12`, `tls13`, or `both`. All fields shown in the example are
required except `content_type` and `min_bytes` (default zero); IDs/group are lowercase bounded identifiers.
There are at most 16 services and 64 resources, 100–30000 ms per TLS probe,
and 1–5242880 maximum decoded bytes. HEAD requires `min_bytes: 0`; its advertised
Content-Length is not downloaded or used as a body limit. No redirects are accepted.
Expected statuses are explicit, non-redirect 200–599 codes; MIME is an optional
exact media type (parameters are ignored in the response).
This slice checks HTTP status, MIME and byte bounds only: no image/media decode,
Range probes, suffix scopes or per-resource DNS settings.
Replace `url` with `url_env` for a private URL; optional `headers_env` maps header
names to environment variable names (e.g. `{"Authorization":"CHECK_TOKEN"}`).
Secrets travel only over private child stdin, never argv/results/database.
Transport/framing headers (including Range and Accept-Encoding) cannot be
overridden. Range probes need a future explicit probe field, not a secret header.
Config files must be regular, non-symlink files no larger than 1 MiB; permissions
are left untouched. Unknown fields, duplicates, missing/invalid settings and
environment references reject the entire configuration before any probes.
Checks issue explicit framed A/AAAA DNS queries to the configured local DoH
forwarder: no `/etc/hosts`, search domains or system DNS fallback. CNAME chains
are bounded and validated; unrelated Additional glue is never a destination.
The root-owned forwarder must match PID 1's private readiness anchors, executable,
PID/network namespaces and both UDP/TCP listeners; the owned Shadowsocks chain
must actually route its resolver to that same listener before any probes.
Explicit native mode requires an IP-valued `SS_DNS_SERVER` and is **plaintext DNS,
not encrypted DoH**. Every answer must pass
the destination policy, then the chosen IP is pinned for SOCKS CONNECT while the
original hostname remains the TLS/SNI/certificate authority. Addresses are resolved
fresh per probe, not stored. Private targets require `allow_private: true` **and**
explicit `allowed_networks` CIDRs. CIDRs always restrict; metadata, link-local,
CGNAT, loopback, benchmark and other reserved ranges remain denied even with that opt-in.
Output is one JSON line per resource/TLS probe: IDs, bounded outcome, TLS,
decoded bytes, milliseconds and HTTP status only. Exit 0 means all checks passed;
1 means a resource failed/cancellation/output failure; 2 means invalid config or
unavailable ownership. Resource failures continue to other resources; ownership
failure stops all probes. These checks have unit/loopback-fixture coverage only,
not new remote acceptance or generic automated recovery/controller support.
## Production preparation (isolated clean-run acceptance passed) ## Production preparation (isolated clean-run acceptance passed)
The standalone deployment candidate is a **new isolated target**, never an The standalone deployment candidate is a **new isolated target**, never an
+5
View File
@@ -22,6 +22,8 @@ Commands:
managed init|reconcile|status|apply|monitor managed init|reconcile|status|apply|monitor
Guarded activation/monitoring in the dedicated managed-test target only Guarded activation/monitoring in the dedicated managed-test target only
check youtube Refuse checks until probe URLs/video IDs are configured and agreed check youtube Refuse checks until probe URLs/video IDs are configured and agreed
check --config FILE
Check configured HTTPS resources (checks only; no strategy selection)
tune youtube Test youtube.com frontend only; print options without applying tune youtube Test youtube.com frontend only; print options without applying
tune googlevideo --video-id ID --proxy socks5://HOST:PORT tune googlevideo --video-id ID --proxy socks5://HOST:PORT
Discover and verify one exact cache host; proposal only Discover and verify one exact cache host; proposal only
@@ -39,6 +41,9 @@ func run(args []string, stdout, stderr io.Writer) int {
} }
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int { func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
if len(args) == 3 && args[0] == "check" && args[1] == "--config" {
return configuredResourceCheck(args[2], stdout, stderr)
}
if len(args) > 0 && args[0] == "dns-forwarder" { if len(args) > 0 && args[0] == "dns-forwarder" {
return dnsForwarderCLI(args[1:], stdout, stderr) return dnsForwarderCLI(args[1:], stdout, stderr)
} }
+427
View File
@@ -0,0 +1,427 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net"
"net/netip"
"net/url"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
)
var errResourceConfig = errors.New("invalid resource configuration")
var resourceID = regexp.MustCompile(`^[a-z][a-z0-9_-]{0,47}$`)
var resourceEnv = regexp.MustCompile(`^[A-Z_][A-Z0-9_]{0,127}$`)
var resourceHeader = regexp.MustCompile("^[!#$%&'*+.^_`|~0-9A-Za-z-]{1,64}$")
var resourceNumericHost = regexp.MustCompile(`(?i)^(?:[0-9]+|0x[0-9a-f]+)(?:\.(?:[0-9]+|0x[0-9a-f]+))*$`)
type resourceCheckConfig struct {
Version int `json:"version"`
Services []resourceCheckService `json:"services"`
}
type resourceCheckService struct {
ID string `json:"id"`
Resources []resourceCheckSpec `json:"resources"`
}
type resourceCheckSpec struct {
ID string `json:"id"`
Group string `json:"group"`
Kind string `json:"kind"`
URL string `json:"url,omitempty"`
URLEnv string `json:"url_env,omitempty"`
Method string `json:"method"`
TLS string `json:"tls"`
TimeoutMS int `json:"timeout_ms"`
Expect resourceCheckExpect `json:"expect"`
HeadersEnv map[string]string `json:"headers_env,omitempty"`
AllowPrivate bool `json:"allow_private,omitempty"`
AllowedNetworks []string `json:"allowed_networks,omitempty"`
host, privateConfig string
networks []netip.Prefix
}
type resourceCheckExpect struct {
Statuses []int `json:"statuses"`
ContentType string `json:"content_type,omitempty"`
MinBytes int `json:"min_bytes"`
MaxBytes int `json:"max_bytes"`
}
// encoding/json otherwise silently accepts duplicate keys and null scalars.
func uniqueResourceJSON(d *json.Decoder, depth int) error {
if depth > 16 {
return errResourceConfig
}
t, err := d.Token()
if err != nil || t == nil {
return errResourceConfig
}
if delim, ok := t.(json.Delim); ok {
switch delim {
case '{':
seen := map[string]bool{}
for d.More() {
key, err := d.Token()
if err != nil {
return errResourceConfig
}
s, ok := key.(string)
if !ok || seen[s] {
return errResourceConfig
}
seen[s] = true
if err := uniqueResourceJSON(d, depth+1); err != nil {
return err
}
}
case '[':
for d.More() {
if err := uniqueResourceJSON(d, depth+1); err != nil {
return err
}
}
default:
return errResourceConfig
}
if _, err := d.Token(); err != nil {
return errResourceConfig
}
}
return nil
}
func privateCurlValue(v string) string {
return "\"" + strings.NewReplacer("\\", "\\\\", "\"", "\\\"").Replace(v) + "\""
}
func safeResourceValue(v string) bool {
return len(v) <= 16<<10 && strings.IndexFunc(v, func(c rune) bool { return c < 32 || c == 127 }) < 0
}
func parseResourceChecks(data []byte, lookup func(string) (string, bool)) (resourceCheckConfig, error) {
var cfg resourceCheckConfig
fail := func() (resourceCheckConfig, error) { return resourceCheckConfig{}, errResourceConfig }
if len(data) > 1<<20 || lookup == nil {
return fail()
}
d := json.NewDecoder(bytes.NewReader(data))
if uniqueResourceJSON(d, 0) != nil {
return fail()
}
if _, err := d.Token(); err != io.EOF {
return fail()
}
d = json.NewDecoder(bytes.NewReader(data))
d.DisallowUnknownFields()
if d.Decode(&cfg) != nil || cfg.Version != 1 || len(cfg.Services) == 0 || len(cfg.Services) > 16 {
return fail()
}
services, total := map[string]bool{}, 0
for si := range cfg.Services {
s := &cfg.Services[si]
if !resourceID.MatchString(s.ID) || services[s.ID] || len(s.Resources) == 0 {
return fail()
}
services[s.ID] = true
ids := map[string]bool{}
for ri := range s.Resources {
r := &s.Resources[ri]
total++
if total > 64 || !resourceID.MatchString(r.ID) || ids[r.ID] || !resourceID.MatchString(r.Group) || r.Kind != "http" {
return fail()
}
ids[r.ID] = true
if (r.URL == "") == (r.URLEnv == "") {
return fail()
}
if r.URLEnv != "" {
if !resourceEnv.MatchString(r.URLEnv) {
return fail()
}
var ok bool
r.URL, ok = lookup(r.URLEnv)
if !ok || r.URL == "" {
return fail()
}
}
u, err := url.Parse(r.URL)
if err != nil {
return fail()
}
r.host = u.Hostname()
// curl normalizes legacy IPv4 spellings before matching --connect-to.
// Only canonical literals may bypass DNS without defeating that match.
if resourceNumericHost.MatchString(r.host) {
ip := net.ParseIP(r.host)
if ip == nil || ip.To4() == nil || ip.String() != r.host {
return fail()
}
}
if _, err := privateResourceURL(r.URL, r.host); err != nil {
return fail()
}
// Reject encoded controls too, without ever displaying the decoded path.
decoded, err := url.PathUnescape(r.URL)
if err != nil || !safeResourceValue(decoded) {
return fail()
}
if r.Method != "GET" && r.Method != "HEAD" {
return fail()
}
if r.TLS != "tls12" && r.TLS != "tls13" && r.TLS != "both" {
return fail()
}
if r.TimeoutMS < 100 || r.TimeoutMS > 30000 || r.Expect.MaxBytes < 1 || r.Expect.MaxBytes > 5<<20 || r.Expect.MinBytes < 0 || r.Expect.MinBytes > r.Expect.MaxBytes || (r.Method == "HEAD" && r.Expect.MinBytes != 0) {
return fail()
}
if len(r.Expect.Statuses) == 0 || len(r.Expect.Statuses) > 16 {
return fail()
}
statuses := map[int]bool{}
for _, status := range r.Expect.Statuses {
if status < 200 || status > 599 || status >= 300 && status < 400 || statuses[status] {
return fail()
}
statuses[status] = true
}
if r.Expect.ContentType != "" {
mt, params, err := mime.ParseMediaType(r.Expect.ContentType)
if err != nil || len(params) != 0 || mt != r.Expect.ContentType {
return fail()
}
}
if len(r.HeadersEnv) > 16 {
return fail()
}
headers := map[string]bool{}
for name, env := range r.HeadersEnv {
lower := strings.ToLower(name)
if !resourceHeader.MatchString(name) || headers[lower] || !resourceEnv.MatchString(env) {
return fail()
}
headers[lower] = true
switch lower {
case "host", "connection", "transfer-encoding", "content-length", "expect", "upgrade", "proxy-authorization", "proxy-connection", "te", "trailer", "range", "accept-encoding":
return fail()
}
value, ok := lookup(env)
if !ok || value == "" || !safeResourceValue(value) {
return fail()
}
r.privateConfig += "header = " + privateCurlValue(name+": "+value) + "\n"
}
if len(r.AllowedNetworks) > 16 || r.AllowPrivate && len(r.AllowedNetworks) == 0 {
return fail()
}
for _, raw := range r.AllowedNetworks {
prefix, err := netip.ParsePrefix(raw)
if err != nil || prefix != prefix.Masked() || prefix.Addr().Is4In6() {
return fail()
}
r.networks = append(r.networks, prefix)
}
}
}
return cfg, nil
}
func readResourceChecks(path string, lookup func(string) (string, bool)) (resourceCheckConfig, error) {
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if err != nil {
return resourceCheckConfig{}, errResourceConfig
}
f := os.NewFile(uintptr(fd), "resource config")
defer f.Close()
info, err := f.Stat()
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
return resourceCheckConfig{}, errResourceConfig
}
data, err := io.ReadAll(io.LimitReader(f, (1<<20)+1))
if err != nil {
return resourceCheckConfig{}, errResourceConfig
}
return parseResourceChecks(data, lookup)
}
var resourceDeniedNetworks = []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32", "2001::/23", "2002::/16", "64:ff9b::/96", "64:ff9b:1::/48", "100::/64"}
func approvedResourceIP(r resourceCheckSpec, ip net.IP) bool {
a, ok := netip.AddrFromSlice(ip)
if !ok {
return false
}
a = a.Unmap()
// Link-local and metadata are never enabled by allow_private.
if !a.IsGlobalUnicast() || a.IsLoopback() || a.IsLinkLocalUnicast() || a.IsLinkLocalMulticast() || a.String() == "100.100.100.200" || a.String() == "fd00:ec2::254" || a.String() == "168.63.129.16" {
return false
}
for _, raw := range resourceDeniedNetworks {
p := netip.MustParsePrefix(raw)
if p.Contains(a) {
return false
}
}
if a.Is6() && !a.IsPrivate() && !netip.MustParsePrefix("2000::/3").Contains(a) {
return false
}
if a.IsPrivate() || a.IsLoopback() {
if !r.AllowPrivate {
return false
}
}
if len(r.networks) != 0 {
for _, p := range r.networks {
if p.Contains(a) {
return true
}
}
return false
}
return true
}
type resourceCheckDeps struct {
lookup func(string) (string, bool)
guard func() error
resolve func(context.Context, string, string) ([]net.IP, error)
runner subprocessRunner
}
func resolveResourceIPs(ctx context.Context, server, host string) ([]net.IP, error) {
return resolveResourceIPsWithDial(ctx, server, host, (&net.Dialer{}).DialContext)
}
func productionResourceCheckDeps() resourceCheckDeps {
d := managedDependencies(runPlayerCurl)
return resourceCheckDeps{lookup: os.LookupEnv, guard: func() error {
if _, err := managedRuntimeGuard(d); err != nil {
return err
}
// The provisioned local.json and owned SS chain use the target's fixed
// internal port, not an additional ss-local listener in this namespace.
if port, _ := d.lookup("SOCKS_PORT"); port != "1080" {
return errDNSForwarder
}
cfg, err := parseManagedDNSConfig(d.lookup)
if err != nil {
return err
}
return verifyResourceDNSRuntime(cfg)
}, resolve: resolveResourceIPs, runner: runResourceCurl}
}
type resourceCheckResult struct {
Service string `json:"service"`
Resource string `json:"resource"`
Group string `json:"group"`
Outcome string `json:"outcome"`
TLS string `json:"tls"`
Bytes int `json:"bytes"`
TimeMS float64 `json:"time_ms"`
Status int `json:"status"`
}
func resourceCheckCLI(ctx context.Context, path string, stdout, stderr io.Writer, d resourceCheckDeps) int {
if d.lookup == nil {
fmt.Fprintln(stderr, "configerror: invalid resource check configuration")
return 2
}
cfg, err := readResourceChecks(path, d.lookup)
dns, dnsErr := parseManagedDNSConfig(d.lookup)
proxy, _ := d.lookup("ZAPRET_AUTOTUNE_SOCKS_PROXY")
proxy, proxyErr := managedProbeProxy(proxy)
if err != nil || dnsErr != nil || proxyErr != nil || d.guard == nil || d.resolve == nil || d.runner == nil || (dns.Mode == "native" && dns.NativeServer == "") {
fmt.Fprintln(stderr, "configerror: invalid resource check configuration")
return 2
}
server := dns.serverAddress()
if dns.Mode == "native" {
server = net.JoinHostPort(server, "53")
}
code := 0
for _, s := range cfg.Services {
for _, r := range s.Resources {
protocols := []string{r.TLS}
if r.TLS == "both" {
protocols = []string{"tls12", "tls13"}
}
for _, protocol := range protocols {
if ctx.Err() != nil {
return 1
}
if d.guard() != nil {
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
return 2
}
result := resourceCheckResult{Service: s.ID, Resource: r.ID, Group: r.Group, TLS: protocol, Outcome: "dns_failed"}
probeCtx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeoutMS)*time.Millisecond)
ips, err := d.resolve(probeCtx, server, r.host)
if err == nil && len(ips) > 0 && len(ips) <= 64 {
approved := true
for _, ip := range ips {
if !approvedResourceIP(r, ip) {
approved = false
}
}
result.Outcome = "destination_denied"
if approved {
if d.guard() != nil {
cancel()
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
return 2
}
// --connect-to changes the SOCKS CONNECT destination even for socks5h;
// URL authority, HTTP Host, SNI and certificate verification stay original.
pin := r.host + ":443:" + net.JoinHostPort(ips[0].String(), "443")
options := resourceTransferOptions{generic: true, method: r.Method, limit: r.Expect.MaxBytes, timeout: time.Duration(r.TimeoutMS) * time.Millisecond, config: r.privateConfig + "connect-to = " + privateCurlValue(pin) + "\n"}
transfer, _, err := collectResourceTransferOptions(probeCtx, proxy, r.URL, r.host, protocol, false, d.runner, options)
result.Outcome = "transfer_failed"
if err == nil {
result.Bytes, result.TimeMS, result.Status = len(transfer.Body), transfer.LatencyMS, transfer.Status
result.Outcome = "expectation_failed"
statusOK := false
for _, status := range r.Expect.Statuses {
if status == transfer.Status {
statusOK = true
}
}
if statusOK {
if len(transfer.Body) >= r.Expect.MinBytes && (r.Expect.ContentType == "" || transfer.ContentType == r.Expect.ContentType) {
result.Outcome = "pass"
}
}
}
}
}
if probeCtx.Err() != nil {
result.Outcome = "timeout"
if ctx.Err() != nil {
result.Outcome = "cancelled"
}
}
cancel()
if result.Outcome != "pass" {
code = 1
}
if json.NewEncoder(stdout).Encode(result) != nil {
return 1
}
}
}
}
return code
}
func configuredResourceCheck(path string, stdout, stderr io.Writer) int {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
return resourceCheckCLI(ctx, path, stdout, stderr, productionResourceCheckDeps())
}
+496
View File
@@ -0,0 +1,496 @@
package main
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
)
const resourceCheckExample = `{"version":1,"services":[{"id":"first","resources":[{"id":"page","group":"web","kind":"http","url":"https://example.com/private?secret=value","method":"GET","tls":"tls12","timeout_ms":1000,"expect":{"statuses":[200],"content_type":"text/plain","min_bytes":1,"max_bytes":100}}]},{"id":"second","resources":[{"id":"headers","group":"web","kind":"http","url_env":"CHECK_URL","method":"HEAD","tls":"both","timeout_ms":1000,"expect":{"statuses":[200],"min_bytes":0,"max_bytes":100},"headers_env":{"Authorization":"CHECK_TOKEN"}}]}]}`
func resourceCheckLookup(key string) (string, bool) {
values := map[string]string{"CHECK_URL": "https://example.org/secret", "CHECK_TOKEN": "Bearer private-token", "ZAPRET_AUTOTUNE_SOCKS_PROXY": "socks5h://127.0.0.1:1080"}
v, ok := values[key]
return v, ok
}
func resourceCheckFile(t *testing.T, data string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "checks.json")
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
t.Fatal(err)
}
return path
}
func configuredWire(raw, method, protocol, mode string, out, diag io.Writer) {
body := "hello"
length := len(body)
if method == "HEAD" {
body, length = "", 999999999
}
status, verify, redirects, speed := 200, 0, 0, 50
if method == "HEAD" {
speed = 0
}
if mode == "redirect" {
status = 302
}
if mode == "redirect-count" {
redirects = 1
}
if mode == "verify" {
verify = 60
}
if mode == "origin" {
raw = "https://evil.example/"
}
if mode == "overflow" {
body, length = strings.Repeat("a", 101), 101
}
if mode == "truncated" {
length++
}
if mode == "extra" {
body += "extra"
}
if mode == "chunked" {
fmt.Fprint(out, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n")
body = "5\r\nhello\r\n0\r\n\r\n"
} else {
fmt.Fprintf(out, "HTTP/1.1 %d OK\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n", status, length)
}
fmt.Fprint(out, body)
metrics, _ := json.Marshal(map[string]interface{}{"url_effective": raw, "ssl_verify_result": verify, "num_redirects": redirects, "http_code": status, "time_total": 0.1, "speed_download": speed, "size_download": len(body)})
fmt.Fprint(out, resourceTrailer, string(metrics))
if mode == "tls" {
protocol = "tls13"
}
version := "1.2"
if protocol == "tls13" {
version = "1.3"
}
fmt.Fprintln(diag, "SSL connection using TLSv"+version)
}
func TestResourceCheckSchema(t *testing.T) {
if _, err := parseResourceChecks([]byte(resourceCheckExample), resourceCheckLookup); err != nil {
t.Fatal(err)
}
for _, data := range []string{
resourceCheckExample + ` {}`, strings.Replace(resourceCheckExample, `"version":1`, `"version":2`, 1),
strings.Replace(resourceCheckExample, `"version":1`, `"version":1,"version":1`, 1),
strings.Replace(resourceCheckExample, `"version":1`, `"unknown":1,"version":1`, 1),
strings.Replace(resourceCheckExample, `"id":"first"`, `"id":"First /"`, 1),
strings.Replace(resourceCheckExample, `"id":"second"`, `"id":"first"`, 1),
strings.Replace(resourceCheckExample, `"max_bytes":100`, `"max_bytes":0`, 1),
strings.Replace(resourceCheckExample, `"method":"GET"`, `"method":"POST"`, 1),
strings.Replace(resourceCheckExample, `"kind":"http"`, `"kind":"image"`, 1),
strings.Replace(resourceCheckExample, `"statuses":[200]`, `"statuses":[302]`, 1),
strings.Replace(resourceCheckExample, `"statuses":[200]`, `"statuses":[200,200]`, 1),
strings.Replace(resourceCheckExample, `"tls":"tls12"`, `"tls":"auto"`, 1),
strings.Replace(resourceCheckExample, `"timeout_ms":1000`, `"timeout_ms":30001`, 1),
strings.Replace(resourceCheckExample, `"headers_env":{"Authorization":"CHECK_TOKEN"}`, `"headers_env":{"Host":"CHECK_TOKEN"}`, 1),
strings.Replace(resourceCheckExample, `"CHECK_TOKEN"`, `"missing"`, 1),
strings.Replace(resourceCheckExample, `"CHECK_TOKEN"`, `"MISSING"`, 1),
strings.Replace(resourceCheckExample, `"CHECK_URL"`, `"MISSING"`, 1),
strings.Replace(resourceCheckExample, `"method":"GET"`, `"method":null`, 1),
strings.Replace(resourceCheckExample, `"kind":"http"`, `"allow_private":true,"kind":"http"`, 1),
strings.Replace(resourceCheckExample, `"kind":"http"`, `"allowed_networks":["10.0.0.1/24"],"kind":"http"`, 1),
} {
if _, err := parseResourceChecks([]byte(data), resourceCheckLookup); err == nil {
t.Fatal("invalid schema accepted")
}
}
for _, raw := range []string{"http://example.com/", "https://user:password@example.com/", "https://example.com:444/", "https://example.com/a%0d%0aHeader", "https://example.com/a\n", "https://example.com/#fragment", "https://example.com:/", "https://[::1]/", "https://2130706433/", "https://127.1/", "https://0x7f000001/", "https://0177.0.0.1/"} {
lookup := func(key string) (string, bool) {
if key == "CHECK_URL" {
return raw, true
}
return resourceCheckLookup(key)
}
if _, err := parseResourceChecks([]byte(resourceCheckExample), lookup); err == nil {
t.Fatal("invalid origin accepted")
}
}
lookup := func(key string) (string, bool) {
if key == "CHECK_TOKEN" {
return "secret\r\nHost: evil", true
}
return resourceCheckLookup(key)
}
if _, err := parseResourceChecks([]byte(resourceCheckExample), lookup); err == nil {
t.Fatal("header injection")
}
for _, name := range []string{"Range", "rAnGe", "Accept-Encoding", "Host", "Connection", "Content-Length", "Proxy-Authorization"} {
data := strings.Replace(resourceCheckExample, `"headers_env":{"Authorization":"CHECK_TOKEN"}`, `"headers_env":{"`+name+`":"CHECK_TOKEN"}`, 1)
if _, err := parseResourceChecks([]byte(data), resourceCheckLookup); err == nil {
t.Fatal("transport header accepted", name)
}
}
}
func TestResourceCheckFiles(t *testing.T) {
path := resourceCheckFile(t, resourceCheckExample)
link := filepath.Join(t.TempDir(), "link")
if err := os.Symlink(path, link); err != nil {
t.Fatal(err)
}
for _, p := range []string{link, t.TempDir(), resourceCheckFile(t, strings.Repeat("x", (1<<20)+1))} {
if _, err := readResourceChecks(p, resourceCheckLookup); err == nil {
t.Fatal("unsafe file accepted")
}
}
if _, err := readResourceChecks(path, resourceCheckLookup); err != nil {
t.Fatal(err)
}
info, _ := os.Stat(path)
if info.Mode().Perm() != 0600 {
t.Fatal("changed permissions")
}
}
func TestResourceCheckResolverPinnedServer(t *testing.T) {
var workers sync.WaitGroup
dial := func(ctx context.Context, network, address string) (net.Conn, error) {
if network != "tcp" || address != dnsListenAddress {
t.Error("DNS fallback", network, address)
}
client, server := net.Pipe()
workers.Add(1)
go func() {
defer workers.Done()
defer server.Close()
server.SetDeadline(time.Now().Add(time.Second))
var length [2]byte
if _, err := io.ReadFull(server, length[:]); err != nil {
t.Error(err)
return
}
query := make([]byte, int(binary.BigEndian.Uint16(length[:])))
if _, err := io.ReadFull(server, query); err != nil {
t.Error(err)
return
}
end, name, ok := dnsQuestion(query)
if !ok {
t.Error("invalid query")
return
}
if !bytes.Equal(name[:len(name)-4], []byte{7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0}) {
t.Error("search-domain expansion")
}
response := append([]byte(nil), query[:end]...)
response[2], response[3] = 0x81, 0x80
response[10], response[11] = 0, 0 // strip query EDNS
if binary.BigEndian.Uint16(query[end-4:end-2]) == 1 {
response[7] = 2
for _, ip := range [][]byte{{93, 184, 216, 34}, {10, 0, 0, 1}} {
response = append(response, 0xc0, 12, 0, 1, 0, 1, 0, 0, 0, 1, 0, 4)
response = append(response, ip...)
}
}
binary.BigEndian.PutUint16(length[:], uint16(len(response)))
server.Write(append(length[:], response...))
}()
return client, nil
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
ips, err := resolveResourceIPsWithDial(ctx, dnsListenAddress, "example.com", dial)
workers.Wait()
if err != nil || len(ips) != 2 {
t.Fatal("lost mixed DNS answers", ips, err)
}
for _, ip := range ips {
if ip.String() != "93.184.216.34" && ip.String() != "10.0.0.1" {
t.Fatal("unexpected answer", ip)
}
}
_, err = resolveResourceIPsWithDial(ctx, dnsListenAddress, "example.com", func(context.Context, string, string) (net.Conn, error) {
return nil, errors.New("forwarder unavailable")
})
if err == nil {
t.Fatal("DNS outage fell back")
}
}
func TestResourceCheckDestinationPolicy(t *testing.T) {
cfg, _ := parseResourceChecks([]byte(resourceCheckExample), resourceCheckLookup)
r := cfg.Services[0].Resources[0]
for _, raw := range []string{"127.0.0.1", "10.0.0.1", "172.16.0.1", "192.168.0.1", "169.254.169.254", "0.0.0.0", "224.0.0.1", "100.64.0.1", "100.100.100.200", "198.18.0.1", "::1", "::2", "fe80::1", "fc00::1", "fd00:ec2::254", "168.63.129.16", "::ffff:127.0.0.1", "2002:7f00:1::1"} {
if approvedResourceIP(r, net.ParseIP(raw)) {
t.Fatal("unsafe destination", raw)
}
}
if !approvedResourceIP(r, net.ParseIP("93.184.216.34")) {
t.Fatal("public denied")
}
data := strings.Replace(resourceCheckExample, `"kind":"http"`, `"allow_private":true,"allowed_networks":["10.20.0.0/16"],"kind":"http"`, 1)
cfg, err := parseResourceChecks([]byte(data), resourceCheckLookup)
if err != nil {
t.Fatal(err)
}
r = cfg.Services[0].Resources[0]
if !approvedResourceIP(r, net.ParseIP("10.20.1.2")) || approvedResourceIP(r, net.ParseIP("10.21.1.2")) || approvedResourceIP(r, net.ParseIP("93.184.216.34")) || approvedResourceIP(r, net.ParseIP("169.254.169.254")) {
t.Fatal("explicit subnet policy")
}
data = strings.Replace(resourceCheckExample, `"kind":"http"`, `"allowed_networks":["93.184.216.0/24"],"kind":"http"`, 1)
cfg, err = parseResourceChecks([]byte(data), resourceCheckLookup)
if err != nil {
t.Fatal(err)
}
r = cfg.Services[0].Resources[0]
if !approvedResourceIP(r, net.ParseIP("93.184.216.34")) || approvedResourceIP(r, net.ParseIP("8.8.8.8")) {
t.Fatal("public CIDR did not restrict")
}
data = strings.Replace(resourceCheckExample, `"kind":"http"`, `"allow_private":true,"allowed_networks":["0.0.0.0/0","::/0"],"kind":"http"`, 1)
cfg, err = parseResourceChecks([]byte(data), resourceCheckLookup)
if err != nil {
t.Fatal(err)
}
r = cfg.Services[0].Resources[0]
for _, raw := range []string{"127.0.0.1", "::1", "::ffff:127.0.0.1", "169.254.169.254", "fd00:ec2::254", "100.100.100.200", "168.63.129.16", "198.18.0.1", "fe80::1"} {
if approvedResourceIP(r, net.ParseIP(raw)) {
t.Fatal("private opt-in enabled metadata/reserved", raw)
}
}
}
func TestResourceCheckIsolationPrivacyAndPinning(t *testing.T) {
for _, mode := range []string{"pass", "mixed", "rebind", "runner", "expect", "guard"} {
t.Run(mode, func(t *testing.T) {
calls, resolves, guards := 0, 0, 0
var output, diagnostic bytes.Buffer
d := resourceCheckDeps{lookup: resourceCheckLookup, guard: func() error {
guards++
if mode == "guard" {
return errors.New("private-secret")
}
return nil
}, resolve: func(_ context.Context, server, host string) ([]net.IP, error) {
resolves++
if server != dnsListenAddress {
t.Fatal("wrong resolver")
}
if host == "example.com" && mode == "mixed" {
return []net.IP{net.ParseIP("93.184.216.34"), net.ParseIP("127.0.0.1")}, nil
}
if resolves == 3 && mode == "rebind" {
return []net.IP{net.ParseIP("10.0.0.1")}, nil
}
return []net.IP{net.ParseIP("93.184.216.34")}, nil
}, runner: func(ctx context.Context, args []string, stdin io.Reader, out, diag io.Writer) error {
calls++
argv := strings.Join(args, " ")
if strings.Contains(argv, "example.") || strings.Contains(argv, "secret") || strings.Contains(argv, "private-token") || strings.Contains(argv, "93.184.") {
t.Fatal("private argv", args)
}
config, _ := io.ReadAll(stdin)
if !strings.Contains(string(config), `connect-to = "example.`) || !strings.Contains(string(config), `:443:93.184.216.34:443"`) || strings.Contains(argv, "--resolve") {
t.Fatal("not pinning CONNECT")
}
lines := strings.Split(string(config), "\n")
raw, err := strconv.Unquote(strings.TrimPrefix(lines[0], "url = "))
if err != nil {
t.Fatal(err)
}
method, protocol := "GET", "tls12"
if strings.Contains(argv, "--head") {
method = "HEAD"
if !strings.Contains(string(config), "Bearer private-token") {
t.Fatal("missing private header")
}
}
if strings.Contains(argv, "--tlsv1.3") {
protocol = "tls13"
}
if method == "GET" && mode == "runner" {
return errors.New("private-secret")
}
wireMode := ""
if mode == "expect" && method == "GET" {
wireMode = "redirect"
}
configuredWire(raw, method, protocol, wireMode, out, diag)
return nil
}}
code := resourceCheckCLI(context.Background(), resourceCheckFile(t, resourceCheckExample), &output, &diagnostic, d)
if mode == "pass" && (code != 0 || calls != 3 || resolves != 3) {
t.Fatalf("code=%d calls=%d output=%s", code, calls, output.String())
}
if mode != "pass" && code == 0 {
t.Fatal("false pass")
}
if mode == "rebind" && (calls != 2 || !strings.Contains(output.String(), `"outcome":"destination_denied"`)) {
t.Fatal("rebinding was not revalidated", output.String())
}
if mode != "guard" && strings.Count(output.String(), `"outcome":"pass"`) < 2 {
t.Fatal("failure stopped second service", output.String())
}
if mode == "guard" && (calls != 0 || resolves != 0 || guards != 1) {
t.Fatal("unguarded probe")
}
if strings.Contains(output.String()+diagnostic.String(), "secret") || strings.Contains(output.String(), "example.") || strings.Contains(output.String(), "Bearer") {
t.Fatal("private output")
}
})
}
}
func TestResourceCheckTransferContract(t *testing.T) {
for _, method := range []string{"GET", "HEAD"} {
for _, mode := range []string{"pass", "chunked", "redirect", "redirect-count", "verify", "origin", "tls", "overflow", "truncated", "extra"} {
t.Run(method+"/"+mode, func(t *testing.T) {
if method == "HEAD" && mode == "chunked" {
return
}
runner := func(_ context.Context, _ []string, _ io.Reader, out, diag io.Writer) error {
configuredWire("https://example.com/", method, "tls12", mode, out, diag)
return nil
}
transfer, _, err := collectResourceTransferOptions(context.Background(), "socks5h://127.0.0.1:1080", "https://example.com/", "example.com", "tls12", false, runner, resourceTransferOptions{generic: true, method: method, limit: 100, timeout: time.Second})
valid := mode == "pass" || mode == "chunked" || method == "HEAD" && mode == "truncated"
if (err == nil) != valid {
t.Fatalf("err=%v", err)
}
if method == "HEAD" && err == nil && len(transfer.Body) != 0 {
t.Fatal("HEAD body")
}
})
}
}
}
func TestResourceCheckCancellationAndPrevalidation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
joined := false
d := resourceCheckDeps{lookup: resourceCheckLookup, guard: func() error { return nil }, resolve: func(context.Context, string, string) ([]net.IP, error) {
return []net.IP{net.ParseIP("93.184.216.34")}, nil
}, runner: func(ctx context.Context, _ []string, _ io.Reader, _, _ io.Writer) error {
cancel()
<-ctx.Done()
joined = true
return ctx.Err()
}}
var output, diagnostic bytes.Buffer
if resourceCheckCLI(ctx, resourceCheckFile(t, resourceCheckExample), &output, &diagnostic, d) != 1 || !joined || strings.Count(output.String(), "cancelled") != 1 {
t.Fatal("cancel/join")
}
d.guard = func() error { t.Fatal("guard before all config validated"); return nil }
bad := strings.Replace(resourceCheckExample, `"CHECK_URL"`, `"MISSING"`, 1)
if resourceCheckCLI(context.Background(), resourceCheckFile(t, bad), &output, &diagnostic, d) != 2 {
t.Fatal("prevalidation")
}
// Runtime child deadlines are bounded and returned only after the child joins.
d.guard = func() error { return nil }
d.runner = func(ctx context.Context, _ []string, _ io.Reader, _, _ io.Writer) error {
<-ctx.Done()
return ctx.Err()
}
short := strings.ReplaceAll(resourceCheckExample, `"timeout_ms":1000`, `"timeout_ms":100`)
output.Reset()
if resourceCheckCLI(context.Background(), resourceCheckFile(t, short), &output, &diagnostic, d) != 1 || strings.Count(output.String(), `"outcome":"timeout"`) != 3 {
t.Fatal("timeouts", output.String())
}
}
// This loopback-only SOCKS fixture observes real curl's CONNECT destination.
// It rejects CONNECT immediately: no TLS or resource traffic leaves the fixture.
func TestResourceCheckCurlSOCKSPinnedDestination(t *testing.T) {
if _, err := exec.LookPath("curl"); err != nil {
t.Skip("curl unavailable")
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
target := make(chan string, 1)
go func() {
conn, err := listener.Accept()
if err != nil {
target <- "accept failed"
return
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(3 * time.Second))
fail := func() { target <- "invalid SOCKS framing" }
var hello [2]byte
if _, err := io.ReadFull(conn, hello[:]); err != nil || hello[0] != 5 {
fail()
return
}
methods := make([]byte, int(hello[1]))
if _, err := io.ReadFull(conn, methods); err != nil {
fail()
return
}
conn.Write([]byte{5, 0})
var header [4]byte
if _, err := io.ReadFull(conn, header[:]); err != nil || header[0] != 5 || header[1] != 1 {
fail()
return
}
var host string
switch header[3] {
case 1:
b := make([]byte, 4)
if _, err := io.ReadFull(conn, b); err != nil {
fail()
return
}
host = net.IP(b).String()
case 3:
var size [1]byte
if _, err := io.ReadFull(conn, size[:]); err != nil {
fail()
return
}
b := make([]byte, int(size[0]))
if _, err := io.ReadFull(conn, b); err != nil {
fail()
return
}
host = string(b)
default:
fail()
return
}
var port [2]byte
if _, err := io.ReadFull(conn, port[:]); err != nil || port != [2]byte{1, 187} {
fail()
return
}
target <- host
conn.Write([]byte{5, 5, 0, 1, 0, 0, 0, 0, 0, 0})
}()
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
_, _, err = collectResourceTransferOptions(ctx, "socks5h://"+listener.Addr().String(), "https://example.com/private?secret=value", "example.com", "tls12", false, runResourceCurl, resourceTransferOptions{generic: true, method: "GET", limit: 100, timeout: 3 * time.Second, config: "connect-to = \"example.com:443:93.184.216.34:443\"\n"})
if err == nil {
t.Fatal("fixture unexpectedly transferred")
}
select {
case host := <-target:
if host != "93.184.216.34" {
t.Fatal("CONNECT not pinned", host)
}
case <-ctx.Done():
t.Fatal("SOCKS child not joined")
}
}
+46 -6
View File
@@ -220,6 +220,19 @@ func playerMediaResource(body []byte) (mediaResource, mediaCapability, error) {
const resourceTrailer = "\nAUTOTUNE_RESOURCE_METRICS:" const resourceTrailer = "\nAUTOTUNE_RESOURCE_METRICS:"
func collectResourceTransfer(ctx context.Context, proxy, raw, host, protocol string, media bool, runner subprocessRunner) (assetTransfer, http.Header, error) { func collectResourceTransfer(ctx context.Context, proxy, raw, host, protocol string, media bool, runner subprocessRunner) (assetTransfer, http.Header, error) {
return collectResourceTransferOptions(ctx, proxy, raw, host, protocol, media, runner, resourceTransferOptions{})
}
type resourceTransferOptions struct {
method string
limit int
timeout time.Duration
// Private curl config only: never placed in argv or public evidence.
config string
generic bool
}
func collectResourceTransferOptions(ctx context.Context, proxy, raw, host, protocol string, media bool, runner subprocessRunner, options resourceTransferOptions) (assetTransfer, http.Header, error) {
fail := func(err error) (assetTransfer, http.Header, error) { fail := func(err error) (assetTransfer, http.Header, error) {
return assetTransfer{}, nil, resourceFailure(ctx, err) return assetTransfer{}, nil, resourceFailure(ctx, err)
} }
@@ -240,15 +253,32 @@ func collectResourceTransfer(ctx context.Context, proxy, raw, host, protocol str
if media { if media {
limit = 65536 limit = 65536
} }
if options.generic {
limit = options.limit
}
// Qualification uses HTTP/1.1, not browser HTTP/2. --raw preserves native // Qualification uses HTTP/1.1, not browser HTTP/2. --raw preserves native
// transfer framing so net/http, rather than curl and Go twice, decodes it. // transfer framing so net/http, rather than curl and Go twice, decodes it.
args := []string{"--disable", "--silent", "--show-error", "--verbose", "--http1.1", "--raw", "--noproxy", "", "--proxy", proxy, "--proto", "=https", tlsFlag, "--tls-max", maxTLS, "--max-time", "30", "--max-filesize", strconv.Itoa(limit), "--suppress-connect-headers", "--include", "--write-out", resourceTrailer + "%{json}", "--config", "-"} args := []string{"--disable", "--silent", "--show-error", "--verbose", "--http1.1", "--raw", "--noproxy", "", "--proxy", proxy, "--proto", "=https", tlsFlag, "--tls-max", maxTLS, "--max-time", "30", "--max-filesize", strconv.Itoa(limit), "--suppress-connect-headers", "--include", "--write-out", resourceTrailer + "%{json}", "--config", "-"}
if media { if media {
args = append(args, "--range", "0-65535") args = append(args, "--range", "0-65535")
} }
if options.generic {
if (options.method != "GET" && options.method != "HEAD") || options.limit < 1 || options.limit > 5<<20 || options.timeout < 100*time.Millisecond || options.timeout > 30*time.Second {
return fail(errors.New("invalid transfer options"))
}
args = append(args, "--max-time", strconv.FormatFloat(options.timeout.Seconds(), 'f', 3, 64))
if options.method == "HEAD" {
args = append(args, "--head", "--max-filesize", "0")
}
}
// curl config quotes are not JSON quotes; reject controls above, escape only these. // curl config quotes are not JSON quotes; reject controls above, escape only these.
config := "url = \"" + strings.NewReplacer("\\", "\\\\", "\"", "\\\"").Replace(raw) + "\"\n" config := "url = \"" + strings.NewReplacer("\\", "\\\\", "\"", "\\\"").Replace(raw) + "\"\n"
childCtx, cancel := context.WithTimeout(ctx, 35*time.Second) config += options.config
timeout := 35 * time.Second
if options.generic {
timeout = options.timeout
}
childCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel() defer cancel()
var output, diagnostic bytes.Buffer var output, diagnostic bytes.Buffer
out := &playerBoundedWriter{dst: &output, limit: limit + (96 << 10)} out := &playerBoundedWriter{dst: &output, limit: limit + (96 << 10)}
@@ -317,31 +347,41 @@ func collectResourceTransfer(ctx context.Context, proxy, raw, host, protocol str
return fail(errors.New("ambiguous response framing")) return fail(errors.New("ambiguous response framing"))
} }
r := bufio.NewReader(bytes.NewReader(wire)) r := bufio.NewReader(bytes.NewReader(wire))
response, err := http.ReadResponse(r, nil) var request *http.Request
if options.generic {
request = &http.Request{Method: options.method}
}
response, err := http.ReadResponse(r, request)
if err != nil { if err != nil {
return fail(errors.New("invalid response headers")) return fail(errors.New("invalid response headers"))
} }
defer response.Body.Close() defer response.Body.Close()
// CONNECT headers are suppressed by curl. Interim/duplicate responses and // CONNECT headers are suppressed by curl. Interim/duplicate responses and
// unrequested versions are outside this contract, never heuristically skipped. // unrequested versions are outside this contract, never heuristically skipped.
if response.ProtoMajor != 1 || response.ProtoMinor != 1 || response.StatusCode < 200 || response.Header.Get("Content-Type") == "" { if response.ProtoMajor != 1 || response.ProtoMinor != 1 || response.StatusCode < 200 || (!options.generic && response.Header.Get("Content-Type") == "") {
return fail(errors.New("unsupported response contract")) return fail(errors.New("unsupported response contract"))
} }
if options.generic && response.StatusCode >= 300 && response.StatusCode < 400 {
return fail(errors.New("redirect rejected"))
}
body, err := io.ReadAll(io.LimitReader(response.Body, int64(limit+1))) body, err := io.ReadAll(io.LimitReader(response.Body, int64(limit+1)))
// With --raw curl counts the wire body, including chunk framing; the image // With --raw curl counts the wire body, including chunk framing; the image
// limit and durable byte identity apply to the fully decoded payload instead. // limit and durable byte identity apply to the fully decoded payload instead.
wireSize := len(wire) - headerEnd - 4 wireSize := len(wire) - headerEnd - 4
_, remaining := r.Peek(1) _, remaining := r.Peek(1)
if err != nil || remaining != io.EOF || len(body) == 0 || len(body) > limit || metrics.Size != float64(wireSize) || response.StatusCode != metrics.Status { if err != nil || remaining != io.EOF || (!options.generic && len(body) == 0) || len(body) > limit || metrics.Size != float64(wireSize) || response.StatusCode != metrics.Status {
return fail(errors.New("incomplete or oversized resource body")) return fail(errors.New("incomplete or oversized resource body"))
} }
for _, n := range []float64{metrics.Time, metrics.Speed} { for _, n := range []float64{metrics.Time, metrics.Speed} {
if n <= 0 || math.IsNaN(n) || math.IsInf(n, 0) { if n < 0 || (!options.generic && n == 0) || math.IsNaN(n) || math.IsInf(n, 0) {
return fail(errors.New("invalid resource metrics")) return fail(errors.New("invalid resource metrics"))
} }
} }
if options.generic && metrics.Time > options.timeout.Seconds()+1 {
return fail(errors.New("resource timing exceeds budget"))
}
contentType, _, err := mime.ParseMediaType(response.Header.Get("Content-Type")) contentType, _, err := mime.ParseMediaType(response.Header.Get("Content-Type"))
if err != nil { if err != nil && (!options.generic || response.Header.Get("Content-Type") != "") {
return fail(errors.New("invalid content type")) return fail(errors.New("invalid content type"))
} }
t := assetTransfer{EffectiveHost: host, SSLVerifyResult: metrics.Verify, Redirects: *metrics.Redirects, TLSVersion: version, Status: metrics.Status, ContentType: contentType, Body: body, LatencyMS: metrics.Time * 1000, SpeedKBPS: metrics.Speed / 1024} t := assetTransfer{EffectiveHost: host, SSLVerifyResult: metrics.Verify, Redirects: *metrics.Redirects, TLSVersion: version, Status: metrics.Status, ContentType: contentType, Body: body, LatencyMS: metrics.Time * 1000, SpeedKBPS: metrics.Speed / 1024}
+245
View File
@@ -0,0 +1,245 @@
package main
import (
"context"
"crypto/rand"
"encoding/binary"
"io"
"net"
"strings"
"time"
)
func resourceDNSQuery(host string, kind uint16) ([]byte, error) {
if !discoveryHostname(host) {
return nil, errDNSForwarder
}
query := make([]byte, 12)
if _, err := rand.Read(query[:2]); err != nil {
return nil, errDNSForwarder
}
query[2], query[5] = 1, 1
for _, label := range strings.Split(strings.ToLower(host), ".") {
query = append(query, byte(len(label)))
query = append(query, label...)
}
query = append(query, 0, byte(kind>>8), byte(kind), 0, 1)
if !validDNSQuery(query) {
return nil, errDNSForwarder
}
return query, nil
}
func resourceDNSExchange(ctx context.Context, server string, query []byte, dial dnsDialFunc) ([]byte, error) {
conn, err := dial(ctx, "tcp", server)
if err != nil {
return nil, errDNSForwarder
}
defer conn.Close()
deadline := time.Now().Add(5 * time.Second)
if d, ok := ctx.Deadline(); ok && d.Before(deadline) {
deadline = d
}
if conn.SetDeadline(deadline) != nil {
return nil, errDNSForwarder
}
// Closing the connection interrupts framed I/O, including contexts cancelled
// without a deadline. The callback is joined before returning.
done := make(chan struct{})
stop := context.AfterFunc(ctx, func() { conn.Close(); close(done) })
defer func() {
if !stop() {
<-done
}
}()
var size [2]byte
binary.BigEndian.PutUint16(size[:], uint16(len(query)))
if writeDNSFrame(conn, size[:], query) != nil {
return nil, errDNSForwarder
}
if _, err := io.ReadFull(conn, size[:]); err != nil {
return nil, errDNSForwarder
}
n := int(binary.BigEndian.Uint16(size[:]))
if n < 12 {
return nil, errDNSForwarder
}
response := make([]byte, n)
if _, err := io.ReadFull(conn, response); err != nil {
return nil, errDNSForwarder
}
if ctx.Err() != nil || !validDNSResponse(query, response) || response[2]&2 != 0 || response[3]&15 != 0 {
return nil, errDNSForwarder
}
return response, nil
}
func resourceDNSHost(name []byte) (string, bool) {
var labels []string
for i := 0; i < len(name); {
n := int(name[i])
i++
if n == 0 {
host := strings.ToLower(strings.Join(labels, "."))
return host, i == len(name) && discoveryHostname(host)
}
if n > 63 || i+n > len(name) {
return "", false
}
labels = append(labels, string(name[i:i+n]))
i += n
}
return "", false
}
// Only Answer records on a complete, unambiguous chain from the original
// question contribute addresses. Authority/Additional glue is never a target.
func resourceDNSAnswers(query, wire []byte, priorAliases map[string]bool) ([]net.IP, string, error) {
if !validDNSResponse(query, wire) {
return nil, "", errDNSForwarder
}
_, question, ok := dnsQuestion(query)
if !ok {
return nil, "", errDNSForwarder
}
host, ok := resourceDNSHost(question[:len(question)-4])
if !ok {
return nil, "", errDNSForwarder
}
end, ok := dnsQuestionEnd(wire)
if !ok {
return nil, "", errDNSForwarder
}
count := int(binary.BigEndian.Uint16(wire[6:8]))
if count > 128 {
return nil, "", errDNSForwarder
}
aliases := map[string]string{}
addresses := map[string][]net.IP{}
for i := 0; i < count; i++ {
header, name, ok := dnsName(wire, end)
if !ok || header+10 > len(wire) {
return nil, "", errDNSForwarder
}
owner, valid := resourceDNSHost(name)
if !valid {
return nil, "", errDNSForwarder
}
kind, class := binary.BigEndian.Uint16(wire[header:]), binary.BigEndian.Uint16(wire[header+2:])
start := header + 10
end = start + int(binary.BigEndian.Uint16(wire[header+8:]))
if end > len(wire) || class != 1 {
return nil, "", errDNSForwarder
}
switch kind {
case 1, 28:
size := 4
if kind == 28 {
size = 16
}
if end-start != size {
return nil, "", errDNSForwarder
}
addresses[owner] = append(addresses[owner], append(net.IP(nil), wire[start:end]...))
case 5:
last, target, ok := dnsName(wire, start)
if !ok || last != end {
return nil, "", errDNSForwarder
}
alias, ok := resourceDNSHost(target)
if !ok || aliases[owner] != "" {
return nil, "", errDNSForwarder
}
aliases[owner] = alias
}
}
seen := map[string]bool{}
for steps := 0; steps < 16; steps++ {
if seen[host] || priorAliases[host] {
return nil, "", errDNSForwarder
}
seen[host] = true
if alias := aliases[host]; alias != "" {
if len(addresses[host]) != 0 {
return nil, "", errDNSForwarder
}
priorAliases[host] = true
if len(priorAliases) > 16 {
return nil, "", errDNSForwarder
}
host = alias
continue
}
// Unrelated answer addresses/aliases are ambiguous, not a safe partial set.
for owner := range addresses {
if !seen[owner] {
return nil, "", errDNSForwarder
}
}
for owner := range aliases {
if !seen[owner] {
return nil, "", errDNSForwarder
}
}
if len(addresses[host]) > 0 {
return addresses[host], "", nil
}
if len(aliases) > 0 {
return nil, host, nil
}
return nil, "", nil // authenticated framing with NOERROR/NODATA
}
return nil, "", errDNSForwarder
}
func resolveResourceIPsWithDial(ctx context.Context, server, host string, dial dnsDialFunc) ([]net.IP, error) {
serverHost, port, err := net.SplitHostPort(server)
if err != nil || net.ParseIP(serverHost) == nil || (port != "53" && server != dnsListenAddress) || dial == nil {
return nil, errDNSForwarder
}
if ip := net.ParseIP(host); ip != nil {
return []net.IP{ip}, nil
}
var ips []net.IP
unique := map[string]bool{}
for _, kind := range []uint16{1, 28} {
name := host
seen := map[string]bool{}
priorAliases := map[string]bool{}
for steps := 0; ; steps++ {
if steps >= 16 || seen[strings.ToLower(name)] || ctx.Err() != nil {
return nil, errDNSForwarder
}
seen[strings.ToLower(name)] = true
query, err := resourceDNSQuery(name, kind)
if err != nil {
return nil, err
}
wire, err := resourceDNSExchange(ctx, server, query, dial)
if err != nil {
return nil, err
}
answers, alias, err := resourceDNSAnswers(query, wire, priorAliases)
if err != nil {
return nil, err
}
for _, ip := range answers {
if !unique[ip.String()] {
unique[ip.String()] = true
ips = append(ips, ip)
}
if len(ips) > 64 {
return nil, errDNSForwarder
}
}
if alias == "" {
if steps > 0 && len(answers) == 0 {
return nil, errDNSForwarder
}
break
}
name = alias
}
}
return ips, nil
}
+229
View File
@@ -0,0 +1,229 @@
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Generic checks do not inherit PID 1's anchors. Read them through procfs,
// without changing the stricter inherited-FD readiness/cleanup protocol.
func verifyResourceDNSRuntime(cfg managedDNSConfig) error {
if os.Geteuid() != 0 {
return errDNSForwarder
}
return verifyResourceDNSRuntimeAt(cfg, "/proc", filepath.Dir(dnsReadyPath), "/usr/local/bin/zapret-autotune")
}
func resourcePrivateFile(dir *os.File, name string) (*os.File, error) {
fd, err := syscall.Openat(int(dir.Fd()), name, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if err != nil {
return nil, errDNSForwarder
}
f := os.NewFile(uintptr(fd), name)
info, err := f.Stat()
if err != nil || !dnsOwnedAnchor(info, false) || info.Size() > 64<<10 {
f.Close()
return nil, errDNSForwarder
}
return f, nil
}
func resourceProcessOwned(proc string, pid int, exe string, args []string) bool {
base := filepath.Join(proc, strconv.Itoa(pid))
actual, err := os.Readlink(filepath.Join(base, "exe"))
if err != nil || actual != exe {
return false
}
cmd, err := os.ReadFile(filepath.Join(base, "cmdline"))
if err != nil || string(cmd) != strings.Join(args, "\x00")+"\x00" {
return false
}
status, err := os.ReadFile(filepath.Join(base, "status"))
if err != nil {
return false
}
uid := strconv.Itoa(os.Geteuid())
parent, owner := false, false
for _, line := range strings.Split(string(status), "\n") {
fields := strings.Fields(line)
if len(fields) == 2 && fields[0] == "PPid:" && fields[1] == "1" {
parent = true
}
if len(fields) == 5 && fields[0] == "Uid:" && fields[1] == uid && fields[2] == uid && fields[3] == uid && fields[4] == uid {
owner = true
}
}
if !parent || !owner {
return false
}
for _, ns := range []string{"pid", "net"} {
self, err := os.Readlink(filepath.Join(proc, "self/ns", ns))
if err != nil {
return false
}
other, err := os.Readlink(filepath.Join(base, "ns", ns))
if err != nil || other != self {
return false
}
}
return true
}
// All sockets matching a binding must be held by this process; finding just
// one matching inode is insufficient when another process owns UDP/TCP.
func resourceProcessSockets(proc string, pid, port int, udp, wildcard bool) bool {
fds, err := os.ReadDir(filepath.Join(proc, strconv.Itoa(pid), "fd"))
if err != nil {
return false
}
owned := map[string]bool{}
for _, fd := range fds {
link, err := os.Readlink(filepath.Join(proc, strconv.Itoa(pid), "fd", fd.Name()))
if err == nil {
owned[link] = true
}
}
tables := []string{"tcp"}
if udp {
tables = append(tables, "udp")
}
for _, table := range tables {
data, err := os.ReadFile(filepath.Join(proc, "net", table))
if err != nil {
return false
}
found := false
for _, line := range strings.Split(string(data), "\n") {
f := strings.Fields(line)
if len(f) < 10 {
continue
}
address := fmt.Sprintf("0100007F:%04X", port)
if !wildcard && f[1] == fmt.Sprintf("00000000:%04X", port) {
return false
}
match := f[1] == address || wildcard && f[1] == fmt.Sprintf("00000000:%04X", port)
if !match {
continue
}
if table == "tcp" && f[3] != "0A" {
continue
}
if table == "udp" && (f[3] != "07" || f[2] != "00000000:0000") {
return false
}
if !owned["socket:["+f[9]+"]"] {
return false
}
found = true
}
if !found {
return false
}
}
return true
}
func verifyResourceDNSRuntimeAt(cfg managedDNSConfig, proc, dir, executable string) error {
if cfg.Mode != "doh" && (cfg.Mode != "native" || cfg.NativeServer == "") {
return errDNSForwarder
}
if managedDirectory(dir) != nil {
return errDNSForwarder
}
fd, err := syscall.Open(dir, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)
if err != nil {
return errDNSForwarder
}
parent := os.NewFile(uintptr(fd), "DNS private directory")
defer parent.Close()
info, err := parent.Stat()
if err != nil || !dnsOwnedAnchor(info, true) {
return errDNSForwarder
}
if cfg.Mode == "doh" {
ready, err := resourcePrivateFile(parent, "dns-ready")
if err != nil {
return err
}
defer ready.Close()
data, err := io.ReadAll(io.LimitReader(ready, 32))
if err != nil {
return errDNSForwarder
}
pid, err := strconv.Atoi(strings.TrimSuffix(string(data), "\n"))
if err != nil || pid <= 1 || string(data) != strconv.Itoa(pid)+"\n" {
return errDNSForwarder
}
anchorDir, err := os.Open(filepath.Join(proc, "1/fd/8"))
if err != nil {
return errDNSForwarder
}
defer anchorDir.Close()
anchor, err := os.Open(filepath.Join(proc, "1/fd/9"))
if err != nil {
return errDNSForwarder
}
defer anchor.Close()
anchorInfo, err := anchorDir.Stat()
if err != nil || !os.SameFile(info, anchorInfo) || dnsReadinessAction(anchorDir, anchor, pid, false) != nil {
return errDNSForwarder
}
if !resourceProcessOwned(proc, pid, executable, []string{"/usr/local/bin/zapret-autotune", "dns-forwarder", "serve"}) || !resourceProcessSockets(proc, pid, 5353, true, false) {
return errDNSForwarder
}
self, err := os.Stat(filepath.Join(proc, "self/exe"))
if err != nil {
return errDNSForwarder
}
peer, err := os.Stat(filepath.Join(proc, strconv.Itoa(pid), "exe"))
if err != nil || !os.SameFile(self, peer) {
return errDNSForwarder
}
}
local, err := resourcePrivateFile(parent, "local.json")
if err != nil {
return err
}
defer local.Close()
var localConfig struct {
Server string `json:"server"`
ServerPort int `json:"server_port"`
LocalPort int `json:"local_port"`
}
decoder := json.NewDecoder(io.LimitReader(local, 64<<10))
if decoder.Decode(&localConfig) != nil || localConfig.Server != "127.0.0.1" || localConfig.ServerPort != 8388 || localConfig.LocalPort != 1080 {
return errDNSForwarder
}
var extra interface{}
if decoder.Decode(&extra) != io.EOF {
return errDNSForwarder
}
processes, err := os.ReadDir(proc)
if err != nil {
return errDNSForwarder
}
localOwned, serverOwned := false, false
for _, entry := range processes {
pid, err := strconv.Atoi(entry.Name())
if err != nil || pid <= 1 {
continue
}
if resourceProcessOwned(proc, pid, "/usr/bin/ss-local", []string{"ss-local", "-c", dir + "/local.json"}) && resourceProcessSockets(proc, pid, 1080, false, true) {
localOwned = true
}
if resourceProcessOwned(proc, pid, "/usr/bin/ss-server", []string{"ss-server", "-c", dir + "/server.json", "-d", cfg.serverAddress()}) && resourceProcessSockets(proc, pid, 8388, false, true) {
serverOwned = true
}
}
if !localOwned || !serverOwned {
return errDNSForwarder
}
return nil
}
+316
View File
@@ -0,0 +1,316 @@
package main
import (
"context"
"encoding/binary"
"errors"
"io"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
)
func resourceDNSNameForTest(host string) []byte {
var name []byte
for _, s := range strings.Split(host, ".") {
name = append(name, byte(len(s)))
name = append(name, s...)
}
return append(name, 0)
}
func resourceDNSRecordForTest(owner string, kind uint16, data []byte) []byte {
r := resourceDNSNameForTest(owner)
r = append(r, byte(kind>>8), byte(kind), 0, 1, 0, 0, 0, 1, byte(len(data)>>8), byte(len(data)))
return append(r, data...)
}
func resourceDNSResponseForTest(q []byte, answers, additional [][]byte) []byte {
r := append([]byte(nil), q...)
r[2], r[3] = 0x81, 0x80
binary.BigEndian.PutUint16(r[6:8], uint16(len(answers)))
binary.BigEndian.PutUint16(r[10:12], uint16(len(additional)))
for _, rr := range answers {
r = append(r, rr...)
}
for _, rr := range additional {
r = append(r, rr...)
}
return r
}
func TestResourceDNSWireOnlyAndAliases(t *testing.T) {
for _, mode := range []string{"hosts", "alias", "follow", "loop", "cross-loop", "incomplete", "additional", "unrelated", "mixed", "mapped", "linklocal", "servfail", "wrong-id", "wrong-question", "bad-cname", "duplicate-cname", "alias-address", "bad-address", "truncated", "cancel"} {
t.Run(mode, func(t *testing.T) {
calls := 0
var workers sync.WaitGroup
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
dial := func(ctx context.Context, network, server string) (net.Conn, error) {
calls++
if network != "tcp" || server != dnsListenAddress {
t.Fatal("wrong DNS route")
}
client, peer := net.Pipe()
workers.Add(1)
go func() {
defer workers.Done()
defer peer.Close()
peer.SetDeadline(time.Now().Add(time.Second))
var size [2]byte
if _, err := io.ReadFull(peer, size[:]); err != nil {
return
}
q := make([]byte, int(binary.BigEndian.Uint16(size[:])))
if _, err := io.ReadFull(peer, q); err != nil {
return
}
end, question, ok := dnsQuestion(q)
if !ok {
t.Error("bad query")
return
}
host, _ := resourceDNSHost(question[:len(question)-4])
kind := binary.BigEndian.Uint16(q[end-4:])
if mode == "cancel" {
cancel()
var b [1]byte
peer.Read(b[:])
return
}
if mode == "truncated" {
peer.Write([]byte{0, 20, 1})
return
}
address := resourceDNSRecordForTest(host, 1, []byte{93, 184, 216, 34})
var answer, additional [][]byte
if kind == 1 {
answer = [][]byte{address}
}
switch mode {
case "alias":
if kind == 1 {
answer = [][]byte{resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("cdn.example")), resourceDNSRecordForTest("cdn.example", 1, []byte{93, 184, 216, 34})}
}
case "follow", "incomplete":
if host != "cdn.example" {
answer = [][]byte{resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("cdn.example"))}
} else if mode == "incomplete" {
answer = nil
} else {
answer = [][]byte{resourceDNSRecordForTest(host, kind, net.ParseIP("2606:4700::1111").To16())}
if kind == 1 {
answer = [][]byte{address}
}
}
case "loop":
answer = [][]byte{resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("cdn.example")), resourceDNSRecordForTest("cdn.example", 5, resourceDNSNameForTest(host))}
case "cross-loop":
if host == "example.com" {
answer = [][]byte{resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("cdn.example"))}
} else {
answer = [][]byte{resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("example.com")), resourceDNSRecordForTest("example.com", 1, []byte{93, 184, 216, 34})}
}
case "bad-cname":
answer = [][]byte{resourceDNSRecordForTest(host, 5, []byte{0xc0, 12, 0})}
case "duplicate-cname":
answer = [][]byte{resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("one.example")), resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("two.example"))}
case "alias-address":
answer = [][]byte{address, resourceDNSRecordForTest(host, 5, resourceDNSNameForTest("cdn.example"))}
case "bad-address":
answer = [][]byte{resourceDNSRecordForTest(host, 1, []byte{1, 2, 3})}
case "additional":
additional = [][]byte{resourceDNSRecordForTest("other.example", 1, []byte{127, 0, 0, 1})}
case "unrelated":
answer = append(answer, resourceDNSRecordForTest("other.example", 1, []byte{127, 0, 0, 1}))
case "mixed":
if kind == 1 {
answer = append(answer, resourceDNSRecordForTest(host, 1, []byte{10, 0, 0, 1}))
}
case "mapped":
if kind == 28 {
answer = [][]byte{resourceDNSRecordForTest(host, 28, net.ParseIP("::ffff:10.0.0.1").To16())}
}
case "linklocal":
if kind == 28 {
answer = [][]byte{resourceDNSRecordForTest(host, 28, net.ParseIP("fe80::1").To16())}
}
}
r := resourceDNSResponseForTest(q, answer, additional)
if mode == "servfail" {
r[3] = 0x82
}
if mode == "wrong-id" {
r[0] ^= 1
}
if mode == "wrong-question" {
r[13] ^= 1
}
binary.BigEndian.PutUint16(size[:], uint16(len(r)))
writeDNSFrame(peer, size[:], r)
}()
return client, nil
}
host := "example.com"
if mode == "hosts" {
host = "localhost"
}
ips, err := resolveResourceIPsWithDial(ctx, dnsListenAddress, host, dial)
workers.Wait()
valid := mode == "hosts" || mode == "alias" || mode == "follow" || mode == "additional" || mode == "mixed" || mode == "mapped" || mode == "linklocal"
if (err == nil) != valid || calls == 0 {
t.Fatalf("calls=%d ips=%v err=%v", calls, ips, err)
}
if mode == "hosts" && (calls != 2 || len(ips) != 1 || ips[0].String() != "93.184.216.34") {
t.Fatal("hosts file used", ips)
}
if mode == "additional" && len(ips) != 1 {
t.Fatal("additional glue collected", ips)
}
if mode == "mixed" || mode == "mapped" || mode == "linklocal" {
denied := false
for _, ip := range ips {
if !approvedResourceIP(resourceCheckSpec{}, ip) {
denied = true
}
}
if !denied {
t.Fatal("unsafe answer lost", ips)
}
}
})
}
for _, host := range []string{"localhost", "dev.home.zeusina.ru"} {
calls := 0
_, err := resolveResourceIPsWithDial(context.Background(), dnsListenAddress, host, func(context.Context, string, string) (net.Conn, error) { calls++; return nil, errors.New("offline") })
if err == nil || calls != 1 {
t.Fatal("system hosts/DNS bypass")
}
}
}
func resourceRuntimeFixture(t *testing.T) (managedDNSConfig, string, string, string) {
t.Helper()
root := t.TempDir()
proc, dir := filepath.Join(root, "proc"), filepath.Join(root, "private")
mkdir := func(p string) {
t.Helper()
if err := os.MkdirAll(p, 0700); err != nil {
t.Fatal(err)
}
}
write := func(p, s string) {
t.Helper()
if err := os.WriteFile(p, []byte(s), 0600); err != nil {
t.Fatal(err)
}
}
link := func(target, path string) {
t.Helper()
if err := os.Symlink(target, path); err != nil {
t.Fatal(err)
}
}
mkdir(dir)
mkdir(filepath.Join(proc, "net"))
mkdir(filepath.Join(proc, "1/fd"))
mkdir(filepath.Join(proc, "self/ns"))
executable := filepath.Join(root, "binary")
write(executable, "binary")
link(executable, filepath.Join(proc, "self/exe"))
for _, ns := range []string{"pid", "net"} {
link(ns+"[1]", filepath.Join(proc, "self/ns", ns))
}
write(filepath.Join(dir, "dns-ready"), "42\n")
link(dir, filepath.Join(proc, "1/fd/8"))
link(filepath.Join(dir, "dns-ready"), filepath.Join(proc, "1/fd/9"))
write(filepath.Join(dir, "local.json"), `{"server":"127.0.0.1","server_port":8388,"local_port":1080}`)
for _, p := range []struct {
pid int
exe string
args []string
sockets []string
}{
{42, executable, []string{"/usr/local/bin/zapret-autotune", "dns-forwarder", "serve"}, []string{"11", "12"}},
{43, "/usr/bin/ss-local", []string{"ss-local", "-c", dir + "/local.json"}, []string{"13"}},
{44, "/usr/bin/ss-server", []string{"ss-server", "-c", dir + "/server.json", "-d", dnsListenAddress}, []string{"14"}},
} {
base := filepath.Join(proc, strconv.Itoa(p.pid))
mkdir(filepath.Join(base, "fd"))
mkdir(filepath.Join(base, "ns"))
link(p.exe, filepath.Join(base, "exe"))
write(filepath.Join(base, "cmdline"), strings.Join(p.args, "\x00")+"\x00")
uid := strconv.Itoa(os.Geteuid())
write(filepath.Join(base, "status"), "PPid:\t1\nUid:\t"+strings.Join([]string{uid, uid, uid, uid}, "\t")+"\n")
for _, ns := range []string{"pid", "net"} {
link(ns+"[1]", filepath.Join(base, "ns", ns))
}
for i, inode := range p.sockets {
link("socket:["+inode+"]", filepath.Join(base, "fd", strconv.Itoa(i)))
}
}
write(filepath.Join(proc, "net/tcp"), "0: 0100007F:14E9 00000000:0000 0A 0 0 0 0 0 11\n1: 00000000:0438 00000000:0000 0A 0 0 0 0 0 13\n2: 00000000:20C4 00000000:0000 0A 0 0 0 0 0 14\n")
write(filepath.Join(proc, "net/udp"), "0: 0100007F:14E9 00000000:0000 07 0 0 0 0 0 12\n")
return managedDNSConfig{Mode: "doh"}, proc, dir, executable
}
func TestResourceDNSOwnedRuntime(t *testing.T) {
for _, mode := range []string{"valid", "missing", "symlink", "permissions", "stale", "unanchored", "foreign-udp", "foreign-tcp", "foreign-exe", "wrong-parent", "namespace", "ss-dns", "ss-remote"} {
t.Run(mode, func(t *testing.T) {
cfg, proc, dir, exe := resourceRuntimeFixture(t)
write := func(path, data string) {
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
t.Fatal(err)
}
}
switch mode {
case "missing":
os.Remove(filepath.Join(dir, "dns-ready"))
case "symlink":
os.Rename(filepath.Join(dir, "dns-ready"), filepath.Join(dir, "other"))
os.Symlink("other", filepath.Join(dir, "dns-ready"))
case "permissions":
os.Chmod(filepath.Join(dir, "dns-ready"), 0644)
case "stale":
write(filepath.Join(dir, "dns-ready"), "45\n")
case "unanchored":
os.Remove(filepath.Join(proc, "1/fd/9"))
case "foreign-udp":
write(filepath.Join(proc, "net/udp"), "0: 0100007F:14E9 00000000:0000 07 0 0 0 0 0 99\n")
case "foreign-tcp":
write(filepath.Join(proc, "net/tcp"), "0: 0100007F:14E9 00000000:0000 0A 0 0 0 0 0 99\n")
case "foreign-exe":
os.Remove(filepath.Join(proc, "42/exe"))
os.Symlink("/usr/bin/other", filepath.Join(proc, "42/exe"))
case "wrong-parent":
write(filepath.Join(proc, "42/status"), "PPid: 45\nUid: 0 0 0 0\n")
case "namespace":
os.Remove(filepath.Join(proc, "42/ns/net"))
os.Symlink("net[other]", filepath.Join(proc, "42/ns/net"))
case "ss-dns":
write(filepath.Join(proc, "44/cmdline"), "ss-server\x00-c\x00"+dir+"/server.json\x00-d\x008.8.8.8\x00")
case "ss-remote":
write(filepath.Join(dir, "local.json"), `{"server":"8.8.8.8","server_port":8388,"local_port":1080}`)
}
err := verifyResourceDNSRuntimeAt(cfg, proc, dir, exe)
if (err == nil) != (mode == "valid") {
t.Fatalf("err=%v", err)
}
if mode != "valid" {
d := resourceCheckDeps{lookup: resourceCheckLookup, guard: func() error { return verifyResourceDNSRuntimeAt(cfg, proc, dir, exe) }, resolve: func(context.Context, string, string) ([]net.IP, error) {
t.Fatal("DNS probe before ownership")
return nil, nil
}, runner: func(context.Context, []string, io.Reader, io.Writer, io.Writer) error {
t.Fatal("transfer before ownership")
return nil
}}
if code := resourceCheckCLI(context.Background(), resourceCheckFile(t, resourceCheckExample), io.Discard, io.Discard, d); code != 2 {
t.Fatal("ownership failure did not block", code)
}
}
})
}
}
+25
View File
@@ -0,0 +1,25 @@
{
"version": 1,
"services": [
{
"id": "example",
"resources": [
{
"id": "homepage",
"group": "web",
"kind": "http",
"url": "https://example.com/",
"method": "GET",
"tls": "both",
"timeout_ms": 10000,
"expect": {
"statuses": [200],
"content_type": "text/html",
"min_bytes": 1,
"max_bytes": 1048576
}
}
]
}
]
}