From 8f070b16cb86a39bcff48e755a2480bb7edfd93e Mon Sep 17 00:00:00 2001 From: Jonathan Nobels Date: Mon, 19 Oct 2026 12:04:38 +0000 Subject: [PATCH] feature/taildrop: stage consent and incoming file integrity Add the per-file consent state machine and hash-verified receiving path, with lifecycle, limits, resume, and integrity tests. Keep production PUTs on the original receiving implementation until the enablement commit. Leave preferences, policy registration, and published consent metrics for that commit as well. Add an integration guard that checks the production daemon and CLI keep new routes, preferences, commands, metrics, and capabilities hidden at every intermediate boundary; remove it when enabling the feature. Updates tailscale/corp#49569 Signed-off-by: Jonathan Nobels Change-Id: Id790b0133fbbba1e3ba42c0f6bb3bd8785daba6a --- cmd/tailscaled/depaware.txt | 1 + feature/taildrop/consent.go | 683 ++++++++++++ feature/taildrop/consent_test.go | 973 ++++++++++++++++++ feature/taildrop/legacy_send.go | 114 ++ feature/taildrop/peerapi.go | 6 +- feature/taildrop/resume.go | 6 +- feature/taildrop/resume_test.go | 4 +- feature/taildrop/send.go | 115 ++- feature/taildrop/send_test.go | 266 ++++- feature/taildrop/staging_test.go | 102 ++ feature/taildrop/taildrop.go | 46 +- feature/taildrop/taildroptype/taildroptype.go | 42 + ipn/store.go | 5 + 13 files changed, 2323 insertions(+), 40 deletions(-) create mode 100644 feature/taildrop/consent.go create mode 100644 feature/taildrop/consent_test.go create mode 100644 feature/taildrop/legacy_send.go create mode 100644 feature/taildrop/staging_test.go create mode 100644 feature/taildrop/taildroptype/taildroptype.go diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index 6d7749789..7d7683f97 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -336,6 +336,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de L tailscale.com/feature/syslog from tailscale.com/feature/condregister tailscale.com/feature/syspolicy from tailscale.com/feature/condregister tailscale.com/feature/taildrop from tailscale.com/feature/condregister + tailscale.com/feature/taildrop/taildroptype from tailscale.com/feature/taildrop tailscale.com/feature/tailnetlock from tailscale.com/feature/condregister tailscale.com/feature/tailnetlock/tslockjsonv1 from tailscale.com/feature/tailnetlock L tailscale.com/feature/tap from tailscale.com/feature/condregister diff --git a/feature/taildrop/consent.go b/feature/taildrop/consent.go new file mode 100644 index 000000000..6db9520f2 --- /dev/null +++ b/feature/taildrop/consent.go @@ -0,0 +1,683 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package taildrop + +import ( + "cmp" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/binary" + "encoding/hex" + "errors" + "net/http" + "slices" + "strings" + "sync" + "time" + + "golang.org/x/time/rate" + "tailscale.com/feature/taildrop/taildroptype" + "tailscale.com/ipn" + "tailscale.com/tailcfg" + "tailscale.com/util/rands" +) + +// Consent lets the receiving device's owner approve each inbound Taildrop +// transfer before any file data is sent. +// +// If the recipient supports it, the sender first POSTs to /v0/put-request/{filename} +// with a transaction ID, file size, and SHA-256 hash. Approval returns an HMAC token +// binding those fields to the sender, filename, expiry, and a random nonce. The +// sender presents the token on the subsequent PUT, which the receiver verifies +// before reading a single body byte. +// +// The HMAC key never leaves this node, so a token is only meaningful to the node +// that issued it. The nonce keys a server-side record carrying a consumed flag, +// which is what actually enforces one approval per delivery. +// +// Cross-user requests require AllowExternalTaildrop; otherwise they are rejected +// without prompting. Tagged endpoints also require opt-in and consent; unsigned +// nodes cannot participate. Same-user untagged transfers are always implicitly +// allowed, independent of new preferences or +// the testing override. See [putAuthFor]. + +const ( + // consentTokenTTL is how long an approved consent token remains usable. + consentTokenTTL = 5 * time.Minute + + // consentPendingTTL is how long a request may sit awaiting a decision + // before the sender has to ask again. + consentPendingTTL = 5 * time.Minute + + // Bound the number of simultaneous prompts awaiting an owner decision. + maxConsentPeerFiles = 8 + maxConsentFiles = 16 + + // Retain transaction history separately so decisions and expiry free prompt + // slots without allowing repeated polls to resurrect old prompts. + consentTerminalTTL = 5 * time.Minute + maxConsentPeerHistory = 256 + maxConsentHistory = 1024 +) + +// Consent HTTP headers, sent by the sender on a consented PUT. +const ( + hdrConsentTxID = "X-Taildrop-Txid" + hdrConsentToken = "X-Taildrop-Token" + hdrConsentNonce = "X-Taildrop-Nonce" + hdrConsentExpires = "X-Taildrop-Expires" +) + +// PutRequest is the JSON body of a POST /v0/put-request/{filename} request. +type PutRequest struct { + Size int64 `json:"size"` // in bytes + TxID string `json:"txid"` + Hash string `json:"hash"` // lowercase SHA-256 hex digest + +} + +func (r PutRequest) validate() error { + if r.Size < 0 || len(r.TxID) == 0 || len(r.TxID) > 128 || strings.ContainsAny(r.TxID, "\x00\r\n") { + return errors.New("invalid transfer size or transaction ID") + } + b, err := hex.DecodeString(r.Hash) + if err != nil || len(b) != sha256.Size || r.Hash != strings.ToLower(r.Hash) { + return errors.New("invalid SHA-256 hash") + } + return nil +} + +// ConsentToken authorizes exactly one delivery of one file. It is the JSON body +// of a successful POST /v0/put-request/{filename} response. +type ConsentToken struct { + TxID string `json:"txid"` + Hash string `json:"hash"` + Token string `json:"token"` // base64url of the 32-byte HMAC + Nonce string `json:"nonce"` // base64url of the 32-byte nonce + ExpiresAt time.Time `json:"expires_at"` // RFC 3339 +} + +// ConsentState is the state of a consent request, as reported to both the +// sending peer and to local clients. +type ConsentState string + +const ( + ConsentNotAllowed ConsentState = "notallowed" + // ConsentNotRequired means the receiver does not require consent, so the + // sender should proceed with an ordinary PUT. + ConsentNotRequired ConsentState = "notRequired" + + // ConsentPending means the device owner has not yet decided. + ConsentPending ConsentState = "pending" + + // ConsentApproved means the owner approved and a token was issued. + ConsentApproved ConsentState = "approved" + + // ConsentDenied means the owner declined, or the request expired + // undecided. + ConsentDenied ConsentState = "denied" +) + +// Errors reported by [manager.beginConsentedPut]. They are distinct so the +// PeerAPI handler can map each to the HTTP status the sender needs in order to +// decide whether to re-request consent, retry, or give up. +var ( + ErrConsentNotAllowed = errors.New("taildrop: this user has not enabled Taildrop from other users; ask them to enable it on this device") + ErrConsentHashMismatch = errors.New("taildrop: file mismatch") + ErrConsentRequired = errors.New("taildrop: consent required") + ErrConsentExpired = errors.New("taildrop: consent expired") + ErrConsentUnknown = errors.New("taildrop: no matching consent") + ErrConsentConsumed = errors.New("taildrop: consent already used") + ErrConsentMismatch = errors.New("taildrop: consent does not match transfer") + ErrConsentInvalid = errors.New("taildrop: invalid consent token") + ErrConsentMalformed = errors.New("taildrop: malformed consent headers") + ErrConsentDenied = errors.New("taildrop: transfer declined") + ErrConsentLimit = errors.New("taildrop: too many consent requests; try again later") +) + +// consentRecord is the receiver's record of one issued [ConsentToken]. +// +// Records are in-memory only. Losing them across a restart just means the +// sender must ask for consent again. +type consentRecord struct { + TxID string + Hash string + SenderID tailcfg.StableNodeID + Filename string + Size int64 + Nonce [32]byte + ExpiresAt time.Time + + // BytesReceived is how much of the file has been durably received. It + // exists so an interrupted transfer can resume under the same consent: + // the record is not consumed until the whole file has arrived. + BytesReceived int64 + + // InFlight is set while a PUT is actively using this record, so two + // concurrent PUTs can't both slip through before either is consumed. + InFlight bool + + // Consumed is set once the full file has been received. A consumed record + // is never usable again. + Consumed bool +} + +// consentFile is the file awaiting consent. +type consentFile struct { + used bool + txid string + hash string + name string + size int64 + token *ConsentToken // non-nil once the request is approved +} + +// consentRequest tracks the decision for one inbound file. +type consentRequest struct { + id string + senderID tailcfg.StableNodeID + senderName string + file consentFile + requested time.Time + expires time.Time + + decided bool + allowed bool + expiryNotified bool +} + +func (g *consentRequest) asNotify() taildroptype.ConsentRequest { + req := taildroptype.ConsentRequest{ + RequestID: g.id, + PeerID: g.senderID, + PeerName: g.senderName, + File: taildroptype.ConsentFile{Name: g.file.name, Size: g.file.size}, + Requested: g.requested, + Expires: g.expires, + } + return req +} + +// consentStore holds pending consent requests and issued consent records. +// +// Expired transactions remain as bounded tombstones for consentTerminalTTL. +// The expiry worker publishes removals even when there is no peer activity. +type consentStore struct { + mu sync.Mutex + requests map[string]*consentRequest // by request ID + records map[[32]byte]*consentRecord // by nonce + secret [32]byte // HMAC key; valid iff haveSecret + haveSecret bool + admission *rate.Limiter // global new-file rate; polls do not consume tokens + expiryChanged bool // an unannounced prompt was removed by garbage collection +} + +// gcLocked drops requests and records that are no longer actionable. +func (s *consentStore) gcLocked(now time.Time) { + for id, g := range s.requests { + if now.Before(g.expires.Add(consentTerminalTTL)) { + continue + } + if !g.decided && !g.expiryNotified { + s.expiryChanged = true + } + delete(s.requests, id) + } + for nonce, rec := range s.records { + if !rec.InFlight && !now.Before(rec.ExpiresAt.Add(consentTerminalTTL)) { + delete(s.records, nonce) + } + } +} + +// consentSecret returns this node's consent HMAC key, generating and persisting +// it on first use. +func (m *manager) consentSecret() ([32]byte, error) { + s := &m.consent + s.mu.Lock() + defer s.mu.Unlock() + return m.consentSecretLocked() +} + +func (m *manager) consentSecretLocked() ([32]byte, error) { + s := &m.consent + if s.haveSecret { + return s.secret, nil + } + store := m.opts.State + if store != nil { + switch b, err := store.ReadState(ipn.TaildropConsentSecretKey); { + case err == nil && len(b) == len(s.secret): + copy(s.secret[:], b) + s.haveSecret = true + return s.secret, nil + case err != nil && err != ipn.ErrStateNotExist: + return [32]byte{}, err + } + } + var secret [32]byte + if _, err := rand.Read(secret[:]); err != nil { + return [32]byte{}, err + } + if store != nil { + if err := store.WriteState(ipn.TaildropConsentSecretKey, secret[:]); err != nil { + return [32]byte{}, err + } + } + s.secret = secret + s.haveSecret = true + return secret, nil +} + +// consentMessage builds the byte string that a consent token is an HMAC over. +// +// Fields are NULL-separated because neither a StableNodeID nor a filename may +// contain a NULL byte, which makes the encoding unambiguous. +func consentMessage(sender tailcfg.StableNodeID, filename string, expiresAt time.Time, nonce [32]byte, txid, hash string) []byte { + b := make([]byte, 0, len(sender)+len(filename)+8+8+len(nonce)) + b = append(b, sender...) + b = append(b, 0) + b = append(b, filename...) + b = append(b, 0) + b = binary.BigEndian.AppendUint64(b, uint64(expiresAt.Unix())) + b = append(b, nonce[:]...) + b = append(b, txid...) + b = append(b, 0) + return append(b, hash...) +} + +func consentMAC(secret [32]byte, sender tailcfg.StableNodeID, filename string, expiresAt time.Time, nonce [32]byte, txid, hash string) []byte { + mac := hmac.New(sha256.New, secret[:]) + mac.Write(consentMessage(sender, filename, expiresAt, nonce, txid, hash)) + return mac.Sum(nil) +} + +// allowExternalTaildrop reports the effective opt-in preference. +func (m *manager) allowExternalTaildrop() bool { + return m != nil && m.opts.AllowExternalTaildrop != nil && m.opts.AllowExternalTaildrop() +} + +// consentForOwnDevices allows one to override the default behavior for self-owned nodes, simplifying +// local testing and development on a single-user tailnet. +func (m *manager) consentForOwnDevices() bool { + return m.allowExternalTaildrop() && m.opts.ConsentForOwnDevices != nil && m.opts.ConsentForOwnDevices() +} + +// requestConsent registers an inbound transfer as awaiting the device owner's +// approval and reports its current state. +// +// It is idempotent for a given (sender, transaction ID): repeated calls report +// the state of the existing request rather than prompting again, so the sender +// may poll. Each file has an independent request and decision. +func (m *manager) requestConsent(sender tailcfg.StableNodeID, senderName, filename string, req PutRequest) (ConsentState, *ConsentToken, error) { + if err := req.validate(); err != nil { + return "", nil, err + } + size := req.Size + now := m.opts.Clock.Now() + s := &m.consent + s.mu.Lock() + s.gcLocked(now) + defer m.wakeConsentExpiry() + + // Match the transaction across all requests, so concurrent sends from the + // same peer cannot hide each other's decisions. + for _, g := range s.requests { + if g.senderID != sender { + continue + } + if f := &g.file; f.txid == req.TxID { + if f.name != filename || f.size != size || f.hash != req.Hash { + s.mu.Unlock() + return "", nil, ErrConsentMismatch + } + state, tok := requestStateLocked(g, f) + if !now.Before(g.expires) { + state, tok = ConsentDenied, nil + } + s.mu.Unlock() + return state, tok, nil + } + } + var pending, peerPending, peerHistory int + for _, g := range s.requests { + if g.senderID == sender { + peerHistory++ + } + if !g.decided && now.Before(g.expires) { + pending++ + if g.senderID == sender { + peerPending++ + } + } + } + if pending >= maxConsentFiles || peerPending >= maxConsentPeerFiles || + len(s.requests) >= maxConsentHistory || peerHistory >= maxConsentPeerHistory || len(s.records) >= maxConsentHistory { + s.mu.Unlock() + return "", nil, ErrConsentLimit + } + if s.admission == nil { + s.admission = rate.NewLimiter(16, maxConsentFiles) + } + if !s.admission.AllowN(now, 1) { + s.mu.Unlock() + return "", nil, ErrConsentLimit + } + g := &consentRequest{ + id: rands.HexString(16), + senderID: sender, + senderName: senderName, + file: consentFile{name: filename, size: size, txid: req.TxID, hash: req.Hash}, + requested: now, + expires: now.Add(consentPendingTTL), + } + if s.requests == nil { + s.requests = make(map[string]*consentRequest) + } + s.requests[g.id] = g + s.mu.Unlock() + + m.notifyConsent() + return ConsentPending, nil, nil +} + +func requestStateLocked(g *consentRequest, f *consentFile) (ConsentState, *ConsentToken) { + switch { + case !g.decided: + return ConsentPending, nil + case !g.allowed || f.used: + return ConsentDenied, nil + default: + return ConsentApproved, f.token + } +} + +// resolveConsent applies the device owner's decision to a pending request. +// +// On approval it mints a token and a consent record for the file. +func (m *manager) resolveConsent(id string, allow bool) error { + if m == nil { + return ErrNoTaildrop + } + if allow && !m.allowExternalTaildrop() { + return ErrConsentNotAllowed + } + now := m.opts.Clock.Now() + s := &m.consent + s.mu.Lock() + s.gcLocked(now) + + g := s.requests[id] + if g == nil || !now.Before(g.expires) { + s.mu.Unlock() + return ErrConsentUnknown + } + if g.decided { + s.mu.Unlock() + return ErrConsentConsumed + } + + g.decided = true + defer m.wakeConsentExpiry() + g.allowed = allow + + if allow { + metricConsentApproved.Add(1) + } else { + metricConsentDenied.Add(1) + } + + if !allow { + // Keep the request around until it expires so a polling sender learns + // it was denied rather than timing out. + s.mu.Unlock() + m.notifyConsent() + return nil + } + + secret, err := m.consentSecretLocked() + if err != nil { + g.allowed = false + s.mu.Unlock() + m.notifyConsent() + return err + } + + expiresAt := now.Add(consentTokenTTL).Truncate(time.Second) + if g.expires.Before(expiresAt) { + g.expires = expiresAt + } + if s.records == nil { + s.records = make(map[[32]byte]*consentRecord) + } + f := &g.file + var nonce [32]byte + if _, err := rand.Read(nonce[:]); err != nil { + g.allowed = false + s.mu.Unlock() + m.notifyConsent() + return err + } + mac := consentMAC(secret, g.senderID, f.name, expiresAt, nonce, f.txid, f.hash) + f.token = &ConsentToken{ + TxID: f.txid, Hash: f.hash, + Token: base64.RawURLEncoding.EncodeToString(mac), + Nonce: base64.RawURLEncoding.EncodeToString(nonce[:]), + ExpiresAt: expiresAt, + } + s.records[nonce] = &consentRecord{ + TxID: f.txid, Hash: f.hash, + SenderID: g.senderID, + Filename: f.name, + Size: f.size, + Nonce: nonce, + ExpiresAt: expiresAt, + } + s.mu.Unlock() + + m.notifyConsent() + return nil +} + +// pendingConsentRequests returns the requests still awaiting a decision, oldest +// first. +// +// The result is always non-nil, so an empty result tells a client there is +// nothing outstanding rather than telling it nothing at all. +func (m *manager) pendingConsentRequests() []taildroptype.ConsentRequest { + out := make([]taildroptype.ConsentRequest, 0) + if m == nil { + return out + } + now := m.opts.Clock.Now() + s := &m.consent + s.mu.Lock() + defer s.mu.Unlock() + for _, g := range s.requests { + if g.decided || !now.Before(g.expires) { + continue + } + out = append(out, g.asNotify()) + } + slices.SortFunc(out, func(a, b taildroptype.ConsentRequest) int { + if c := a.Requested.Compare(b.Requested); c != 0 { + return c + } + return cmp.Compare(a.RequestID, b.RequestID) + }) + return out +} + +func (m *manager) notifyConsent() { + if m != nil && m.opts.NotifyConsent != nil { + m.opts.NotifyConsent() + } +} + +func (m *manager) wakeConsentExpiry() { + m.consentStartOnce.Do(func() { go m.runConsentExpiry() }) + select { + case m.consentWake <- struct{}{}: + default: + } +} + +// runConsentExpiry owns the timer and exits before manager.Shutdown returns. +// Reads filter expired entries immediately; this worker also dismisses prompts +// and eventually frees terminal records without needing another HTTP request. +func (m *manager) runConsentExpiry() { + defer close(m.consentDone) + for { + now := m.opts.Clock.Now() + s := &m.consent + s.mu.Lock() + s.gcLocked(now) + var next time.Time + changed := s.expiryChanged + s.expiryChanged = false + for _, g := range s.requests { + if !g.decided && !g.expiryNotified && !now.Before(g.expires) { + g.expiryNotified = true + changed = true + } + deadline := g.expires.Add(consentTerminalTTL) + if !g.decided && !g.expiryNotified { + deadline = g.expires + } + if next.IsZero() || deadline.Before(next) { + next = deadline + } + } + for _, rec := range s.records { + deadline := rec.ExpiresAt.Add(consentTerminalTTL) + if !rec.InFlight && (next.IsZero() || deadline.Before(next)) { + next = deadline + } + } + s.mu.Unlock() + if changed { + m.notifyConsent() + } + var timerChan <-chan time.Time + var stop func() + if !next.IsZero() { + timer, ch := m.opts.Clock.NewTimer(max(0, next.Sub(m.opts.Clock.Now()))) + timerChan = ch + stop = func() { timer.Stop() } + } + select { + case <-m.consentStop: + if stop != nil { + stop() + } + return + case <-m.consentWake: + case <-timerChan: + } + if stop != nil { + stop() + } + } +} + +// beginConsentedPut authorizes one consented PUT and marks its consent record +// in use. The caller must pair every nil-error return with exactly one +// [manager.endConsentedPut] for the returned nonce. +// +// It performs every check before the caller reads any request body, so an +// unauthorized sender never gets to write a byte to disk. +func (m *manager) beginConsentedPut(sender tailcfg.StableNodeID, filename string, hdr http.Header, offset, contentLength int64) (nonce [32]byte, err error) { + rawToken := hdr.Get(hdrConsentToken) + rawNonce := hdr.Get(hdrConsentNonce) + rawExpires := hdr.Get(hdrConsentExpires) + if rawToken == "" && rawNonce == "" && rawExpires == "" { + return nonce, ErrConsentRequired + } + + hdrExpires, err := time.Parse(time.RFC3339, rawExpires) + if err != nil { + return nonce, ErrConsentMalformed + } + nonceBytes, err := base64.RawURLEncoding.DecodeString(rawNonce) + if err != nil || len(nonceBytes) != len(nonce) { + return nonce, ErrConsentMalformed + } + copy(nonce[:], nonceBytes) + token, err := base64.RawURLEncoding.DecodeString(rawToken) + if err != nil || len(token) != sha256.Size { + return nonce, ErrConsentMalformed + } + + // A consented transfer must declare its length: the token is bound to a + // specific size, and we can't check a size we weren't told. + if contentLength < 0 { + return nonce, ErrConsentMismatch + } + + now := m.opts.Clock.Now() + s := &m.consent + s.mu.Lock() + defer s.mu.Unlock() + + rec := s.records[nonce] + switch { + case rec == nil: + return nonce, ErrConsentUnknown + case rec.Consumed || rec.InFlight: + return nonce, ErrConsentConsumed + case !now.Before(rec.ExpiresAt): + return nonce, ErrConsentExpired + case rec.SenderID != sender || rec.Filename != filename || rec.TxID != hdr.Get(hdrConsentTxID): + return nonce, ErrConsentMismatch + case offset+contentLength != rec.Size || offset > rec.BytesReceived: + return nonce, ErrConsentMismatch + case !hdrExpires.Equal(rec.ExpiresAt): + // The record's expiry is authoritative; the header only has to agree + // with it, since it is one of the fields the token commits to. + return nonce, ErrConsentMismatch + } + + secret, err := m.consentSecretLocked() + if err != nil { + return nonce, err + } + want := consentMAC(secret, rec.SenderID, rec.Filename, rec.ExpiresAt, nonce, rec.TxID, rec.Hash) + if !hmac.Equal(token, want) { + return nonce, ErrConsentInvalid + } + + rec.InFlight = true + return nonce, nil +} + +// endConsentedPut releases the consent record for nonce. +// +// Completion or a hash mismatch consumes the record. An interrupted transfer +// records its received prefix and leaves the grant available for a retry. +func (m *manager) endConsentedPut(nonce [32]byte, fileLength int64, transferErr error) { + defer m.wakeConsentExpiry() + s := &m.consent + s.mu.Lock() + defer s.mu.Unlock() + rec := s.records[nonce] + if rec == nil { + return + } + rec.InFlight = false + if fileLength > rec.BytesReceived { + rec.BytesReceived = fileLength + } + if transferErr == ErrConsentHashMismatch || (transferErr == nil && rec.BytesReceived >= rec.Size) { + rec.Consumed = true + for _, g := range s.requests { + if g.senderID == rec.SenderID { + if f := &g.file; f.txid == rec.TxID { + f.used = true + } + } + } + } +} diff --git a/feature/taildrop/consent_test.go b/feature/taildrop/consent_test.go new file mode 100644 index 000000000..5a2ce70d5 --- /dev/null +++ b/feature/taildrop/consent_test.go @@ -0,0 +1,973 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package taildrop + +import ( + "crypto/sha256" + "encoding/base64" + "fmt" + "io" + "net/http" + "sync/atomic" + "testing" + "time" + + "tailscale.com/ipn" + "tailscale.com/ipn/store/mem" + "tailscale.com/tailcfg" + "tailscale.com/tstest" + "tailscale.com/tstime" +) + +const ( + testPeerA = tailcfg.StableNodeID("nAAAAAAACNTRL") + testPeerB = tailcfg.StableNodeID("nBBBBBBBCNTRL") +) + +// consentTestManager returns a manager with consent enabled, along with the +// clock driving it and a counter of consent notifications sent. +func consentTestManager(t *testing.T) (m *manager, clock *tstest.Clock, notifies *atomic.Int64) { + t.Helper() + fo, err := newFileOps(t.TempDir()) + if err != nil { + t.Fatal(err) + } + clock = tstest.NewClock(tstest.ClockOpts{Start: time.Date(2026, 6, 15, 14, 0, 0, 0, time.UTC)}) + var n atomic.Int64 + m = managerOptions{ + Logf: t.Logf, + Clock: tstime.DefaultClock{Clock: clock}, + State: &mem.Store{}, + fileOps: fo, + AllowExternalTaildrop: func() bool { return true }, + ConsentForOwnDevices: func() bool { return true }, + NotifyConsent: func() { n.Add(1) }, + }.New() + t.Cleanup(m.Shutdown) + return m, clock, &n +} + +// approve resolves the manager's single pending request and returns the token +// issued for the named file. +func approve(t *testing.T, m *manager, name string, size int64) *ConsentToken { + t.Helper() + pending := m.pendingConsentRequests() + if len(pending) != 1 { + t.Fatalf("pending requests = %d; want 1", len(pending)) + } + if err := m.resolveConsent(pending[0].RequestID, true); err != nil { + t.Fatalf("resolveConsent: %v", err) + } + state, tok, err := m.requestConsent(testPeerA, "peer-a", name, testConsentMetadata(name, size)) + if err != nil { + t.Fatalf("requestConsent after approve: %v", err) + } + if state != ConsentApproved { + t.Fatalf("state after approve = %q; want %q", state, ConsentApproved) + } + if tok == nil { + t.Fatal("approved with no token") + } + return tok +} + +// consentHeaders builds the headers a sender presents on a consented PUT. +func consentHeaders(tok *ConsentToken) http.Header { + return http.Header{ + hdrConsentTxID: []string{tok.TxID}, + hdrConsentToken: []string{tok.Token}, + hdrConsentNonce: []string{tok.Nonce}, + hdrConsentExpires: []string{tok.ExpiresAt.Format(time.RFC3339)}, + } +} + +// TestConsentForOwnDevices verifies that the manager's consentForOwnDevices method +// returns the correct value based on the AllowExternalTaildrop and ConsentForOwnDevices options. +// Typically, self-owned devices do not require consent, but this can be overridden for testing +// or development purposes. +func TestConsentForOwnDevices(t *testing.T) { + tests := []struct { + name string + optIn func() bool + override func() bool + want bool + }{ + { + name: "default", + }, + { + name: "override_without_opt_in", + override: func() bool { return true }, + }, + { + name: "opt_in_without_override", + optIn: func() bool { return true }, + }, + { + name: "opt_in_with_override", + optIn: func() bool { return true }, + override: func() bool { return true }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := managerOptions{Logf: t.Logf, AllowExternalTaildrop: tt.optIn, ConsentForOwnDevices: tt.override}.New() + t.Cleanup(m.Shutdown) + if got := m.consentForOwnDevices(); got != tt.want { + t.Errorf("consentForOwnDevices = %v; want %v", got, tt.want) + } + }) + } +} + +func TestConsentApprovalAfterOptOut(t *testing.T) { + m, _, _ := consentTestManager(t) + m.opts.AllowExternalTaildrop = func() bool { return false } + if err := m.resolveConsent("pending", true); err != ErrConsentNotAllowed { + t.Fatalf("approve after opt-out: %v", err) + } +} + +// TestConsentApproveAndConsume verifies that approving a request issues a +// single-use token, and that polling does not generate duplicate prompts. +func TestConsentApproveAndConsume(t *testing.T) { + tests := []struct { + name string + filename string + size int64 + }{ + {name: "small_file", filename: "foo.txt", size: 5}, + {name: "larger_file", filename: "report.pdf", size: 100}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, notifies := consentTestManager(t) + + state, tok, err := m.requestConsent(testPeerA, "peer-a", tt.filename, testConsentMetadata(tt.filename, tt.size)) + if err != nil { + t.Fatal(err) + } + if state != ConsentPending { + t.Fatalf("state = %q; want %q", state, ConsentPending) + } + if tok != nil { + t.Error("pending request came with a token") + } + if notifies.Load() != 1 { + t.Errorf("notifies = %d; want 1", notifies.Load()) + } + + // Polling must not re-prompt. + if _, _, err := m.requestConsent(testPeerA, "peer-a", tt.filename, testConsentMetadata(tt.filename, tt.size)); err != nil { + t.Fatal(err) + } + if notifies.Load() != 1 { + t.Errorf("notifies after poll = %d; want 1", notifies.Load()) + } + + tok = approve(t, m, tt.filename, tt.size) + + nonce, err := m.beginConsentedPut(testPeerA, tt.filename, consentHeaders(tok), 0, tt.size) + if err != nil { + t.Fatalf("beginConsentedPut: %v", err) + } + m.endConsentedPut(nonce, tt.size, nil) + + // One consent, one transfer: the same token must not work twice. + if _, err := m.beginConsentedPut(testPeerA, tt.filename, consentHeaders(tok), 0, tt.size); err != ErrConsentConsumed { + t.Errorf("second use error = %v; want %v", err, ErrConsentConsumed) + } + + // Nothing is pending once decided. + if got := m.pendingConsentRequests(); len(got) != 0 { + t.Errorf("pending after approval = %d; want 0", len(got)) + } + }) + } +} + +// TestConsentDeny verifies that denied requests remain denied and cannot be +// resolved again. +func TestConsentDeny(t *testing.T) { + tests := []struct { + name string + filename string + size int64 + }{ + {name: "small_file", filename: "foo.txt", size: 5}, + {name: "larger_file", filename: "report.pdf", size: 100}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + + if _, _, err := m.requestConsent(testPeerA, "peer-a", tt.filename, testConsentMetadata(tt.filename, tt.size)); err != nil { + t.Fatal(err) + } + pending := m.pendingConsentRequests() + if len(pending) != 1 { + t.Fatalf("pending = %d; want 1", len(pending)) + } + if err := m.resolveConsent(pending[0].RequestID, false); err != nil { + t.Fatal(err) + } + + state, tok, err := m.requestConsent(testPeerA, "peer-a", tt.filename, testConsentMetadata(tt.filename, tt.size)) + if err != nil { + t.Fatal(err) + } + if state != ConsentDenied { + t.Errorf("state = %q; want %q", state, ConsentDenied) + } + if tok != nil { + t.Error("denied request came with a token") + } + + // A decision may only be made once. + if err := m.resolveConsent(pending[0].RequestID, true); err != ErrConsentConsumed { + t.Errorf("second resolve error = %v; want %v", err, ErrConsentConsumed) + } + // An unknown ID is reported, not silently ignored. + if err := m.resolveConsent("nope", true); err != ErrConsentUnknown { + t.Errorf("unknown resolve error = %v; want %v", err, ErrConsentUnknown) + } + }) + } +} + +// TestConsentTokenTampering verifies that consent tokens reject altered, +// malformed, missing, or mismatched transfer metadata. +func TestConsentTokenTampering(t *testing.T) { + tests := []struct { + name string + sender tailcfg.StableNodeID + filename string + length int64 + mutate func(*ConsentToken, http.Header) + wantErr error + }{ + { + name: "good", + sender: testPeerA, + filename: "foo.txt", + length: 100, + }, + { + name: "wrong_sender", + sender: testPeerB, + filename: "foo.txt", + length: 100, + wantErr: ErrConsentMismatch, + }, + { + name: "wrong_filename", + sender: testPeerA, + filename: "bar.txt", + length: 100, + wantErr: ErrConsentMismatch, + }, + { + name: "wrong_size", + sender: testPeerA, + filename: "foo.txt", + length: 99, + wantErr: ErrConsentMismatch, + }, + { + name: "unknown_length", + sender: testPeerA, + filename: "foo.txt", + length: -1, + wantErr: ErrConsentMismatch, + }, + { + name: "flipped_token", + sender: testPeerA, + filename: "foo.txt", + length: 100, + mutate: func(tok *ConsentToken, hdr http.Header) { + raw, _ := base64.RawURLEncoding.DecodeString(tok.Token) + raw[0] ^= 0x80 + hdr.Set(hdrConsentToken, base64.RawURLEncoding.EncodeToString(raw)) + }, + wantErr: ErrConsentInvalid, + }, + { + name: "no_headers", + sender: testPeerA, + filename: "foo.txt", + length: 100, + mutate: func(_ *ConsentToken, hdr http.Header) { + clear(hdr) + }, + wantErr: ErrConsentRequired, + }, + { + name: "shifted_expiry", + sender: testPeerA, + filename: "foo.txt", + length: 100, + mutate: func(tok *ConsentToken, hdr http.Header) { + hdr.Set(hdrConsentExpires, tok.ExpiresAt.Add(time.Hour).Format(time.RFC3339)) + }, + wantErr: ErrConsentMismatch, + }, + { + name: "malformed_token", + sender: testPeerA, + filename: "foo.txt", + length: 100, + mutate: func(_ *ConsentToken, hdr http.Header) { + hdr.Set(hdrConsentToken, "!!!not-base64!!!") + }, + wantErr: ErrConsentMalformed, + }, + { + name: "malformed_nonce", + sender: testPeerA, + filename: "foo.txt", + length: 100, + mutate: func(_ *ConsentToken, hdr http.Header) { + hdr.Set(hdrConsentNonce, "!!!not-base64!!!") + }, + wantErr: ErrConsentMalformed, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", testConsentMetadata("foo.txt", 100)); err != nil { + t.Fatal(err) + } + tok := approve(t, m, "foo.txt", 100) + hdr := consentHeaders(tok) + if tt.mutate != nil { + tt.mutate(tok, hdr) + } + if _, err := m.beginConsentedPut(tt.sender, tt.filename, hdr, 0, tt.length); err != tt.wantErr { + t.Errorf("beginConsentedPut = %v; want %v", err, tt.wantErr) + } + }) + } +} + +// TestConsentUnknownNonce verifies that unrecognized consent nonces are rejected. +func TestConsentUnknownNonce(t *testing.T) { + tests := []struct { + name string + nonceByte byte + }{ + {name: "zero_nonce", nonceByte: 0}, + {name: "nonzero_nonce", nonceByte: 1}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + var zero [32]byte + for i := range zero { + zero[i] = tt.nonceByte + } + hdr := http.Header{ + hdrConsentToken: []string{base64.RawURLEncoding.EncodeToString(zero[:])}, + hdrConsentNonce: []string{base64.RawURLEncoding.EncodeToString(zero[:])}, + hdrConsentExpires: []string{time.Now().Add(time.Minute).Format(time.RFC3339)}, + } + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", hdr, 0, 100); err != ErrConsentUnknown { + t.Errorf("err = %v; want %v", err, ErrConsentUnknown) + } + }) + } +} + +// TestConsentExpiry verifies that approved consent tokens are accepted before +// expiry and rejected at or after expiry. +func TestConsentExpiry(t *testing.T) { + tests := []struct { + name string + delay time.Duration + wantErr error + }{ + {name: "before_expiry", delay: consentTokenTTL - time.Nanosecond}, + {name: "at_expiry", delay: consentTokenTTL, wantErr: ErrConsentExpired}, + {name: "after_expiry", delay: consentTokenTTL + time.Second, wantErr: ErrConsentExpired}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, clock, _ := consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", testConsentMetadata("foo.txt", 100)); err != nil { + t.Fatal(err) + } + tok := approve(t, m, "foo.txt", 100) + + clock.Advance(tt.delay) + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100); err != tt.wantErr { + t.Errorf("err = %v; want %v", err, tt.wantErr) + } + }) + } +} + +// TestConsentPendingExpiry verifies that pending requests disappear and can no +// longer be resolved once their pending lifetime expires. +func TestConsentPendingExpiry(t *testing.T) { + tests := []struct { + name string + delay time.Duration + wantPending int + wantErr error + }{ + {name: "before_expiry", delay: consentPendingTTL - time.Nanosecond, wantPending: 1}, + {name: "at_expiry", delay: consentPendingTTL, wantErr: ErrConsentUnknown}, + {name: "after_expiry", delay: consentPendingTTL + time.Second, wantErr: ErrConsentUnknown}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, clock, _ := consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", testConsentMetadata("foo.txt", 100)); err != nil { + t.Fatal(err) + } + pending := m.pendingConsentRequests() + if len(pending) != 1 { + t.Fatalf("pending = %d; want 1", len(pending)) + } + + clock.Advance(tt.delay) + if got := m.pendingConsentRequests(); len(got) != tt.wantPending { + t.Errorf("pending after expiry = %d; want %d", len(got), tt.wantPending) + } + // Only requests that have not expired can still be decided. + if err := m.resolveConsent(pending[0].RequestID, true); err != tt.wantErr { + t.Errorf("resolve expired error = %v; want %v", err, tt.wantErr) + } + }) + } +} + +// TestConsentConcurrentPutRejected verifies that a token cannot start two +// concurrent transfers, but may be retried after a failed transfer. +func TestConsentConcurrentPutRejected(t *testing.T) { + tests := []struct { + name string + failure error + }{ + {name: "unexpected_eof", failure: io.ErrUnexpectedEOF}, + {name: "closed_connection", failure: io.ErrClosedPipe}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", testConsentMetadata("foo.txt", 100)); err != nil { + t.Fatal(err) + } + tok := approve(t, m, "foo.txt", 100) + + nonce, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100) + if err != nil { + t.Fatal(err) + } + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100); err != ErrConsentConsumed { + t.Errorf("concurrent use error = %v; want %v", err, ErrConsentConsumed) + } + // A failed transfer releases the record rather than burning it. + m.endConsentedPut(nonce, 0, tt.failure) + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100); err != nil { + t.Errorf("retry after failure: %v", err) + } + }) + } +} + +// TestConsentResume verifies that an approved transfer can resume after a +// failed attempt and is consumed only after the complete file arrives. +func TestConsentResume(t *testing.T) { + tests := []struct { + name string + received int64 + }{ + {name: "restart", received: 0}, + {name: "partial", received: 60}, + {name: "all_bytes_before_failure", received: 100}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", testConsentMetadata("foo.txt", 100)); err != nil { + t.Fatal(err) + } + tok := approve(t, m, "foo.txt", 100) + + // The first attempt fails after receiving the specified prefix. + nonce, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100) + if err != nil { + t.Fatal(err) + } + m.endConsentedPut(nonce, tt.received, io.ErrUnexpectedEOF) + + // The same consent covers the rest, so the user isn't asked twice for one + // file. + nonce, err = m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), tt.received, 100-tt.received) + if err != nil { + t.Fatalf("resume: %v", err) + } + m.endConsentedPut(nonce, 100, nil) + + // Now that the whole file has arrived, it is spent. + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100); err != ErrConsentConsumed { + t.Errorf("after completion err = %v; want %v", err, ErrConsentConsumed) + } + }) + } +} + +// TestConsentResumeBeyondProgress verifies that resumptions cannot claim an +// offset beyond the bytes actually received. +func TestConsentResumeBeyondProgress(t *testing.T) { + tests := []struct { + name string + offset int64 + }{ + {name: "one_byte", offset: 1}, + {name: "partial_file", offset: 40}, + {name: "whole_file", offset: 100}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", testConsentMetadata("foo.txt", 100)); err != nil { + t.Fatal(err) + } + tok := approve(t, m, "foo.txt", 100) + + // Claiming an offset past what we've actually received would let a sender + // leave a hole in the file. + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), tt.offset, 100-tt.offset); err != ErrConsentMismatch { + t.Errorf("err = %v; want %v", err, ErrConsentMismatch) + } + }) + } +} + +// TestConsentSecretPersists verifies that the consent signing secret is +// persisted and reused by managers sharing a state store. +func TestConsentSecretPersists(t *testing.T) { + tests := []struct { + name string + preload bool + }{ + {name: "new_secret"}, + {name: "existing_secret", preload: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store := &mem.Store{} + if tt.preload { + if err := store.WriteState(ipn.TaildropConsentSecretKey, []byte("01234567890123456789012345678901")); err != nil { + t.Fatal(err) + } + } + newMgr := func() *manager { + return managerOptions{ + Logf: t.Logf, + State: store, + AllowExternalTaildrop: func() bool { return true }, + ConsentForOwnDevices: func() bool { return true }, + }.New() + } + + m1 := newMgr() + s1, err := m1.consentSecret() + if err != nil { + t.Fatal(err) + } + m1.Shutdown() + + m2 := newMgr() + s2, err := m2.consentSecret() + if err != nil { + t.Fatal(err) + } + m2.Shutdown() + + if s1 != s2 { + t.Error("consent secret changed across managers sharing a state store") + } + if s1 == ([32]byte{}) { + t.Error("consent secret is all zeros") + } + b, err := store.ReadState(ipn.TaildropConsentSecretKey) + if err != nil { + t.Fatalf("secret not persisted: %v", err) + } + if len(b) != 32 { + t.Errorf("persisted secret length = %d; want 32", len(b)) + } + }) + } +} + +// TestConsentSecretWithoutStateStore verifies that a manager without a state +// store generates and caches a nonzero in-memory consent secret. +func TestConsentSecretWithoutStateStore(t *testing.T) { + tests := []struct { + name string + reads int + }{ + {name: "initial", reads: 1}, + {name: "cached", reads: 3}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := managerOptions{Logf: t.Logf, AllowExternalTaildrop: func() bool { return true }, ConsentForOwnDevices: func() bool { return true }}.New() + t.Cleanup(m.Shutdown) + s, err := m.consentSecret() + if err != nil { + t.Fatalf("consentSecret with no state store: %v", err) + } + for range tt.reads - 1 { + got, err := m.consentSecret() + if err != nil || got != s { + t.Fatalf("cached secret changed: %v", err) + } + } + if s == ([32]byte{}) { + t.Error("consent secret is all zeros") + } + }) + } +} + +// TestConsentTokenBindsToIssuer verifies that a token issued by one manager +// cannot be used with another manager. +func TestConsentTokenBindsToIssuer(t *testing.T) { + tests := []struct { + name string + otherIssuer bool + wantErr error + }{ + {name: "issuing_node"}, {name: "different_node", otherIssuer: true, wantErr: ErrConsentUnknown}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + req := testConsentMetadata("foo.txt", 100) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", req); err != nil { + t.Fatal(err) + } + tok := approve(t, m, "foo.txt", 100) + if tt.otherIssuer { + m, _, _ = consentTestManager(t) + if _, _, err := m.requestConsent(testPeerA, "peer-a", "foo.txt", req); err != nil { + t.Fatal(err) + } + approve(t, m, "foo.txt", 100) + } + if _, err := m.beginConsentedPut(testPeerA, "foo.txt", consentHeaders(tok), 0, 100); err != tt.wantErr { + t.Errorf("beginConsentedPut = %v; want %v", err, tt.wantErr) + } + }) + } +} + +// testConsentMetadata supplies stable transactions for polling tests. +func testConsentMetadata(name string, size int64) PutRequest { + content := "" + switch size { + case 2: + content = "hi" + case 4: + content = "abcd" + case 5: + content = "hello" + case 11: + content = "hello world" + } + return PutRequest{Size: size, TxID: name, Hash: fmt.Sprintf("%x", sha256.Sum256([]byte(content)))} +} + +// TestConsentTransactionMetadataImmutable verifies that polling an existing +// transaction with changed hash or size metadata is rejected. +func TestConsentTransactionMetadataImmutable(t *testing.T) { + tests := []struct { + name string + mutate func(*PutRequest) + wantErr error + }{ + {name: "unchanged"}, + {name: "hash_changed", mutate: func(r *PutRequest) { r.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte("other"))) }, wantErr: ErrConsentMismatch}, + {name: "size_changed", mutate: func(r *PutRequest) { r.Size++ }, wantErr: ErrConsentMismatch}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, _, _ := consentTestManager(t) + req := testConsentMetadata("a", 5) + if _, _, err := m.requestConsent(testPeerA, "peer", "a", req); err != nil { + t.Fatal(err) + } + if tt.mutate != nil { + tt.mutate(&req) + } + if _, _, err := m.requestConsent(testPeerA, "peer", "a", req); err != tt.wantErr { + t.Fatalf("changed metadata: %v; want %v", err, tt.wantErr) + } + }) + } +} + +// TestConsentPollingOlderRequest verifies that deciding a newer request does +// not prevent polling an earlier approved or denied transaction. +func TestConsentPollingOlderRequest(t *testing.T) { + tests := []struct { + name string + allow bool + wantState ConsentState + }{ + {name: "approved", allow: true, wantState: ConsentApproved}, + {name: "denied", wantState: ConsentDenied}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, clock, _ := consentTestManager(t) + first := testConsentMetadata("first", 5) + second := testConsentMetadata("second", 5) + if _, _, err := m.requestConsent(testPeerA, "peer", "first", first); err != nil { + t.Fatal(err) + } + id := m.pendingConsentRequests()[0].RequestID + clock.Advance(time.Second) + if _, _, err := m.requestConsent(testPeerA, "peer", "second", second); err != nil { + t.Fatal(err) + } + if err := m.resolveConsent(id, tt.allow); err != nil { + t.Fatal(err) + } + pending := m.pendingConsentRequests() + if len(pending) != 1 || pending[0].File.Name != "second" { + t.Fatalf("newer pending request = %v; want second", pending) + } + if err := m.resolveConsent(pending[0].RequestID, !tt.allow); err != nil { + t.Fatal(err) + } + if state, _, err := m.requestConsent(testPeerA, "peer", "first", first); err != nil || state != tt.wantState { + t.Fatalf("older transaction lost: %s, %v", state, err) + } + }) + } +} + +// TestConsentIndependentDecisions verifies that consent decisions are scoped to +// individual files: approving or denying one request does not affect another. +func TestConsentIndependentDecisions(t *testing.T) { + m, _, notifies := consentTestManager(t) + a, b := testConsentMetadata("a", 1), testConsentMetadata("b", 1) + if _, _, err := m.requestConsent(testPeerA, "peer", "a", a); err != nil { + t.Fatal(err) + } + shown := m.pendingConsentRequests()[0] + if _, _, err := m.requestConsent(testPeerA, "peer", "b", b); err != nil { + t.Fatal(err) + } + pending := m.pendingConsentRequests() + if len(pending) != 2 || notifies.Load() != 2 { + t.Fatalf("pending=%v, notifications=%d", pending, notifies.Load()) + } + for _, req := range pending { + if (req.File.Name != "a" && req.File.Name != "b") || req.File.Size != 1 { + t.Fatalf("request=%v", req) + } + } + if err := m.resolveConsent(shown.RequestID, true); err != nil { + t.Fatal(err) + } + state, token, err := m.requestConsent(testPeerA, "peer", "a", a) + if err != nil || state != ConsentApproved || token == nil { + t.Fatalf("approved file: %v, %v, %v", state, token, err) + } + state, token, err = m.requestConsent(testPeerA, "peer", "b", b) + if err != nil || state != ConsentPending || token != nil { + t.Fatalf("other file: %v, %v, %v", state, token, err) + } + if err := m.resolveConsent(m.pendingConsentRequests()[0].RequestID, false); err != nil { + t.Fatal(err) + } + state, token, err = m.requestConsent(testPeerA, "peer", "b", b) + if err != nil || state != ConsentDenied || token != nil { + t.Fatalf("denied file: %v, %v, %v", state, token, err) + } + state, token, err = m.requestConsent(testPeerA, "peer", "a", a) + if err != nil || state != ConsentApproved || token == nil { + t.Fatalf("other decision changed approval: %v, %v, %v", state, token, err) + } +} + +// TestConsentExpiryLifecycle verifies that pending requests expire into denied +// tombstones, which are later reclaimed after the terminal-record retention period. +func TestConsentExpiryLifecycle(t *testing.T) { + m, clock, notifies := consentTestManager(t) + req := testConsentMetadata("a", 1) + m.requestConsent(testPeerA, "peer", "a", req) + before := notifies.Load() + clock.Advance(consentPendingTTL) + if err := tstest.WaitFor(time.Second, func() error { + if notifies.Load() <= before { + return fmt.Errorf("no expiry notification") + } + return nil + }); err != nil { + t.Fatal(err) + } + if len(m.pendingConsentRequests()) != 0 { + t.Fatal("expired prompt still visible") + } + for range 3 { + state, token, err := m.requestConsent(testPeerA, "peer", "a", req) + if err != nil || state != ConsentDenied || token != nil { + t.Fatalf("expired poll: %v %v", state, err) + } + clock.Advance(time.Second) + } + clock.Advance(consentTerminalTTL) + if err := tstest.WaitFor(time.Second, func() error { + m.consent.mu.Lock() + defer m.consent.mu.Unlock() + if len(m.consent.requests) != 0 { + return fmt.Errorf("terminal records not reclaimed") + } + return nil + }); err != nil { + t.Fatal(err) + } +} + +// TestConsentAdmissionLimits verifies per-peer, global, and rate admission +// limits, while allowing idempotent polling of an already queued request. +func TestConsentAdmissionLimits(t *testing.T) { + for _, scope := range []string{"peer", "global", "rate"} { + t.Run(scope, func(t *testing.T) { + m, clock, _ := consentTestManager(t) + limit := maxConsentPeerFiles + if scope != "peer" { + limit = maxConsentFiles + } + for i := range limit + 1 { + peer := testPeerA + if scope != "peer" { + peer = tailcfg.StableNodeID(fmt.Sprintf("peer-%d", i)) + } + name := fmt.Sprintf("file-%d", i) + _, _, err := m.requestConsent(peer, "peer", name, testConsentMetadata(name, 1)) + if i == limit { + if err != ErrConsentLimit { + t.Fatalf("limit error = %v", err) + } + } else if err != nil { + t.Fatalf("request %d: %v", i, err) + } + if scope == "rate" && err == nil { + pending := m.pendingConsentRequests() + if len(pending) != 1 { + t.Fatalf("pending requests = %d, want 1", len(pending)) + } + if err := m.resolveConsent(pending[0].RequestID, false); err != nil { + t.Fatal(err) + } + } else if scope != "rate" { + clock.Advance(100 * time.Millisecond) + } + } + // Idempotent polling must still work with a full queue. + peer := testPeerA + if scope != "peer" { + peer = "peer-0" + } + if _, _, err := m.requestConsent(peer, "peer", "file-0", testConsentMetadata("file-0", 1)); err != nil { + t.Fatal(err) + } + }) + } +} + +// TestConsentExpiryAfterSleep verifies that a large clock jump, such as after +// device sleep, clears expired prompts and notifies the GUI to dismiss them. +func TestConsentExpiryAfterSleep(t *testing.T) { + m, clock, notifies := consentTestManager(t) + m.requestConsent(testPeerA, "peer", "a", testConsentMetadata("a", 1)) + before := notifies.Load() + // Timers may not run while the machine sleeps, including past the + // tombstone retention deadline. The GUI must still dismiss its prompt. + clock.Advance(consentPendingTTL + consentTerminalTTL + time.Second) + if err := tstest.WaitFor(time.Second, func() error { + if notifies.Load() <= before { + return fmt.Errorf("no clearing notification after sleep") + } + return nil + }); err != nil { + t.Fatal(err) + } + if len(m.pendingConsentRequests()) != 0 { + t.Fatal("expired prompt remains") + } +} + +// Decided and expired requests retain their history without occupying prompt +// slots. Exercise both the per-peer and receiver-wide limits. +func TestConsentPromptSlotsReleased(t *testing.T) { + tests := []struct { + name string + limit int + global bool + action string // "approve", "deny", or "expire" + }{ + {"per-peer/approve", maxConsentPeerFiles, false, "approve"}, + {"per-peer/deny", maxConsentPeerFiles, false, "deny"}, + {"per-peer/expire", maxConsentPeerFiles, false, "expire"}, + {"global/approve", maxConsentFiles, true, "approve"}, + {"global/deny", maxConsentFiles, true, "deny"}, + {"global/expire", maxConsentFiles, true, "expire"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m, clock, _ := consentTestManager(t) + for round := range 2 { + for i := range tt.limit { + peer := testPeerA + if tt.global && i >= maxConsentPeerFiles { + peer = testPeerB + } + name := fmt.Sprintf("file-%d-%d", round, i) + if state, _, err := m.requestConsent(peer, "peer", name, testConsentMetadata(name, 1)); err != nil || state != ConsentPending { + t.Fatalf("request %s = %v, %v", name, state, err) + } + clock.Advance(100 * time.Millisecond) + } + if _, _, err := m.requestConsent(testPeerA, "peer", "overflow", testConsentMetadata("overflow", 1)); err != ErrConsentLimit { + t.Fatalf("full prompt queue: %v", err) + } + pending := m.pendingConsentRequests() + if len(pending) != tt.limit { + t.Fatalf("pending = %d, want %d", len(pending), tt.limit) + } + if tt.action == "expire" { + clock.Advance(consentPendingTTL) + } else { + for _, req := range pending { + if err := m.resolveConsent(req.RequestID, tt.action == "approve"); err != nil { + t.Fatal(err) + } + } + } + if got := len(m.pendingConsentRequests()); got != 0 { + t.Fatalf("pending after %s = %d", tt.action, got) + } + // The just-resolved requests must remain available to idempotent polls. + m.consent.mu.Lock() + retained := len(m.consent.requests) + m.consent.mu.Unlock() + if retained < tt.limit { + t.Fatalf("history discarded: %d entries", retained) + } + } + }) + } +} diff --git a/feature/taildrop/legacy_send.go b/feature/taildrop/legacy_send.go new file mode 100644 index 000000000..6e3b06fb7 --- /dev/null +++ b/feature/taildrop/legacy_send.go @@ -0,0 +1,114 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package taildrop + +// These entrypoints preserve production behavior until the full taildrop consent flow +// is complete and available for all clients. + +import ( + "fmt" + "io" + + "tailscale.com/envknob" + "tailscale.com/ipn" + "tailscale.com/version/distro" +) + +// PutFile stores a file into [manager.Dir] from a given client id. +// The baseName must be a base filename without any slashes. +// The length is the expected length of content to read from r, +// it may be negative to indicate that it is unknown. +// It returns the length of the entire file. +// +// If there is a failure reading from r, then the partial file is not deleted +// for some period of time. The [manager.PartialFiles] and [manager.HashPartialFile] +// methods may be used to list all partial files and to compute the hash for a +// specific partial file. This allows the client to determine whether to resume +// a partial file. While resuming, PutFile may be called again with a non-zero +// offset to specify where to resume receiving data at. +func (m *manager) legacyPutFile(id clientID, baseName string, r io.Reader, offset, length int64) (fileLength int64, err error) { + + switch { + case m == nil || m.opts.fileOps == nil: + return 0, ErrNoTaildrop + case !envknob.CanTaildrop(): + return 0, ErrNoTaildrop + case distro.Get() == distro.Unraid && !m.opts.DirectFileMode: + return 0, ErrNotAccessible + } + + if err := validateBaseName(baseName); err != nil { + return 0, err + } + + m.deleter.Remove(baseName) + + partialName := baseName + id.partialSuffix() + wc, partialPath, err := m.opts.fileOps.OpenWriter(partialName, offset, 0o666) + if err != nil { + return 0, m.redactAndLogError("Create", err) + } + defer func() { + wc.Close() + if err != nil { + m.deleter.Insert(partialName) + } + }() + + inFileKey := incomingFileKey{id, baseName} + inFile, loaded := m.incomingFiles.LoadOrInit(inFileKey, func() *incomingFile { + inFile := &incomingFile{ + clock: m.opts.Clock, + started: m.opts.Clock.Now(), + size: length, + sendFileNotify: m.opts.SendFileNotify, + } + if m.opts.DirectFileMode { + inFile.partialPath = partialPath + } + return inFile + }) + + inFile.w = wc + + if loaded { + return 0, ErrFileExists + } + defer m.incomingFiles.Delete(inFileKey) + + if st := m.opts.State; st != nil { + if b, _ := st.ReadState(ipn.TaildropReceivedKey); len(b) == 0 { + if werr := st.WriteState(ipn.TaildropReceivedKey, []byte{1}); werr != nil { + m.opts.Logf("WriteState error: %v", werr) + } + } + } + + copyLength, err := io.Copy(wc, r) + if err != nil { + return 0, m.redactAndLogError("Copy", err) + } + if length >= 0 && copyLength != length { + return 0, m.redactAndLogError("Copy", fmt.Errorf("copied %d bytes; expected %d", copyLength, length)) + } + if err := wc.Close(); err != nil { + return 0, m.redactAndLogError("Close", err) + } + + fileLength = offset + copyLength + + inFile.mu.Lock() + inFile.done = true + inFile.mu.Unlock() + + finalPath, err := m.opts.fileOps.Rename(partialPath, baseName) + if err != nil { + return 0, m.redactAndLogError("Rename", err) + } + inFile.finalPath = finalPath + + m.totalReceived.Add(1) + m.opts.SendFileNotify() + return fileLength, nil +} diff --git a/feature/taildrop/peerapi.go b/feature/taildrop/peerapi.go index 62a51ae65..08134c51c 100644 --- a/feature/taildrop/peerapi.go +++ b/feature/taildrop/peerapi.go @@ -10,6 +10,7 @@ import ( "net/http" "net/url" "strings" + "sync/atomic" "time" "tailscale.com/ipn/ipnlocal" @@ -21,6 +22,9 @@ import ( var ( metricPutCalls = clientmetric.NewCounter("peerapi_put") + // Publish consent counters only in the enablement commit. + metricConsentApproved = new(atomic.Int64) + metricConsentDenied = new(atomic.Int64) ) // canPutFile reports whether h can put a file ("Taildrop") to this node. @@ -134,7 +138,7 @@ func handlePeerPutWithBackend(h ipnlocal.PeerAPIHandler, ext extensionForPut, w } offset = ranges[0].Start } - n, err := taildropMgr.PutFile(clientID(fmt.Sprint(id)), baseName, r.Body, offset, r.ContentLength) + n, err := taildropMgr.legacyPutFile(clientID(fmt.Sprint(id)), baseName, r.Body, offset, r.ContentLength) switch err { case nil: d := ext.Clock().Since(t0).Round(time.Second / 10) diff --git a/feature/taildrop/resume.go b/feature/taildrop/resume.go index 208d61de3..01e8e1bd3 100644 --- a/feature/taildrop/resume.go +++ b/feature/taildrop/resume.go @@ -28,7 +28,7 @@ type blockChecksum struct { // checksum is an opaque checksum that is comparable. type checksum struct{ cs [sha256.Size]byte } -func hash(b []byte) checksum { +func hashBlock(b []byte) checksum { return checksum{sha256.Sum256(b)} } func (cs checksum) String() string { @@ -95,7 +95,7 @@ func (m *manager) HashPartialFile(id clientID, baseName string) (next func() (bl case n == 0: return blockChecksum{}, io.EOF default: - return blockChecksum{hash(b[:n]), hashAlgorithm, int64(n)}, nil + return blockChecksum{hashBlock(b[:n]), hashAlgorithm, int64(n)}, nil } } close = f.Close @@ -140,7 +140,7 @@ func resumeReader(r io.Reader, hashNext func() (blockChecksum, error)) (int64, i // Compare the local and remote block checksums. // If it mismatches, then resume from this point. - if cs.Checksum != hash(b) { + if cs.Checksum != hashBlock(b) { return offset, io.MultiReader(bytes.NewReader(b), r), nil } offset += int64(len(b)) diff --git a/feature/taildrop/resume_test.go b/feature/taildrop/resume_test.go index 69dd547a1..98b981e8f 100644 --- a/feature/taildrop/resume_test.go +++ b/feature/taildrop/resume_test.go @@ -39,7 +39,7 @@ func TestResume(t *testing.T) { must.Do(err) must.Do(close()) // Windows wants the file handle to be closed to rename it. - must.Get(m.PutFile("", "foo", r, offset, -1)) + must.Get(m.PutFile("", "foo", r, offset, -1, "")) got := must.Get(os.ReadFile(filepath.Join(dir, "foo"))) if !bytes.Equal(got, want) { t.Errorf("content mismatches") @@ -62,7 +62,7 @@ func TestResume(t *testing.T) { if offset < int64(len(want)) { r = io.MultiReader(io.LimitReader(r, numWant), iotest.ErrReader(io.ErrClosedPipe)) } - if _, err := m.PutFile("", "bar", r, offset, -1); err == nil { + if _, err := m.PutFile("", "bar", r, offset, -1, ""); err == nil { break } if i > 1000 { diff --git a/feature/taildrop/send.go b/feature/taildrop/send.go index 668166d44..39f43447f 100644 --- a/feature/taildrop/send.go +++ b/feature/taildrop/send.go @@ -4,7 +4,10 @@ package taildrop import ( + "crypto/sha256" + "encoding/hex" "fmt" + "hash" "io" "sync" "time" @@ -24,7 +27,7 @@ type incomingFile struct { clock tstime.DefaultClock started time.Time - size int64 // or -1 if unknown; never 0 + size int64 // or -1 if unknown w io.Writer // underlying writer sendFileNotify func() // called when done partialPath string // non-empty in direct mode @@ -63,6 +66,9 @@ func (f *incomingFile) Write(p []byte) (n int, err error) { // The length is the expected length of content to read from r, // it may be negative to indicate that it is unknown. // It returns the length of the entire file. +// If expectedHash is non-empty, it is the expected SHA-256 hash of the entire file +// content, and PutFile will verify that the hash matches after writing the file. +// If the hash does not match, PutFile will delete the partial file and return ErrConsentHashMismatch. // // If there is a failure reading from r, then the partial file is not deleted // for some period of time. The [manager.PartialFiles] and [manager.HashPartialFile] @@ -70,8 +76,7 @@ func (f *incomingFile) Write(p []byte) (n int, err error) { // specific partial file. This allows the client to determine whether to resume // a partial file. While resuming, PutFile may be called again with a non-zero // offset to specify where to resume receiving data at. -func (m *manager) PutFile(id clientID, baseName string, r io.Reader, offset, length int64) (fileLength int64, err error) { - +func (m *manager) PutFile(id clientID, baseName string, r io.Reader, offset, length int64, expectedHash string) (fileLength int64, err error) { switch { case m == nil || m.opts.fileOps == nil: return 0, ErrNoTaildrop @@ -85,11 +90,37 @@ func (m *manager) PutFile(id clientID, baseName string, r io.Reader, offset, len return 0, err } - // and make sure we don't delete it while uploading: - m.deleter.Remove(baseName) + // Check whether there is an in-progress transfer for the file. + inFileKey := incomingFileKey{id, baseName} + inFile, loaded := m.incomingFiles.LoadOrInit(inFileKey, func() *incomingFile { + size := length + if size >= 0 { + size += offset + } + inFile := &incomingFile{ + clock: m.opts.Clock, + started: m.opts.Clock.Now(), + size: size, + copied: offset, + sendFileNotify: m.opts.SendFileNotify, + } + return inFile + }) + + if loaded { + return 0, ErrFileExists + } + defer func() { + m.incomingFiles.Delete(inFileKey) + if err != nil { + m.opts.SendFileNotify() + } + }() // Create (if not already) the partial file with read-write permissions. partialName := baseName + id.partialSuffix() + // A retry must protect the sender's partial, which is what the deleter queues. + m.deleter.Remove(partialName) wc, partialPath, err := m.opts.fileOps.OpenWriter(partialName, offset, 0o666) if err != nil { return 0, m.redactAndLogError("Create", err) @@ -101,27 +132,12 @@ func (m *manager) PutFile(id clientID, baseName string, r io.Reader, offset, len } }() - // Check whether there is an in-progress transfer for the file. - inFileKey := incomingFileKey{id, baseName} - inFile, loaded := m.incomingFiles.LoadOrInit(inFileKey, func() *incomingFile { - inFile := &incomingFile{ - clock: m.opts.Clock, - started: m.opts.Clock.Now(), - size: length, - sendFileNotify: m.opts.SendFileNotify, - } - if m.opts.DirectFileMode { - inFile.partialPath = partialPath - } - return inFile - }) - inFile.w = wc - - if loaded { - return 0, ErrFileExists + if m.opts.DirectFileMode { + inFile.mu.Lock() + inFile.partialPath = partialPath + inFile.mu.Unlock() } - defer m.incomingFiles.Delete(inFileKey) // Record that we have started to receive at least one file. // This is used by the deleter upon a cold-start to scan the directory @@ -134,36 +150,71 @@ func (m *manager) PutFile(id clientID, baseName string, r io.Reader, offset, len } } + var h hash.Hash + var dst io.Writer = inFile + if expectedHash != "" { + h = sha256.New() + if err := m.seedHashForPartialFile(partialName, offset, h); err != nil { + return 0, err + } + dst = io.MultiWriter(inFile, h) + } + // Copy the contents of the file to the writer. - copyLength, err := io.Copy(wc, r) + copyLength, err := io.Copy(dst, r) if err != nil { - return 0, m.redactAndLogError("Copy", err) + return offset + copyLength, m.redactAndLogError("Copy", err) } if length >= 0 && copyLength != length { - return 0, m.redactAndLogError("Copy", fmt.Errorf("copied %d bytes; expected %d", copyLength, length)) + return offset + copyLength, m.redactAndLogError("Copy", fmt.Errorf("copied %d bytes; expected %d", copyLength, length)) } if err := wc.Close(); err != nil { return 0, m.redactAndLogError("Close", err) } + if h != nil { + if hex.EncodeToString(h.Sum(nil)) != expectedHash { + if err := m.opts.fileOps.Remove(partialName); err != nil { + m.opts.Logf("discarding mismatched file: %v", err) + } + return 0, ErrConsentHashMismatch + } + } fileLength = offset + copyLength - inFile.mu.Lock() - inFile.done = true - inFile.mu.Unlock() - - // 6) Finalize (rename/move) the partial into place via FileOps.Rename finalPath, err := m.opts.fileOps.Rename(partialPath, baseName) if err != nil { return 0, m.redactAndLogError("Rename", err) } + inFile.mu.Lock() + inFile.done = true inFile.finalPath = finalPath + inFile.mu.Unlock() m.totalReceived.Add(1) m.opts.SendFileNotify() return fileLength, nil } +// seedHashForPartialFile Seeds the hash for a partial file up to the given offset into the provided hash writer. +// no-op for offset <= 0. Returns an error if the partial file cannot be opened or read. +func (m *manager) seedHashForPartialFile(partialName string, offset int64, h io.Writer) error { + if offset <= 0 { + return nil + } + + rc, err := m.opts.fileOps.OpenReader(partialName) + if err != nil { + return err + } + _, copyErr := io.CopyN(h, rc, offset) + closeErr := rc.Close() + if copyErr != nil { + return copyErr + } + return closeErr +} + func (m *manager) redactAndLogError(stage string, err error) error { err = redactError(err) m.opts.Logf("put %s error: %v", stage, err) diff --git a/feature/taildrop/send_test.go b/feature/taildrop/send_test.go index c1def2afd..15642d5bd 100644 --- a/feature/taildrop/send_test.go +++ b/feature/taildrop/send_test.go @@ -4,11 +4,17 @@ package taildrop import ( + "crypto/sha256" + "errors" + "fmt" + "io" "os" "path/filepath" "strings" "testing" + "testing/iotest" + "tailscale.com/ipn" "tailscale.com/tstime" "tailscale.com/util/must" ) @@ -37,7 +43,7 @@ func TestPutFile(t *testing.T) { }.New() id := clientID("0") - n, err := mgr.PutFile(id, "file.txt", strings.NewReader(content), 0, int64(len(content))) + n, err := mgr.PutFile(id, "file.txt", strings.NewReader(content), 0, int64(len(content)), "") if err != nil { t.Fatalf("PutFile error: %v", err) } @@ -67,3 +73,261 @@ func TestPutFile(t *testing.T) { }) } } + +// hashTestFileOps observes reads and injects failures without changing the +// on-disk contents, so tests can distinguish streaming from post-write hashing. +type hashTestFileOps struct { + FileOps + openReader func(string) (io.ReadCloser, error) + wrapWriter func(io.WriteCloser) io.WriteCloser +} + +func (f hashTestFileOps) OpenReader(name string) (io.ReadCloser, error) { + return f.openReader(name) +} + +func (f hashTestFileOps) OpenWriter(name string, offset int64, perm os.FileMode) (io.WriteCloser, string, error) { + w, path, err := f.FileOps.OpenWriter(name, offset, perm) + if err == nil && f.wrapWriter != nil { + w = f.wrapWriter(w) + } + return w, path, err +} + +type hashTestWriter struct { + io.WriteCloser + writeErr, closeErr error +} + +func (w hashTestWriter) Write(p []byte) (int, error) { + if w.writeErr != nil { + return 0, w.writeErr + } + return w.WriteCloser.Write(p) +} + +func (w hashTestWriter) Close() error { + err := w.WriteCloser.Close() + if w.closeErr != nil { + return w.closeErr + } + return err +} + +// TestPutFileStreamingHash verifies that consented uploads hash incoming bytes +// plus any resumed prefix before publishing; corrupt content and read, write, +// or close failures must not publish a file. +func TestPutFileStreamingHash(t *testing.T) { + fault := errors.New("injected failure") + for _, tt := range []struct { + name, content, prefix string + offset int64 + shortPrefix bool + interrupted bool + readErr, writeErr, closeErr error + wantErr error + }{ + {name: "fresh", content: "hello"}, + {name: "empty"}, + {name: "resume", content: "hello", prefix: "hel", offset: 3}, + {name: "interrupted_resume", content: "hello", prefix: "hel", offset: 3, interrupted: true}, + {name: "resume_earlier", content: "hello", prefix: "hell", offset: 2}, + {name: "resume_complete", content: "hello", prefix: "hello", offset: 5}, + {name: "restart", content: "hello", prefix: "wrong"}, + {name: "corrupt_prefix", content: "hello", prefix: "bad", offset: 3, wantErr: ErrConsentHashMismatch}, + {name: "short_prefix", content: "hello", prefix: "hel", offset: 3, shortPrefix: true, wantErr: io.EOF}, + {name: "prefix_read_error", content: "hello", prefix: "hel", offset: 3, readErr: fault, wantErr: fault}, + {name: "write_error", content: "hello", writeErr: fault, wantErr: fault}, + {name: "close_error", content: "hello", closeErr: fault, wantErr: fault}, + } { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + fo := must.Get(newFileOps(dir)) + id := clientID("hash-test") + const name = "file.txt" + partialName := name + id.partialSuffix() + if tt.prefix != "" && !tt.interrupted { + if err := os.WriteFile(filepath.Join(dir, partialName), []byte(tt.prefix), 0600); err != nil { + t.Fatal(err) + } + } + reads := 0 + ops := hashTestFileOps{FileOps: fo, openReader: func(n string) (io.ReadCloser, error) { + reads++ + if tt.readErr != nil { + return nil, tt.readErr + } + if tt.shortPrefix { + return io.NopCloser(strings.NewReader("h")), nil + } + // The new suffix must not have been written yet. + if fi := must.Get(fo.Stat(n)); fi.Size() != tt.offset { + t.Errorf("read after suffix was written: size=%d, offset=%d", fi.Size(), tt.offset) + } + return fo.OpenReader(n) + }, wrapWriter: func(w io.WriteCloser) io.WriteCloser { + return hashTestWriter{w, tt.writeErr, tt.closeErr} + }} + m := managerOptions{Logf: t.Logf, Clock: tstime.DefaultClock{}, fileOps: ops, SendFileNotify: func() {}}.New() + t.Cleanup(m.Shutdown) + hash := fmt.Sprintf("%x", sha256.Sum256([]byte(tt.content))) + if tt.interrupted { + r := io.MultiReader(strings.NewReader(tt.prefix), iotest.ErrReader(fault)) + n, err := m.PutFile(id, name, r, 0, int64(len(tt.content)), hash) + if !errors.Is(err, fault) || n != int64(len(tt.prefix)) { + t.Fatalf("interrupted PutFile = %d, %v; want %d, %v", n, err, len(tt.prefix), fault) + } + if _, err := fo.Stat(name); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("interrupted transfer published a file: %v", err) + } + } + suffix := tt.content[tt.offset:] + n, err := m.PutFile(id, name, strings.NewReader(suffix), tt.offset, int64(len(suffix)), hash) + if !errors.Is(err, tt.wantErr) { + t.Fatalf("PutFile: %v; want %v", err, tt.wantErr) + } + wantReads := 0 + if tt.offset > 0 { + wantReads = 1 + } + if reads != wantReads { + t.Errorf("OpenReader calls = %d; want %d", reads, wantReads) + } + if tt.wantErr != nil { + if _, err := fo.Stat(name); !errors.Is(err, os.ErrNotExist) { + t.Errorf("failed transfer published a file: %v", err) + } + return + } + if n != int64(len(tt.content)) { + t.Errorf("length = %d; want %d", n, len(tt.content)) + } + if got := must.Get(os.ReadFile(filepath.Join(dir, name))); string(got) != tt.content { + t.Errorf("content = %q; want %q", got, tt.content) + } + }) + } +} + +// TestPutFileWithoutConsentDoesNotReadPrefix verifies that ordinary resumed +// uploads preserve existing bytes without paying the extra prefix-read cost of +// consent hash verification. +func TestPutFileWithoutConsentDoesNotReadPrefix(t *testing.T) { + dir := t.TempDir() + fo := must.Get(newFileOps(dir)) + id := clientID("ordinary") + const name = "file.txt" + if err := os.WriteFile(filepath.Join(dir, name+id.partialSuffix()), []byte("hello"), 0600); err != nil { + t.Fatal(err) + } + ops := hashTestFileOps{FileOps: fo, openReader: func(string) (io.ReadCloser, error) { + t.Error("ordinary resumed PUT opened its prefix for hashing") + return nil, errors.New("unexpected prefix read") + }} + m := managerOptions{Logf: t.Logf, fileOps: ops}.New() + t.Cleanup(m.Shutdown) + n, err := m.PutFile(id, name, strings.NewReader(" world"), 5, 6, "") + if err != nil || n != 11 { + t.Fatalf("PutFile = %d, %v", n, err) + } + if got := string(must.Get(os.ReadFile(filepath.Join(dir, name)))); got != "hello world" { + t.Fatalf("content = %q", got) + } +} + +// Progress describes the whole file, including a resumed prefix, and completion +// is only published once the file has passed integrity checks and been renamed. +func TestPutFileIncomingProgress(t *testing.T) { + for _, hashed := range []bool{false, true} { + for _, tc := range []struct { + name string + offset, length int64 + interrupted bool + }{ + {name: "new", length: 5}, + {name: "resumed", offset: 3, length: 2}, + {name: "complete_prefix", offset: 5}, + {name: "unknown_length", offset: 3, length: -1}, + {name: "interrupted", offset: 3, length: 2, interrupted: true}, + } { + t.Run(fmt.Sprintf("%s/hash=%v", tc.name, hashed), func(t *testing.T) { + dir := t.TempDir() + fo := must.Get(newFileOps(dir)) + id := clientID("sender") + const name = "file.txt" + const content = "hello" + partialName := name + id.partialSuffix() + if tc.offset > 0 { + must.Do(os.WriteFile(filepath.Join(dir, partialName), []byte(content[:tc.offset]), 0600)) + } + var m *manager + var snapshots [][]ipn.PartialFile + m = managerOptions{ + Logf: t.Logf, fileOps: fo, DirectFileMode: true, + SendFileNotify: func() { + files := m.IncomingFiles() + snapshots = append(snapshots, files) + for _, f := range files { + if f.Done { + if got := string(must.Get(os.ReadFile(f.FinalPath))); got != content { + t.Errorf("completed file = %q; want %q", got, content) + } + } else if _, err := os.Stat(filepath.Join(dir, name)); !errors.Is(err, os.ErrNotExist) { + t.Errorf("unfinished file published: %v", err) + } + } + }, + }.New() + t.Cleanup(m.Shutdown) + // An earlier failed attempt queued this partial for deletion. + m.deleter.Insert(partialName) + var body io.Reader = strings.NewReader(content[tc.offset:]) + fault := errors.New("interrupted") + if tc.interrupted { + body = io.MultiReader(body, iotest.ErrReader(fault)) + } + hash := "" + if hashed { + hash = fmt.Sprintf("%x", sha256.Sum256([]byte(content))) + } + n, err := m.PutFile(id, name, body, tc.offset, tc.length, hash) + if n != 5 || (tc.interrupted && !errors.Is(err, fault)) || (!tc.interrupted && err != nil) { + t.Fatalf("PutFile = %d, %v", n, err) + } + if len(snapshots) == 0 { + t.Fatal("no incoming notifications") + } + last := snapshots[len(snapshots)-1] + if tc.interrupted { + if len(last) != 0 { + t.Fatalf("failed transfer remains active: %+v", last) + } + } else if len(last) != 1 || !last[0].Done || last[0].FinalPath == "" { + t.Fatalf("missing completion: %+v", last) + } + wantSize := int64(5) + if tc.length < 0 { + wantSize = -1 + } + for _, files := range snapshots { + for _, f := range files { + if f.Received != 5 || f.DeclaredSize != wantSize { + t.Errorf("progress Received=%d DeclaredSize=%d; want 5, %d", f.Received, f.DeclaredSize, wantSize) + } + if tc.interrupted && f.Done { + t.Error("interrupted transfer reported completion") + } + } + } + if !tc.interrupted { + m.deleter.mu.Lock() + queued := m.deleter.byName[partialName] != nil + m.deleter.mu.Unlock() + if queued { + t.Error("resumed partial is still queued for deletion") + } + } + }) + } + } +} diff --git a/feature/taildrop/staging_test.go b/feature/taildrop/staging_test.go new file mode 100644 index 000000000..fa92d85b4 --- /dev/null +++ b/feature/taildrop/staging_test.go @@ -0,0 +1,102 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package taildrop_test + +import ( + "bytes" + "context" + "encoding/json" + "io" + "net/http" + "runtime" + "strings" + "testing" + "time" + + "tailscale.com/client/tailscale/apitype" + "tailscale.com/tailcfg" + "tailscale.com/tstest/integration" +) + +// Removed by the enablement commit: every preceding commit must keep the +// protocol and its user-visible surfaces dormant in real production binaries. +func TestTaildropStagingSurfaces(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("requires userspace-peer harness") + } + if tailcfg.CurrentCapabilityVersion != 149 { + t.Fatal("consent capability advertised before enablement") + } + env := integration.NewTestEnv(t) + n := integration.NewTestNode(t, env) + n.StartDaemon() + n.AwaitListening() + n.MustUp() + n.AwaitRunning() + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + defer cancel() + c := n.LocalClient() + for _, route := range []struct{ method, path string }{ + {"POST", "file-put-request/unknown/file"}, + {"GET", "taildrop-consent/pending"}, + {"POST", "taildrop-consent/respond"}, + } { + req, err := http.NewRequestWithContext(ctx, route.method, "http://"+apitype.LocalAPIHost+"/localapi/v0/"+route.path, strings.NewReader("{}")) + if err != nil { + t.Fatal(err) + } + resp, err := c.DoLocalRequest(req) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusNotFound { + t.Errorf("%s exposed before enablement: %s", route.path, resp.Status) + } + } + req, err := http.NewRequestWithContext(ctx, "GET", "http://"+apitype.LocalAPIHost+"/localapi/v0/prefs", nil) + if err != nil { + t.Fatal(err) + } + resp, err := c.DoLocalRequest(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + raw, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatal(err) + } + var prefs map[string]json.RawMessage + if err := json.Unmarshal(raw, &prefs); err != nil { + t.Fatal(err) + } + if _, ok := prefs["AllowExternalTaildrop"]; ok { + t.Fatal("receive preference exposed before enablement") + } + metrics, err := c.DaemonMetrics(ctx) + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"taildrop_consent", "peerapi_put_request", "localapi_file_put_request"} { + if bytes.Contains(metrics, []byte(name)) { + t.Errorf("%s metrics published before enablement", name) + } + } + for _, cmd := range []struct { + args []string + hidden string + }{ + {[]string{"file", "--help"}, "consent"}, + {[]string{"set", "--help"}, "allow-external-taildrop"}, + } { + output, err := n.TailscaleForOutput(cmd.args...).CombinedOutput() + if err != nil { + t.Fatalf("%v: %v: %s", cmd.args, err, output) + } + if bytes.Contains(output, []byte(cmd.hidden)) { + t.Errorf("%s exposed before enablement", cmd.hidden) + } + } +} diff --git a/feature/taildrop/taildrop.go b/feature/taildrop/taildrop.go index 9839b8330..2ccc62717 100644 --- a/feature/taildrop/taildrop.go +++ b/feature/taildrop/taildrop.go @@ -13,6 +13,7 @@ import ( "sort" "strconv" "strings" + "sync" "sync/atomic" "unicode" "unicode/utf8" @@ -87,6 +88,18 @@ type managerOptions struct { // to the function when reception completes. // It is not called if nil. SendFileNotify func() + + // ConsentForOwnDevices retains the explicit testing override, gated by opt-in. + // Authorization always exempts same-user transfers, even with this override. + ConsentForOwnDevices func() bool + + // AllowExternalTaildrop must be enabled before cross-user requests may prompt. + AllowExternalTaildrop func() bool + + // NotifyConsent is called whenever the set of transfers awaiting the + // device owner's approval changes, so the host application can present or + // dismiss a prompt. It is not called if nil. + NotifyConsent func() } // manager manages the state for receiving and managing taildropped files. @@ -103,6 +116,29 @@ type manager struct { // emptySince specifies that there were no waiting files // since this value of totalReceived. emptySince atomic.Int64 + + // consent holds pending consent requests and issued consent tokens. + // See consent.go. + consent consentStore + + // consentWake signals the expiry worker to recompute its next deadline + // after consent state changes. Its buffer coalesces multiple wakeups. + consentWake chan struct{} + + // consentStop is closed by Shutdown to stop the consent expiry worker. + consentStop chan struct{} + + // consentDone is closed when the expiry worker exits, or by Shutdown if + // the worker never started. Shutdown waits on it before returning. + consentDone chan struct{} + + // consentStopOnce ensures Shutdown closes consentStop at most once. + consentStopOnce sync.Once + + // consentStartOnce starts the expiry worker at most once, on first use. + // Shutdown also uses it to prevent a later start and close consentDone + // if the worker never started. + consentStartOnce sync.Once } // New initializes a new taildrop manager. @@ -115,7 +151,12 @@ func (opts managerOptions) New() *manager { if opts.SendFileNotify == nil { opts.SendFileNotify = func() {} } - m := &manager{opts: opts} + m := &manager{ + opts: opts, + consentWake: make(chan struct{}, 1), + consentStop: make(chan struct{}), + consentDone: make(chan struct{}), + } m.deleter.Init(m, func(string) {}) m.emptySince.Store(-1) // invalidate this cache return m @@ -125,6 +166,9 @@ func (opts managerOptions) New() *manager { // It blocks until all spawned goroutines have stopped running. func (m *manager) Shutdown() { if m != nil { + m.consentStopOnce.Do(func() { close(m.consentStop) }) + m.consentStartOnce.Do(func() { close(m.consentDone) }) + <-m.consentDone m.deleter.shutdown() m.deleter.group.Wait() } diff --git a/feature/taildrop/taildroptype/taildroptype.go b/feature/taildrop/taildroptype/taildroptype.go new file mode 100644 index 000000000..ba36b9d96 --- /dev/null +++ b/feature/taildrop/taildroptype/taildroptype.go @@ -0,0 +1,42 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +// Package taildroptype holds Taildrop types shared by the feature, notifications, +// and the LocalAPI client. +package taildroptype + +import ( + "time" + + "tailscale.com/tailcfg" +) + +// ConsentRequest is an inbound Taildrop file awaiting the device owner's +// approval. Each file has its own request and consent token. +type ConsentRequest struct { + // RequestID identifies this request. It is passed back to the LocalAPI + // to approve or deny, and is not reused. + RequestID string + + // PeerID is the stable node ID of the sending node. + PeerID tailcfg.StableNodeID + + // PeerName is the sending node's display name, for showing to the user. + PeerName string + + // File is the file the peer wants to send. + File ConsentFile + + // Requested is when consent for this file was requested. + Requested time.Time + + // Expires is when this request stops being actionable. Responding after + // this point has no effect and the sender must ask again. + Expires time.Time +} + +// ConsentFile describes the file in a [ConsentRequest]. +type ConsentFile struct { + Name string // the filename as it will be written to disk i.e. "image.jpg" + Size int64 // declared size in bytes +} diff --git a/ipn/store.go b/ipn/store.go index 49007c14e..a5591bbde 100644 --- a/ipn/store.go +++ b/ipn/store.go @@ -60,6 +60,11 @@ const ( // has ever been received (even if partially). // Any non-empty value indicates that at least one file has been received. TaildropReceivedKey = StateKey("_taildrop-received") + + // TaildropConsentSecretKey stores a 32-byte random key used to HMAC + // Taildrop consent tokens. It is generated on first use and never rotated + // unless the state store is wiped. The secret never leaves this node. + TaildropConsentSecretKey = StateKey("_taildrop-consent-secret") ) // StateStoreHealth is a Warnable set when store.New fails at startup. If