web_test.go 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package web
  4. import (
  5. "bytes"
  6. "context"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "net/http/httptest"
  13. "net/netip"
  14. "net/url"
  15. "slices"
  16. "strings"
  17. "testing"
  18. "time"
  19. "github.com/google/go-cmp/cmp"
  20. "tailscale.com/client/local"
  21. "tailscale.com/client/tailscale/apitype"
  22. "tailscale.com/ipn"
  23. "tailscale.com/ipn/ipnstate"
  24. "tailscale.com/net/memnet"
  25. "tailscale.com/tailcfg"
  26. "tailscale.com/types/views"
  27. "tailscale.com/util/httpm"
  28. "tailscale.com/util/syspolicy/policyclient"
  29. )
  30. func TestQnapAuthnURL(t *testing.T) {
  31. query := url.Values{
  32. "qtoken": []string{"token"},
  33. }
  34. tests := []struct {
  35. name string
  36. in string
  37. want string
  38. }{
  39. {
  40. name: "localhost http",
  41. in: "http://localhost:8088/",
  42. want: "http://localhost:8088/cgi-bin/authLogin.cgi?qtoken=token",
  43. },
  44. {
  45. name: "localhost https",
  46. in: "https://localhost:5000/",
  47. want: "https://localhost:5000/cgi-bin/authLogin.cgi?qtoken=token",
  48. },
  49. {
  50. name: "IP http",
  51. in: "http://10.1.20.4:80/",
  52. want: "http://10.1.20.4:80/cgi-bin/authLogin.cgi?qtoken=token",
  53. },
  54. {
  55. name: "IP6 https",
  56. in: "https://[ff7d:0:1:2::1]/",
  57. want: "https://[ff7d:0:1:2::1]/cgi-bin/authLogin.cgi?qtoken=token",
  58. },
  59. {
  60. name: "hostname https",
  61. in: "https://qnap.example.com/",
  62. want: "https://qnap.example.com/cgi-bin/authLogin.cgi?qtoken=token",
  63. },
  64. {
  65. name: "invalid URL",
  66. in: "This is not a URL, it is a really really really really really really really really really really really really long string to exercise the URL truncation code in the error path.",
  67. want: "http://localhost/cgi-bin/authLogin.cgi?qtoken=token",
  68. },
  69. {
  70. name: "err != nil",
  71. in: "http://192.168.0.%31/",
  72. want: "http://localhost/cgi-bin/authLogin.cgi?qtoken=token",
  73. },
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. u := qnapAuthnURL(tt.in, query)
  78. if u != tt.want {
  79. t.Errorf("expected url: %q, got: %q", tt.want, u)
  80. }
  81. })
  82. }
  83. }
  84. // TestServeAPI tests the web client api's handling of
  85. // 1. invalid endpoint errors
  86. // 2. permissioning of api endpoints based on node capabilities
  87. func TestServeAPI(t *testing.T) {
  88. selfTags := views.SliceOf([]string{"tag:server"})
  89. self := &ipnstate.PeerStatus{ID: "self", Tags: &selfTags}
  90. prefs := &ipn.Prefs{}
  91. remoteUser := &tailcfg.UserProfile{ID: tailcfg.UserID(1)}
  92. remoteIPWithAllCapabilities := "100.100.100.101"
  93. remoteIPWithNoCapabilities := "100.100.100.102"
  94. lal := memnet.Listen("local-tailscaled.sock:80")
  95. defer lal.Close()
  96. localapi := mockLocalAPI(t,
  97. map[string]*apitype.WhoIsResponse{
  98. remoteIPWithAllCapabilities: {
  99. Node: &tailcfg.Node{StableID: "node1"},
  100. UserProfile: remoteUser,
  101. CapMap: tailcfg.PeerCapMap{tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{"{\"canEdit\":[\"*\"]}"}},
  102. },
  103. remoteIPWithNoCapabilities: {
  104. Node: &tailcfg.Node{StableID: "node2"},
  105. UserProfile: remoteUser,
  106. },
  107. },
  108. func() *ipnstate.PeerStatus { return self },
  109. func() *ipn.Prefs { return prefs },
  110. nil,
  111. )
  112. defer localapi.Close()
  113. go localapi.Serve(lal)
  114. s := &Server{
  115. mode: ManageServerMode,
  116. lc: &local.Client{Dial: lal.Dial},
  117. timeNow: time.Now,
  118. }
  119. type requestTest struct {
  120. remoteIP string
  121. wantResponse string
  122. wantStatus int
  123. }
  124. tests := []struct {
  125. reqPath string
  126. reqMethod string
  127. reqContentType string
  128. reqBody string
  129. tests []requestTest
  130. }{{
  131. reqPath: "/not-an-endpoint",
  132. reqMethod: httpm.POST,
  133. tests: []requestTest{{
  134. remoteIP: remoteIPWithNoCapabilities,
  135. wantResponse: "invalid endpoint",
  136. wantStatus: http.StatusNotFound,
  137. }, {
  138. remoteIP: remoteIPWithAllCapabilities,
  139. wantResponse: "invalid endpoint",
  140. wantStatus: http.StatusNotFound,
  141. }},
  142. }, {
  143. reqPath: "/local/v0/not-an-endpoint",
  144. reqMethod: httpm.POST,
  145. tests: []requestTest{{
  146. remoteIP: remoteIPWithNoCapabilities,
  147. wantResponse: "invalid endpoint",
  148. wantStatus: http.StatusNotFound,
  149. }, {
  150. remoteIP: remoteIPWithAllCapabilities,
  151. wantResponse: "invalid endpoint",
  152. wantStatus: http.StatusNotFound,
  153. }},
  154. }, {
  155. reqPath: "/local/v0/logout",
  156. reqMethod: httpm.POST,
  157. tests: []requestTest{{
  158. remoteIP: remoteIPWithNoCapabilities,
  159. wantResponse: "not allowed", // requesting node has insufficient permissions
  160. wantStatus: http.StatusUnauthorized,
  161. }, {
  162. remoteIP: remoteIPWithAllCapabilities,
  163. wantResponse: "success", // requesting node has sufficient permissions
  164. wantStatus: http.StatusOK,
  165. }},
  166. }, {
  167. reqPath: "/exit-nodes",
  168. reqMethod: httpm.GET,
  169. tests: []requestTest{{
  170. remoteIP: remoteIPWithNoCapabilities,
  171. wantResponse: "null",
  172. wantStatus: http.StatusOK, // allowed, no additional capabilities required
  173. }, {
  174. remoteIP: remoteIPWithAllCapabilities,
  175. wantResponse: "null",
  176. wantStatus: http.StatusOK,
  177. }},
  178. }, {
  179. reqPath: "/routes",
  180. reqMethod: httpm.POST,
  181. reqBody: "{\"setExitNode\":true}",
  182. tests: []requestTest{{
  183. remoteIP: remoteIPWithNoCapabilities,
  184. wantResponse: "not allowed",
  185. wantStatus: http.StatusUnauthorized,
  186. }, {
  187. remoteIP: remoteIPWithAllCapabilities,
  188. wantStatus: http.StatusOK,
  189. }},
  190. }, {
  191. reqPath: "/local/v0/prefs",
  192. reqMethod: httpm.PATCH,
  193. reqBody: "{\"runSSHSet\":true}",
  194. reqContentType: "application/json",
  195. tests: []requestTest{{
  196. remoteIP: remoteIPWithNoCapabilities,
  197. wantResponse: "not allowed",
  198. wantStatus: http.StatusUnauthorized,
  199. }, {
  200. remoteIP: remoteIPWithAllCapabilities,
  201. wantStatus: http.StatusOK,
  202. }},
  203. }, {
  204. reqPath: "/local/v0/prefs",
  205. reqMethod: httpm.PATCH,
  206. reqContentType: "multipart/form-data",
  207. tests: []requestTest{{
  208. remoteIP: remoteIPWithNoCapabilities,
  209. wantResponse: "invalid request",
  210. wantStatus: http.StatusBadRequest,
  211. }, {
  212. remoteIP: remoteIPWithAllCapabilities,
  213. wantResponse: "invalid request",
  214. wantStatus: http.StatusBadRequest,
  215. }},
  216. }}
  217. for _, tt := range tests {
  218. for _, req := range tt.tests {
  219. t.Run(req.remoteIP+"_requesting_"+tt.reqPath, func(t *testing.T) {
  220. var reqBody io.Reader
  221. if tt.reqBody != "" {
  222. reqBody = bytes.NewBuffer([]byte(tt.reqBody))
  223. }
  224. r := httptest.NewRequest(tt.reqMethod, "/api"+tt.reqPath, reqBody)
  225. r.RemoteAddr = req.remoteIP
  226. if tt.reqContentType != "" {
  227. r.Header.Add("Content-Type", tt.reqContentType)
  228. }
  229. w := httptest.NewRecorder()
  230. s.serveAPI(w, r)
  231. res := w.Result()
  232. defer res.Body.Close()
  233. if gotStatus := res.StatusCode; req.wantStatus != gotStatus {
  234. t.Errorf("wrong status; want=%v, got=%v", req.wantStatus, gotStatus)
  235. }
  236. body, err := io.ReadAll(res.Body)
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. gotResp := strings.TrimSuffix(string(body), "\n") // trim trailing newline
  241. if req.wantResponse != gotResp {
  242. t.Errorf("wrong response; want=%q, got=%q", req.wantResponse, gotResp)
  243. }
  244. })
  245. }
  246. }
  247. }
  248. func TestGetTailscaleBrowserSession(t *testing.T) {
  249. userA := &tailcfg.UserProfile{ID: tailcfg.UserID(1)}
  250. userB := &tailcfg.UserProfile{ID: tailcfg.UserID(2)}
  251. userANodeIP := "100.100.100.101"
  252. userBNodeIP := "100.100.100.102"
  253. taggedNodeIP := "100.100.100.103"
  254. var selfNode *ipnstate.PeerStatus
  255. tags := views.SliceOf([]string{"tag:server"})
  256. tailnetNodes := map[string]*apitype.WhoIsResponse{
  257. userANodeIP: {
  258. Node: &tailcfg.Node{ID: 1, StableID: "1"},
  259. UserProfile: userA,
  260. },
  261. userBNodeIP: {
  262. Node: &tailcfg.Node{ID: 2, StableID: "2"},
  263. UserProfile: userB,
  264. },
  265. taggedNodeIP: {
  266. Node: &tailcfg.Node{ID: 3, StableID: "3", Tags: tags.AsSlice()},
  267. },
  268. }
  269. lal := memnet.Listen("local-tailscaled.sock:80")
  270. defer lal.Close()
  271. localapi := mockLocalAPI(t, tailnetNodes, func() *ipnstate.PeerStatus { return selfNode }, nil, nil)
  272. defer localapi.Close()
  273. go localapi.Serve(lal)
  274. s := &Server{
  275. timeNow: time.Now,
  276. lc: &local.Client{Dial: lal.Dial},
  277. }
  278. // Add some browser sessions to cache state.
  279. userASession := &browserSession{
  280. ID: "cookie1",
  281. SrcNode: 1,
  282. SrcUser: userA.ID,
  283. Created: time.Now(),
  284. Authenticated: false, // not yet authenticated
  285. }
  286. userBSession := &browserSession{
  287. ID: "cookie2",
  288. SrcNode: 2,
  289. SrcUser: userB.ID,
  290. Created: time.Now().Add(-2 * sessionCookieExpiry),
  291. Authenticated: true, // expired
  292. }
  293. userASessionAuthorized := &browserSession{
  294. ID: "cookie3",
  295. SrcNode: 1,
  296. SrcUser: userA.ID,
  297. Created: time.Now(),
  298. Authenticated: true, // authenticated and not expired
  299. }
  300. s.browserSessions.Store(userASession.ID, userASession)
  301. s.browserSessions.Store(userBSession.ID, userBSession)
  302. s.browserSessions.Store(userASessionAuthorized.ID, userASessionAuthorized)
  303. tests := []struct {
  304. name string
  305. selfNode *ipnstate.PeerStatus
  306. remoteAddr string
  307. cookie string
  308. wantSession *browserSession
  309. wantError error
  310. wantIsAuthorized bool // response from session.isAuthorized
  311. }{
  312. {
  313. name: "not-connected-over-tailscale",
  314. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userA.ID},
  315. remoteAddr: "77.77.77.77",
  316. wantSession: nil,
  317. wantError: errNotUsingTailscale,
  318. },
  319. {
  320. name: "no-session-user-self-node",
  321. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userA.ID},
  322. remoteAddr: userANodeIP,
  323. cookie: "not-a-cookie",
  324. wantSession: nil,
  325. wantError: errNoSession,
  326. },
  327. {
  328. name: "no-session-tagged-self-node",
  329. selfNode: &ipnstate.PeerStatus{ID: "self", Tags: &tags},
  330. remoteAddr: userANodeIP,
  331. wantSession: nil,
  332. wantError: errNoSession,
  333. },
  334. {
  335. name: "not-owner",
  336. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userA.ID},
  337. remoteAddr: userBNodeIP,
  338. wantSession: nil,
  339. wantError: errNotOwner,
  340. },
  341. {
  342. name: "tagged-remote-source",
  343. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userA.ID},
  344. remoteAddr: taggedNodeIP,
  345. wantSession: nil,
  346. wantError: errTaggedRemoteSource,
  347. },
  348. {
  349. name: "tagged-local-source",
  350. selfNode: &ipnstate.PeerStatus{ID: "3"},
  351. remoteAddr: taggedNodeIP, // same node as selfNode
  352. wantSession: nil,
  353. wantError: errTaggedLocalSource,
  354. },
  355. {
  356. name: "not-tagged-local-source",
  357. selfNode: &ipnstate.PeerStatus{ID: "1", UserID: userA.ID},
  358. remoteAddr: userANodeIP, // same node as selfNode
  359. cookie: userASession.ID,
  360. wantSession: userASession,
  361. wantError: nil, // should not error
  362. },
  363. {
  364. name: "has-session",
  365. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userA.ID},
  366. remoteAddr: userANodeIP,
  367. cookie: userASession.ID,
  368. wantSession: userASession,
  369. wantError: nil,
  370. },
  371. {
  372. name: "has-authorized-session",
  373. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userA.ID},
  374. remoteAddr: userANodeIP,
  375. cookie: userASessionAuthorized.ID,
  376. wantSession: userASessionAuthorized,
  377. wantError: nil,
  378. wantIsAuthorized: true,
  379. },
  380. {
  381. name: "session-associated-with-different-source",
  382. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userB.ID},
  383. remoteAddr: userBNodeIP,
  384. cookie: userASession.ID,
  385. wantSession: nil,
  386. wantError: errNoSession,
  387. },
  388. {
  389. name: "session-expired",
  390. selfNode: &ipnstate.PeerStatus{ID: "self", UserID: userB.ID},
  391. remoteAddr: userBNodeIP,
  392. cookie: userBSession.ID,
  393. wantSession: nil,
  394. wantError: errNoSession,
  395. },
  396. }
  397. for _, tt := range tests {
  398. t.Run(tt.name, func(t *testing.T) {
  399. selfNode = tt.selfNode
  400. r := &http.Request{RemoteAddr: tt.remoteAddr, Header: http.Header{}}
  401. if tt.cookie != "" {
  402. r.AddCookie(&http.Cookie{Name: sessionCookieName, Value: tt.cookie})
  403. }
  404. session, _, _, err := s.getSession(r)
  405. if !errors.Is(err, tt.wantError) {
  406. t.Errorf("wrong error; want=%v, got=%v", tt.wantError, err)
  407. }
  408. if diff := cmp.Diff(session, tt.wantSession); diff != "" {
  409. t.Errorf("wrong session; (-got+want):%v", diff)
  410. }
  411. if gotIsAuthorized := session.isAuthorized(s.timeNow()); gotIsAuthorized != tt.wantIsAuthorized {
  412. t.Errorf("wrong isAuthorized; want=%v, got=%v", tt.wantIsAuthorized, gotIsAuthorized)
  413. }
  414. })
  415. }
  416. }
  417. // TestAuthorizeRequest tests the s.authorizeRequest function.
  418. // 2023-10-18: These tests currently cover tailscale auth mode (not platform auth).
  419. func TestAuthorizeRequest(t *testing.T) {
  420. // Create self and remoteNode owned by same user.
  421. // See TestGetTailscaleBrowserSession for tests of
  422. // browser sessions w/ different users.
  423. user := &tailcfg.UserProfile{ID: tailcfg.UserID(1)}
  424. self := &ipnstate.PeerStatus{ID: "self", UserID: user.ID}
  425. remoteNode := &apitype.WhoIsResponse{Node: &tailcfg.Node{StableID: "node"}, UserProfile: user}
  426. remoteIP := "100.100.100.101"
  427. lal := memnet.Listen("local-tailscaled.sock:80")
  428. defer lal.Close()
  429. localapi := mockLocalAPI(t,
  430. map[string]*apitype.WhoIsResponse{remoteIP: remoteNode},
  431. func() *ipnstate.PeerStatus { return self },
  432. nil,
  433. nil,
  434. )
  435. defer localapi.Close()
  436. go localapi.Serve(lal)
  437. s := &Server{
  438. mode: ManageServerMode,
  439. lc: &local.Client{Dial: lal.Dial},
  440. timeNow: time.Now,
  441. }
  442. validCookie := "ts-cookie"
  443. s.browserSessions.Store(validCookie, &browserSession{
  444. ID: validCookie,
  445. SrcNode: remoteNode.Node.ID,
  446. SrcUser: user.ID,
  447. Created: time.Now(),
  448. Authenticated: true,
  449. })
  450. tests := []struct {
  451. reqPath string
  452. reqMethod string
  453. wantOkNotOverTailscale bool // simulates req over public internet
  454. wantOkWithoutSession bool // simulates req over TS without valid browser session
  455. wantOkWithSession bool // simulates req over TS with valid browser session
  456. }{{
  457. reqPath: "/api/data",
  458. reqMethod: httpm.GET,
  459. wantOkNotOverTailscale: false,
  460. wantOkWithoutSession: true,
  461. wantOkWithSession: true,
  462. }, {
  463. reqPath: "/api/data",
  464. reqMethod: httpm.POST,
  465. wantOkNotOverTailscale: false,
  466. wantOkWithoutSession: false,
  467. wantOkWithSession: true,
  468. }, {
  469. reqPath: "/api/somethingelse",
  470. reqMethod: httpm.GET,
  471. wantOkNotOverTailscale: false,
  472. wantOkWithoutSession: false,
  473. wantOkWithSession: true,
  474. }, {
  475. reqPath: "/assets/styles.css",
  476. wantOkNotOverTailscale: false,
  477. wantOkWithoutSession: true,
  478. wantOkWithSession: true,
  479. }}
  480. for _, tt := range tests {
  481. t.Run(fmt.Sprintf("%s-%s", tt.reqMethod, tt.reqPath), func(t *testing.T) {
  482. doAuthorize := func(remoteAddr string, cookie string) bool {
  483. r := httptest.NewRequest(tt.reqMethod, tt.reqPath, nil)
  484. r.RemoteAddr = remoteAddr
  485. if cookie != "" {
  486. r.AddCookie(&http.Cookie{Name: sessionCookieName, Value: cookie})
  487. }
  488. w := httptest.NewRecorder()
  489. return s.authorizeRequest(w, r)
  490. }
  491. // Do request from non-Tailscale IP.
  492. if gotOk := doAuthorize("123.456.789.999", ""); gotOk != tt.wantOkNotOverTailscale {
  493. t.Errorf("wantOkNotOverTailscale; want=%v, got=%v", tt.wantOkNotOverTailscale, gotOk)
  494. }
  495. // Do request from Tailscale IP w/o associated session.
  496. if gotOk := doAuthorize(remoteIP, ""); gotOk != tt.wantOkWithoutSession {
  497. t.Errorf("wantOkWithoutSession; want=%v, got=%v", tt.wantOkWithoutSession, gotOk)
  498. }
  499. // Do request from Tailscale IP w/ associated session.
  500. if gotOk := doAuthorize(remoteIP, validCookie); gotOk != tt.wantOkWithSession {
  501. t.Errorf("wantOkWithSession; want=%v, got=%v", tt.wantOkWithSession, gotOk)
  502. }
  503. })
  504. }
  505. }
  506. func TestServeAuth(t *testing.T) {
  507. user := &tailcfg.UserProfile{LoginName: "[email protected]", ID: tailcfg.UserID(1)}
  508. self := &ipnstate.PeerStatus{
  509. ID: "self",
  510. UserID: user.ID,
  511. TailscaleIPs: []netip.Addr{netip.MustParseAddr("100.1.2.3")},
  512. }
  513. remoteIP := "100.100.100.101"
  514. remoteNode := &apitype.WhoIsResponse{
  515. Node: &tailcfg.Node{
  516. Name: "nodey",
  517. ID: 1,
  518. Addresses: []netip.Prefix{netip.MustParsePrefix(remoteIP + "/32")},
  519. },
  520. UserProfile: user,
  521. }
  522. vi := &viewerIdentity{
  523. LoginName: user.LoginName,
  524. NodeName: remoteNode.Node.Name,
  525. NodeIP: remoteIP,
  526. ProfilePicURL: user.ProfilePicURL,
  527. Capabilities: peerCapabilities{capFeatureAll: true},
  528. }
  529. testControlURL := &defaultControlURL
  530. lal := memnet.Listen("local-tailscaled.sock:80")
  531. defer lal.Close()
  532. localapi := mockLocalAPI(t,
  533. map[string]*apitype.WhoIsResponse{remoteIP: remoteNode},
  534. func() *ipnstate.PeerStatus { return self },
  535. func() *ipn.Prefs {
  536. return &ipn.Prefs{ControlURL: *testControlURL}
  537. },
  538. nil,
  539. )
  540. defer localapi.Close()
  541. go localapi.Serve(lal)
  542. timeNow := time.Now()
  543. oneHourAgo := timeNow.Add(-time.Hour)
  544. sixtyDaysAgo := timeNow.Add(-sessionCookieExpiry * 2)
  545. s := &Server{
  546. mode: ManageServerMode,
  547. lc: &local.Client{Dial: lal.Dial},
  548. timeNow: func() time.Time { return timeNow },
  549. newAuthURL: mockNewAuthURL,
  550. waitAuthURL: mockWaitAuthURL,
  551. polc: policyclient.NoPolicyClient{},
  552. }
  553. successCookie := "ts-cookie-success"
  554. s.browserSessions.Store(successCookie, &browserSession{
  555. ID: successCookie,
  556. SrcNode: remoteNode.Node.ID,
  557. SrcUser: user.ID,
  558. Created: oneHourAgo,
  559. AuthID: testAuthPathSuccess,
  560. AuthURL: *testControlURL + testAuthPathSuccess,
  561. })
  562. failureCookie := "ts-cookie-failure"
  563. s.browserSessions.Store(failureCookie, &browserSession{
  564. ID: failureCookie,
  565. SrcNode: remoteNode.Node.ID,
  566. SrcUser: user.ID,
  567. Created: oneHourAgo,
  568. AuthID: testAuthPathError,
  569. AuthURL: *testControlURL + testAuthPathError,
  570. })
  571. expiredCookie := "ts-cookie-expired"
  572. s.browserSessions.Store(expiredCookie, &browserSession{
  573. ID: expiredCookie,
  574. SrcNode: remoteNode.Node.ID,
  575. SrcUser: user.ID,
  576. Created: sixtyDaysAgo,
  577. AuthID: "/a/old-auth-url",
  578. AuthURL: *testControlURL + "/a/old-auth-url",
  579. })
  580. tests := []struct {
  581. name string
  582. controlURL string // if empty, defaultControlURL is used
  583. cookie string // cookie attached to request
  584. wantNewCookie bool // want new cookie generated during request
  585. wantSession *browserSession // session associated w/ cookie after request
  586. path string
  587. wantStatus int
  588. wantResp any
  589. }{
  590. {
  591. name: "no-session",
  592. path: "/api/auth",
  593. wantStatus: http.StatusOK,
  594. wantResp: &authResponse{ViewerIdentity: vi, ServerMode: ManageServerMode},
  595. wantNewCookie: false,
  596. wantSession: nil,
  597. },
  598. {
  599. name: "new-session",
  600. path: "/api/auth/session/new",
  601. wantStatus: http.StatusOK,
  602. wantResp: &newSessionAuthResponse{AuthURL: *testControlURL + testAuthPath},
  603. wantNewCookie: true,
  604. wantSession: &browserSession{
  605. ID: "GENERATED_ID", // gets swapped for newly created ID by test
  606. SrcNode: remoteNode.Node.ID,
  607. SrcUser: user.ID,
  608. Created: timeNow,
  609. AuthID: testAuthPath,
  610. AuthURL: *testControlURL + testAuthPath,
  611. Authenticated: false,
  612. },
  613. },
  614. {
  615. name: "query-existing-incomplete-session",
  616. path: "/api/auth",
  617. cookie: successCookie,
  618. wantStatus: http.StatusOK,
  619. wantResp: &authResponse{ViewerIdentity: vi, ServerMode: ManageServerMode},
  620. wantSession: &browserSession{
  621. ID: successCookie,
  622. SrcNode: remoteNode.Node.ID,
  623. SrcUser: user.ID,
  624. Created: oneHourAgo,
  625. AuthID: testAuthPathSuccess,
  626. AuthURL: *testControlURL + testAuthPathSuccess,
  627. Authenticated: false,
  628. },
  629. },
  630. {
  631. name: "existing-session-used",
  632. path: "/api/auth/session/new", // should not create new session
  633. cookie: successCookie,
  634. wantStatus: http.StatusOK,
  635. wantResp: &newSessionAuthResponse{AuthURL: *testControlURL + testAuthPathSuccess},
  636. wantSession: &browserSession{
  637. ID: successCookie,
  638. SrcNode: remoteNode.Node.ID,
  639. SrcUser: user.ID,
  640. Created: oneHourAgo,
  641. AuthID: testAuthPathSuccess,
  642. AuthURL: *testControlURL + testAuthPathSuccess,
  643. Authenticated: false,
  644. },
  645. },
  646. {
  647. name: "transition-to-successful-session",
  648. path: "/api/auth/session/wait",
  649. cookie: successCookie,
  650. wantStatus: http.StatusOK,
  651. wantResp: nil,
  652. wantSession: &browserSession{
  653. ID: successCookie,
  654. SrcNode: remoteNode.Node.ID,
  655. SrcUser: user.ID,
  656. Created: oneHourAgo,
  657. AuthID: testAuthPathSuccess,
  658. AuthURL: *testControlURL + testAuthPathSuccess,
  659. Authenticated: true,
  660. },
  661. },
  662. {
  663. name: "query-existing-complete-session",
  664. path: "/api/auth",
  665. cookie: successCookie,
  666. wantStatus: http.StatusOK,
  667. wantResp: &authResponse{Authorized: true, ViewerIdentity: vi, ServerMode: ManageServerMode},
  668. wantSession: &browserSession{
  669. ID: successCookie,
  670. SrcNode: remoteNode.Node.ID,
  671. SrcUser: user.ID,
  672. Created: oneHourAgo,
  673. AuthID: testAuthPathSuccess,
  674. AuthURL: *testControlURL + testAuthPathSuccess,
  675. Authenticated: true,
  676. },
  677. },
  678. {
  679. name: "transition-to-failed-session",
  680. path: "/api/auth/session/wait",
  681. cookie: failureCookie,
  682. wantStatus: http.StatusUnauthorized,
  683. wantResp: nil,
  684. wantSession: nil, // session deleted
  685. },
  686. {
  687. name: "failed-session-cleaned-up",
  688. path: "/api/auth/session/new",
  689. cookie: failureCookie,
  690. wantStatus: http.StatusOK,
  691. wantResp: &newSessionAuthResponse{AuthURL: *testControlURL + testAuthPath},
  692. wantNewCookie: true,
  693. wantSession: &browserSession{
  694. ID: "GENERATED_ID",
  695. SrcNode: remoteNode.Node.ID,
  696. SrcUser: user.ID,
  697. Created: timeNow,
  698. AuthID: testAuthPath,
  699. AuthURL: *testControlURL + testAuthPath,
  700. Authenticated: false,
  701. },
  702. },
  703. {
  704. name: "expired-cookie-gets-new-session",
  705. path: "/api/auth/session/new",
  706. cookie: expiredCookie,
  707. wantStatus: http.StatusOK,
  708. wantResp: &newSessionAuthResponse{AuthURL: *testControlURL + testAuthPath},
  709. wantNewCookie: true,
  710. wantSession: &browserSession{
  711. ID: "GENERATED_ID",
  712. SrcNode: remoteNode.Node.ID,
  713. SrcUser: user.ID,
  714. Created: timeNow,
  715. AuthID: testAuthPath,
  716. AuthURL: *testControlURL + testAuthPath,
  717. Authenticated: false,
  718. },
  719. },
  720. {
  721. name: "control-server-no-check-mode",
  722. controlURL: "http://alternate-server.com/",
  723. path: "/api/auth/session/new",
  724. wantStatus: http.StatusOK,
  725. wantResp: &newSessionAuthResponse{},
  726. wantNewCookie: true,
  727. wantSession: &browserSession{
  728. ID: "GENERATED_ID", // gets swapped for newly created ID by test
  729. SrcNode: remoteNode.Node.ID,
  730. SrcUser: user.ID,
  731. Created: timeNow,
  732. Authenticated: true,
  733. },
  734. },
  735. }
  736. for _, tt := range tests {
  737. t.Run(tt.name, func(t *testing.T) {
  738. if tt.controlURL != "" {
  739. testControlURL = &tt.controlURL
  740. } else {
  741. testControlURL = &defaultControlURL
  742. }
  743. r := httptest.NewRequest("GET", "http://100.1.2.3:5252"+tt.path, nil)
  744. r.RemoteAddr = remoteIP
  745. r.AddCookie(&http.Cookie{Name: sessionCookieName, Value: tt.cookie})
  746. w := httptest.NewRecorder()
  747. s.serve(w, r)
  748. res := w.Result()
  749. defer res.Body.Close()
  750. // Validate response status/data.
  751. if gotStatus := res.StatusCode; tt.wantStatus != gotStatus {
  752. t.Errorf("wrong status; want=%v, got=%v", tt.wantStatus, gotStatus)
  753. }
  754. var gotResp string
  755. if res.StatusCode == http.StatusOK {
  756. body, err := io.ReadAll(res.Body)
  757. if err != nil {
  758. t.Fatal(err)
  759. }
  760. gotResp = strings.Trim(string(body), "\n")
  761. }
  762. var wantResp string
  763. if tt.wantResp != nil {
  764. b, _ := json.Marshal(tt.wantResp)
  765. wantResp = string(b)
  766. }
  767. if diff := cmp.Diff(gotResp, string(wantResp)); diff != "" {
  768. t.Errorf("wrong response; (-got+want):%v", diff)
  769. }
  770. // Validate cookie creation.
  771. sessionID := tt.cookie
  772. var gotCookie bool
  773. for _, c := range w.Result().Cookies() {
  774. if c.Name == sessionCookieName {
  775. gotCookie = true
  776. sessionID = c.Value
  777. break
  778. }
  779. }
  780. if gotCookie != tt.wantNewCookie {
  781. t.Errorf("wantNewCookie wrong; want=%v, got=%v", tt.wantNewCookie, gotCookie)
  782. }
  783. // Validate browser session contents.
  784. var gotSesson *browserSession
  785. if s, ok := s.browserSessions.Load(sessionID); ok {
  786. gotSesson = s.(*browserSession)
  787. }
  788. if tt.wantSession != nil && tt.wantSession.ID == "GENERATED_ID" {
  789. // If requested, swap in the generated session ID before
  790. // comparing got/want.
  791. tt.wantSession.ID = sessionID
  792. }
  793. if diff := cmp.Diff(gotSesson, tt.wantSession); diff != "" {
  794. t.Errorf("wrong session; (-got+want):%v", diff)
  795. }
  796. })
  797. }
  798. }
  799. // TestServeAPIAuthMetricLogging specifically tests metric logging in the serveAPIAuth function.
  800. // For each given test case, we assert that the local API received a request to log the expected metric.
  801. func TestServeAPIAuthMetricLogging(t *testing.T) {
  802. user := &tailcfg.UserProfile{LoginName: "[email protected]", ID: tailcfg.UserID(1)}
  803. otherUser := &tailcfg.UserProfile{LoginName: "[email protected]", ID: tailcfg.UserID(2)}
  804. self := &ipnstate.PeerStatus{
  805. ID: "self",
  806. UserID: user.ID,
  807. TailscaleIPs: []netip.Addr{netip.MustParseAddr("100.1.2.3")},
  808. }
  809. remoteIP := "100.100.100.101"
  810. remoteNode := &apitype.WhoIsResponse{
  811. Node: &tailcfg.Node{
  812. Name: "remote-managed",
  813. ID: 1,
  814. Addresses: []netip.Prefix{netip.MustParsePrefix(remoteIP + "/32")},
  815. },
  816. UserProfile: user,
  817. }
  818. remoteTaggedIP := "100.123.100.213"
  819. remoteTaggedNode := &apitype.WhoIsResponse{
  820. Node: &tailcfg.Node{
  821. Name: "remote-tagged",
  822. ID: 2,
  823. Addresses: []netip.Prefix{netip.MustParsePrefix(remoteTaggedIP + "/32")},
  824. Tags: []string{"dev-machine"},
  825. },
  826. UserProfile: user,
  827. }
  828. localIP := "100.1.2.3"
  829. localNode := &apitype.WhoIsResponse{
  830. Node: &tailcfg.Node{
  831. Name: "local-managed",
  832. ID: 3,
  833. StableID: "self",
  834. Addresses: []netip.Prefix{netip.MustParsePrefix(localIP + "/32")},
  835. },
  836. UserProfile: user,
  837. }
  838. localTaggedIP := "100.1.2.133"
  839. localTaggedNode := &apitype.WhoIsResponse{
  840. Node: &tailcfg.Node{
  841. Name: "local-tagged",
  842. ID: 4,
  843. StableID: "self",
  844. Addresses: []netip.Prefix{netip.MustParsePrefix(localTaggedIP + "/32")},
  845. Tags: []string{"prod-machine"},
  846. },
  847. UserProfile: user,
  848. }
  849. otherIP := "100.100.2.3"
  850. otherNode := &apitype.WhoIsResponse{
  851. Node: &tailcfg.Node{
  852. Name: "other-node",
  853. ID: 5,
  854. Addresses: []netip.Prefix{netip.MustParsePrefix(otherIP + "/32")},
  855. },
  856. UserProfile: otherUser,
  857. }
  858. nonTailscaleIP := "10.100.2.3"
  859. testControlURL := &defaultControlURL
  860. var loggedMetrics []string
  861. lal := memnet.Listen("local-tailscaled.sock:80")
  862. defer lal.Close()
  863. localapi := mockLocalAPI(t,
  864. map[string]*apitype.WhoIsResponse{remoteIP: remoteNode, localIP: localNode, otherIP: otherNode, localTaggedIP: localTaggedNode, remoteTaggedIP: remoteTaggedNode},
  865. func() *ipnstate.PeerStatus { return self },
  866. func() *ipn.Prefs {
  867. return &ipn.Prefs{ControlURL: *testControlURL}
  868. },
  869. func(metricName string) {
  870. loggedMetrics = append(loggedMetrics, metricName)
  871. },
  872. )
  873. defer localapi.Close()
  874. go localapi.Serve(lal)
  875. timeNow := time.Now()
  876. oneHourAgo := timeNow.Add(-time.Hour)
  877. s := &Server{
  878. mode: ManageServerMode,
  879. lc: &local.Client{Dial: lal.Dial},
  880. timeNow: func() time.Time { return timeNow },
  881. newAuthURL: mockNewAuthURL,
  882. waitAuthURL: mockWaitAuthURL,
  883. }
  884. authenticatedRemoteNodeCookie := "ts-cookie-remote-node-authenticated"
  885. s.browserSessions.Store(authenticatedRemoteNodeCookie, &browserSession{
  886. ID: authenticatedRemoteNodeCookie,
  887. SrcNode: remoteNode.Node.ID,
  888. SrcUser: user.ID,
  889. Created: oneHourAgo,
  890. AuthID: testAuthPathSuccess,
  891. AuthURL: *testControlURL + testAuthPathSuccess,
  892. Authenticated: true,
  893. })
  894. authenticatedLocalNodeCookie := "ts-cookie-local-node-authenticated"
  895. s.browserSessions.Store(authenticatedLocalNodeCookie, &browserSession{
  896. ID: authenticatedLocalNodeCookie,
  897. SrcNode: localNode.Node.ID,
  898. SrcUser: user.ID,
  899. Created: oneHourAgo,
  900. AuthID: testAuthPathSuccess,
  901. AuthURL: *testControlURL + testAuthPathSuccess,
  902. Authenticated: true,
  903. })
  904. unauthenticatedRemoteNodeCookie := "ts-cookie-remote-node-unauthenticated"
  905. s.browserSessions.Store(unauthenticatedRemoteNodeCookie, &browserSession{
  906. ID: unauthenticatedRemoteNodeCookie,
  907. SrcNode: remoteNode.Node.ID,
  908. SrcUser: user.ID,
  909. Created: oneHourAgo,
  910. AuthID: testAuthPathSuccess,
  911. AuthURL: *testControlURL + testAuthPathSuccess,
  912. Authenticated: false,
  913. })
  914. unauthenticatedLocalNodeCookie := "ts-cookie-local-node-unauthenticated"
  915. s.browserSessions.Store(unauthenticatedLocalNodeCookie, &browserSession{
  916. ID: unauthenticatedLocalNodeCookie,
  917. SrcNode: localNode.Node.ID,
  918. SrcUser: user.ID,
  919. Created: oneHourAgo,
  920. AuthID: testAuthPathSuccess,
  921. AuthURL: *testControlURL + testAuthPathSuccess,
  922. Authenticated: false,
  923. })
  924. tests := []struct {
  925. name string
  926. cookie string // cookie attached to request
  927. remoteAddr string // remote address to hit
  928. wantLoggedMetric string // expected metric to be logged
  929. }{
  930. {
  931. name: "managing-remote",
  932. cookie: authenticatedRemoteNodeCookie,
  933. remoteAddr: remoteIP,
  934. wantLoggedMetric: "web_client_managing_remote",
  935. },
  936. {
  937. name: "managing-local",
  938. cookie: authenticatedLocalNodeCookie,
  939. remoteAddr: localIP,
  940. wantLoggedMetric: "web_client_managing_local",
  941. },
  942. {
  943. name: "viewing-not-owner",
  944. cookie: authenticatedRemoteNodeCookie,
  945. remoteAddr: otherIP,
  946. wantLoggedMetric: "web_client_viewing_not_owner",
  947. },
  948. {
  949. name: "viewing-local-tagged",
  950. cookie: authenticatedLocalNodeCookie,
  951. remoteAddr: localTaggedIP,
  952. wantLoggedMetric: "web_client_viewing_local_tag",
  953. },
  954. {
  955. name: "viewing-remote-tagged",
  956. cookie: authenticatedRemoteNodeCookie,
  957. remoteAddr: remoteTaggedIP,
  958. wantLoggedMetric: "web_client_viewing_remote_tag",
  959. },
  960. {
  961. name: "viewing-local-non-tailscale",
  962. cookie: authenticatedLocalNodeCookie,
  963. remoteAddr: nonTailscaleIP,
  964. wantLoggedMetric: "web_client_viewing_local",
  965. },
  966. {
  967. name: "viewing-local-unauthenticated",
  968. cookie: unauthenticatedLocalNodeCookie,
  969. remoteAddr: localIP,
  970. wantLoggedMetric: "web_client_viewing_local",
  971. },
  972. {
  973. name: "viewing-remote-unauthenticated",
  974. cookie: unauthenticatedRemoteNodeCookie,
  975. remoteAddr: remoteIP,
  976. wantLoggedMetric: "web_client_viewing_remote",
  977. },
  978. }
  979. for _, tt := range tests {
  980. t.Run(tt.name, func(t *testing.T) {
  981. testControlURL = &defaultControlURL
  982. r := httptest.NewRequest("GET", "http://100.1.2.3:5252/api/auth", nil)
  983. r.RemoteAddr = tt.remoteAddr
  984. r.AddCookie(&http.Cookie{Name: sessionCookieName, Value: tt.cookie})
  985. w := httptest.NewRecorder()
  986. s.serveAPIAuth(w, r)
  987. if !slices.Contains(loggedMetrics, tt.wantLoggedMetric) {
  988. t.Errorf("expected logged metrics to contain: '%s' but was: '%v'", tt.wantLoggedMetric, loggedMetrics)
  989. }
  990. loggedMetrics = []string{}
  991. res := w.Result()
  992. defer res.Body.Close()
  993. })
  994. }
  995. }
  996. // TestPathPrefix tests that the provided path prefix is normalized correctly.
  997. // If a leading '/' is missing, one should be added.
  998. // If multiple leading '/' are present, they should be collapsed to one.
  999. // Additionally verify that this prevents open redirects when enforcing the path prefix.
  1000. func TestPathPrefix(t *testing.T) {
  1001. tests := []struct {
  1002. name string
  1003. prefix string
  1004. wantPrefix string
  1005. wantLocation string
  1006. }{
  1007. {
  1008. name: "no-leading-slash",
  1009. prefix: "javascript:alert(1)",
  1010. wantPrefix: "/javascript:alert(1)",
  1011. wantLocation: "/javascript:alert(1)/",
  1012. },
  1013. {
  1014. name: "2-slashes",
  1015. prefix: "//evil.example.com/goat",
  1016. // We must also get the trailing slash added:
  1017. wantPrefix: "/evil.example.com/goat",
  1018. wantLocation: "/evil.example.com/goat/",
  1019. },
  1020. {
  1021. name: "absolute-url",
  1022. prefix: "http://evil.example.com",
  1023. // We must also get the trailing slash added:
  1024. wantPrefix: "/http:/evil.example.com",
  1025. wantLocation: "/http:/evil.example.com/",
  1026. },
  1027. {
  1028. name: "double-dot",
  1029. prefix: "/../.././etc/passwd",
  1030. // We must also get the trailing slash added:
  1031. wantPrefix: "/etc/passwd",
  1032. wantLocation: "/etc/passwd/",
  1033. },
  1034. }
  1035. for _, tt := range tests {
  1036. t.Run(tt.name, func(t *testing.T) {
  1037. options := ServerOpts{
  1038. Mode: LoginServerMode,
  1039. PathPrefix: tt.prefix,
  1040. CGIMode: true,
  1041. }
  1042. s, err := NewServer(options)
  1043. if err != nil {
  1044. t.Error(err)
  1045. }
  1046. // verify provided prefix was normalized correctly
  1047. if s.pathPrefix != tt.wantPrefix {
  1048. t.Errorf("prefix was not normalized correctly; want=%q, got=%q", tt.wantPrefix, s.pathPrefix)
  1049. }
  1050. s.logf = t.Logf
  1051. r := httptest.NewRequest(httpm.GET, "http://localhost/", nil)
  1052. w := httptest.NewRecorder()
  1053. s.ServeHTTP(w, r)
  1054. res := w.Result()
  1055. defer res.Body.Close()
  1056. location := w.Header().Get("Location")
  1057. if location != tt.wantLocation {
  1058. t.Errorf("request got wrong location; want=%q, got=%q", tt.wantLocation, location)
  1059. }
  1060. })
  1061. }
  1062. }
  1063. func TestRequireTailscaleIP(t *testing.T) {
  1064. self := &ipnstate.PeerStatus{
  1065. TailscaleIPs: []netip.Addr{
  1066. netip.MustParseAddr("100.1.2.3"),
  1067. netip.MustParseAddr("fd7a:115c::1234"),
  1068. },
  1069. }
  1070. lal := memnet.Listen("local-tailscaled.sock:80")
  1071. defer lal.Close()
  1072. localapi := mockLocalAPI(t, nil, func() *ipnstate.PeerStatus { return self }, nil, nil)
  1073. defer localapi.Close()
  1074. go localapi.Serve(lal)
  1075. s := &Server{
  1076. mode: ManageServerMode,
  1077. lc: &local.Client{Dial: lal.Dial},
  1078. timeNow: time.Now,
  1079. logf: t.Logf,
  1080. }
  1081. tests := []struct {
  1082. name string
  1083. target string
  1084. wantHandled bool
  1085. wantLocation string
  1086. }{
  1087. {
  1088. name: "localhost",
  1089. target: "http://localhost/",
  1090. wantHandled: true,
  1091. wantLocation: "http://100.1.2.3:5252/",
  1092. },
  1093. {
  1094. name: "ipv4-no-port",
  1095. target: "http://100.1.2.3/",
  1096. wantHandled: true,
  1097. wantLocation: "http://100.1.2.3:5252/",
  1098. },
  1099. {
  1100. name: "ipv4-correct-port",
  1101. target: "http://100.1.2.3:5252/",
  1102. wantHandled: false,
  1103. },
  1104. {
  1105. name: "ipv6-no-port",
  1106. target: "http://[fd7a:115c::1234]/",
  1107. wantHandled: true,
  1108. wantLocation: "http://100.1.2.3:5252/",
  1109. },
  1110. {
  1111. name: "ipv6-correct-port",
  1112. target: "http://[fd7a:115c::1234]:5252/",
  1113. wantHandled: false,
  1114. },
  1115. {
  1116. name: "quad-100",
  1117. target: "http://100.100.100.100/",
  1118. wantHandled: false,
  1119. },
  1120. {
  1121. name: "ipv6-service-addr",
  1122. target: "http://[fd7a:115c:a1e0::53]/",
  1123. wantHandled: false,
  1124. },
  1125. {
  1126. name: "quad-100:80",
  1127. target: "http://100.100.100.100:80/",
  1128. wantHandled: false,
  1129. },
  1130. {
  1131. name: "ipv6-service-addr:80",
  1132. target: "http://[fd7a:115c:a1e0::53]:80/",
  1133. wantHandled: false,
  1134. },
  1135. }
  1136. for _, tt := range tests {
  1137. t.Run(tt.name, func(t *testing.T) {
  1138. s.logf = t.Logf
  1139. r := httptest.NewRequest(httpm.GET, tt.target, nil)
  1140. w := httptest.NewRecorder()
  1141. handled := s.requireTailscaleIP(w, r)
  1142. if handled != tt.wantHandled {
  1143. t.Errorf("request(%q) was handled; want=%v, got=%v", tt.target, tt.wantHandled, handled)
  1144. }
  1145. location := w.Header().Get("Location")
  1146. if location != tt.wantLocation {
  1147. t.Errorf("request(%q) wrong location; want=%q, got=%q", tt.target, tt.wantLocation, location)
  1148. }
  1149. })
  1150. }
  1151. }
  1152. func TestPeerCapabilities(t *testing.T) {
  1153. userOwnedStatus := &ipnstate.Status{Self: &ipnstate.PeerStatus{UserID: tailcfg.UserID(1)}}
  1154. tags := views.SliceOf[string]([]string{"tag:server"})
  1155. tagOwnedStatus := &ipnstate.Status{Self: &ipnstate.PeerStatus{Tags: &tags}}
  1156. // Testing web.toPeerCapabilities
  1157. toPeerCapsTests := []struct {
  1158. name string
  1159. status *ipnstate.Status
  1160. whois *apitype.WhoIsResponse
  1161. wantCaps peerCapabilities
  1162. }{
  1163. {
  1164. name: "empty-whois",
  1165. status: userOwnedStatus,
  1166. whois: nil,
  1167. wantCaps: peerCapabilities{},
  1168. },
  1169. {
  1170. name: "user-owned-node-non-owner-caps-ignored",
  1171. status: userOwnedStatus,
  1172. whois: &apitype.WhoIsResponse{
  1173. UserProfile: &tailcfg.UserProfile{ID: tailcfg.UserID(2)},
  1174. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1175. CapMap: tailcfg.PeerCapMap{
  1176. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1177. "{\"canEdit\":[\"ssh\",\"subnets\"]}",
  1178. },
  1179. },
  1180. },
  1181. wantCaps: peerCapabilities{},
  1182. },
  1183. {
  1184. name: "user-owned-node-owner-caps-ignored",
  1185. status: userOwnedStatus,
  1186. whois: &apitype.WhoIsResponse{
  1187. UserProfile: &tailcfg.UserProfile{ID: tailcfg.UserID(1)},
  1188. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1189. CapMap: tailcfg.PeerCapMap{
  1190. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1191. "{\"canEdit\":[\"ssh\",\"subnets\"]}",
  1192. },
  1193. },
  1194. },
  1195. wantCaps: peerCapabilities{capFeatureAll: true}, // should just have wildcard
  1196. },
  1197. {
  1198. name: "tag-owned-no-webui-caps",
  1199. status: tagOwnedStatus,
  1200. whois: &apitype.WhoIsResponse{
  1201. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1202. CapMap: tailcfg.PeerCapMap{
  1203. tailcfg.PeerCapabilityDebugPeer: []tailcfg.RawMessage{},
  1204. },
  1205. },
  1206. wantCaps: peerCapabilities{},
  1207. },
  1208. {
  1209. name: "tag-owned-one-webui-cap",
  1210. status: tagOwnedStatus,
  1211. whois: &apitype.WhoIsResponse{
  1212. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1213. CapMap: tailcfg.PeerCapMap{
  1214. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1215. "{\"canEdit\":[\"ssh\",\"subnets\"]}",
  1216. },
  1217. },
  1218. },
  1219. wantCaps: peerCapabilities{
  1220. capFeatureSSH: true,
  1221. capFeatureSubnets: true,
  1222. },
  1223. },
  1224. {
  1225. name: "tag-owned-multiple-webui-cap",
  1226. status: tagOwnedStatus,
  1227. whois: &apitype.WhoIsResponse{
  1228. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1229. CapMap: tailcfg.PeerCapMap{
  1230. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1231. "{\"canEdit\":[\"ssh\",\"subnets\"]}",
  1232. "{\"canEdit\":[\"subnets\",\"exitnodes\",\"*\"]}",
  1233. },
  1234. },
  1235. },
  1236. wantCaps: peerCapabilities{
  1237. capFeatureSSH: true,
  1238. capFeatureSubnets: true,
  1239. capFeatureExitNodes: true,
  1240. capFeatureAll: true,
  1241. },
  1242. },
  1243. {
  1244. name: "tag-owned-case-insensitive-caps",
  1245. status: tagOwnedStatus,
  1246. whois: &apitype.WhoIsResponse{
  1247. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1248. CapMap: tailcfg.PeerCapMap{
  1249. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1250. "{\"canEdit\":[\"SSH\",\"sUBnets\"]}",
  1251. },
  1252. },
  1253. },
  1254. wantCaps: peerCapabilities{
  1255. capFeatureSSH: true,
  1256. capFeatureSubnets: true,
  1257. },
  1258. },
  1259. {
  1260. name: "tag-owned-random-canEdit-contents-get-dropped",
  1261. status: tagOwnedStatus,
  1262. whois: &apitype.WhoIsResponse{
  1263. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1264. CapMap: tailcfg.PeerCapMap{
  1265. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1266. "{\"canEdit\":[\"unknown-feature\"]}",
  1267. },
  1268. },
  1269. },
  1270. wantCaps: peerCapabilities{},
  1271. },
  1272. {
  1273. name: "tag-owned-no-canEdit-section",
  1274. status: tagOwnedStatus,
  1275. whois: &apitype.WhoIsResponse{
  1276. Node: &tailcfg.Node{ID: tailcfg.NodeID(1)},
  1277. CapMap: tailcfg.PeerCapMap{
  1278. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1279. "{\"canDoSomething\":[\"*\"]}",
  1280. },
  1281. },
  1282. },
  1283. wantCaps: peerCapabilities{},
  1284. },
  1285. {
  1286. name: "tagged-source-caps-ignored",
  1287. status: tagOwnedStatus,
  1288. whois: &apitype.WhoIsResponse{
  1289. Node: &tailcfg.Node{ID: tailcfg.NodeID(1), Tags: tags.AsSlice()},
  1290. CapMap: tailcfg.PeerCapMap{
  1291. tailcfg.PeerCapabilityWebUI: []tailcfg.RawMessage{
  1292. "{\"canEdit\":[\"ssh\",\"subnets\"]}",
  1293. },
  1294. },
  1295. },
  1296. wantCaps: peerCapabilities{},
  1297. },
  1298. }
  1299. for _, tt := range toPeerCapsTests {
  1300. t.Run("toPeerCapabilities-"+tt.name, func(t *testing.T) {
  1301. got, err := toPeerCapabilities(tt.status, tt.whois)
  1302. if err != nil {
  1303. t.Fatalf("unexpected: %v", err)
  1304. }
  1305. if diff := cmp.Diff(got, tt.wantCaps); diff != "" {
  1306. t.Errorf("wrong caps; (-got+want):%v", diff)
  1307. }
  1308. })
  1309. }
  1310. // Testing web.peerCapabilities.canEdit
  1311. canEditTests := []struct {
  1312. name string
  1313. caps peerCapabilities
  1314. wantCanEdit map[capFeature]bool
  1315. }{
  1316. {
  1317. name: "empty-caps",
  1318. caps: nil,
  1319. wantCanEdit: map[capFeature]bool{
  1320. capFeatureAll: false,
  1321. capFeatureSSH: false,
  1322. capFeatureSubnets: false,
  1323. capFeatureExitNodes: false,
  1324. capFeatureAccount: false,
  1325. },
  1326. },
  1327. {
  1328. name: "some-caps",
  1329. caps: peerCapabilities{capFeatureSSH: true, capFeatureAccount: true},
  1330. wantCanEdit: map[capFeature]bool{
  1331. capFeatureAll: false,
  1332. capFeatureSSH: true,
  1333. capFeatureSubnets: false,
  1334. capFeatureExitNodes: false,
  1335. capFeatureAccount: true,
  1336. },
  1337. },
  1338. {
  1339. name: "wildcard-in-caps",
  1340. caps: peerCapabilities{capFeatureAll: true, capFeatureAccount: true},
  1341. wantCanEdit: map[capFeature]bool{
  1342. capFeatureAll: true,
  1343. capFeatureSSH: true,
  1344. capFeatureSubnets: true,
  1345. capFeatureExitNodes: true,
  1346. capFeatureAccount: true,
  1347. },
  1348. },
  1349. }
  1350. for _, tt := range canEditTests {
  1351. t.Run("canEdit-"+tt.name, func(t *testing.T) {
  1352. for f, want := range tt.wantCanEdit {
  1353. if got := tt.caps.canEdit(f); got != want {
  1354. t.Errorf("wrong canEdit(%s); got=%v, want=%v", f, got, want)
  1355. }
  1356. }
  1357. })
  1358. }
  1359. }
  1360. var (
  1361. defaultControlURL = "https://controlplane.tailscale.com"
  1362. testAuthPath = "/a/12345"
  1363. testAuthPathSuccess = "/a/will-succeed"
  1364. testAuthPathError = "/a/will-error"
  1365. )
  1366. // mockLocalAPI constructs a test localapi handler that can be used
  1367. // to simulate localapi responses without a functioning tailnet.
  1368. //
  1369. // self accepts a function that resolves to a self node status,
  1370. // so that tests may swap out the /localapi/v0/status response
  1371. // as desired.
  1372. func mockLocalAPI(t *testing.T, whoIs map[string]*apitype.WhoIsResponse, self func() *ipnstate.PeerStatus, prefs func() *ipn.Prefs, metricCapture func(string)) *http.Server {
  1373. return &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1374. switch r.URL.Path {
  1375. case "/localapi/v0/whois":
  1376. addr := r.URL.Query().Get("addr")
  1377. if addr == "" {
  1378. t.Fatalf("/whois call missing \"addr\" query")
  1379. }
  1380. if node := whoIs[addr]; node != nil {
  1381. writeJSON(w, &node)
  1382. return
  1383. }
  1384. http.Error(w, "not a node", http.StatusUnauthorized)
  1385. return
  1386. case "/localapi/v0/status":
  1387. writeJSON(w, ipnstate.Status{Self: self()})
  1388. return
  1389. case "/localapi/v0/prefs":
  1390. writeJSON(w, prefs())
  1391. return
  1392. case "/localapi/v0/upload-client-metrics":
  1393. type metricName struct {
  1394. Name string `json:"name"`
  1395. }
  1396. var metricNames []metricName
  1397. if err := json.NewDecoder(r.Body).Decode(&metricNames); err != nil {
  1398. http.Error(w, "invalid JSON body", http.StatusBadRequest)
  1399. return
  1400. }
  1401. metricCapture(metricNames[0].Name)
  1402. writeJSON(w, struct{}{})
  1403. return
  1404. case "/localapi/v0/logout":
  1405. fmt.Fprintf(w, "success")
  1406. return
  1407. default:
  1408. t.Fatalf("unhandled localapi test endpoint %q, add to localapi handler func in test", r.URL.Path)
  1409. }
  1410. })}
  1411. }
  1412. func mockNewAuthURL(_ context.Context, src tailcfg.NodeID) (*tailcfg.WebClientAuthResponse, error) {
  1413. // Create new dummy auth URL.
  1414. return &tailcfg.WebClientAuthResponse{ID: testAuthPath, URL: defaultControlURL + testAuthPath}, nil
  1415. }
  1416. func mockWaitAuthURL(_ context.Context, id string, src tailcfg.NodeID) (*tailcfg.WebClientAuthResponse, error) {
  1417. switch id {
  1418. case testAuthPathSuccess: // successful auth URL
  1419. return &tailcfg.WebClientAuthResponse{Complete: true}, nil
  1420. case testAuthPathError: // error auth URL
  1421. return nil, errors.New("authenticated as wrong user")
  1422. default:
  1423. return nil, errors.New("unknown id")
  1424. }
  1425. }
  1426. func TestCSRFProtect(t *testing.T) {
  1427. tests := []struct {
  1428. name string
  1429. method string
  1430. secFetchSite string
  1431. host string
  1432. origin string
  1433. originOverride string
  1434. wantError bool
  1435. }{
  1436. {
  1437. name: "GET requests with no header are allowed",
  1438. method: "GET",
  1439. },
  1440. {
  1441. name: "POST requests with same-origin are allowed",
  1442. method: "POST",
  1443. secFetchSite: "same-origin",
  1444. },
  1445. {
  1446. name: "POST requests with cross-site are not allowed",
  1447. method: "POST",
  1448. secFetchSite: "cross-site",
  1449. wantError: true,
  1450. },
  1451. {
  1452. name: "POST requests with unknown sec-fetch-site values are not allowed",
  1453. method: "POST",
  1454. secFetchSite: "new-unknown-value",
  1455. wantError: true,
  1456. },
  1457. {
  1458. name: "POST requests with none are not allowed",
  1459. method: "POST",
  1460. secFetchSite: "none",
  1461. wantError: true,
  1462. },
  1463. {
  1464. name: "POST requests with no sec-fetch-site header but matching host and origin are allowed",
  1465. method: "POST",
  1466. host: "example.com",
  1467. origin: "https://example.com",
  1468. },
  1469. {
  1470. name: "POST requests with no sec-fetch-site and non-matching host and origin are not allowed",
  1471. method: "POST",
  1472. host: "example.com",
  1473. origin: "https://example.net",
  1474. wantError: true,
  1475. },
  1476. {
  1477. name: "POST requests with no sec-fetch-site and and origin that matches the override are allowed",
  1478. method: "POST",
  1479. originOverride: "example.net",
  1480. host: "internal.example.foo", // Host can be changed by reverse proxies
  1481. origin: "http://example.net",
  1482. },
  1483. }
  1484. for _, tt := range tests {
  1485. t.Run(tt.name, func(t *testing.T) {
  1486. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1487. fmt.Fprintf(w, "OK")
  1488. })
  1489. s := &Server{
  1490. originOverride: tt.originOverride,
  1491. }
  1492. withCSRF := s.csrfProtect(handler)
  1493. r := httptest.NewRequest(tt.method, "http://example.com/", nil)
  1494. if tt.secFetchSite != "" {
  1495. r.Header.Set("Sec-Fetch-Site", tt.secFetchSite)
  1496. }
  1497. if tt.host != "" {
  1498. r.Host = tt.host
  1499. }
  1500. if tt.origin != "" {
  1501. r.Header.Set("Origin", tt.origin)
  1502. }
  1503. w := httptest.NewRecorder()
  1504. withCSRF.ServeHTTP(w, r)
  1505. res := w.Result()
  1506. defer res.Body.Close()
  1507. if tt.wantError {
  1508. if res.StatusCode != http.StatusForbidden {
  1509. t.Errorf("expected status forbidden, got %v", res.StatusCode)
  1510. }
  1511. return
  1512. }
  1513. if res.StatusCode != http.StatusOK {
  1514. t.Errorf("expected status ok, got %v", res.StatusCode)
  1515. }
  1516. })
  1517. }
  1518. }