Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ server$ tailcat serve --allow=nodekey:cfb6bf...ddfd16 22
# 🐈 Server listening with saved key "default": tcXXXXXXXXX
```

For multiple clients, you can also put one public key per line in a
text file and pass its path to `--allow-list`, for example
`tailcat serve --allow-list=./whitelist.txt 22`. Blank lines and lines
beginning with `#` are ignored.

Publish the tailcat address in DNS as a TXT record:

```
Expand Down
52 changes: 51 additions & 1 deletion cmd/tailcat/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"maps"
"os"
"os/exec"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/tailscale/tailcat"
"github.com/tailscale/tailcat/internal/localhostdns"
"tailscale.com/tstest"
"tailscale.com/types/key"
)

func TestClassifyTailcatAddrArg(t *testing.T) {
Expand Down Expand Up @@ -100,7 +102,7 @@ func TestServeHelpListsServerFlags(t *testing.T) {
t.Fatal("no serve subcommand")
}
help := ffhelp.Command(serve).String()
for _, want := range []string{"--allow", "--full-address", "--hybrid", "--key", "--psk", "--ssh-authorized-keys"} {
for _, want := range []string{"--allow", "--allow-list", "--full-address", "--hybrid", "--key", "--psk", "--ssh-authorized-keys"} {
if !strings.Contains(help, want) {
t.Errorf("serve help is missing %q", want)
}
Expand Down Expand Up @@ -155,6 +157,54 @@ func TestParseFilesFlagWriteOnlyModes(t *testing.T) {
}
}

func TestParseAllowListFile(t *testing.T) {
allowed := key.NewNode().Public()
alsoAllowed := key.NewNode().Public()
notAllowed := key.NewNode().Public()
path := filepath.Join(t.TempDir(), "whitelist.txt")
content := fmt.Sprintf("# permitted clients\n\n%s\n %s \n", allowed.String(), alsoAllowed.String())
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
t.Fatal(err)
}

got := &tailcat.KeySet{}
if err := addAllowedKeysFromFile(got, path); err != nil {
t.Fatalf("addAllowedKeysFromFile(%q): %v", path, err)
}
for _, k := range []key.NodePublic{allowed, alsoAllowed} {
if !got.Contains(k) {
t.Errorf("parsed allow list does not contain %v", k)
}
}
if got.Contains(notAllowed) {
t.Errorf("parsed allow list unexpectedly contains %v", notAllowed)
}
}

func TestParseAllowFlagCommaSeparated(t *testing.T) {
allowed := key.NewNode().Public()
alsoAllowed := key.NewNode().Public()
got, err := parseAllowFlag(allowed.String() + "," + alsoAllowed.String())
if err != nil {
t.Fatalf("parseAllowFlag: %v", err)
}
for _, k := range []key.NodePublic{allowed, alsoAllowed} {
if !got.Contains(k) {
t.Errorf("parsed allow list does not contain %v", k)
}
}
}

func TestParseAllowListFlag(t *testing.T) {
path := filepath.Join(t.TempDir(), "whitelist.txt")
if _, err := parseCLI(t, "serve", "--allow-list="+path); err != nil {
t.Fatal(err)
}
if got := *flagAllowList; got != path {
t.Errorf("--allow-list = %q; want %q", got, path)
}
}

// parseCLI parses args against a fresh command tree and returns the
// root command. It doesn't run anything. The command tree parses into
// package-level flag variables, so tests that parse must not run in
Expand Down
62 changes: 51 additions & 11 deletions cmd/tailcat/tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
flagServe *string
flagKey *string
flagAllow *string
flagAllowList *string
flagFiles *string
flagSSHAuthorizedKeys *string
flagPSK *bool
Expand Down Expand Up @@ -106,6 +107,7 @@ func newRootCommand() *ff.Command {

serveFS = ff.NewFlagSet("serve").SetParent(rootFS)
flagAllow = serveFS.StringLong("allow", "", "comma-separated list of public keys to allow access to the server, or 'none' to allow no clients. If empty, all clients are allowed.")
flagAllowList = serveFS.StringLong("allow-list", "", "path to a text file with one public key per line to allow access to the server")
flagFullAddress = serveFS.BoolLong("full-address", "print a longer tailcat address with embedded DERP server info instead of a reference to a DERP map region ID. This lets clients connect more quickly, without a DERP map fetch.")
flagQR = serveFS.BoolLong("qr", "display the tailcat address as a scannable QR code")
flagFiles = serveFS.StringLong("files", "", "directory to serve to SFTP clients (scp, sftp) with the 'files' service, with an optional :ro (read-only, the default), :rw (read-write), :wo (flat write-only drop box), or :wo+ (recursive write-only drop box) suffix. If empty, the current directory is served read-only. Giving --files implies the 'files' service.")
Expand Down Expand Up @@ -1465,17 +1467,15 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
if servePerf {
s.ServedUDPPorts = []filter.PortRange{{First: perf.Port, Last: perf.Port}}
}
if *flagAllow != "" {
var allow tailcat.KeySet
for ks := range strings.SplitSeq(*flagAllow, ",") {
if ks == "none" {
continue // an empty set allows no clients
}
var k key.NodePublic
if err := k.UnmarshalText([]byte(ks)); err != nil {
log.Fatalf("invalid key %q in --allow: %v", ks, err)
if *flagAllow != "" || *flagAllowList != "" {
allow := &tailcat.KeySet{}
if err := addAllowedKeys(allow, *flagAllow, ""); err != nil {
log.Fatal(err)
}
if *flagAllowList != "" {
if err := addAllowedKeysFromFile(allow, *flagAllowList); err != nil {
log.Fatal(err)
}
allow.Add(k)
}
s.AllowClient = allow.Contains
}
Expand Down Expand Up @@ -1684,7 +1684,7 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
fmt.Fprintf(os.Stderr, "# ⚠️ WARNING: saved key %q is not using a WireGuard PSK\n", *flagKey)
}
}
if sshWithoutAuth && *flagAllow == "" {
if sshWithoutAuth && *flagAllow == "" && *flagAllowList == "" {
if execArgs != nil {
fmt.Fprintln(os.Stderr, "# ⚠️ WARNING: no-auth-ssh runs the command for anyone with this address; keep it secret (never in a DNS TXT record) or restrict clients with --allow")
} else {
Expand Down Expand Up @@ -1744,6 +1744,46 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
select {}
}

// parseAllowFlag parses a comma-separated key list.
func parseAllowFlag(v string) (*tailcat.KeySet, error) {
allow := &tailcat.KeySet{}
if err := addAllowedKeys(allow, v, ""); err != nil {
return nil, err
}
return allow, nil
}

func addAllowedKeys(allow *tailcat.KeySet, v, source string) error {
for ks := range strings.SplitSeq(v, ",") {
if ks == "none" {
continue
}
var k key.NodePublic
if err := k.UnmarshalText([]byte(ks)); err != nil {
return fmt.Errorf("invalid key %q in --allow%s: %w", ks, source, err)
}
allow.Add(k)
}
return nil
}

func addAllowedKeysFromFile(allow *tailcat.KeySet, path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("reading --allow-list file %q: %w", path, err)
}
for i, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
if err := addAllowedKeys(allow, line, fmt.Sprintf("-list file %q, line %d", path, i+1)); err != nil {
return err
}
}
return nil
}

// localDialer dials the local services that incoming connections are
// proxied to. Its resolver answers "localhost" itself with both
// loopback addresses; see the localhostdns package comment for why
Expand Down