| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164 |
- // Copyright (c) Tailscale Inc & AUTHORS
- // SPDX-License-Identifier: BSD-3-Clause
- //go:build linux || darwin
- package tailssh
- import (
- "bytes"
- "context"
- "crypto/ed25519"
- "crypto/rand"
- "crypto/sha256"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net"
- "net/http"
- "net/http/httptest"
- "net/netip"
- "os"
- "os/exec"
- "os/user"
- "reflect"
- "runtime"
- "strconv"
- "strings"
- "sync"
- "sync/atomic"
- "testing"
- "time"
- gossh "github.com/tailscale/golang-x-crypto/ssh"
- "tailscale.com/ipn/ipnlocal"
- "tailscale.com/ipn/store/mem"
- "tailscale.com/net/memnet"
- "tailscale.com/net/tsdial"
- "tailscale.com/tailcfg"
- "tailscale.com/tempfork/gliderlabs/ssh"
- "tailscale.com/tsd"
- "tailscale.com/tstest"
- "tailscale.com/types/key"
- "tailscale.com/types/logger"
- "tailscale.com/types/logid"
- "tailscale.com/types/netmap"
- "tailscale.com/types/ptr"
- "tailscale.com/util/cibuild"
- "tailscale.com/util/lineread"
- "tailscale.com/util/must"
- "tailscale.com/version/distro"
- "tailscale.com/wgengine"
- )
- func TestMatchRule(t *testing.T) {
- someAction := new(tailcfg.SSHAction)
- tests := []struct {
- name string
- rule *tailcfg.SSHRule
- ci *sshConnInfo
- wantErr error
- wantUser string
- }{
- {
- name: "invalid-conn",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{Any: true}},
- SSHUsers: map[string]string{
- "*": "ubuntu",
- },
- },
- wantErr: errInvalidConn,
- },
- {
- name: "nil-rule",
- ci: &sshConnInfo{},
- rule: nil,
- wantErr: errNilRule,
- },
- {
- name: "nil-action",
- ci: &sshConnInfo{},
- rule: &tailcfg.SSHRule{},
- wantErr: errNilAction,
- },
- {
- name: "expired",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- RuleExpires: ptr.To(time.Unix(100, 0)),
- },
- ci: &sshConnInfo{},
- wantErr: errRuleExpired,
- },
- {
- name: "no-principal",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- SSHUsers: map[string]string{
- "*": "ubuntu",
- }},
- ci: &sshConnInfo{},
- wantErr: errPrincipalMatch,
- },
- {
- name: "no-user-match",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{Any: true}},
- },
- ci: &sshConnInfo{sshUser: "alice"},
- wantErr: errUserMatch,
- },
- {
- name: "ok-wildcard",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{Any: true}},
- SSHUsers: map[string]string{
- "*": "ubuntu",
- },
- },
- ci: &sshConnInfo{sshUser: "alice"},
- wantUser: "ubuntu",
- },
- {
- name: "ok-wildcard-and-nil-principal",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{
- nil, // don't crash on this
- {Any: true},
- },
- SSHUsers: map[string]string{
- "*": "ubuntu",
- },
- },
- ci: &sshConnInfo{sshUser: "alice"},
- wantUser: "ubuntu",
- },
- {
- name: "ok-exact",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{Any: true}},
- SSHUsers: map[string]string{
- "*": "ubuntu",
- "alice": "thealice",
- },
- },
- ci: &sshConnInfo{sshUser: "alice"},
- wantUser: "thealice",
- },
- {
- name: "no-users-for-reject",
- rule: &tailcfg.SSHRule{
- Principals: []*tailcfg.SSHPrincipal{{Any: true}},
- Action: &tailcfg.SSHAction{Reject: true},
- },
- ci: &sshConnInfo{sshUser: "alice"},
- },
- {
- name: "match-principal-node-ip",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{NodeIP: "1.2.3.4"}},
- SSHUsers: map[string]string{"*": "ubuntu"},
- },
- ci: &sshConnInfo{src: netip.MustParseAddrPort("1.2.3.4:30343")},
- wantUser: "ubuntu",
- },
- {
- name: "match-principal-node-id",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{Node: "some-node-ID"}},
- SSHUsers: map[string]string{"*": "ubuntu"},
- },
- ci: &sshConnInfo{node: (&tailcfg.Node{StableID: "some-node-ID"}).View()},
- wantUser: "ubuntu",
- },
- {
- name: "match-principal-userlogin",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{UserLogin: "[email protected]"}},
- SSHUsers: map[string]string{"*": "ubuntu"},
- },
- ci: &sshConnInfo{uprof: tailcfg.UserProfile{LoginName: "[email protected]"}},
- wantUser: "ubuntu",
- },
- {
- name: "ssh-user-equal",
- rule: &tailcfg.SSHRule{
- Action: someAction,
- Principals: []*tailcfg.SSHPrincipal{{Any: true}},
- SSHUsers: map[string]string{
- "*": "=",
- },
- },
- ci: &sshConnInfo{sshUser: "alice"},
- wantUser: "alice",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &conn{
- info: tt.ci,
- srv: &server{logf: t.Logf},
- }
- got, gotUser, err := c.matchRule(tt.rule, nil)
- if err != tt.wantErr {
- t.Errorf("err = %v; want %v", err, tt.wantErr)
- }
- if gotUser != tt.wantUser {
- t.Errorf("user = %q; want %q", gotUser, tt.wantUser)
- }
- if err == nil && got == nil {
- t.Errorf("expected non-nil action on success")
- }
- })
- }
- }
- // localState implements ipnLocalBackend for testing.
- type localState struct {
- sshEnabled bool
- matchingRule *tailcfg.SSHRule
- // serverActions is a map of the action name to the action.
- // It is served for paths like https://unused/ssh-action/<action-name>.
- // The action name is the last part of the action URL.
- serverActions map[string]*tailcfg.SSHAction
- }
- var (
- currentUser = os.Getenv("USER") // Use the current user for the test.
- testSigner gossh.Signer
- testSignerOnce sync.Once
- )
- func (ts *localState) Dialer() *tsdial.Dialer {
- return &tsdial.Dialer{}
- }
- func (ts *localState) GetSSH_HostKeys() ([]gossh.Signer, error) {
- testSignerOnce.Do(func() {
- _, priv, err := ed25519.GenerateKey(rand.Reader)
- if err != nil {
- panic(err)
- }
- s, err := gossh.NewSignerFromSigner(priv)
- if err != nil {
- panic(err)
- }
- testSigner = s
- })
- return []gossh.Signer{testSigner}, nil
- }
- func (ts *localState) ShouldRunSSH() bool {
- return ts.sshEnabled
- }
- func (ts *localState) NetMap() *netmap.NetworkMap {
- var policy *tailcfg.SSHPolicy
- if ts.matchingRule != nil {
- policy = &tailcfg.SSHPolicy{
- Rules: []*tailcfg.SSHRule{
- ts.matchingRule,
- },
- }
- }
- return &netmap.NetworkMap{
- SelfNode: (&tailcfg.Node{
- ID: 1,
- }).View(),
- SSHPolicy: policy,
- }
- }
- func (ts *localState) WhoIs(proto string, ipp netip.AddrPort) (n tailcfg.NodeView, u tailcfg.UserProfile, ok bool) {
- if proto != "tcp" {
- return tailcfg.NodeView{}, tailcfg.UserProfile{}, false
- }
- return (&tailcfg.Node{
- ID: 2,
- StableID: "peer-id",
- }).View(), tailcfg.UserProfile{
- LoginName: "peer",
- }, true
- }
- func (ts *localState) DoNoiseRequest(req *http.Request) (*http.Response, error) {
- rec := httptest.NewRecorder()
- k, ok := strings.CutPrefix(req.URL.Path, "/ssh-action/")
- if !ok {
- rec.WriteHeader(http.StatusNotFound)
- }
- a, ok := ts.serverActions[k]
- if !ok {
- rec.WriteHeader(http.StatusNotFound)
- return rec.Result(), nil
- }
- rec.WriteHeader(http.StatusOK)
- if err := json.NewEncoder(rec).Encode(a); err != nil {
- return nil, err
- }
- return rec.Result(), nil
- }
- func (ts *localState) TailscaleVarRoot() string {
- return ""
- }
- func (ts *localState) NodeKey() key.NodePublic {
- return key.NewNode().Public()
- }
- func newSSHRule(action *tailcfg.SSHAction) *tailcfg.SSHRule {
- return &tailcfg.SSHRule{
- SSHUsers: map[string]string{
- "*": currentUser,
- },
- Action: action,
- Principals: []*tailcfg.SSHPrincipal{
- {
- Any: true,
- },
- },
- }
- }
- func TestSSHRecordingCancelsSessionsOnUploadFailure(t *testing.T) {
- if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
- t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
- }
- var handler http.HandlerFunc
- recordingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- handler(w, r)
- }))
- defer recordingServer.Close()
- s := &server{
- logf: t.Logf,
- lb: &localState{
- sshEnabled: true,
- matchingRule: newSSHRule(
- &tailcfg.SSHAction{
- Accept: true,
- Recorders: []netip.AddrPort{
- netip.MustParseAddrPort(recordingServer.Listener.Addr().String()),
- },
- OnRecordingFailure: &tailcfg.SSHRecorderFailureAction{
- RejectSessionWithMessage: "session rejected",
- TerminateSessionWithMessage: "session terminated",
- },
- },
- ),
- },
- }
- defer s.Shutdown()
- const sshUser = "alice"
- cfg := &gossh.ClientConfig{
- User: sshUser,
- HostKeyCallback: gossh.InsecureIgnoreHostKey(),
- }
- tests := []struct {
- name string
- handler func(w http.ResponseWriter, r *http.Request)
- sshCommand string
- wantClientOutput string
- clientOutputMustNotContain []string
- }{
- {
- name: "upload-denied",
- handler: func(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(http.StatusForbidden)
- },
- sshCommand: "echo hello",
- wantClientOutput: "session rejected\r\n",
- clientOutputMustNotContain: []string{"hello"},
- },
- {
- name: "upload-fails-after-starting",
- handler: func(w http.ResponseWriter, r *http.Request) {
- r.Body.Read(make([]byte, 1))
- time.Sleep(100 * time.Millisecond)
- w.WriteHeader(http.StatusInternalServerError)
- },
- sshCommand: "echo hello && sleep 1 && echo world",
- wantClientOutput: "\r\n\r\nsession terminated\r\n\r\n",
- clientOutputMustNotContain: []string{"world"},
- },
- }
- src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tstest.Replace(t, &handler, tt.handler)
- sc, dc := memnet.NewTCPConn(src, dst, 1024)
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- defer wg.Done()
- c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- client := gossh.NewClient(c, chans, reqs)
- defer client.Close()
- session, err := client.NewSession()
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- defer session.Close()
- t.Logf("client established session")
- got, err := session.CombinedOutput(tt.sshCommand)
- if err != nil {
- t.Logf("client got: %q: %v", got, err)
- } else {
- t.Errorf("client did not get kicked out: %q", got)
- }
- gotStr := string(got)
- if !strings.HasSuffix(gotStr, tt.wantClientOutput) {
- t.Errorf("client got %q, want %q", got, tt.wantClientOutput)
- }
- for _, x := range tt.clientOutputMustNotContain {
- if strings.Contains(gotStr, x) {
- t.Errorf("client output must not contain %q", x)
- }
- }
- }()
- if err := s.HandleSSHConn(dc); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- wg.Wait()
- })
- }
- }
- func TestMultipleRecorders(t *testing.T) {
- if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
- t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
- }
- done := make(chan struct{})
- recordingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- defer close(done)
- io.ReadAll(r.Body)
- w.WriteHeader(http.StatusOK)
- }))
- defer recordingServer.Close()
- badRecorder, err := net.Listen("tcp", ":0")
- if err != nil {
- t.Fatal(err)
- }
- badRecorderAddr := badRecorder.Addr().String()
- badRecorder.Close()
- badRecordingServer500 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(500)
- }))
- defer badRecordingServer500.Close()
- badRecordingServer200 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(200)
- }))
- defer badRecordingServer200.Close()
- s := &server{
- logf: t.Logf,
- lb: &localState{
- sshEnabled: true,
- matchingRule: newSSHRule(
- &tailcfg.SSHAction{
- Accept: true,
- Recorders: []netip.AddrPort{
- netip.MustParseAddrPort(badRecorderAddr),
- netip.MustParseAddrPort(badRecordingServer500.Listener.Addr().String()),
- netip.MustParseAddrPort(badRecordingServer200.Listener.Addr().String()),
- netip.MustParseAddrPort(recordingServer.Listener.Addr().String()),
- },
- OnRecordingFailure: &tailcfg.SSHRecorderFailureAction{
- RejectSessionWithMessage: "session rejected",
- TerminateSessionWithMessage: "session terminated",
- },
- },
- ),
- },
- }
- defer s.Shutdown()
- src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
- sc, dc := memnet.NewTCPConn(src, dst, 1024)
- const sshUser = "alice"
- cfg := &gossh.ClientConfig{
- User: sshUser,
- HostKeyCallback: gossh.InsecureIgnoreHostKey(),
- }
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- defer wg.Done()
- c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- client := gossh.NewClient(c, chans, reqs)
- defer client.Close()
- session, err := client.NewSession()
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- defer session.Close()
- t.Logf("client established session")
- out, err := session.CombinedOutput("echo Ran echo!")
- if err != nil {
- t.Errorf("client: %v", err)
- }
- if string(out) != "Ran echo!\n" {
- t.Errorf("client: unexpected output: %q", out)
- }
- }()
- if err := s.HandleSSHConn(dc); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- wg.Wait()
- select {
- case <-done:
- case <-time.After(1 * time.Second):
- t.Fatal("timed out waiting for recording")
- }
- }
- // TestSSHRecordingNonInteractive tests that the SSH server records the SSH session
- // when the client is not interactive (i.e. no PTY).
- // It starts a local SSH server and a recording server. The recording server
- // records the SSH session and returns it to the test.
- // The test then verifies that the recording has a valid CastHeader, it does not
- // validate the contents of the recording.
- func TestSSHRecordingNonInteractive(t *testing.T) {
- if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
- t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
- }
- var recording []byte
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- recordingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- defer cancel()
- var err error
- recording, err = io.ReadAll(r.Body)
- if err != nil {
- t.Error(err)
- return
- }
- }))
- defer recordingServer.Close()
- s := &server{
- logf: logger.Discard,
- lb: &localState{
- sshEnabled: true,
- matchingRule: newSSHRule(
- &tailcfg.SSHAction{
- Accept: true,
- Recorders: []netip.AddrPort{
- must.Get(netip.ParseAddrPort(recordingServer.Listener.Addr().String())),
- },
- OnRecordingFailure: &tailcfg.SSHRecorderFailureAction{
- RejectSessionWithMessage: "session rejected",
- TerminateSessionWithMessage: "session terminated",
- },
- },
- ),
- },
- }
- defer s.Shutdown()
- src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
- sc, dc := memnet.NewTCPConn(src, dst, 1024)
- const sshUser = "alice"
- cfg := &gossh.ClientConfig{
- User: sshUser,
- HostKeyCallback: gossh.InsecureIgnoreHostKey(),
- }
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- defer wg.Done()
- c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- client := gossh.NewClient(c, chans, reqs)
- defer client.Close()
- session, err := client.NewSession()
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- defer session.Close()
- t.Logf("client established session")
- _, err = session.CombinedOutput("echo Ran echo!")
- if err != nil {
- t.Errorf("client: %v", err)
- }
- }()
- if err := s.HandleSSHConn(dc); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- wg.Wait()
- <-ctx.Done() // wait for recording to finish
- var ch CastHeader
- if err := json.NewDecoder(bytes.NewReader(recording)).Decode(&ch); err != nil {
- t.Fatal(err)
- }
- if ch.SSHUser != sshUser {
- t.Errorf("SSHUser = %q; want %q", ch.SSHUser, sshUser)
- }
- if ch.Command != "echo Ran echo!" {
- t.Errorf("Command = %q; want %q", ch.Command, "echo Ran echo!")
- }
- }
- func TestSSHAuthFlow(t *testing.T) {
- if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
- t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
- }
- acceptRule := newSSHRule(&tailcfg.SSHAction{
- Accept: true,
- Message: "Welcome to Tailscale SSH!",
- })
- rejectRule := newSSHRule(&tailcfg.SSHAction{
- Reject: true,
- Message: "Go Away!",
- })
- tests := []struct {
- name string
- sshUser string // defaults to alice
- state *localState
- wantBanners []string
- usesPassword bool
- authErr bool
- }{
- {
- name: "no-policy",
- state: &localState{
- sshEnabled: true,
- },
- authErr: true,
- },
- {
- name: "accept",
- state: &localState{
- sshEnabled: true,
- matchingRule: acceptRule,
- },
- wantBanners: []string{"Welcome to Tailscale SSH!"},
- },
- {
- name: "reject",
- state: &localState{
- sshEnabled: true,
- matchingRule: rejectRule,
- },
- wantBanners: []string{"Go Away!"},
- authErr: true,
- },
- {
- name: "simple-check",
- state: &localState{
- sshEnabled: true,
- matchingRule: newSSHRule(&tailcfg.SSHAction{
- HoldAndDelegate: "https://unused/ssh-action/accept",
- }),
- serverActions: map[string]*tailcfg.SSHAction{
- "accept": acceptRule.Action,
- },
- },
- wantBanners: []string{"Welcome to Tailscale SSH!"},
- },
- {
- name: "multi-check",
- state: &localState{
- sshEnabled: true,
- matchingRule: newSSHRule(&tailcfg.SSHAction{
- Message: "First",
- HoldAndDelegate: "https://unused/ssh-action/check1",
- }),
- serverActions: map[string]*tailcfg.SSHAction{
- "check1": {
- Message: "url-here",
- HoldAndDelegate: "https://unused/ssh-action/check2",
- },
- "check2": acceptRule.Action,
- },
- },
- wantBanners: []string{"First", "url-here", "Welcome to Tailscale SSH!"},
- },
- {
- name: "check-reject",
- state: &localState{
- sshEnabled: true,
- matchingRule: newSSHRule(&tailcfg.SSHAction{
- Message: "First",
- HoldAndDelegate: "https://unused/ssh-action/reject",
- }),
- serverActions: map[string]*tailcfg.SSHAction{
- "reject": rejectRule.Action,
- },
- },
- wantBanners: []string{"First", "Go Away!"},
- authErr: true,
- },
- {
- name: "force-password-auth",
- sshUser: "alice+password",
- state: &localState{
- sshEnabled: true,
- matchingRule: acceptRule,
- },
- usesPassword: true,
- wantBanners: []string{"Welcome to Tailscale SSH!"},
- },
- }
- s := &server{
- logf: logger.Discard,
- }
- defer s.Shutdown()
- src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- sc, dc := memnet.NewTCPConn(src, dst, 1024)
- s.lb = tc.state
- sshUser := "alice"
- if tc.sshUser != "" {
- sshUser = tc.sshUser
- }
- var passwordUsed atomic.Bool
- cfg := &gossh.ClientConfig{
- User: sshUser,
- HostKeyCallback: gossh.InsecureIgnoreHostKey(),
- Auth: []gossh.AuthMethod{
- gossh.PasswordCallback(func() (secret string, err error) {
- if !tc.usesPassword {
- t.Error("unexpected use of PasswordCallback")
- return "", errors.New("unexpected use of PasswordCallback")
- }
- passwordUsed.Store(true)
- return "any-pass", nil
- }),
- },
- BannerCallback: func(message string) error {
- if len(tc.wantBanners) == 0 {
- t.Errorf("unexpected banner: %q", message)
- } else if message != tc.wantBanners[0] {
- t.Errorf("banner = %q; want %q", message, tc.wantBanners[0])
- } else {
- t.Logf("banner = %q", message)
- tc.wantBanners = tc.wantBanners[1:]
- }
- return nil
- },
- }
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- defer wg.Done()
- c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
- if err != nil {
- if !tc.authErr {
- t.Errorf("client: %v", err)
- }
- return
- } else if tc.authErr {
- c.Close()
- t.Errorf("client: expected error, got nil")
- return
- }
- client := gossh.NewClient(c, chans, reqs)
- defer client.Close()
- session, err := client.NewSession()
- if err != nil {
- t.Errorf("client: %v", err)
- return
- }
- defer session.Close()
- _, err = session.CombinedOutput("echo Ran echo!")
- if err != nil {
- t.Errorf("client: %v", err)
- }
- }()
- if err := s.HandleSSHConn(dc); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- wg.Wait()
- if len(tc.wantBanners) > 0 {
- t.Errorf("missing banners: %v", tc.wantBanners)
- }
- })
- }
- }
- func TestSSH(t *testing.T) {
- var logf logger.Logf = t.Logf
- sys := &tsd.System{}
- eng, err := wgengine.NewFakeUserspaceEngine(logf, sys.Set, sys.HealthTracker())
- if err != nil {
- t.Fatal(err)
- }
- sys.Set(eng)
- sys.Set(new(mem.Store))
- lb, err := ipnlocal.NewLocalBackend(logf, logid.PublicID{}, sys, 0)
- if err != nil {
- t.Fatal(err)
- }
- defer lb.Shutdown()
- dir := t.TempDir()
- lb.SetVarRoot(dir)
- srv := &server{
- lb: lb,
- logf: logf,
- }
- sc, err := srv.newConn()
- if err != nil {
- t.Fatal(err)
- }
- // Remove the auth checks for the test
- sc.insecureSkipTailscaleAuth = true
- u, err := user.Current()
- if err != nil {
- t.Fatal(err)
- }
- um, err := userLookup(u.Username)
- if err != nil {
- t.Fatal(err)
- }
- sc.localUser = um
- sc.info = &sshConnInfo{
- sshUser: "test",
- src: netip.MustParseAddrPort("1.2.3.4:32342"),
- dst: netip.MustParseAddrPort("1.2.3.5:22"),
- node: (&tailcfg.Node{}).View(),
- uprof: tailcfg.UserProfile{},
- }
- sc.action0 = &tailcfg.SSHAction{Accept: true}
- sc.finalAction = sc.action0
- sc.Handler = func(s ssh.Session) {
- sc.newSSHSession(s).run()
- }
- ln, err := net.Listen("tcp4", "127.0.0.1:0")
- if err != nil {
- t.Fatal(err)
- }
- defer ln.Close()
- port := ln.Addr().(*net.TCPAddr).Port
- go func() {
- for {
- c, err := ln.Accept()
- if err != nil {
- if !errors.Is(err, net.ErrClosed) {
- t.Errorf("Accept: %v", err)
- }
- return
- }
- go sc.HandleConn(c)
- }
- }()
- execSSH := func(args ...string) *exec.Cmd {
- cmd := exec.Command("ssh",
- "-F",
- "none",
- "-v",
- "-p", fmt.Sprint(port),
- "-o", "StrictHostKeyChecking=no",
- "[email protected]")
- cmd.Args = append(cmd.Args, args...)
- return cmd
- }
- t.Run("env", func(t *testing.T) {
- if cibuild.On() {
- t.Skip("Skipping for now; see https://github.com/tailscale/tailscale/issues/4051")
- }
- cmd := execSSH("LANG=foo env")
- cmd.Env = append(os.Environ(), "LOCAL_ENV=bar")
- got, err := cmd.CombinedOutput()
- if err != nil {
- t.Fatal(err, string(got))
- }
- m := parseEnv(got)
- if got := m["USER"]; got == "" || got != u.Username {
- t.Errorf("USER = %q; want %q", got, u.Username)
- }
- if got := m["HOME"]; got == "" || got != u.HomeDir {
- t.Errorf("HOME = %q; want %q", got, u.HomeDir)
- }
- if got := m["PWD"]; got == "" || got != u.HomeDir {
- t.Errorf("PWD = %q; want %q", got, u.HomeDir)
- }
- if got := m["SHELL"]; got == "" {
- t.Errorf("no SHELL")
- }
- if got, want := m["LANG"], "foo"; got != want {
- t.Errorf("LANG = %q; want %q", got, want)
- }
- if got := m["LOCAL_ENV"]; got != "" {
- t.Errorf("LOCAL_ENV leaked over ssh: %v", got)
- }
- t.Logf("got: %+v", m)
- })
- t.Run("stdout_stderr", func(t *testing.T) {
- cmd := execSSH("sh", "-c", "echo foo; echo bar >&2")
- var outBuf, errBuf bytes.Buffer
- cmd.Stdout = &outBuf
- cmd.Stderr = &errBuf
- if err := cmd.Run(); err != nil {
- t.Fatal(err)
- }
- t.Logf("Got: %q and %q", outBuf.Bytes(), errBuf.Bytes())
- // TODO: figure out why these aren't right. should be
- // "foo\n" and "bar\n", not "\n" and "bar\n".
- })
- t.Run("large_file", func(t *testing.T) {
- const wantSize = 1e6
- var outBuf bytes.Buffer
- cmd := execSSH("head", "-c", strconv.Itoa(wantSize), "/dev/zero")
- cmd.Stdout = &outBuf
- if err := cmd.Run(); err != nil {
- t.Fatal(err)
- }
- if gotSize := outBuf.Len(); gotSize != wantSize {
- t.Fatalf("got %d, want %d", gotSize, int(wantSize))
- }
- })
- t.Run("stdin", func(t *testing.T) {
- if cibuild.On() {
- t.Skip("Skipping for now; see https://github.com/tailscale/tailscale/issues/4051")
- }
- cmd := execSSH("cat")
- var outBuf bytes.Buffer
- cmd.Stdout = &outBuf
- const str = "foo\nbar\n"
- cmd.Stdin = strings.NewReader(str)
- if err := cmd.Run(); err != nil {
- t.Fatal(err)
- }
- if got := outBuf.String(); got != str {
- t.Errorf("got %q; want %q", got, str)
- }
- })
- }
- func parseEnv(out []byte) map[string]string {
- e := map[string]string{}
- lineread.Reader(bytes.NewReader(out), func(line []byte) error {
- i := bytes.IndexByte(line, '=')
- if i == -1 {
- return nil
- }
- e[string(line[:i])] = string(line[i+1:])
- return nil
- })
- return e
- }
- func TestPublicKeyFetching(t *testing.T) {
- var reqsTotal, reqsIfNoneMatchHit, reqsIfNoneMatchMiss int32
- ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- atomic.AddInt32((&reqsTotal), 1)
- etag := fmt.Sprintf("W/%q", sha256.Sum256([]byte(r.URL.Path)))
- w.Header().Set("Etag", etag)
- if v := r.Header.Get("If-None-Match"); v != "" {
- if v == etag {
- atomic.AddInt32(&reqsIfNoneMatchHit, 1)
- w.WriteHeader(304)
- return
- }
- atomic.AddInt32(&reqsIfNoneMatchMiss, 1)
- }
- io.WriteString(w, "foo\nbar\n"+string(r.URL.Path)+"\n")
- }))
- ts.StartTLS()
- defer ts.Close()
- keys := ts.URL
- clock := &tstest.Clock{}
- srv := &server{
- pubKeyHTTPClient: ts.Client(),
- timeNow: clock.Now,
- }
- for range 2 {
- got, err := srv.fetchPublicKeysURL(keys + "/alice.keys")
- if err != nil {
- t.Fatal(err)
- }
- if want := []string{"foo", "bar", "/alice.keys"}; !reflect.DeepEqual(got, want) {
- t.Errorf("got %q; want %q", got, want)
- }
- }
- if got, want := atomic.LoadInt32(&reqsTotal), int32(1); got != want {
- t.Errorf("got %d requests; want %d", got, want)
- }
- if got, want := atomic.LoadInt32(&reqsIfNoneMatchHit), int32(0); got != want {
- t.Errorf("got %d etag hits; want %d", got, want)
- }
- clock.Advance(5 * time.Minute)
- got, err := srv.fetchPublicKeysURL(keys + "/alice.keys")
- if err != nil {
- t.Fatal(err)
- }
- if want := []string{"foo", "bar", "/alice.keys"}; !reflect.DeepEqual(got, want) {
- t.Errorf("got %q; want %q", got, want)
- }
- if got, want := atomic.LoadInt32(&reqsTotal), int32(2); got != want {
- t.Errorf("got %d requests; want %d", got, want)
- }
- if got, want := atomic.LoadInt32(&reqsIfNoneMatchHit), int32(1); got != want {
- t.Errorf("got %d etag hits; want %d", got, want)
- }
- if got, want := atomic.LoadInt32(&reqsIfNoneMatchMiss), int32(0); got != want {
- t.Errorf("got %d etag misses; want %d", got, want)
- }
- }
- func TestExpandPublicKeyURL(t *testing.T) {
- c := &conn{
- info: &sshConnInfo{
- uprof: tailcfg.UserProfile{
- LoginName: "[email protected]",
- },
- },
- }
- if got, want := c.expandPublicKeyURL("foo"), "foo"; got != want {
- t.Errorf("basic: got %q; want %q", got, want)
- }
- if got, want := c.expandPublicKeyURL("https://example.com/$LOGINNAME_LOCALPART.keys"), "https://example.com/bar.keys"; got != want {
- t.Errorf("localpart: got %q; want %q", got, want)
- }
- if got, want := c.expandPublicKeyURL("https://example.com/keys?email=$LOGINNAME_EMAIL"), "https://example.com/[email protected]"; got != want {
- t.Errorf("email: got %q; want %q", got, want)
- }
- c.info = new(sshConnInfo)
- if got, want := c.expandPublicKeyURL("https://example.com/keys?email=$LOGINNAME_EMAIL"), "https://example.com/keys?email="; got != want {
- t.Errorf("on empty: got %q; want %q", got, want)
- }
- }
- func TestAcceptEnvPair(t *testing.T) {
- tests := []struct {
- in string
- want bool
- }{
- {"TERM=x", true},
- {"term=x", false},
- {"TERM", false},
- {"LC_FOO=x", true},
- {"LD_PRELOAD=naah", false},
- {"TERM=screen-256color", true},
- }
- for _, tt := range tests {
- if got := acceptEnvPair(tt.in); got != tt.want {
- t.Errorf("for %q, got %v; want %v", tt.in, got, tt.want)
- }
- }
- }
- func TestPathFromPAMEnvLine(t *testing.T) {
- u := &user.User{Username: "foo", HomeDir: "/Homes/Foo"}
- tests := []struct {
- line string
- u *user.User
- want string
- }{
- {"", u, ""},
- {`PATH DEFAULT="/run/wrappers/bin:@{HOME}/.nix-profile/bin:/etc/profiles/per-user/@{PAM_USER}/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin"`,
- u, "/run/wrappers/bin:/Homes/Foo/.nix-profile/bin:/etc/profiles/per-user/foo/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin"},
- {`PATH DEFAULT="@{SOMETHING_ELSE}:nope:@{HOME}"`,
- u, ""},
- }
- for i, tt := range tests {
- got := pathFromPAMEnvLine([]byte(tt.line), tt.u)
- if got != tt.want {
- t.Errorf("%d. got %q; want %q", i, got, tt.want)
- }
- }
- }
- func TestExpandDefaultPathTmpl(t *testing.T) {
- u := &user.User{Username: "foo", HomeDir: "/Homes/Foo"}
- tests := []struct {
- t string
- u *user.User
- want string
- }{
- {"", u, ""},
- {`/run/wrappers/bin:@{HOME}/.nix-profile/bin:/etc/profiles/per-user/@{PAM_USER}/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin`,
- u, "/run/wrappers/bin:/Homes/Foo/.nix-profile/bin:/etc/profiles/per-user/foo/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin"},
- {`@{SOMETHING_ELSE}:nope:@{HOME}`, u, ""},
- }
- for i, tt := range tests {
- got := expandDefaultPathTmpl(tt.t, tt.u)
- if got != tt.want {
- t.Errorf("%d. got %q; want %q", i, got, tt.want)
- }
- }
- }
- func TestPathFromPAMEnvLineOnNixOS(t *testing.T) {
- if runtime.GOOS != "linux" {
- t.Skip("skipping on non-linux")
- }
- if distro.Get() != distro.NixOS {
- t.Skip("skipping on non-NixOS")
- }
- u, err := user.Current()
- if err != nil {
- t.Fatal(err)
- }
- got := defaultPathForUserOnNixOS(u)
- if got == "" {
- x, err := os.ReadFile("/etc/pam/environment")
- t.Fatalf("no result. file was: err=%v, contents=%s", err, x)
- }
- t.Logf("success; got=%q", got)
- }
- func TestStdOsUserUserAssumptions(t *testing.T) {
- v := reflect.TypeFor[user.User]()
- if got, want := v.NumField(), 5; got != want {
- t.Errorf("os/user.User has %v fields; this package assumes %v", got, want)
- }
- }
|