2023-01-27 21:37:20 +00:00
|
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-04-20 03:21:48 +00:00
|
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2023-10-27 21:20:10 +00:00
|
|
|
|
"context"
|
2024-01-31 21:49:52 +00:00
|
|
|
|
"encoding/json"
|
2021-04-20 03:21:48 +00:00
|
|
|
|
"fmt"
|
2021-04-20 04:00:25 +00:00
|
|
|
|
"io"
|
2021-04-20 04:57:08 +00:00
|
|
|
|
"io/fs"
|
2021-04-22 16:21:30 +00:00
|
|
|
|
"math/rand"
|
2021-04-20 03:21:48 +00:00
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2022-07-26 03:55:44 +00:00
|
|
|
|
"net/netip"
|
2021-04-20 03:21:48 +00:00
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2023-10-27 21:20:10 +00:00
|
|
|
|
"slices"
|
2021-04-20 03:21:48 +00:00
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2023-10-12 16:28:46 +00:00
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-07-25 03:08:42 +00:00
|
|
|
|
"go4.org/netipx"
|
2023-10-27 21:20:10 +00:00
|
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
|
|
|
|
"tailscale.com/appc"
|
2024-01-23 00:57:31 +00:00
|
|
|
|
"tailscale.com/appc/appctest"
|
2023-10-12 16:28:46 +00:00
|
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2021-11-25 17:43:39 +00:00
|
|
|
|
"tailscale.com/ipn"
|
2022-11-09 05:58:10 +00:00
|
|
|
|
"tailscale.com/ipn/store/mem"
|
2021-04-20 03:21:48 +00:00
|
|
|
|
"tailscale.com/tailcfg"
|
2023-10-05 23:05:45 +00:00
|
|
|
|
"tailscale.com/taildrop"
|
2021-09-08 02:27:19 +00:00
|
|
|
|
"tailscale.com/tstest"
|
2021-11-25 17:43:39 +00:00
|
|
|
|
"tailscale.com/types/logger"
|
2022-11-16 18:36:01 +00:00
|
|
|
|
"tailscale.com/types/netmap"
|
2022-11-09 05:58:10 +00:00
|
|
|
|
"tailscale.com/util/must"
|
2021-11-25 17:43:39 +00:00
|
|
|
|
"tailscale.com/wgengine"
|
|
|
|
|
"tailscale.com/wgengine/filter"
|
2021-04-20 03:21:48 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type peerAPITestEnv struct {
|
|
|
|
|
ph *peerAPIHandler
|
|
|
|
|
rr *httptest.ResponseRecorder
|
2021-09-08 02:27:19 +00:00
|
|
|
|
logBuf tstest.MemLogger
|
2021-04-20 03:21:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type check func(*testing.T, *peerAPITestEnv)
|
|
|
|
|
|
|
|
|
|
func checks(vv ...check) []check { return vv }
|
|
|
|
|
|
|
|
|
|
func httpStatus(wantStatus int) check {
|
|
|
|
|
return func(t *testing.T, e *peerAPITestEnv) {
|
|
|
|
|
if res := e.rr.Result(); res.StatusCode != wantStatus {
|
|
|
|
|
t.Errorf("HTTP response code = %v; want %v", res.Status, wantStatus)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bodyContains(sub string) check {
|
|
|
|
|
return func(t *testing.T, e *peerAPITestEnv) {
|
|
|
|
|
if body := e.rr.Body.String(); !strings.Contains(body, sub) {
|
|
|
|
|
t.Errorf("HTTP response body does not contain %q; got: %s", sub, body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 04:57:08 +00:00
|
|
|
|
func bodyNotContains(sub string) check {
|
|
|
|
|
return func(t *testing.T, e *peerAPITestEnv) {
|
|
|
|
|
if body := e.rr.Body.String(); strings.Contains(body, sub) {
|
|
|
|
|
t.Errorf("HTTP response body unexpectedly contains %q; got: %s", sub, body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 04:00:25 +00:00
|
|
|
|
func fileHasSize(name string, size int) check {
|
2021-04-20 03:21:48 +00:00
|
|
|
|
return func(t *testing.T, e *peerAPITestEnv) {
|
2023-10-17 20:46:05 +00:00
|
|
|
|
root := e.ph.ps.taildrop.Dir()
|
2021-04-20 03:21:48 +00:00
|
|
|
|
if root == "" {
|
|
|
|
|
t.Errorf("no rootdir; can't check whether %q has size %v", name, size)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
path := filepath.Join(root, name)
|
|
|
|
|
if fi, err := os.Stat(path); err != nil {
|
|
|
|
|
t.Errorf("fileHasSize(%q, %v): %v", name, size, err)
|
2021-04-20 04:00:25 +00:00
|
|
|
|
} else if fi.Size() != int64(size) {
|
2021-04-20 03:21:48 +00:00
|
|
|
|
t.Errorf("file %q has size %v; want %v", name, fi.Size(), size)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 04:00:25 +00:00
|
|
|
|
func fileHasContents(name string, want string) check {
|
|
|
|
|
return func(t *testing.T, e *peerAPITestEnv) {
|
2023-10-17 20:46:05 +00:00
|
|
|
|
root := e.ph.ps.taildrop.Dir()
|
2021-04-20 04:00:25 +00:00
|
|
|
|
if root == "" {
|
|
|
|
|
t.Errorf("no rootdir; can't check contents of %q", name)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
path := filepath.Join(root, name)
|
2022-09-15 12:06:59 +00:00
|
|
|
|
got, err := os.ReadFile(path)
|
2021-04-20 04:00:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("fileHasContents: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if string(got) != want {
|
|
|
|
|
t.Errorf("file contents = %q; want %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 04:57:08 +00:00
|
|
|
|
func hexAll(v string) string {
|
|
|
|
|
var sb strings.Builder
|
2024-04-16 20:15:13 +00:00
|
|
|
|
for i := range len(v) {
|
2021-04-20 04:57:08 +00:00
|
|
|
|
fmt.Fprintf(&sb, "%%%02x", v[i])
|
|
|
|
|
}
|
|
|
|
|
return sb.String()
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 20:01:42 +00:00
|
|
|
|
func TestHandlePeerAPI(t *testing.T) {
|
2021-04-20 03:21:48 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
isSelf bool // the peer sending the request is owned by us
|
2022-09-25 18:29:55 +00:00
|
|
|
|
capSharing bool // self node has file sharing capability
|
2022-11-16 18:36:01 +00:00
|
|
|
|
debugCap bool // self node has debug capability
|
2021-04-20 03:21:48 +00:00
|
|
|
|
omitRoot bool // don't configure
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs []*http.Request
|
2021-04-20 03:21:48 +00:00
|
|
|
|
checks []check
|
|
|
|
|
}{
|
2021-04-20 04:57:08 +00:00
|
|
|
|
{
|
|
|
|
|
name: "not_peer_api",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "/", nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("This is my Tailscale device."),
|
|
|
|
|
bodyContains("You are the owner of this node."),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "not_peer_api_not_owner",
|
|
|
|
|
isSelf: false,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "/", nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("This is my Tailscale device."),
|
|
|
|
|
bodyNotContains("You are the owner of this node."),
|
|
|
|
|
),
|
|
|
|
|
},
|
2021-04-22 20:01:42 +00:00
|
|
|
|
{
|
2022-11-16 18:36:01 +00:00
|
|
|
|
name: "goroutines/deny-self-no-cap",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
debugCap: false,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "/v0/goroutines", nil)},
|
2022-11-16 18:36:01 +00:00
|
|
|
|
checks: checks(httpStatus(403)),
|
2021-04-22 20:01:42 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
2022-11-16 18:36:01 +00:00
|
|
|
|
name: "goroutines/deny-nonself",
|
|
|
|
|
isSelf: false,
|
|
|
|
|
debugCap: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "/v0/goroutines", nil)},
|
2022-11-16 18:36:01 +00:00
|
|
|
|
checks: checks(httpStatus(403)),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "goroutines/accept-self",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
debugCap: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "/v0/goroutines", nil)},
|
2021-04-22 20:01:42 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("ServeHTTP"),
|
|
|
|
|
),
|
|
|
|
|
},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
{
|
|
|
|
|
name: "reject_non_owner_put",
|
|
|
|
|
isSelf: false,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo", nil)},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(http.StatusForbidden),
|
2023-10-16 17:36:31 +00:00
|
|
|
|
bodyContains("Taildrop disabled"),
|
2021-04-20 03:21:48 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "owner_without_cap",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: false,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo", nil)},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(http.StatusForbidden),
|
2023-10-16 19:35:11 +00:00
|
|
|
|
bodyContains("Taildrop disabled"),
|
2021-04-20 03:21:48 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "owner_with_cap_no_rootdir",
|
|
|
|
|
omitRoot: true,
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo", nil)},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
checks: checks(
|
2023-10-12 16:28:46 +00:00
|
|
|
|
httpStatus(http.StatusForbidden),
|
2021-12-01 23:00:23 +00:00
|
|
|
|
bodyContains("Taildrop disabled; no storage directory"),
|
2021-04-20 03:21:48 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-04-20 04:00:25 +00:00
|
|
|
|
name: "bad_method",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("POST", "/v0/put/foo", nil)},
|
2021-04-20 04:00:25 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(405),
|
2023-10-16 19:35:11 +00:00
|
|
|
|
bodyContains("expected method GET or PUT"),
|
2021-04-20 04:00:25 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_zero_length",
|
2021-04-20 03:21:48 +00:00
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo", nil)},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("{}"),
|
|
|
|
|
fileHasSize("foo", 0),
|
2021-04-20 04:00:25 +00:00
|
|
|
|
fileHasContents("foo", ""),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_non_zero_length_content_length",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo", strings.NewReader("contents"))},
|
2021-04-20 04:00:25 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("{}"),
|
|
|
|
|
fileHasSize("foo", len("contents")),
|
|
|
|
|
fileHasContents("foo", "contents"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_non_zero_length_chunked",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo", struct{ io.Reader }{strings.NewReader("contents")})},
|
2021-04-20 04:00:25 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("{}"),
|
|
|
|
|
fileHasSize("foo", len("contents")),
|
|
|
|
|
fileHasContents("foo", "contents"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_partial",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo.partial", nil)},
|
2021-04-20 04:00:25 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:00:25 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
2021-04-26 16:48:34 +00:00
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_deleted",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo.deleted", nil)},
|
2021-04-26 16:48:34 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-26 16:48:34 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
2021-04-20 04:00:25 +00:00
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_dot",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/.", nil)},
|
2021-04-20 04:00:25 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 03:21:48 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_empty",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/", nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-16 17:36:31 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_slash",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/foo/bar", nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-16 17:36:31 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_encoded_dot",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("."), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_encoded_slash",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("/"), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_encoded_backslash",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("\\"), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_encoded_dotdot",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll(".."), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "bad_filename_encoded_dotdot_out",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("foo/../../../../../etc/passwd"), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_spaces_and_caps",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("Foo Bar.dat"), strings.NewReader("baz"))},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("{}"),
|
|
|
|
|
fileHasContents("Foo Bar.dat", "baz"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_unicode",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("Томас и его друзья.mp3"), strings.NewReader("главный озорник"))},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
bodyContains("{}"),
|
|
|
|
|
fileHasContents("Томас и его друзья.mp3", "главный озорник"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_invalid_utf8",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+(hexAll("😜")[:3]), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_invalid_null",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/%00", nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_invalid_non_printable",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/%01", nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_invalid_colon",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll("nul:"), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "put_invalid_surrounding_whitespace",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("PUT", "/v0/put/"+hexAll(" foo "), nil)},
|
2021-04-20 04:57:08 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(400),
|
2023-10-12 16:28:46 +00:00
|
|
|
|
bodyContains("invalid filename"),
|
2021-04-20 04:57:08 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
2022-11-16 16:53:51 +00:00
|
|
|
|
{
|
2022-11-16 18:36:01 +00:00
|
|
|
|
name: "host-val/bad-ip",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
debugCap: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "http://12.23.45.66:1234/v0/env", nil)},
|
2022-11-16 16:53:51 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(403),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-11-16 18:36:01 +00:00
|
|
|
|
name: "host-val/no-port",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
debugCap: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "http://100.100.100.101/v0/env", nil)},
|
2022-11-16 16:53:51 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(403),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-11-16 18:36:01 +00:00
|
|
|
|
name: "host-val/peer",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
debugCap: true,
|
2023-09-26 17:22:13 +00:00
|
|
|
|
reqs: []*http.Request{httptest.NewRequest("GET", "http://peer/v0/env", nil)},
|
2022-11-16 16:53:51 +00:00
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
),
|
|
|
|
|
},
|
2023-09-26 17:22:13 +00:00
|
|
|
|
{
|
2023-10-12 16:28:46 +00:00
|
|
|
|
name: "duplicate_zero_length",
|
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
|
|
|
|
reqs: []*http.Request{
|
|
|
|
|
httptest.NewRequest("PUT", "/v0/put/foo", nil),
|
|
|
|
|
httptest.NewRequest("PUT", "/v0/put/foo", nil),
|
|
|
|
|
},
|
|
|
|
|
checks: checks(
|
|
|
|
|
httpStatus(200),
|
|
|
|
|
func(t *testing.T, env *peerAPITestEnv) {
|
|
|
|
|
got, err := env.ph.ps.taildrop.WaitingFiles()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("WaitingFiles error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
want := []apitype.WaitingFile{{Name: "foo", Size: 0}}
|
|
|
|
|
if diff := cmp.Diff(got, want); diff != "" {
|
|
|
|
|
t.Fatalf("WaitingFile mismatch (-got +want):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "duplicate_non_zero_length_content_length",
|
2023-09-26 17:22:13 +00:00
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-10-12 16:28:46 +00:00
|
|
|
|
reqs: []*http.Request{
|
|
|
|
|
httptest.NewRequest("PUT", "/v0/put/foo", strings.NewReader("contents")),
|
|
|
|
|
httptest.NewRequest("PUT", "/v0/put/foo", strings.NewReader("contents")),
|
|
|
|
|
},
|
2023-09-26 17:22:13 +00:00
|
|
|
|
checks: checks(
|
2023-10-12 16:28:46 +00:00
|
|
|
|
httpStatus(200),
|
|
|
|
|
func(t *testing.T, env *peerAPITestEnv) {
|
|
|
|
|
got, err := env.ph.ps.taildrop.WaitingFiles()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("WaitingFiles error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
want := []apitype.WaitingFile{{Name: "foo", Size: 8}}
|
|
|
|
|
if diff := cmp.Diff(got, want); diff != "" {
|
|
|
|
|
t.Fatalf("WaitingFile mismatch (-got +want):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-09-26 17:22:13 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-10-12 16:28:46 +00:00
|
|
|
|
name: "duplicate_different_files",
|
2023-09-26 17:22:13 +00:00
|
|
|
|
isSelf: true,
|
|
|
|
|
capSharing: true,
|
2023-10-12 16:28:46 +00:00
|
|
|
|
reqs: []*http.Request{
|
|
|
|
|
httptest.NewRequest("PUT", "/v0/put/foo", strings.NewReader("fizz")),
|
|
|
|
|
httptest.NewRequest("PUT", "/v0/put/foo", strings.NewReader("buzz")),
|
|
|
|
|
},
|
2023-09-26 17:22:13 +00:00
|
|
|
|
checks: checks(
|
2023-10-12 16:28:46 +00:00
|
|
|
|
httpStatus(200),
|
|
|
|
|
func(t *testing.T, env *peerAPITestEnv) {
|
|
|
|
|
got, err := env.ph.ps.taildrop.WaitingFiles()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("WaitingFiles error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
want := []apitype.WaitingFile{{Name: "foo", Size: 4}, {Name: "foo (1)", Size: 4}}
|
|
|
|
|
if diff := cmp.Diff(got, want); diff != "" {
|
|
|
|
|
t.Fatalf("WaitingFile mismatch (-got +want):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-09-26 17:22:13 +00:00
|
|
|
|
),
|
|
|
|
|
},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-11-16 18:36:01 +00:00
|
|
|
|
selfNode := &tailcfg.Node{
|
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
|
netip.MustParsePrefix("100.100.100.101/32"),
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-11-16 19:07:21 +00:00
|
|
|
|
if tt.debugCap {
|
2024-03-23 23:23:59 +00:00
|
|
|
|
selfNode.CapMap = tailcfg.NodeCapMap{tailcfg.CapabilityDebug: nil}
|
2022-11-16 19:07:21 +00:00
|
|
|
|
}
|
2021-04-20 03:21:48 +00:00
|
|
|
|
var e peerAPITestEnv
|
|
|
|
|
lb := &LocalBackend{
|
2021-09-08 02:27:19 +00:00
|
|
|
|
logf: e.logBuf.Logf,
|
2021-04-22 07:25:00 +00:00
|
|
|
|
capFileSharing: tt.capSharing,
|
2023-08-21 17:53:57 +00:00
|
|
|
|
netMap: &netmap.NetworkMap{SelfNode: selfNode.View()},
|
2023-07-27 19:41:31 +00:00
|
|
|
|
clock: &tstest.Clock{},
|
2021-04-20 03:21:48 +00:00
|
|
|
|
}
|
|
|
|
|
e.ph = &peerAPIHandler{
|
2022-11-16 19:07:21 +00:00
|
|
|
|
isSelf: tt.isSelf,
|
2023-08-21 17:53:57 +00:00
|
|
|
|
selfNode: selfNode.View(),
|
2023-08-18 14:57:44 +00:00
|
|
|
|
peerNode: (&tailcfg.Node{
|
2021-04-20 03:21:48 +00:00
|
|
|
|
ComputedName: "some-peer-name",
|
2023-08-18 14:57:44 +00:00
|
|
|
|
}).View(),
|
2021-04-20 03:21:48 +00:00
|
|
|
|
ps: &peerAPIServer{
|
2022-11-16 19:07:21 +00:00
|
|
|
|
b: lb,
|
2021-04-20 03:21:48 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
2021-04-20 04:57:08 +00:00
|
|
|
|
var rootDir string
|
2021-04-20 03:21:48 +00:00
|
|
|
|
if !tt.omitRoot {
|
2021-04-20 04:57:08 +00:00
|
|
|
|
rootDir = t.TempDir()
|
2023-10-05 23:05:45 +00:00
|
|
|
|
if e.ph.ps.taildrop == nil {
|
2023-10-17 20:46:05 +00:00
|
|
|
|
e.ph.ps.taildrop = taildrop.ManagerOptions{
|
2023-10-12 23:50:11 +00:00
|
|
|
|
Logf: e.logBuf.Logf,
|
2023-10-17 20:46:05 +00:00
|
|
|
|
Dir: rootDir,
|
|
|
|
|
}.New()
|
2023-10-05 23:05:45 +00:00
|
|
|
|
}
|
2021-04-20 03:21:48 +00:00
|
|
|
|
}
|
2023-09-26 17:22:13 +00:00
|
|
|
|
for _, req := range tt.reqs {
|
|
|
|
|
e.rr = httptest.NewRecorder()
|
|
|
|
|
if req.Host == "example.com" {
|
|
|
|
|
req.Host = "100.100.100.101:12345"
|
|
|
|
|
}
|
|
|
|
|
e.ph.ServeHTTP(e.rr, req)
|
2022-11-16 16:53:51 +00:00
|
|
|
|
}
|
2021-04-20 03:21:48 +00:00
|
|
|
|
for _, f := range tt.checks {
|
|
|
|
|
f(t, &e)
|
|
|
|
|
}
|
2021-04-20 04:57:08 +00:00
|
|
|
|
if t.Failed() && rootDir != "" {
|
|
|
|
|
t.Logf("Contents of %s:", rootDir)
|
|
|
|
|
des, _ := fs.ReadDir(os.DirFS(rootDir), ".")
|
|
|
|
|
for _, de := range des {
|
|
|
|
|
fi, err := de.Info()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Log(err)
|
|
|
|
|
} else {
|
|
|
|
|
t.Logf(" %v %5d %s", fi.Mode(), fi.Size(), de.Name())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-20 03:21:48 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-22 16:21:30 +00:00
|
|
|
|
|
|
|
|
|
// Windows likes to hold on to file descriptors for some indeterminate
|
|
|
|
|
// amount of time after you close them and not let you delete them for
|
|
|
|
|
// a bit. So test that we work around that sufficiently.
|
|
|
|
|
func TestFileDeleteRace(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
ps := &peerAPIServer{
|
|
|
|
|
b: &LocalBackend{
|
2021-04-22 07:25:00 +00:00
|
|
|
|
logf: t.Logf,
|
|
|
|
|
capFileSharing: true,
|
2023-07-27 19:41:31 +00:00
|
|
|
|
clock: &tstest.Clock{},
|
2021-04-22 16:21:30 +00:00
|
|
|
|
},
|
2023-10-17 20:46:05 +00:00
|
|
|
|
taildrop: taildrop.ManagerOptions{
|
2023-10-12 23:50:11 +00:00
|
|
|
|
Logf: t.Logf,
|
|
|
|
|
Dir: dir,
|
2023-10-17 20:46:05 +00:00
|
|
|
|
}.New(),
|
2021-04-22 16:21:30 +00:00
|
|
|
|
}
|
|
|
|
|
ph := &peerAPIHandler{
|
|
|
|
|
isSelf: true,
|
2023-08-18 14:57:44 +00:00
|
|
|
|
peerNode: (&tailcfg.Node{
|
2021-04-22 16:21:30 +00:00
|
|
|
|
ComputedName: "some-peer-name",
|
2023-08-18 14:57:44 +00:00
|
|
|
|
}).View(),
|
2023-08-21 17:53:57 +00:00
|
|
|
|
selfNode: (&tailcfg.Node{
|
2022-11-16 19:07:21 +00:00
|
|
|
|
Addresses: []netip.Prefix{netip.MustParsePrefix("100.100.100.101/32")},
|
2023-08-21 17:53:57 +00:00
|
|
|
|
}).View(),
|
2021-04-22 16:21:30 +00:00
|
|
|
|
ps: ps,
|
|
|
|
|
}
|
|
|
|
|
buf := make([]byte, 2<<20)
|
2024-04-16 20:15:13 +00:00
|
|
|
|
for range 30 {
|
2021-04-22 16:21:30 +00:00
|
|
|
|
rr := httptest.NewRecorder()
|
2022-11-16 16:53:51 +00:00
|
|
|
|
ph.ServeHTTP(rr, httptest.NewRequest("PUT", "http://100.100.100.101:123/v0/put/foo.txt", bytes.NewReader(buf[:rand.Intn(len(buf))])))
|
2021-04-22 16:21:30 +00:00
|
|
|
|
if res := rr.Result(); res.StatusCode != 200 {
|
|
|
|
|
t.Fatal(res.Status)
|
|
|
|
|
}
|
2023-10-05 23:05:45 +00:00
|
|
|
|
wfs, err := ps.taildrop.WaitingFiles()
|
2021-04-22 16:21:30 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(wfs) != 1 {
|
|
|
|
|
t.Fatalf("waiting files = %d; want 1", len(wfs))
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 23:05:45 +00:00
|
|
|
|
if err := ps.taildrop.DeleteFile("foo.txt"); err != nil {
|
2021-04-22 16:21:30 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2023-10-05 23:05:45 +00:00
|
|
|
|
wfs, err = ps.taildrop.WaitingFiles()
|
2021-04-22 16:21:30 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(wfs) != 0 {
|
|
|
|
|
t.Fatalf("waiting files = %d; want 0", len(wfs))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-26 16:48:34 +00:00
|
|
|
|
|
2021-11-25 17:43:39 +00:00
|
|
|
|
func TestPeerAPIReplyToDNSQueries(t *testing.T) {
|
|
|
|
|
var h peerAPIHandler
|
|
|
|
|
|
|
|
|
|
h.isSelf = true
|
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("for isSelf = false; want true")
|
|
|
|
|
}
|
|
|
|
|
h.isSelf = false
|
2022-07-26 03:55:44 +00:00
|
|
|
|
h.remoteAddr = netip.MustParseAddrPort("100.150.151.152:12345")
|
2021-11-25 17:43:39 +00:00
|
|
|
|
|
|
|
|
|
eng, _ := wgengine.NewFakeUserspaceEngine(logger.Discard, 0)
|
2023-01-31 01:28:13 +00:00
|
|
|
|
pm := must.Get(newProfileManager(new(mem.Store), t.Logf))
|
2021-11-25 17:43:39 +00:00
|
|
|
|
h.ps = &peerAPIServer{
|
|
|
|
|
b: &LocalBackend{
|
2022-11-09 05:58:10 +00:00
|
|
|
|
e: eng,
|
|
|
|
|
pm: pm,
|
|
|
|
|
store: pm.Store(),
|
2021-11-25 17:43:39 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
if h.ps.b.OfferingExitNode() {
|
|
|
|
|
t.Fatal("unexpectedly offering exit node")
|
|
|
|
|
}
|
2022-11-09 05:58:10 +00:00
|
|
|
|
h.ps.b.pm.SetPrefs((&ipn.Prefs{
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
|
AdvertiseRoutes: []netip.Prefix{
|
2022-07-26 03:55:44 +00:00
|
|
|
|
netip.MustParsePrefix("0.0.0.0/0"),
|
|
|
|
|
netip.MustParsePrefix("::/0"),
|
2021-11-25 17:43:39 +00:00
|
|
|
|
},
|
2023-11-17 02:40:23 +00:00
|
|
|
|
}).View(), ipn.NetworkProfile{})
|
2021-11-25 17:43:39 +00:00
|
|
|
|
if !h.ps.b.OfferingExitNode() {
|
|
|
|
|
t.Fatal("unexpectedly not offering exit node")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly doing DNS without filter")
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
|
h.ps.b.setFilter(filter.NewAllowNone(logger.Discard, new(netipx.IPSet)))
|
2021-11-25 17:43:39 +00:00
|
|
|
|
if h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly doing DNS without filter")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f := filter.NewAllowAllForTest(logger.Discard)
|
|
|
|
|
|
|
|
|
|
h.ps.b.setFilter(f)
|
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly deny; wanted to be a DNS server")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also test IPv6.
|
2022-07-26 03:55:44 +00:00
|
|
|
|
h.remoteAddr = netip.MustParseAddrPort("[fe70::1]:12345")
|
2021-11-25 17:43:39 +00:00
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly IPv6 deny; wanted to be a DNS server")
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
|
|
2024-01-31 21:49:52 +00:00
|
|
|
|
func TestPeerAPIPrettyReplyCNAME(t *testing.T) {
|
2024-04-11 17:12:13 +00:00
|
|
|
|
for _, shouldStore := range []bool{false, true} {
|
|
|
|
|
var h peerAPIHandler
|
|
|
|
|
h.remoteAddr = netip.MustParseAddrPort("100.150.151.152:12345")
|
|
|
|
|
|
|
|
|
|
eng, _ := wgengine.NewFakeUserspaceEngine(logger.Discard, 0)
|
|
|
|
|
pm := must.Get(newProfileManager(new(mem.Store), t.Logf))
|
|
|
|
|
var a *appc.AppConnector
|
|
|
|
|
if shouldStore {
|
|
|
|
|
a = appc.NewAppConnector(t.Logf, &appctest.RouteCollector{}, &appc.RouteInfo{}, fakeStoreRoutes)
|
|
|
|
|
} else {
|
|
|
|
|
a = appc.NewAppConnector(t.Logf, &appctest.RouteCollector{}, nil, nil)
|
|
|
|
|
}
|
|
|
|
|
h.ps = &peerAPIServer{
|
|
|
|
|
b: &LocalBackend{
|
|
|
|
|
e: eng,
|
|
|
|
|
pm: pm,
|
|
|
|
|
store: pm.Store(),
|
|
|
|
|
// configure as an app connector just to enable the API.
|
|
|
|
|
appConnector: a,
|
2024-01-31 21:49:52 +00:00
|
|
|
|
},
|
2024-04-11 17:12:13 +00:00
|
|
|
|
}
|
2024-01-31 21:49:52 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
h.ps.resolver = &fakeResolver{build: func(b *dnsmessage.Builder) {
|
|
|
|
|
b.CNAMEResource(
|
|
|
|
|
dnsmessage.ResourceHeader{
|
|
|
|
|
Name: dnsmessage.MustNewName("www.example.com."),
|
|
|
|
|
Type: dnsmessage.TypeCNAME,
|
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
|
TTL: 0,
|
|
|
|
|
},
|
|
|
|
|
dnsmessage.CNAMEResource{
|
|
|
|
|
CNAME: dnsmessage.MustNewName("example.com."),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
b.AResource(
|
|
|
|
|
dnsmessage.ResourceHeader{
|
|
|
|
|
Name: dnsmessage.MustNewName("example.com."),
|
|
|
|
|
Type: dnsmessage.TypeA,
|
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
|
TTL: 0,
|
|
|
|
|
},
|
|
|
|
|
dnsmessage.AResource{
|
|
|
|
|
A: [4]byte{192, 0, 0, 8},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
f := filter.NewAllowAllForTest(logger.Discard)
|
|
|
|
|
h.ps.b.setFilter(f)
|
2024-01-31 21:49:52 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly deny; wanted to be a DNS server")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
h.handleDNSQuery(w, httptest.NewRequest("GET", "/dns-query?q=www.example.com.", nil))
|
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
|
t.Errorf("unexpected status code: %v", w.Code)
|
|
|
|
|
}
|
|
|
|
|
var addrs []string
|
|
|
|
|
json.NewDecoder(w.Body).Decode(&addrs)
|
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
|
t.Fatalf("no addresses returned")
|
|
|
|
|
}
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
|
netip.MustParseAddr(addr)
|
|
|
|
|
}
|
2024-01-31 21:49:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
|
func TestPeerAPIReplyToDNSQueriesAreObserved(t *testing.T) {
|
2024-04-11 17:12:13 +00:00
|
|
|
|
for _, shouldStore := range []bool{false, true} {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
var h peerAPIHandler
|
|
|
|
|
h.remoteAddr = netip.MustParseAddrPort("100.150.151.152:12345")
|
|
|
|
|
|
|
|
|
|
rc := &appctest.RouteCollector{}
|
|
|
|
|
eng, _ := wgengine.NewFakeUserspaceEngine(logger.Discard, 0)
|
|
|
|
|
pm := must.Get(newProfileManager(new(mem.Store), t.Logf))
|
|
|
|
|
var a *appc.AppConnector
|
|
|
|
|
if shouldStore {
|
|
|
|
|
a = appc.NewAppConnector(t.Logf, rc, &appc.RouteInfo{}, fakeStoreRoutes)
|
|
|
|
|
} else {
|
|
|
|
|
a = appc.NewAppConnector(t.Logf, rc, nil, nil)
|
|
|
|
|
}
|
|
|
|
|
h.ps = &peerAPIServer{
|
|
|
|
|
b: &LocalBackend{
|
|
|
|
|
e: eng,
|
|
|
|
|
pm: pm,
|
|
|
|
|
store: pm.Store(),
|
|
|
|
|
appConnector: a,
|
2024-01-31 21:49:52 +00:00
|
|
|
|
},
|
2024-04-11 17:12:13 +00:00
|
|
|
|
}
|
|
|
|
|
h.ps.b.appConnector.UpdateDomains([]string{"example.com"})
|
|
|
|
|
h.ps.b.appConnector.Wait(ctx)
|
|
|
|
|
|
|
|
|
|
h.ps.resolver = &fakeResolver{build: func(b *dnsmessage.Builder) {
|
|
|
|
|
b.AResource(
|
|
|
|
|
dnsmessage.ResourceHeader{
|
|
|
|
|
Name: dnsmessage.MustNewName("example.com."),
|
|
|
|
|
Type: dnsmessage.TypeA,
|
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
|
TTL: 0,
|
|
|
|
|
},
|
|
|
|
|
dnsmessage.AResource{
|
|
|
|
|
A: [4]byte{192, 0, 0, 8},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
f := filter.NewAllowAllForTest(logger.Discard)
|
|
|
|
|
h.ps.b.setFilter(f)
|
2023-10-27 21:20:10 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
if !h.ps.b.OfferingAppConnector() {
|
|
|
|
|
t.Fatal("expecting to be offering app connector")
|
|
|
|
|
}
|
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly deny; wanted to be a DNS server")
|
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
h.handleDNSQuery(w, httptest.NewRequest("GET", "/dns-query?q=example.com.", nil))
|
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
|
t.Errorf("unexpected status code: %v", w.Code)
|
|
|
|
|
}
|
|
|
|
|
h.ps.b.appConnector.Wait(ctx)
|
2023-10-27 21:20:10 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
wantRoutes := []netip.Prefix{netip.MustParsePrefix("192.0.0.8/32")}
|
|
|
|
|
if !slices.Equal(rc.Routes(), wantRoutes) {
|
|
|
|
|
t.Errorf("got %v; want %v", rc.Routes(), wantRoutes)
|
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 22:33:14 +00:00
|
|
|
|
func TestPeerAPIReplyToDNSQueriesAreObservedWithCNAMEFlattening(t *testing.T) {
|
2024-04-11 17:12:13 +00:00
|
|
|
|
for _, shouldStore := range []bool{false, true} {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
var h peerAPIHandler
|
|
|
|
|
h.remoteAddr = netip.MustParseAddrPort("100.150.151.152:12345")
|
|
|
|
|
|
|
|
|
|
rc := &appctest.RouteCollector{}
|
|
|
|
|
eng, _ := wgengine.NewFakeUserspaceEngine(logger.Discard, 0)
|
|
|
|
|
pm := must.Get(newProfileManager(new(mem.Store), t.Logf))
|
|
|
|
|
var a *appc.AppConnector
|
|
|
|
|
if shouldStore {
|
|
|
|
|
a = appc.NewAppConnector(t.Logf, rc, &appc.RouteInfo{}, fakeStoreRoutes)
|
|
|
|
|
} else {
|
|
|
|
|
a = appc.NewAppConnector(t.Logf, rc, nil, nil)
|
|
|
|
|
}
|
|
|
|
|
h.ps = &peerAPIServer{
|
|
|
|
|
b: &LocalBackend{
|
|
|
|
|
e: eng,
|
|
|
|
|
pm: pm,
|
|
|
|
|
store: pm.Store(),
|
|
|
|
|
appConnector: a,
|
2024-01-31 22:33:14 +00:00
|
|
|
|
},
|
2024-04-11 17:12:13 +00:00
|
|
|
|
}
|
|
|
|
|
h.ps.b.appConnector.UpdateDomains([]string{"www.example.com"})
|
|
|
|
|
h.ps.b.appConnector.Wait(ctx)
|
|
|
|
|
|
|
|
|
|
h.ps.resolver = &fakeResolver{build: func(b *dnsmessage.Builder) {
|
|
|
|
|
b.CNAMEResource(
|
|
|
|
|
dnsmessage.ResourceHeader{
|
|
|
|
|
Name: dnsmessage.MustNewName("www.example.com."),
|
|
|
|
|
Type: dnsmessage.TypeCNAME,
|
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
|
TTL: 0,
|
|
|
|
|
},
|
|
|
|
|
dnsmessage.CNAMEResource{
|
|
|
|
|
CNAME: dnsmessage.MustNewName("example.com."),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
b.AResource(
|
|
|
|
|
dnsmessage.ResourceHeader{
|
|
|
|
|
Name: dnsmessage.MustNewName("example.com."),
|
|
|
|
|
Type: dnsmessage.TypeA,
|
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
|
TTL: 0,
|
|
|
|
|
},
|
|
|
|
|
dnsmessage.AResource{
|
|
|
|
|
A: [4]byte{192, 0, 0, 8},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
f := filter.NewAllowAllForTest(logger.Discard)
|
|
|
|
|
h.ps.b.setFilter(f)
|
2024-01-31 22:33:14 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
if !h.ps.b.OfferingAppConnector() {
|
|
|
|
|
t.Fatal("expecting to be offering app connector")
|
|
|
|
|
}
|
|
|
|
|
if !h.replyToDNSQueries() {
|
|
|
|
|
t.Errorf("unexpectedly deny; wanted to be a DNS server")
|
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
h.handleDNSQuery(w, httptest.NewRequest("GET", "/dns-query?q=www.example.com.", nil))
|
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
|
t.Errorf("unexpected status code: %v", w.Code)
|
|
|
|
|
}
|
|
|
|
|
h.ps.b.appConnector.Wait(ctx)
|
2024-01-31 22:33:14 +00:00
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
|
wantRoutes := []netip.Prefix{netip.MustParsePrefix("192.0.0.8/32")}
|
|
|
|
|
if !slices.Equal(rc.Routes(), wantRoutes) {
|
|
|
|
|
t.Errorf("got %v; want %v", rc.Routes(), wantRoutes)
|
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 21:49:52 +00:00
|
|
|
|
type fakeResolver struct {
|
|
|
|
|
build func(*dnsmessage.Builder)
|
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
|
|
|
|
|
|
func (f *fakeResolver) HandlePeerDNSQuery(ctx context.Context, q []byte, from netip.AddrPort, allowName func(name string) bool) (res []byte, err error) {
|
|
|
|
|
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{})
|
|
|
|
|
b.EnableCompression()
|
|
|
|
|
b.StartAnswers()
|
2024-01-31 21:49:52 +00:00
|
|
|
|
f.build(&b)
|
2023-10-27 21:20:10 +00:00
|
|
|
|
return b.Finish()
|
|
|
|
|
}
|