tailssh_test.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux || darwin
  4. package tailssh
  5. import (
  6. "bytes"
  7. "context"
  8. "crypto/ed25519"
  9. "crypto/rand"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "net"
  15. "net/http"
  16. "net/http/httptest"
  17. "net/netip"
  18. "os"
  19. "os/exec"
  20. "os/user"
  21. "reflect"
  22. "runtime"
  23. "slices"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "sync/atomic"
  28. "testing"
  29. "time"
  30. gossh "github.com/tailscale/golang-x-crypto/ssh"
  31. "golang.org/x/net/http2"
  32. "golang.org/x/net/http2/h2c"
  33. "tailscale.com/ipn/ipnlocal"
  34. "tailscale.com/ipn/store/mem"
  35. "tailscale.com/net/memnet"
  36. "tailscale.com/net/tsdial"
  37. "tailscale.com/sessionrecording"
  38. "tailscale.com/tailcfg"
  39. "tailscale.com/tempfork/gliderlabs/ssh"
  40. "tailscale.com/tsd"
  41. "tailscale.com/tstest"
  42. "tailscale.com/types/key"
  43. "tailscale.com/types/logger"
  44. "tailscale.com/types/logid"
  45. "tailscale.com/types/netmap"
  46. "tailscale.com/types/ptr"
  47. "tailscale.com/util/cibuild"
  48. "tailscale.com/util/lineiter"
  49. "tailscale.com/util/must"
  50. "tailscale.com/version/distro"
  51. "tailscale.com/wgengine"
  52. )
  53. func TestMatchRule(t *testing.T) {
  54. someAction := new(tailcfg.SSHAction)
  55. tests := []struct {
  56. name string
  57. rule *tailcfg.SSHRule
  58. ci *sshConnInfo
  59. wantErr error
  60. wantUser string
  61. wantAcceptEnv []string
  62. }{
  63. {
  64. name: "invalid-conn",
  65. rule: &tailcfg.SSHRule{
  66. Action: someAction,
  67. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  68. SSHUsers: map[string]string{
  69. "*": "ubuntu",
  70. },
  71. },
  72. wantErr: errInvalidConn,
  73. },
  74. {
  75. name: "nil-rule",
  76. ci: &sshConnInfo{},
  77. rule: nil,
  78. wantErr: errNilRule,
  79. },
  80. {
  81. name: "nil-action",
  82. ci: &sshConnInfo{},
  83. rule: &tailcfg.SSHRule{},
  84. wantErr: errNilAction,
  85. },
  86. {
  87. name: "expired",
  88. rule: &tailcfg.SSHRule{
  89. Action: someAction,
  90. RuleExpires: ptr.To(time.Unix(100, 0)),
  91. },
  92. ci: &sshConnInfo{},
  93. wantErr: errRuleExpired,
  94. },
  95. {
  96. name: "no-principal",
  97. rule: &tailcfg.SSHRule{
  98. Action: someAction,
  99. SSHUsers: map[string]string{
  100. "*": "ubuntu",
  101. }},
  102. ci: &sshConnInfo{},
  103. wantErr: errPrincipalMatch,
  104. },
  105. {
  106. name: "no-user-match",
  107. rule: &tailcfg.SSHRule{
  108. Action: someAction,
  109. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  110. },
  111. ci: &sshConnInfo{sshUser: "alice"},
  112. wantErr: errUserMatch,
  113. },
  114. {
  115. name: "ok-wildcard",
  116. rule: &tailcfg.SSHRule{
  117. Action: someAction,
  118. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  119. SSHUsers: map[string]string{
  120. "*": "ubuntu",
  121. },
  122. },
  123. ci: &sshConnInfo{sshUser: "alice"},
  124. wantUser: "ubuntu",
  125. },
  126. {
  127. name: "ok-wildcard-and-nil-principal",
  128. rule: &tailcfg.SSHRule{
  129. Action: someAction,
  130. Principals: []*tailcfg.SSHPrincipal{
  131. nil, // don't crash on this
  132. {Any: true},
  133. },
  134. SSHUsers: map[string]string{
  135. "*": "ubuntu",
  136. },
  137. },
  138. ci: &sshConnInfo{sshUser: "alice"},
  139. wantUser: "ubuntu",
  140. },
  141. {
  142. name: "ok-exact",
  143. rule: &tailcfg.SSHRule{
  144. Action: someAction,
  145. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  146. SSHUsers: map[string]string{
  147. "*": "ubuntu",
  148. "alice": "thealice",
  149. },
  150. },
  151. ci: &sshConnInfo{sshUser: "alice"},
  152. wantUser: "thealice",
  153. },
  154. {
  155. name: "ok-with-accept-env",
  156. rule: &tailcfg.SSHRule{
  157. Action: someAction,
  158. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  159. SSHUsers: map[string]string{
  160. "*": "ubuntu",
  161. "alice": "thealice",
  162. },
  163. AcceptEnv: []string{"EXAMPLE", "?_?", "TEST_*"},
  164. },
  165. ci: &sshConnInfo{sshUser: "alice"},
  166. wantUser: "thealice",
  167. wantAcceptEnv: []string{"EXAMPLE", "?_?", "TEST_*"},
  168. },
  169. {
  170. name: "no-users-for-reject",
  171. rule: &tailcfg.SSHRule{
  172. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  173. Action: &tailcfg.SSHAction{Reject: true},
  174. },
  175. ci: &sshConnInfo{sshUser: "alice"},
  176. },
  177. {
  178. name: "match-principal-node-ip",
  179. rule: &tailcfg.SSHRule{
  180. Action: someAction,
  181. Principals: []*tailcfg.SSHPrincipal{{NodeIP: "1.2.3.4"}},
  182. SSHUsers: map[string]string{"*": "ubuntu"},
  183. },
  184. ci: &sshConnInfo{src: netip.MustParseAddrPort("1.2.3.4:30343")},
  185. wantUser: "ubuntu",
  186. },
  187. {
  188. name: "match-principal-node-id",
  189. rule: &tailcfg.SSHRule{
  190. Action: someAction,
  191. Principals: []*tailcfg.SSHPrincipal{{Node: "some-node-ID"}},
  192. SSHUsers: map[string]string{"*": "ubuntu"},
  193. },
  194. ci: &sshConnInfo{node: (&tailcfg.Node{StableID: "some-node-ID"}).View()},
  195. wantUser: "ubuntu",
  196. },
  197. {
  198. name: "match-principal-userlogin",
  199. rule: &tailcfg.SSHRule{
  200. Action: someAction,
  201. Principals: []*tailcfg.SSHPrincipal{{UserLogin: "[email protected]"}},
  202. SSHUsers: map[string]string{"*": "ubuntu"},
  203. },
  204. ci: &sshConnInfo{uprof: tailcfg.UserProfile{LoginName: "[email protected]"}},
  205. wantUser: "ubuntu",
  206. },
  207. {
  208. name: "ssh-user-equal",
  209. rule: &tailcfg.SSHRule{
  210. Action: someAction,
  211. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  212. SSHUsers: map[string]string{
  213. "*": "=",
  214. },
  215. },
  216. ci: &sshConnInfo{sshUser: "alice"},
  217. wantUser: "alice",
  218. },
  219. }
  220. for _, tt := range tests {
  221. t.Run(tt.name, func(t *testing.T) {
  222. c := &conn{
  223. info: tt.ci,
  224. srv: &server{logf: t.Logf},
  225. }
  226. got, gotUser, gotAcceptEnv, err := c.matchRule(tt.rule)
  227. if err != tt.wantErr {
  228. t.Errorf("err = %v; want %v", err, tt.wantErr)
  229. }
  230. if gotUser != tt.wantUser {
  231. t.Errorf("user = %q; want %q", gotUser, tt.wantUser)
  232. }
  233. if err == nil && got == nil {
  234. t.Errorf("expected non-nil action on success")
  235. }
  236. if !slices.Equal(gotAcceptEnv, tt.wantAcceptEnv) {
  237. t.Errorf("acceptEnv = %v; want %v", gotAcceptEnv, tt.wantAcceptEnv)
  238. }
  239. })
  240. }
  241. }
  242. func TestEvalSSHPolicy(t *testing.T) {
  243. someAction := new(tailcfg.SSHAction)
  244. tests := []struct {
  245. name string
  246. policy *tailcfg.SSHPolicy
  247. ci *sshConnInfo
  248. wantMatch bool
  249. wantUser string
  250. wantAcceptEnv []string
  251. }{
  252. {
  253. name: "multiple-matches-picks-first-match",
  254. policy: &tailcfg.SSHPolicy{
  255. Rules: []*tailcfg.SSHRule{
  256. {
  257. Action: someAction,
  258. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  259. SSHUsers: map[string]string{
  260. "other": "other1",
  261. },
  262. },
  263. {
  264. Action: someAction,
  265. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  266. SSHUsers: map[string]string{
  267. "*": "ubuntu",
  268. "alice": "thealice",
  269. },
  270. AcceptEnv: []string{"EXAMPLE", "?_?", "TEST_*"},
  271. },
  272. {
  273. Action: someAction,
  274. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  275. SSHUsers: map[string]string{
  276. "other2": "other3",
  277. },
  278. },
  279. {
  280. Action: someAction,
  281. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  282. SSHUsers: map[string]string{
  283. "*": "ubuntu",
  284. "alice": "thealice",
  285. "mark": "markthe",
  286. },
  287. AcceptEnv: []string{"*"},
  288. },
  289. },
  290. },
  291. ci: &sshConnInfo{sshUser: "alice"},
  292. wantUser: "thealice",
  293. wantAcceptEnv: []string{"EXAMPLE", "?_?", "TEST_*"},
  294. wantMatch: true,
  295. },
  296. {
  297. name: "no-matches-returns-failure",
  298. policy: &tailcfg.SSHPolicy{
  299. Rules: []*tailcfg.SSHRule{
  300. {
  301. Action: someAction,
  302. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  303. SSHUsers: map[string]string{
  304. "other": "other1",
  305. },
  306. },
  307. {
  308. Action: someAction,
  309. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  310. SSHUsers: map[string]string{
  311. "fedora": "ubuntu",
  312. },
  313. AcceptEnv: []string{"EXAMPLE", "?_?", "TEST_*"},
  314. },
  315. {
  316. Action: someAction,
  317. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  318. SSHUsers: map[string]string{
  319. "other2": "other3",
  320. },
  321. },
  322. {
  323. Action: someAction,
  324. Principals: []*tailcfg.SSHPrincipal{{Any: true}},
  325. SSHUsers: map[string]string{
  326. "mark": "markthe",
  327. },
  328. AcceptEnv: []string{"*"},
  329. },
  330. },
  331. },
  332. ci: &sshConnInfo{sshUser: "alice"},
  333. wantUser: "",
  334. wantAcceptEnv: nil,
  335. wantMatch: false,
  336. },
  337. }
  338. for _, tt := range tests {
  339. t.Run(tt.name, func(t *testing.T) {
  340. c := &conn{
  341. info: tt.ci,
  342. srv: &server{logf: t.Logf},
  343. }
  344. got, gotUser, gotAcceptEnv, match := c.evalSSHPolicy(tt.policy)
  345. if match != tt.wantMatch {
  346. t.Errorf("match = %v; want %v", match, tt.wantMatch)
  347. }
  348. if gotUser != tt.wantUser {
  349. t.Errorf("user = %q; want %q", gotUser, tt.wantUser)
  350. }
  351. if tt.wantMatch == true && got == nil {
  352. t.Errorf("expected non-nil action on success")
  353. }
  354. if !slices.Equal(gotAcceptEnv, tt.wantAcceptEnv) {
  355. t.Errorf("acceptEnv = %v; want %v", gotAcceptEnv, tt.wantAcceptEnv)
  356. }
  357. })
  358. }
  359. }
  360. // localState implements ipnLocalBackend for testing.
  361. type localState struct {
  362. sshEnabled bool
  363. matchingRule *tailcfg.SSHRule
  364. // serverActions is a map of the action name to the action.
  365. // It is served for paths like https://unused/ssh-action/<action-name>.
  366. // The action name is the last part of the action URL.
  367. serverActions map[string]*tailcfg.SSHAction
  368. }
  369. var (
  370. currentUser = os.Getenv("USER") // Use the current user for the test.
  371. testSigner gossh.Signer
  372. testSignerOnce sync.Once
  373. )
  374. func (ts *localState) Dialer() *tsdial.Dialer {
  375. return &tsdial.Dialer{}
  376. }
  377. func (ts *localState) GetSSH_HostKeys() ([]gossh.Signer, error) {
  378. testSignerOnce.Do(func() {
  379. _, priv, err := ed25519.GenerateKey(rand.Reader)
  380. if err != nil {
  381. panic(err)
  382. }
  383. s, err := gossh.NewSignerFromSigner(priv)
  384. if err != nil {
  385. panic(err)
  386. }
  387. testSigner = s
  388. })
  389. return []gossh.Signer{testSigner}, nil
  390. }
  391. func (ts *localState) ShouldRunSSH() bool {
  392. return ts.sshEnabled
  393. }
  394. func (ts *localState) NetMap() *netmap.NetworkMap {
  395. var policy *tailcfg.SSHPolicy
  396. if ts.matchingRule != nil {
  397. policy = &tailcfg.SSHPolicy{
  398. Rules: []*tailcfg.SSHRule{
  399. ts.matchingRule,
  400. },
  401. }
  402. }
  403. return &netmap.NetworkMap{
  404. SelfNode: (&tailcfg.Node{
  405. ID: 1,
  406. }).View(),
  407. SSHPolicy: policy,
  408. }
  409. }
  410. func (ts *localState) WhoIs(proto string, ipp netip.AddrPort) (n tailcfg.NodeView, u tailcfg.UserProfile, ok bool) {
  411. if proto != "tcp" {
  412. return tailcfg.NodeView{}, tailcfg.UserProfile{}, false
  413. }
  414. return (&tailcfg.Node{
  415. ID: 2,
  416. StableID: "peer-id",
  417. }).View(), tailcfg.UserProfile{
  418. LoginName: "peer",
  419. }, true
  420. }
  421. func (ts *localState) DoNoiseRequest(req *http.Request) (*http.Response, error) {
  422. rec := httptest.NewRecorder()
  423. k, ok := strings.CutPrefix(req.URL.Path, "/ssh-action/")
  424. if !ok {
  425. rec.WriteHeader(http.StatusNotFound)
  426. }
  427. a, ok := ts.serverActions[k]
  428. if !ok {
  429. rec.WriteHeader(http.StatusNotFound)
  430. return rec.Result(), nil
  431. }
  432. rec.WriteHeader(http.StatusOK)
  433. if err := json.NewEncoder(rec).Encode(a); err != nil {
  434. return nil, err
  435. }
  436. return rec.Result(), nil
  437. }
  438. func (ts *localState) TailscaleVarRoot() string {
  439. return ""
  440. }
  441. func (ts *localState) NodeKey() key.NodePublic {
  442. return key.NewNode().Public()
  443. }
  444. func newSSHRule(action *tailcfg.SSHAction) *tailcfg.SSHRule {
  445. return &tailcfg.SSHRule{
  446. SSHUsers: map[string]string{
  447. "*": currentUser,
  448. },
  449. Action: action,
  450. Principals: []*tailcfg.SSHPrincipal{
  451. {
  452. Any: true,
  453. },
  454. },
  455. }
  456. }
  457. func TestSSHRecordingCancelsSessionsOnUploadFailure(t *testing.T) {
  458. if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
  459. t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
  460. }
  461. var handler http.HandlerFunc
  462. recordingServer := mockRecordingServer(t, func(w http.ResponseWriter, r *http.Request) {
  463. handler(w, r)
  464. })
  465. s := &server{
  466. logf: t.Logf,
  467. lb: &localState{
  468. sshEnabled: true,
  469. matchingRule: newSSHRule(
  470. &tailcfg.SSHAction{
  471. Accept: true,
  472. Recorders: []netip.AddrPort{
  473. netip.MustParseAddrPort(recordingServer.Listener.Addr().String()),
  474. },
  475. OnRecordingFailure: &tailcfg.SSHRecorderFailureAction{
  476. RejectSessionWithMessage: "session rejected",
  477. TerminateSessionWithMessage: "session terminated",
  478. },
  479. },
  480. ),
  481. },
  482. }
  483. defer s.Shutdown()
  484. const sshUser = "alice"
  485. cfg := &gossh.ClientConfig{
  486. User: sshUser,
  487. HostKeyCallback: gossh.InsecureIgnoreHostKey(),
  488. }
  489. tests := []struct {
  490. name string
  491. handler func(w http.ResponseWriter, r *http.Request)
  492. sshCommand string
  493. wantClientOutput string
  494. clientOutputMustNotContain []string
  495. }{
  496. {
  497. name: "upload-denied",
  498. handler: func(w http.ResponseWriter, r *http.Request) {
  499. w.WriteHeader(http.StatusForbidden)
  500. },
  501. sshCommand: "echo hello",
  502. wantClientOutput: "session rejected\r\n",
  503. clientOutputMustNotContain: []string{"hello"},
  504. },
  505. {
  506. name: "upload-fails-after-starting",
  507. handler: func(w http.ResponseWriter, r *http.Request) {
  508. w.WriteHeader(http.StatusOK)
  509. w.(http.Flusher).Flush()
  510. r.Body.Read(make([]byte, 1))
  511. time.Sleep(100 * time.Millisecond)
  512. },
  513. sshCommand: "echo hello && sleep 1 && echo world",
  514. wantClientOutput: "\r\n\r\nsession terminated\r\n\r\n",
  515. clientOutputMustNotContain: []string{"world"},
  516. },
  517. }
  518. src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
  519. for _, tt := range tests {
  520. t.Run(tt.name, func(t *testing.T) {
  521. s.logf = t.Logf
  522. tstest.Replace(t, &handler, tt.handler)
  523. sc, dc := memnet.NewTCPConn(src, dst, 1024)
  524. var wg sync.WaitGroup
  525. wg.Add(1)
  526. go func() {
  527. defer wg.Done()
  528. c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
  529. if err != nil {
  530. t.Errorf("client: %v", err)
  531. return
  532. }
  533. client := gossh.NewClient(c, chans, reqs)
  534. defer client.Close()
  535. session, err := client.NewSession()
  536. if err != nil {
  537. t.Errorf("client: %v", err)
  538. return
  539. }
  540. defer session.Close()
  541. t.Logf("client established session")
  542. got, err := session.CombinedOutput(tt.sshCommand)
  543. if err != nil {
  544. t.Logf("client got: %q: %v", got, err)
  545. } else {
  546. t.Errorf("client did not get kicked out: %q", got)
  547. }
  548. gotStr := string(got)
  549. if !strings.HasSuffix(gotStr, tt.wantClientOutput) {
  550. t.Errorf("client got %q, want %q", got, tt.wantClientOutput)
  551. }
  552. for _, x := range tt.clientOutputMustNotContain {
  553. if strings.Contains(gotStr, x) {
  554. t.Errorf("client output must not contain %q", x)
  555. }
  556. }
  557. }()
  558. if err := s.HandleSSHConn(dc); err != nil {
  559. t.Errorf("unexpected error: %v", err)
  560. }
  561. wg.Wait()
  562. })
  563. }
  564. }
  565. func TestMultipleRecorders(t *testing.T) {
  566. if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
  567. t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
  568. }
  569. done := make(chan struct{})
  570. recordingServer := mockRecordingServer(t, func(w http.ResponseWriter, r *http.Request) {
  571. defer close(done)
  572. w.WriteHeader(http.StatusOK)
  573. w.(http.Flusher).Flush()
  574. io.ReadAll(r.Body)
  575. })
  576. badRecorder, err := net.Listen("tcp", ":0")
  577. if err != nil {
  578. t.Fatal(err)
  579. }
  580. badRecorderAddr := badRecorder.Addr().String()
  581. badRecorder.Close()
  582. badRecordingServer500 := mockRecordingServer(t, func(w http.ResponseWriter, r *http.Request) {
  583. w.WriteHeader(http.StatusInternalServerError)
  584. })
  585. s := &server{
  586. logf: t.Logf,
  587. lb: &localState{
  588. sshEnabled: true,
  589. matchingRule: newSSHRule(
  590. &tailcfg.SSHAction{
  591. Accept: true,
  592. Recorders: []netip.AddrPort{
  593. netip.MustParseAddrPort(badRecorderAddr),
  594. netip.MustParseAddrPort(badRecordingServer500.Listener.Addr().String()),
  595. netip.MustParseAddrPort(recordingServer.Listener.Addr().String()),
  596. },
  597. OnRecordingFailure: &tailcfg.SSHRecorderFailureAction{
  598. RejectSessionWithMessage: "session rejected",
  599. TerminateSessionWithMessage: "session terminated",
  600. },
  601. },
  602. ),
  603. },
  604. }
  605. defer s.Shutdown()
  606. src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
  607. sc, dc := memnet.NewTCPConn(src, dst, 1024)
  608. const sshUser = "alice"
  609. cfg := &gossh.ClientConfig{
  610. User: sshUser,
  611. HostKeyCallback: gossh.InsecureIgnoreHostKey(),
  612. }
  613. var wg sync.WaitGroup
  614. wg.Add(1)
  615. go func() {
  616. defer wg.Done()
  617. c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
  618. if err != nil {
  619. t.Errorf("client: %v", err)
  620. return
  621. }
  622. client := gossh.NewClient(c, chans, reqs)
  623. defer client.Close()
  624. session, err := client.NewSession()
  625. if err != nil {
  626. t.Errorf("client: %v", err)
  627. return
  628. }
  629. defer session.Close()
  630. t.Logf("client established session")
  631. out, err := session.CombinedOutput("echo Ran echo!")
  632. if err != nil {
  633. t.Errorf("client: %v", err)
  634. }
  635. if string(out) != "Ran echo!\n" {
  636. t.Errorf("client: unexpected output: %q", out)
  637. }
  638. }()
  639. if err := s.HandleSSHConn(dc); err != nil {
  640. t.Errorf("unexpected error: %v", err)
  641. }
  642. wg.Wait()
  643. select {
  644. case <-done:
  645. case <-time.After(1 * time.Second):
  646. t.Fatal("timed out waiting for recording")
  647. }
  648. }
  649. // TestSSHRecordingNonInteractive tests that the SSH server records the SSH session
  650. // when the client is not interactive (i.e. no PTY).
  651. // It starts a local SSH server and a recording server. The recording server
  652. // records the SSH session and returns it to the test.
  653. // The test then verifies that the recording has a valid CastHeader, it does not
  654. // validate the contents of the recording.
  655. func TestSSHRecordingNonInteractive(t *testing.T) {
  656. if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
  657. t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
  658. }
  659. var recording []byte
  660. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  661. recordingServer := mockRecordingServer(t, func(w http.ResponseWriter, r *http.Request) {
  662. defer cancel()
  663. w.WriteHeader(http.StatusOK)
  664. w.(http.Flusher).Flush()
  665. var err error
  666. recording, err = io.ReadAll(r.Body)
  667. if err != nil {
  668. t.Error(err)
  669. return
  670. }
  671. })
  672. s := &server{
  673. logf: t.Logf,
  674. lb: &localState{
  675. sshEnabled: true,
  676. matchingRule: newSSHRule(
  677. &tailcfg.SSHAction{
  678. Accept: true,
  679. Recorders: []netip.AddrPort{
  680. must.Get(netip.ParseAddrPort(recordingServer.Listener.Addr().String())),
  681. },
  682. OnRecordingFailure: &tailcfg.SSHRecorderFailureAction{
  683. RejectSessionWithMessage: "session rejected",
  684. TerminateSessionWithMessage: "session terminated",
  685. },
  686. },
  687. ),
  688. },
  689. }
  690. defer s.Shutdown()
  691. src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
  692. sc, dc := memnet.NewTCPConn(src, dst, 1024)
  693. const sshUser = "alice"
  694. cfg := &gossh.ClientConfig{
  695. User: sshUser,
  696. HostKeyCallback: gossh.InsecureIgnoreHostKey(),
  697. }
  698. var wg sync.WaitGroup
  699. wg.Add(1)
  700. go func() {
  701. defer wg.Done()
  702. c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
  703. if err != nil {
  704. t.Errorf("client: %v", err)
  705. return
  706. }
  707. client := gossh.NewClient(c, chans, reqs)
  708. defer client.Close()
  709. session, err := client.NewSession()
  710. if err != nil {
  711. t.Errorf("client: %v", err)
  712. return
  713. }
  714. defer session.Close()
  715. t.Logf("client established session")
  716. _, err = session.CombinedOutput("echo Ran echo!")
  717. if err != nil {
  718. t.Errorf("client: %v", err)
  719. }
  720. }()
  721. if err := s.HandleSSHConn(dc); err != nil {
  722. t.Errorf("unexpected error: %v", err)
  723. }
  724. wg.Wait()
  725. <-ctx.Done() // wait for recording to finish
  726. var ch sessionrecording.CastHeader
  727. if err := json.NewDecoder(bytes.NewReader(recording)).Decode(&ch); err != nil {
  728. t.Fatal(err)
  729. }
  730. if ch.SSHUser != sshUser {
  731. t.Errorf("SSHUser = %q; want %q", ch.SSHUser, sshUser)
  732. }
  733. if ch.Command != "echo Ran echo!" {
  734. t.Errorf("Command = %q; want %q", ch.Command, "echo Ran echo!")
  735. }
  736. }
  737. func TestSSHAuthFlow(t *testing.T) {
  738. if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
  739. t.Skipf("skipping on %q; only runs on linux and darwin", runtime.GOOS)
  740. }
  741. acceptRule := newSSHRule(&tailcfg.SSHAction{
  742. Accept: true,
  743. Message: "Welcome to Tailscale SSH!",
  744. })
  745. rejectRule := newSSHRule(&tailcfg.SSHAction{
  746. Reject: true,
  747. Message: "Go Away!",
  748. })
  749. tests := []struct {
  750. name string
  751. sshUser string // defaults to alice
  752. state *localState
  753. wantBanners []string
  754. usesPassword bool
  755. authErr bool
  756. }{
  757. {
  758. name: "no-policy",
  759. state: &localState{
  760. sshEnabled: true,
  761. },
  762. authErr: true,
  763. },
  764. {
  765. name: "accept",
  766. state: &localState{
  767. sshEnabled: true,
  768. matchingRule: acceptRule,
  769. },
  770. wantBanners: []string{"Welcome to Tailscale SSH!"},
  771. },
  772. {
  773. name: "reject",
  774. state: &localState{
  775. sshEnabled: true,
  776. matchingRule: rejectRule,
  777. },
  778. wantBanners: []string{"Go Away!"},
  779. authErr: true,
  780. },
  781. {
  782. name: "simple-check",
  783. state: &localState{
  784. sshEnabled: true,
  785. matchingRule: newSSHRule(&tailcfg.SSHAction{
  786. HoldAndDelegate: "https://unused/ssh-action/accept",
  787. }),
  788. serverActions: map[string]*tailcfg.SSHAction{
  789. "accept": acceptRule.Action,
  790. },
  791. },
  792. wantBanners: []string{"Welcome to Tailscale SSH!"},
  793. },
  794. {
  795. name: "multi-check",
  796. state: &localState{
  797. sshEnabled: true,
  798. matchingRule: newSSHRule(&tailcfg.SSHAction{
  799. Message: "First",
  800. HoldAndDelegate: "https://unused/ssh-action/check1",
  801. }),
  802. serverActions: map[string]*tailcfg.SSHAction{
  803. "check1": {
  804. Message: "url-here",
  805. HoldAndDelegate: "https://unused/ssh-action/check2",
  806. },
  807. "check2": acceptRule.Action,
  808. },
  809. },
  810. wantBanners: []string{"First", "url-here", "Welcome to Tailscale SSH!"},
  811. },
  812. {
  813. name: "check-reject",
  814. state: &localState{
  815. sshEnabled: true,
  816. matchingRule: newSSHRule(&tailcfg.SSHAction{
  817. Message: "First",
  818. HoldAndDelegate: "https://unused/ssh-action/reject",
  819. }),
  820. serverActions: map[string]*tailcfg.SSHAction{
  821. "reject": rejectRule.Action,
  822. },
  823. },
  824. wantBanners: []string{"First", "Go Away!"},
  825. authErr: true,
  826. },
  827. {
  828. name: "force-password-auth",
  829. sshUser: "alice+password",
  830. state: &localState{
  831. sshEnabled: true,
  832. matchingRule: acceptRule,
  833. },
  834. usesPassword: true,
  835. wantBanners: []string{"Welcome to Tailscale SSH!"},
  836. },
  837. }
  838. s := &server{
  839. logf: logger.Discard,
  840. }
  841. defer s.Shutdown()
  842. src, dst := must.Get(netip.ParseAddrPort("100.100.100.101:2231")), must.Get(netip.ParseAddrPort("100.100.100.102:22"))
  843. for _, tc := range tests {
  844. t.Run(tc.name, func(t *testing.T) {
  845. sc, dc := memnet.NewTCPConn(src, dst, 1024)
  846. s.lb = tc.state
  847. sshUser := "alice"
  848. if tc.sshUser != "" {
  849. sshUser = tc.sshUser
  850. }
  851. var passwordUsed atomic.Bool
  852. cfg := &gossh.ClientConfig{
  853. User: sshUser,
  854. HostKeyCallback: gossh.InsecureIgnoreHostKey(),
  855. Auth: []gossh.AuthMethod{
  856. gossh.PasswordCallback(func() (secret string, err error) {
  857. if !tc.usesPassword {
  858. t.Error("unexpected use of PasswordCallback")
  859. return "", errors.New("unexpected use of PasswordCallback")
  860. }
  861. passwordUsed.Store(true)
  862. return "any-pass", nil
  863. }),
  864. },
  865. BannerCallback: func(message string) error {
  866. if len(tc.wantBanners) == 0 {
  867. t.Errorf("unexpected banner: %q", message)
  868. } else if message != tc.wantBanners[0] {
  869. t.Errorf("banner = %q; want %q", message, tc.wantBanners[0])
  870. } else {
  871. t.Logf("banner = %q", message)
  872. tc.wantBanners = tc.wantBanners[1:]
  873. }
  874. return nil
  875. },
  876. }
  877. var wg sync.WaitGroup
  878. wg.Add(1)
  879. go func() {
  880. defer wg.Done()
  881. c, chans, reqs, err := gossh.NewClientConn(sc, sc.RemoteAddr().String(), cfg)
  882. if err != nil {
  883. if !tc.authErr {
  884. t.Errorf("client: %v", err)
  885. }
  886. return
  887. } else if tc.authErr {
  888. c.Close()
  889. t.Errorf("client: expected error, got nil")
  890. return
  891. }
  892. client := gossh.NewClient(c, chans, reqs)
  893. defer client.Close()
  894. session, err := client.NewSession()
  895. if err != nil {
  896. t.Errorf("client: %v", err)
  897. return
  898. }
  899. defer session.Close()
  900. _, err = session.CombinedOutput("echo Ran echo!")
  901. if err != nil {
  902. t.Errorf("client: %v", err)
  903. }
  904. }()
  905. if err := s.HandleSSHConn(dc); err != nil {
  906. t.Errorf("unexpected error: %v", err)
  907. }
  908. wg.Wait()
  909. if len(tc.wantBanners) > 0 {
  910. t.Errorf("missing banners: %v", tc.wantBanners)
  911. }
  912. })
  913. }
  914. }
  915. func TestSSH(t *testing.T) {
  916. var logf logger.Logf = t.Logf
  917. sys := &tsd.System{}
  918. eng, err := wgengine.NewFakeUserspaceEngine(logf, sys.Set, sys.HealthTracker(), sys.UserMetricsRegistry())
  919. if err != nil {
  920. t.Fatal(err)
  921. }
  922. sys.Set(eng)
  923. sys.Set(new(mem.Store))
  924. lb, err := ipnlocal.NewLocalBackend(logf, logid.PublicID{}, sys, 0)
  925. if err != nil {
  926. t.Fatal(err)
  927. }
  928. defer lb.Shutdown()
  929. dir := t.TempDir()
  930. lb.SetVarRoot(dir)
  931. srv := &server{
  932. lb: lb,
  933. logf: logf,
  934. }
  935. sc, err := srv.newConn()
  936. if err != nil {
  937. t.Fatal(err)
  938. }
  939. // Remove the auth checks for the test
  940. sc.insecureSkipTailscaleAuth = true
  941. u, err := user.Current()
  942. if err != nil {
  943. t.Fatal(err)
  944. }
  945. um, err := userLookup(u.Username)
  946. if err != nil {
  947. t.Fatal(err)
  948. }
  949. sc.localUser = um
  950. sc.info = &sshConnInfo{
  951. sshUser: "test",
  952. src: netip.MustParseAddrPort("1.2.3.4:32342"),
  953. dst: netip.MustParseAddrPort("1.2.3.5:22"),
  954. node: (&tailcfg.Node{}).View(),
  955. uprof: tailcfg.UserProfile{},
  956. }
  957. sc.action0 = &tailcfg.SSHAction{Accept: true}
  958. sc.finalAction = sc.action0
  959. sc.Handler = func(s ssh.Session) {
  960. sc.newSSHSession(s).run()
  961. }
  962. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  963. if err != nil {
  964. t.Fatal(err)
  965. }
  966. defer ln.Close()
  967. port := ln.Addr().(*net.TCPAddr).Port
  968. go func() {
  969. for {
  970. c, err := ln.Accept()
  971. if err != nil {
  972. if !errors.Is(err, net.ErrClosed) {
  973. t.Errorf("Accept: %v", err)
  974. }
  975. return
  976. }
  977. go sc.HandleConn(c)
  978. }
  979. }()
  980. execSSH := func(args ...string) *exec.Cmd {
  981. cmd := exec.Command("ssh",
  982. "-F",
  983. "none",
  984. "-v",
  985. "-p", fmt.Sprint(port),
  986. "-o", "StrictHostKeyChecking=no",
  987. "[email protected]")
  988. cmd.Args = append(cmd.Args, args...)
  989. return cmd
  990. }
  991. t.Run("env", func(t *testing.T) {
  992. if cibuild.On() {
  993. t.Skip("Skipping for now; see https://github.com/tailscale/tailscale/issues/4051")
  994. }
  995. cmd := execSSH("LANG=foo env")
  996. cmd.Env = append(os.Environ(), "LOCAL_ENV=bar")
  997. got, err := cmd.CombinedOutput()
  998. if err != nil {
  999. t.Fatal(err, string(got))
  1000. }
  1001. m := parseEnv(got)
  1002. if got := m["USER"]; got == "" || got != u.Username {
  1003. t.Errorf("USER = %q; want %q", got, u.Username)
  1004. }
  1005. if got := m["HOME"]; got == "" || got != u.HomeDir {
  1006. t.Errorf("HOME = %q; want %q", got, u.HomeDir)
  1007. }
  1008. if got := m["PWD"]; got == "" || got != u.HomeDir {
  1009. t.Errorf("PWD = %q; want %q", got, u.HomeDir)
  1010. }
  1011. if got := m["SHELL"]; got == "" {
  1012. t.Errorf("no SHELL")
  1013. }
  1014. if got, want := m["LANG"], "foo"; got != want {
  1015. t.Errorf("LANG = %q; want %q", got, want)
  1016. }
  1017. if got := m["LOCAL_ENV"]; got != "" {
  1018. t.Errorf("LOCAL_ENV leaked over ssh: %v", got)
  1019. }
  1020. t.Logf("got: %+v", m)
  1021. })
  1022. t.Run("stdout_stderr", func(t *testing.T) {
  1023. cmd := execSSH("sh", "-c", "echo foo; echo bar >&2")
  1024. var outBuf, errBuf bytes.Buffer
  1025. cmd.Stdout = &outBuf
  1026. cmd.Stderr = &errBuf
  1027. if err := cmd.Run(); err != nil {
  1028. t.Fatal(err)
  1029. }
  1030. t.Logf("Got: %q and %q", outBuf.Bytes(), errBuf.Bytes())
  1031. // TODO: figure out why these aren't right. should be
  1032. // "foo\n" and "bar\n", not "\n" and "bar\n".
  1033. })
  1034. t.Run("large_file", func(t *testing.T) {
  1035. const wantSize = 1e6
  1036. var outBuf bytes.Buffer
  1037. cmd := execSSH("head", "-c", strconv.Itoa(wantSize), "/dev/zero")
  1038. cmd.Stdout = &outBuf
  1039. if err := cmd.Run(); err != nil {
  1040. t.Fatal(err)
  1041. }
  1042. if gotSize := outBuf.Len(); gotSize != wantSize {
  1043. t.Fatalf("got %d, want %d", gotSize, int(wantSize))
  1044. }
  1045. })
  1046. t.Run("stdin", func(t *testing.T) {
  1047. if cibuild.On() {
  1048. t.Skip("Skipping for now; see https://github.com/tailscale/tailscale/issues/4051")
  1049. }
  1050. cmd := execSSH("cat")
  1051. var outBuf bytes.Buffer
  1052. cmd.Stdout = &outBuf
  1053. const str = "foo\nbar\n"
  1054. cmd.Stdin = strings.NewReader(str)
  1055. if err := cmd.Run(); err != nil {
  1056. t.Fatal(err)
  1057. }
  1058. if got := outBuf.String(); got != str {
  1059. t.Errorf("got %q; want %q", got, str)
  1060. }
  1061. })
  1062. }
  1063. func parseEnv(out []byte) map[string]string {
  1064. e := map[string]string{}
  1065. for line := range lineiter.Bytes(out) {
  1066. if i := bytes.IndexByte(line, '='); i != -1 {
  1067. e[string(line[:i])] = string(line[i+1:])
  1068. }
  1069. }
  1070. return e
  1071. }
  1072. func TestAcceptEnvPair(t *testing.T) {
  1073. tests := []struct {
  1074. in string
  1075. want bool
  1076. }{
  1077. {"TERM=x", true},
  1078. {"term=x", false},
  1079. {"TERM", false},
  1080. {"LC_FOO=x", true},
  1081. {"LD_PRELOAD=naah", false},
  1082. {"TERM=screen-256color", true},
  1083. }
  1084. for _, tt := range tests {
  1085. if got := acceptEnvPair(tt.in); got != tt.want {
  1086. t.Errorf("for %q, got %v; want %v", tt.in, got, tt.want)
  1087. }
  1088. }
  1089. }
  1090. func TestPathFromPAMEnvLine(t *testing.T) {
  1091. u := &user.User{Username: "foo", HomeDir: "/Homes/Foo"}
  1092. tests := []struct {
  1093. line string
  1094. u *user.User
  1095. want string
  1096. }{
  1097. {"", u, ""},
  1098. {`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"`,
  1099. 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"},
  1100. {`PATH DEFAULT="@{SOMETHING_ELSE}:nope:@{HOME}"`,
  1101. u, ""},
  1102. }
  1103. for i, tt := range tests {
  1104. got := pathFromPAMEnvLine([]byte(tt.line), tt.u)
  1105. if got != tt.want {
  1106. t.Errorf("%d. got %q; want %q", i, got, tt.want)
  1107. }
  1108. }
  1109. }
  1110. func TestExpandDefaultPathTmpl(t *testing.T) {
  1111. u := &user.User{Username: "foo", HomeDir: "/Homes/Foo"}
  1112. tests := []struct {
  1113. t string
  1114. u *user.User
  1115. want string
  1116. }{
  1117. {"", u, ""},
  1118. {`/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`,
  1119. 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"},
  1120. {`@{SOMETHING_ELSE}:nope:@{HOME}`, u, ""},
  1121. }
  1122. for i, tt := range tests {
  1123. got := expandDefaultPathTmpl(tt.t, tt.u)
  1124. if got != tt.want {
  1125. t.Errorf("%d. got %q; want %q", i, got, tt.want)
  1126. }
  1127. }
  1128. }
  1129. func TestPathFromPAMEnvLineOnNixOS(t *testing.T) {
  1130. if runtime.GOOS != "linux" {
  1131. t.Skip("skipping on non-linux")
  1132. }
  1133. if distro.Get() != distro.NixOS {
  1134. t.Skip("skipping on non-NixOS")
  1135. }
  1136. u, err := user.Current()
  1137. if err != nil {
  1138. t.Fatal(err)
  1139. }
  1140. got := defaultPathForUserOnNixOS(u)
  1141. if got == "" {
  1142. x, err := os.ReadFile("/etc/pam/environment")
  1143. t.Fatalf("no result. file was: err=%v, contents=%s", err, x)
  1144. }
  1145. t.Logf("success; got=%q", got)
  1146. }
  1147. func TestStdOsUserUserAssumptions(t *testing.T) {
  1148. v := reflect.TypeFor[user.User]()
  1149. if got, want := v.NumField(), 5; got != want {
  1150. t.Errorf("os/user.User has %v fields; this package assumes %v", got, want)
  1151. }
  1152. }
  1153. func mockRecordingServer(t *testing.T, handleRecord http.HandlerFunc) *httptest.Server {
  1154. t.Helper()
  1155. mux := http.NewServeMux()
  1156. mux.HandleFunc("POST /record", func(http.ResponseWriter, *http.Request) {
  1157. t.Errorf("v1 recording endpoint called")
  1158. })
  1159. mux.HandleFunc("HEAD /v2/record", func(http.ResponseWriter, *http.Request) {})
  1160. mux.HandleFunc("POST /v2/record", handleRecord)
  1161. h2s := &http2.Server{}
  1162. srv := httptest.NewUnstartedServer(h2c.NewHandler(mux, h2s))
  1163. if err := http2.ConfigureServer(srv.Config, h2s); err != nil {
  1164. t.Errorf("configuring HTTP/2 support in recording server: %v", err)
  1165. }
  1166. srv.Start()
  1167. t.Cleanup(srv.Close)
  1168. return srv
  1169. }