api_test.go 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package api
  7. import (
  8. "bytes"
  9. "compress/gzip"
  10. "context"
  11. "encoding/json"
  12. "fmt"
  13. "io"
  14. "net"
  15. "net/http"
  16. "net/http/httptest"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "strconv"
  21. "strings"
  22. "testing"
  23. "time"
  24. "github.com/d4l3k/messagediff"
  25. "github.com/syncthing/syncthing/lib/assets"
  26. "github.com/syncthing/syncthing/lib/build"
  27. "github.com/syncthing/syncthing/lib/config"
  28. connmocks "github.com/syncthing/syncthing/lib/connections/mocks"
  29. discovermocks "github.com/syncthing/syncthing/lib/discover/mocks"
  30. "github.com/syncthing/syncthing/lib/events"
  31. eventmocks "github.com/syncthing/syncthing/lib/events/mocks"
  32. "github.com/syncthing/syncthing/lib/fs"
  33. "github.com/syncthing/syncthing/lib/locations"
  34. "github.com/syncthing/syncthing/lib/logger"
  35. loggermocks "github.com/syncthing/syncthing/lib/logger/mocks"
  36. "github.com/syncthing/syncthing/lib/model"
  37. modelmocks "github.com/syncthing/syncthing/lib/model/mocks"
  38. "github.com/syncthing/syncthing/lib/protocol"
  39. "github.com/syncthing/syncthing/lib/rand"
  40. "github.com/syncthing/syncthing/lib/svcutil"
  41. "github.com/syncthing/syncthing/lib/sync"
  42. "github.com/syncthing/syncthing/lib/tlsutil"
  43. "github.com/syncthing/syncthing/lib/ur"
  44. "github.com/thejerf/suture/v4"
  45. "golang.org/x/exp/slices"
  46. )
  47. var (
  48. confDir = filepath.Join("testdata", "config")
  49. token = filepath.Join(confDir, "csrftokens.txt")
  50. dev1 protocol.DeviceID
  51. apiCfg = newMockedConfig()
  52. testAPIKey = "foobarbaz"
  53. )
  54. func init() {
  55. dev1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  56. apiCfg.GUIReturns(config.GUIConfiguration{APIKey: testAPIKey, RawAddress: "127.0.0.1:0"})
  57. }
  58. func TestMain(m *testing.M) {
  59. orig := locations.GetBaseDir(locations.ConfigBaseDir)
  60. locations.SetBaseDir(locations.ConfigBaseDir, confDir)
  61. exitCode := m.Run()
  62. locations.SetBaseDir(locations.ConfigBaseDir, orig)
  63. os.Exit(exitCode)
  64. }
  65. func TestCSRFToken(t *testing.T) {
  66. t.Parallel()
  67. max := 10 * maxCsrfTokens
  68. int := 5
  69. if testing.Short() {
  70. max = 1 + maxCsrfTokens
  71. int = 2
  72. }
  73. m := newCsrfManager("unique", "prefix", config.GUIConfiguration{}, nil, "")
  74. t1 := m.newToken()
  75. t2 := m.newToken()
  76. t3 := m.newToken()
  77. if !m.validToken(t3) {
  78. t.Fatal("t3 should be valid")
  79. }
  80. valid := make(map[string]struct{}, maxCsrfTokens)
  81. for _, token := range m.tokens {
  82. valid[token] = struct{}{}
  83. }
  84. for i := 0; i < max; i++ {
  85. if i%int == 0 {
  86. // t1 and t2 should remain valid by virtue of us checking them now
  87. // and then.
  88. if !m.validToken(t1) {
  89. t.Fatal("t1 should be valid at iteration", i)
  90. }
  91. if !m.validToken(t2) {
  92. t.Fatal("t2 should be valid at iteration", i)
  93. }
  94. }
  95. if len(m.tokens) == maxCsrfTokens {
  96. // We're about to add a token, which will remove the last token
  97. // from m.tokens.
  98. delete(valid, m.tokens[len(m.tokens)-1])
  99. }
  100. // The newly generated token is always valid
  101. t4 := m.newToken()
  102. if !m.validToken(t4) {
  103. t.Fatal("t4 should be valid at iteration", i)
  104. }
  105. valid[t4] = struct{}{}
  106. v := make(map[string]struct{}, maxCsrfTokens)
  107. for _, token := range m.tokens {
  108. v[token] = struct{}{}
  109. }
  110. if !reflect.DeepEqual(v, valid) {
  111. t.Fatalf("want valid tokens %v, got %v", valid, v)
  112. }
  113. }
  114. if m.validToken(t3) {
  115. t.Fatal("t3 should have expired by now")
  116. }
  117. }
  118. func TestStopAfterBrokenConfig(t *testing.T) {
  119. t.Parallel()
  120. cfg := config.Configuration{
  121. GUI: config.GUIConfiguration{
  122. RawAddress: "127.0.0.1:0",
  123. RawUseTLS: false,
  124. },
  125. }
  126. w := config.Wrap("/dev/null", cfg, protocol.LocalDeviceID, events.NoopLogger)
  127. srv := New(protocol.LocalDeviceID, w, "", "syncthing", nil, nil, nil, events.NoopLogger, nil, nil, nil, nil, nil, nil, false).(*service)
  128. defer os.Remove(token)
  129. srv.started = make(chan string)
  130. sup := suture.New("test", svcutil.SpecWithDebugLogger(l))
  131. sup.Add(srv)
  132. ctx, cancel := context.WithCancel(context.Background())
  133. sup.ServeBackground(ctx)
  134. <-srv.started
  135. // Service is now running, listening on a random port on localhost. Now we
  136. // request a config change to a completely invalid listen address. The
  137. // commit will fail and the service will be in a broken state.
  138. newCfg := config.Configuration{
  139. GUI: config.GUIConfiguration{
  140. RawAddress: "totally not a valid address",
  141. RawUseTLS: false,
  142. },
  143. }
  144. if err := srv.VerifyConfiguration(cfg, newCfg); err == nil {
  145. t.Fatal("Verify config should have failed")
  146. }
  147. cancel()
  148. }
  149. func TestAssetsDir(t *testing.T) {
  150. t.Parallel()
  151. // For any given request to $FILE, we should return the first found of
  152. // - assetsdir/$THEME/$FILE
  153. // - compiled in asset $THEME/$FILE
  154. // - assetsdir/default/$FILE
  155. // - compiled in asset default/$FILE
  156. // The asset map contains compressed assets, so create a couple of gzip compressed assets here.
  157. buf := new(bytes.Buffer)
  158. gw := gzip.NewWriter(buf)
  159. gw.Write([]byte("default"))
  160. gw.Close()
  161. def := assets.Asset{
  162. Content: buf.String(),
  163. Gzipped: true,
  164. }
  165. buf = new(bytes.Buffer)
  166. gw = gzip.NewWriter(buf)
  167. gw.Write([]byte("foo"))
  168. gw.Close()
  169. foo := assets.Asset{
  170. Content: buf.String(),
  171. Gzipped: true,
  172. }
  173. e := &staticsServer{
  174. theme: "foo",
  175. mut: sync.NewRWMutex(),
  176. assetDir: "testdata",
  177. assets: map[string]assets.Asset{
  178. "foo/a": foo, // overridden in foo/a
  179. "foo/b": foo,
  180. "default/a": def, // overridden in default/a (but foo/a takes precedence)
  181. "default/b": def, // overridden in default/b (but foo/b takes precedence)
  182. "default/c": def,
  183. },
  184. }
  185. s := httptest.NewServer(e)
  186. defer s.Close()
  187. // assetsdir/foo/a exists, overrides compiled in
  188. expectURLToContain(t, s.URL+"/a", "overridden-foo")
  189. // foo/b is compiled in, default/b is overridden, return compiled in
  190. expectURLToContain(t, s.URL+"/b", "foo")
  191. // only exists as compiled in default/c so use that
  192. expectURLToContain(t, s.URL+"/c", "default")
  193. // only exists as overridden default/d so use that
  194. expectURLToContain(t, s.URL+"/d", "overridden-default")
  195. }
  196. func expectURLToContain(t *testing.T, url, exp string) {
  197. res, err := http.Get(url)
  198. if err != nil {
  199. t.Error(err)
  200. return
  201. }
  202. if res.StatusCode != 200 {
  203. t.Errorf("Got %s instead of 200 OK", res.Status)
  204. return
  205. }
  206. data, err := io.ReadAll(res.Body)
  207. res.Body.Close()
  208. if err != nil {
  209. t.Error(err)
  210. return
  211. }
  212. if string(data) != exp {
  213. t.Errorf("Got %q instead of %q on %q", data, exp, url)
  214. return
  215. }
  216. }
  217. func TestDirNames(t *testing.T) {
  218. t.Parallel()
  219. names := dirNames("testdata")
  220. expected := []string{"config", "default", "foo", "testfolder"}
  221. if diff, equal := messagediff.PrettyDiff(expected, names); !equal {
  222. t.Errorf("Unexpected dirNames return: %#v\n%s", names, diff)
  223. }
  224. }
  225. type httpTestCase struct {
  226. URL string // URL to check
  227. Code int // Expected result code
  228. Type string // Expected content type
  229. Prefix string // Expected result prefix
  230. Timeout time.Duration // Defaults to a second
  231. }
  232. func TestAPIServiceRequests(t *testing.T) {
  233. t.Parallel()
  234. baseURL, cancel, err := startHTTP(apiCfg)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. t.Cleanup(cancel)
  239. cases := []httpTestCase{
  240. // /rest/db
  241. {
  242. URL: "/rest/db/completion?device=" + protocol.LocalDeviceID.String() + "&folder=default",
  243. Code: 200,
  244. Type: "application/json",
  245. Prefix: "{",
  246. },
  247. {
  248. URL: "/rest/db/file?folder=default&file=something",
  249. Code: 404,
  250. },
  251. {
  252. URL: "/rest/db/ignores?folder=default",
  253. Code: 200,
  254. Type: "application/json",
  255. Prefix: "{",
  256. },
  257. {
  258. URL: "/rest/db/need?folder=default",
  259. Code: 200,
  260. Type: "application/json",
  261. Prefix: "{",
  262. },
  263. {
  264. URL: "/rest/db/status?folder=default",
  265. Code: 200,
  266. Type: "application/json",
  267. Prefix: "{",
  268. },
  269. {
  270. URL: "/rest/db/browse?folder=default",
  271. Code: 200,
  272. Type: "application/json",
  273. Prefix: "null",
  274. },
  275. {
  276. URL: "/rest/db/status?folder=default",
  277. Code: 200,
  278. Type: "application/json",
  279. Prefix: "",
  280. },
  281. // /rest/stats
  282. {
  283. URL: "/rest/stats/device",
  284. Code: 200,
  285. Type: "application/json",
  286. Prefix: "null",
  287. },
  288. {
  289. URL: "/rest/stats/folder",
  290. Code: 200,
  291. Type: "application/json",
  292. Prefix: "null",
  293. },
  294. // /rest/svc
  295. {
  296. URL: "/rest/svc/deviceid?id=" + protocol.LocalDeviceID.String(),
  297. Code: 200,
  298. Type: "application/json",
  299. Prefix: "{",
  300. },
  301. {
  302. URL: "/rest/svc/lang",
  303. Code: 200,
  304. Type: "application/json",
  305. Prefix: "[",
  306. },
  307. {
  308. URL: "/rest/svc/report",
  309. Code: 200,
  310. Type: "application/json",
  311. Prefix: "{",
  312. Timeout: 5 * time.Second,
  313. },
  314. // /rest/system
  315. {
  316. URL: "/rest/system/browse?current=~",
  317. Code: 200,
  318. Type: "application/json",
  319. Prefix: "[",
  320. },
  321. {
  322. URL: "/rest/system/config",
  323. Code: 200,
  324. Type: "application/json",
  325. Prefix: "{",
  326. },
  327. {
  328. URL: "/rest/system/config/insync",
  329. Code: 200,
  330. Type: "application/json",
  331. Prefix: "{",
  332. },
  333. {
  334. URL: "/rest/system/connections",
  335. Code: 200,
  336. Type: "application/json",
  337. Prefix: "null",
  338. },
  339. {
  340. URL: "/rest/system/discovery",
  341. Code: 200,
  342. Type: "application/json",
  343. Prefix: "{",
  344. },
  345. {
  346. URL: "/rest/system/error?since=0",
  347. Code: 200,
  348. Type: "application/json",
  349. Prefix: "{",
  350. },
  351. {
  352. URL: "/rest/system/ping",
  353. Code: 200,
  354. Type: "application/json",
  355. Prefix: "{",
  356. },
  357. {
  358. URL: "/rest/system/status",
  359. Code: 200,
  360. Type: "application/json",
  361. Prefix: "{",
  362. },
  363. {
  364. URL: "/rest/system/version",
  365. Code: 200,
  366. Type: "application/json",
  367. Prefix: "{",
  368. },
  369. {
  370. URL: "/rest/system/debug",
  371. Code: 200,
  372. Type: "application/json",
  373. Prefix: "{",
  374. },
  375. {
  376. URL: "/rest/system/log?since=0",
  377. Code: 200,
  378. Type: "application/json",
  379. Prefix: "{",
  380. },
  381. {
  382. URL: "/rest/system/log.txt?since=0",
  383. Code: 200,
  384. Type: "text/plain",
  385. Prefix: "",
  386. },
  387. // /rest/config
  388. {
  389. URL: "/rest/config",
  390. Code: 200,
  391. Type: "application/json",
  392. Prefix: "",
  393. },
  394. {
  395. URL: "/rest/config/folders",
  396. Code: 200,
  397. Type: "application/json",
  398. Prefix: "",
  399. },
  400. {
  401. URL: "/rest/config/folders/missing",
  402. Code: 404,
  403. Type: "text/plain",
  404. Prefix: "",
  405. },
  406. {
  407. URL: "/rest/config/devices",
  408. Code: 200,
  409. Type: "application/json",
  410. Prefix: "",
  411. },
  412. {
  413. URL: "/rest/config/devices/illegalid",
  414. Code: 400,
  415. Type: "text/plain",
  416. Prefix: "",
  417. },
  418. {
  419. URL: "/rest/config/devices/" + protocol.GlobalDeviceID.String(),
  420. Code: 404,
  421. Type: "text/plain",
  422. Prefix: "",
  423. },
  424. {
  425. URL: "/rest/config/options",
  426. Code: 200,
  427. Type: "application/json",
  428. Prefix: "{",
  429. },
  430. {
  431. URL: "/rest/config/gui",
  432. Code: 200,
  433. Type: "application/json",
  434. Prefix: "{",
  435. },
  436. {
  437. URL: "/rest/config/ldap",
  438. Code: 200,
  439. Type: "application/json",
  440. Prefix: "{",
  441. },
  442. }
  443. for _, tc := range cases {
  444. tc := tc
  445. t.Run(cases[0].URL, func(t *testing.T) {
  446. t.Parallel()
  447. testHTTPRequest(t, baseURL, tc, testAPIKey)
  448. })
  449. }
  450. }
  451. // testHTTPRequest tries the given test case, comparing the result code,
  452. // content type, and result prefix.
  453. func testHTTPRequest(t *testing.T, baseURL string, tc httpTestCase, apikey string) {
  454. // Should not be parallelized, as that just causes timeouts eventually with more test-cases
  455. timeout := time.Second
  456. if tc.Timeout > 0 {
  457. timeout = tc.Timeout
  458. }
  459. cli := &http.Client{
  460. Timeout: timeout,
  461. }
  462. req, err := http.NewRequest("GET", baseURL+tc.URL, nil)
  463. if err != nil {
  464. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  465. return
  466. }
  467. req.Header.Set("X-API-Key", apikey)
  468. resp, err := cli.Do(req)
  469. if err != nil {
  470. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  471. return
  472. }
  473. defer resp.Body.Close()
  474. if resp.StatusCode != tc.Code {
  475. t.Errorf("Get on %s should have returned status code %d, not %s", tc.URL, tc.Code, resp.Status)
  476. return
  477. }
  478. ct := resp.Header.Get("Content-Type")
  479. if !strings.HasPrefix(ct, tc.Type) {
  480. t.Errorf("The content type on %s should be %q, not %q", tc.URL, tc.Type, ct)
  481. return
  482. }
  483. data, err := io.ReadAll(resp.Body)
  484. if err != nil {
  485. t.Errorf("Unexpected error reading %s: %v", tc.URL, err)
  486. return
  487. }
  488. if !bytes.HasPrefix(data, []byte(tc.Prefix)) {
  489. t.Errorf("Returned data from %s does not have prefix %q: %s", tc.URL, tc.Prefix, data)
  490. return
  491. }
  492. }
  493. func hasSessionCookie(cookies []*http.Cookie) bool {
  494. for _, cookie := range cookies {
  495. if cookie.MaxAge >= 0 && strings.HasPrefix(cookie.Name, "sessionid") {
  496. return true
  497. }
  498. }
  499. return false
  500. }
  501. func httpGet(url string, basicAuthUsername string, basicAuthPassword string, xapikeyHeader string, authorizationBearer string, cookies []*http.Cookie, t *testing.T) *http.Response {
  502. req, err := http.NewRequest("GET", url, nil)
  503. for _, cookie := range cookies {
  504. req.AddCookie(cookie)
  505. }
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. if basicAuthUsername != "" || basicAuthPassword != "" {
  510. req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
  511. }
  512. if xapikeyHeader != "" {
  513. req.Header.Set("X-API-Key", xapikeyHeader)
  514. }
  515. if authorizationBearer != "" {
  516. req.Header.Set("Authorization", "Bearer "+authorizationBearer)
  517. }
  518. resp, err := http.DefaultClient.Do(req)
  519. if err != nil {
  520. t.Fatal(err)
  521. }
  522. return resp
  523. }
  524. func httpPost(url string, body map[string]string, t *testing.T) *http.Response {
  525. bodyBytes, err := json.Marshal(body)
  526. if err != nil {
  527. t.Fatal(err)
  528. }
  529. req, err := http.NewRequest("POST", url, bytes.NewReader(bodyBytes))
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. resp, err := http.DefaultClient.Do(req)
  534. if err != nil {
  535. t.Fatal(err)
  536. }
  537. return resp
  538. }
  539. func TestHTTPLogin(t *testing.T) {
  540. t.Parallel()
  541. httpGetBasicAuth := func(url string, username string, password string) *http.Response {
  542. return httpGet(url, username, password, "", "", nil, t)
  543. }
  544. httpGetXapikey := func(url string, xapikeyHeader string) *http.Response {
  545. return httpGet(url, "", "", xapikeyHeader, "", nil, t)
  546. }
  547. httpGetAuthorizationBearer := func(url string, bearer string) *http.Response {
  548. return httpGet(url, "", "", "", bearer, nil, t)
  549. }
  550. testWith := func(sendBasicAuthPrompt bool, expectedOkStatus int, expectedFailStatus int, path string) {
  551. cfg := newMockedConfig()
  552. cfg.GUIReturns(config.GUIConfiguration{
  553. User: "üser",
  554. Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8
  555. RawAddress: "127.0.0.1:0",
  556. APIKey: testAPIKey,
  557. SendBasicAuthPrompt: sendBasicAuthPrompt,
  558. })
  559. baseURL, cancel, err := startHTTP(cfg)
  560. if err != nil {
  561. t.Fatal(err)
  562. }
  563. t.Cleanup(cancel)
  564. url := baseURL + path
  565. t.Run(fmt.Sprintf("%d path", expectedOkStatus), func(t *testing.T) {
  566. t.Run("no auth is rejected", func(t *testing.T) {
  567. t.Parallel()
  568. resp := httpGetBasicAuth(url, "", "")
  569. if resp.StatusCode != expectedFailStatus {
  570. t.Errorf("Unexpected non-%d return code %d for unauthed request", expectedFailStatus, resp.StatusCode)
  571. }
  572. if hasSessionCookie(resp.Cookies()) {
  573. t.Errorf("Unexpected session cookie for unauthed request")
  574. }
  575. })
  576. t.Run("incorrect password is rejected", func(t *testing.T) {
  577. t.Parallel()
  578. resp := httpGetBasicAuth(url, "üser", "rksmrgs")
  579. if resp.StatusCode != expectedFailStatus {
  580. t.Errorf("Unexpected non-%d return code %d for incorrect password", expectedFailStatus, resp.StatusCode)
  581. }
  582. if hasSessionCookie(resp.Cookies()) {
  583. t.Errorf("Unexpected session cookie for incorrect password")
  584. }
  585. })
  586. t.Run("incorrect username is rejected", func(t *testing.T) {
  587. t.Parallel()
  588. resp := httpGetBasicAuth(url, "user", "räksmörgås") // string literals in Go source code are in UTF-8
  589. if resp.StatusCode != expectedFailStatus {
  590. t.Errorf("Unexpected non-%d return code %d for incorrect username", expectedFailStatus, resp.StatusCode)
  591. }
  592. if hasSessionCookie(resp.Cookies()) {
  593. t.Errorf("Unexpected session cookie for incorrect username")
  594. }
  595. })
  596. t.Run("UTF-8 auth works", func(t *testing.T) {
  597. t.Parallel()
  598. resp := httpGetBasicAuth(url, "üser", "räksmörgås") // string literals in Go source code are in UTF-8
  599. if resp.StatusCode != expectedOkStatus {
  600. t.Errorf("Unexpected non-%d return code %d for authed request (UTF-8)", expectedOkStatus, resp.StatusCode)
  601. }
  602. if !hasSessionCookie(resp.Cookies()) {
  603. t.Errorf("Expected session cookie for authed request (UTF-8)")
  604. }
  605. })
  606. t.Run("ISO-8859-1 auth works", func(t *testing.T) {
  607. t.Parallel()
  608. resp := httpGetBasicAuth(url, "\xfcser", "r\xe4ksm\xf6rg\xe5s") // escaped ISO-8859-1
  609. if resp.StatusCode != expectedOkStatus {
  610. t.Errorf("Unexpected non-%d return code %d for authed request (ISO-8859-1)", expectedOkStatus, resp.StatusCode)
  611. }
  612. if !hasSessionCookie(resp.Cookies()) {
  613. t.Errorf("Expected session cookie for authed request (ISO-8859-1)")
  614. }
  615. })
  616. t.Run("bad X-API-Key is rejected", func(t *testing.T) {
  617. t.Parallel()
  618. resp := httpGetXapikey(url, testAPIKey+"X")
  619. if resp.StatusCode != expectedFailStatus {
  620. t.Errorf("Unexpected non-%d return code %d for bad API key", expectedFailStatus, resp.StatusCode)
  621. }
  622. if hasSessionCookie(resp.Cookies()) {
  623. t.Errorf("Unexpected session cookie for bad API key")
  624. }
  625. })
  626. t.Run("good X-API-Key is accepted", func(t *testing.T) {
  627. t.Parallel()
  628. resp := httpGetXapikey(url, testAPIKey)
  629. if resp.StatusCode != expectedOkStatus {
  630. t.Errorf("Unexpected non-%d return code %d for API key", expectedOkStatus, resp.StatusCode)
  631. }
  632. if hasSessionCookie(resp.Cookies()) {
  633. t.Errorf("Unexpected session cookie for API key")
  634. }
  635. })
  636. t.Run("bad Bearer is rejected", func(t *testing.T) {
  637. t.Parallel()
  638. resp := httpGetAuthorizationBearer(url, testAPIKey+"X")
  639. if resp.StatusCode != expectedFailStatus {
  640. t.Errorf("Unexpected non-%d return code %d for bad Authorization: Bearer", expectedFailStatus, resp.StatusCode)
  641. }
  642. if hasSessionCookie(resp.Cookies()) {
  643. t.Errorf("Unexpected session cookie for bad Authorization: Bearer")
  644. }
  645. })
  646. t.Run("good Bearer is accepted", func(t *testing.T) {
  647. t.Parallel()
  648. resp := httpGetAuthorizationBearer(url, testAPIKey)
  649. if resp.StatusCode != expectedOkStatus {
  650. t.Errorf("Unexpected non-%d return code %d for Authorization: Bearer", expectedOkStatus, resp.StatusCode)
  651. }
  652. if hasSessionCookie(resp.Cookies()) {
  653. t.Errorf("Unexpected session cookie for bad Authorization: Bearer")
  654. }
  655. })
  656. })
  657. }
  658. testWith(true, http.StatusOK, http.StatusOK, "/")
  659. testWith(true, http.StatusOK, http.StatusUnauthorized, "/meta.js")
  660. testWith(true, http.StatusNotFound, http.StatusUnauthorized, "/any-path/that/does/nooooooot/match-any/noauth-pattern")
  661. testWith(false, http.StatusOK, http.StatusOK, "/")
  662. testWith(false, http.StatusOK, http.StatusForbidden, "/meta.js")
  663. testWith(false, http.StatusNotFound, http.StatusForbidden, "/any-path/that/does/nooooooot/match-any/noauth-pattern")
  664. }
  665. func TestHtmlFormLogin(t *testing.T) {
  666. t.Parallel()
  667. cfg := newMockedConfig()
  668. cfg.GUIReturns(config.GUIConfiguration{
  669. User: "üser",
  670. Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8
  671. SendBasicAuthPrompt: false,
  672. })
  673. baseURL, cancel, err := startHTTP(cfg)
  674. if err != nil {
  675. t.Fatal(err)
  676. }
  677. t.Cleanup(cancel)
  678. loginUrl := baseURL + "/rest/noauth/auth/password"
  679. resourceUrl := baseURL + "/meta.js"
  680. resourceUrl404 := baseURL + "/any-path/that/does/nooooooot/match-any/noauth-pattern"
  681. performLogin := func(username string, password string) *http.Response {
  682. return httpPost(loginUrl, map[string]string{"username": username, "password": password}, t)
  683. }
  684. performResourceRequest := func(url string, cookies []*http.Cookie) *http.Response {
  685. return httpGet(url, "", "", "", "", cookies, t)
  686. }
  687. testNoAuthPath := func(noAuthPath string) {
  688. t.Run("auth is not needed for "+noAuthPath, func(t *testing.T) {
  689. t.Parallel()
  690. resp := httpGet(baseURL+noAuthPath, "", "", "", "", nil, t)
  691. if resp.StatusCode != http.StatusOK {
  692. t.Errorf("Unexpected non-200 return code %d at %s", resp.StatusCode, noAuthPath)
  693. }
  694. if hasSessionCookie(resp.Cookies()) {
  695. t.Errorf("Unexpected session cookie at " + noAuthPath)
  696. }
  697. })
  698. }
  699. testNoAuthPath("/index.html")
  700. testNoAuthPath("/rest/svc/lang")
  701. t.Run("incorrect password is rejected with 403", func(t *testing.T) {
  702. t.Parallel()
  703. resp := performLogin("üser", "rksmrgs") // string literals in Go source code are in UTF-8
  704. if resp.StatusCode != http.StatusForbidden {
  705. t.Errorf("Unexpected non-403 return code %d for incorrect password", resp.StatusCode)
  706. }
  707. if hasSessionCookie(resp.Cookies()) {
  708. t.Errorf("Unexpected session cookie for incorrect password")
  709. }
  710. resp = performResourceRequest(resourceUrl, resp.Cookies())
  711. if resp.StatusCode != http.StatusForbidden {
  712. t.Errorf("Unexpected non-403 return code %d for incorrect password", resp.StatusCode)
  713. }
  714. })
  715. t.Run("incorrect username is rejected with 403", func(t *testing.T) {
  716. t.Parallel()
  717. resp := performLogin("user", "räksmörgås") // string literals in Go source code are in UTF-8
  718. if resp.StatusCode != http.StatusForbidden {
  719. t.Errorf("Unexpected non-403 return code %d for incorrect username", resp.StatusCode)
  720. }
  721. if hasSessionCookie(resp.Cookies()) {
  722. t.Errorf("Unexpected session cookie for incorrect username")
  723. }
  724. resp = performResourceRequest(resourceUrl, resp.Cookies())
  725. if resp.StatusCode != http.StatusForbidden {
  726. t.Errorf("Unexpected non-403 return code %d for incorrect username", resp.StatusCode)
  727. }
  728. })
  729. t.Run("UTF-8 auth works", func(t *testing.T) {
  730. t.Parallel()
  731. // JSON is always UTF-8, so ISO-8859-1 case is not applicable
  732. resp := performLogin("üser", "räksmörgås") // string literals in Go source code are in UTF-8
  733. if resp.StatusCode != http.StatusNoContent {
  734. t.Errorf("Unexpected non-204 return code %d for authed request (UTF-8)", resp.StatusCode)
  735. }
  736. resp = performResourceRequest(resourceUrl, resp.Cookies())
  737. if resp.StatusCode != http.StatusOK {
  738. t.Errorf("Unexpected non-200 return code %d for authed request (UTF-8)", resp.StatusCode)
  739. }
  740. })
  741. t.Run("form login is not applicable to other URLs", func(t *testing.T) {
  742. t.Parallel()
  743. resp := httpPost(baseURL+"/meta.js", map[string]string{"username": "üser", "password": "räksmörgås"}, t)
  744. if resp.StatusCode != http.StatusForbidden {
  745. t.Errorf("Unexpected non-403 return code %d for incorrect form login URL", resp.StatusCode)
  746. }
  747. if hasSessionCookie(resp.Cookies()) {
  748. t.Errorf("Unexpected session cookie for incorrect form login URL")
  749. }
  750. })
  751. t.Run("invalid URL returns 403 before auth and 404 after auth", func(t *testing.T) {
  752. t.Parallel()
  753. resp := performResourceRequest(resourceUrl404, nil)
  754. if resp.StatusCode != http.StatusForbidden {
  755. t.Errorf("Unexpected non-403 return code %d for unauthed request", resp.StatusCode)
  756. }
  757. resp = performLogin("üser", "räksmörgås")
  758. if resp.StatusCode != http.StatusNoContent {
  759. t.Errorf("Unexpected non-204 return code %d for authed request", resp.StatusCode)
  760. }
  761. resp = performResourceRequest(resourceUrl404, resp.Cookies())
  762. if resp.StatusCode != http.StatusNotFound {
  763. t.Errorf("Unexpected non-404 return code %d for authed request", resp.StatusCode)
  764. }
  765. })
  766. }
  767. func TestApiCache(t *testing.T) {
  768. t.Parallel()
  769. cfg := newMockedConfig()
  770. cfg.GUIReturns(config.GUIConfiguration{
  771. RawAddress: "127.0.0.1:0",
  772. APIKey: testAPIKey,
  773. })
  774. baseURL, cancel, err := startHTTP(cfg)
  775. if err != nil {
  776. t.Fatal(err)
  777. }
  778. t.Cleanup(cancel)
  779. httpGet := func(url string, bearer string) *http.Response {
  780. return httpGet(url, "", "", "", bearer, nil, t)
  781. }
  782. t.Run("meta.js has no-cache headers", func(t *testing.T) {
  783. t.Parallel()
  784. url := baseURL + "/meta.js"
  785. resp := httpGet(url, testAPIKey)
  786. if resp.Header.Get("Cache-Control") != "max-age=0, no-cache, no-store" {
  787. t.Errorf("Expected no-cache headers at %s", url)
  788. }
  789. })
  790. t.Run("/rest/ has no-cache headers", func(t *testing.T) {
  791. t.Parallel()
  792. url := baseURL + "/rest/system/version"
  793. resp := httpGet(url, testAPIKey)
  794. if resp.Header.Get("Cache-Control") != "max-age=0, no-cache, no-store" {
  795. t.Errorf("Expected no-cache headers at %s", url)
  796. }
  797. })
  798. }
  799. func startHTTP(cfg config.Wrapper) (string, context.CancelFunc, error) {
  800. m := new(modelmocks.Model)
  801. assetDir := "../../gui"
  802. eventSub := new(eventmocks.BufferedSubscription)
  803. diskEventSub := new(eventmocks.BufferedSubscription)
  804. discoverer := new(discovermocks.Manager)
  805. connections := new(connmocks.Service)
  806. errorLog := new(loggermocks.Recorder)
  807. systemLog := new(loggermocks.Recorder)
  808. for _, l := range []*loggermocks.Recorder{errorLog, systemLog} {
  809. l.SinceReturns([]logger.Line{
  810. {
  811. When: time.Now(),
  812. Message: "Test message",
  813. },
  814. })
  815. }
  816. addrChan := make(chan string)
  817. mockedSummary := &modelmocks.FolderSummaryService{}
  818. mockedSummary.SummaryReturns(new(model.FolderSummary), nil)
  819. // Instantiate the API service
  820. urService := ur.New(cfg, m, connections, false)
  821. svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, mockedSummary, errorLog, systemLog, false).(*service)
  822. defer os.Remove(token)
  823. svc.started = addrChan
  824. // Actually start the API service
  825. supervisor := suture.New("API test", suture.Spec{
  826. PassThroughPanics: true,
  827. })
  828. supervisor.Add(svc)
  829. ctx, cancel := context.WithCancel(context.Background())
  830. supervisor.ServeBackground(ctx)
  831. // Make sure the API service is listening, and get the URL to use.
  832. addr := <-addrChan
  833. tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
  834. if err != nil {
  835. cancel()
  836. return "", cancel, fmt.Errorf("weird address from API service: %w", err)
  837. }
  838. host, _, _ := net.SplitHostPort(cfg.GUI().RawAddress)
  839. if host == "" || host == "0.0.0.0" {
  840. host = "127.0.0.1"
  841. }
  842. baseURL := fmt.Sprintf("http://%s", net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port)))
  843. return baseURL, cancel, nil
  844. }
  845. func TestCSRFRequired(t *testing.T) {
  846. t.Parallel()
  847. baseURL, cancel, err := startHTTP(apiCfg)
  848. if err != nil {
  849. t.Fatal("Unexpected error from getting base URL:", err)
  850. }
  851. t.Cleanup(cancel)
  852. cli := &http.Client{
  853. Timeout: time.Minute,
  854. }
  855. // Getting the base URL (i.e. "/") should succeed.
  856. resp, err := cli.Get(baseURL)
  857. if err != nil {
  858. t.Fatal("Unexpected error from getting base URL:", err)
  859. }
  860. resp.Body.Close()
  861. if resp.StatusCode != http.StatusOK {
  862. t.Fatal("Getting base URL should succeed, not", resp.Status)
  863. }
  864. // Find the returned CSRF token for future use
  865. var csrfTokenName, csrfTokenValue string
  866. for _, cookie := range resp.Cookies() {
  867. if strings.HasPrefix(cookie.Name, "CSRF-Token") {
  868. csrfTokenName = cookie.Name
  869. csrfTokenValue = cookie.Value
  870. break
  871. }
  872. }
  873. if csrfTokenValue == "" {
  874. t.Fatal("Failed to initialize CSRF test: no CSRF cookie returned from " + baseURL)
  875. }
  876. t.Run("/rest without a token should fail", func(t *testing.T) {
  877. t.Parallel()
  878. resp, err := cli.Get(baseURL + "/rest/system/config")
  879. if err != nil {
  880. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  881. }
  882. resp.Body.Close()
  883. if resp.StatusCode != http.StatusForbidden {
  884. t.Fatal("Getting /rest/system/config without CSRF token should fail, not", resp.Status)
  885. }
  886. })
  887. t.Run("/rest with a token should succeed", func(t *testing.T) {
  888. t.Parallel()
  889. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  890. req.Header.Set("X-"+csrfTokenName, csrfTokenValue)
  891. resp, err := cli.Do(req)
  892. if err != nil {
  893. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  894. }
  895. resp.Body.Close()
  896. if resp.StatusCode != http.StatusOK {
  897. t.Fatal("Getting /rest/system/config with CSRF token should succeed, not", resp.Status)
  898. }
  899. })
  900. t.Run("/rest with an incorrect API key should fail, X-API-Key version", func(t *testing.T) {
  901. t.Parallel()
  902. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  903. req.Header.Set("X-API-Key", testAPIKey+"X")
  904. resp, err := cli.Do(req)
  905. if err != nil {
  906. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  907. }
  908. resp.Body.Close()
  909. if resp.StatusCode != http.StatusForbidden {
  910. t.Fatal("Getting /rest/system/config with incorrect API token should fail, not", resp.Status)
  911. }
  912. })
  913. t.Run("/rest with an incorrect API key should fail, Bearer auth version", func(t *testing.T) {
  914. t.Parallel()
  915. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  916. req.Header.Set("Authorization", "Bearer "+testAPIKey+"X")
  917. resp, err := cli.Do(req)
  918. if err != nil {
  919. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  920. }
  921. resp.Body.Close()
  922. if resp.StatusCode != http.StatusForbidden {
  923. t.Fatal("Getting /rest/system/config with incorrect API token should fail, not", resp.Status)
  924. }
  925. })
  926. t.Run("/rest with the API key should succeed", func(t *testing.T) {
  927. t.Parallel()
  928. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  929. req.Header.Set("X-API-Key", testAPIKey)
  930. resp, err := cli.Do(req)
  931. if err != nil {
  932. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  933. }
  934. resp.Body.Close()
  935. if resp.StatusCode != http.StatusOK {
  936. t.Fatal("Getting /rest/system/config with API key should succeed, not", resp.Status)
  937. }
  938. })
  939. t.Run("/rest with the API key as a bearer token should succeed", func(t *testing.T) {
  940. t.Parallel()
  941. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  942. req.Header.Set("Authorization", "Bearer "+testAPIKey)
  943. resp, err := cli.Do(req)
  944. if err != nil {
  945. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  946. }
  947. resp.Body.Close()
  948. if resp.StatusCode != http.StatusOK {
  949. t.Fatal("Getting /rest/system/config with API key should succeed, not", resp.Status)
  950. }
  951. })
  952. }
  953. func TestRandomString(t *testing.T) {
  954. t.Parallel()
  955. baseURL, cancel, err := startHTTP(apiCfg)
  956. if err != nil {
  957. t.Fatal(err)
  958. }
  959. defer cancel()
  960. cli := &http.Client{
  961. Timeout: time.Second,
  962. }
  963. // The default should be to return a 32 character random string
  964. for _, url := range []string{"/rest/svc/random/string", "/rest/svc/random/string?length=-1", "/rest/svc/random/string?length=yo"} {
  965. req, _ := http.NewRequest("GET", baseURL+url, nil)
  966. req.Header.Set("X-API-Key", testAPIKey)
  967. resp, err := cli.Do(req)
  968. if err != nil {
  969. t.Fatal(err)
  970. }
  971. var res map[string]string
  972. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  973. t.Fatal(err)
  974. }
  975. if len(res["random"]) != 32 {
  976. t.Errorf("Expected 32 random characters, got %q of length %d", res["random"], len(res["random"]))
  977. }
  978. }
  979. // We can ask for a different length if we like
  980. req, _ := http.NewRequest("GET", baseURL+"/rest/svc/random/string?length=27", nil)
  981. req.Header.Set("X-API-Key", testAPIKey)
  982. resp, err := cli.Do(req)
  983. if err != nil {
  984. t.Fatal(err)
  985. }
  986. var res map[string]string
  987. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  988. t.Fatal(err)
  989. }
  990. if len(res["random"]) != 27 {
  991. t.Errorf("Expected 27 random characters, got %q of length %d", res["random"], len(res["random"]))
  992. }
  993. }
  994. func TestConfigPostOK(t *testing.T) {
  995. t.Parallel()
  996. cfg := bytes.NewBuffer([]byte(`{
  997. "version": 15,
  998. "folders": [
  999. {
  1000. "id": "foo",
  1001. "path": "TestConfigPostOK"
  1002. }
  1003. ]
  1004. }`))
  1005. resp, err := testConfigPost(cfg)
  1006. if err != nil {
  1007. t.Fatal(err)
  1008. }
  1009. if resp.StatusCode != http.StatusOK {
  1010. t.Error("Expected 200 OK, not", resp.Status)
  1011. }
  1012. os.RemoveAll("TestConfigPostOK")
  1013. }
  1014. func TestConfigPostDupFolder(t *testing.T) {
  1015. t.Parallel()
  1016. cfg := bytes.NewBuffer([]byte(`{
  1017. "version": 15,
  1018. "folders": [
  1019. {"id": "foo"},
  1020. {"id": "foo"}
  1021. ]
  1022. }`))
  1023. resp, err := testConfigPost(cfg)
  1024. if err != nil {
  1025. t.Fatal(err)
  1026. }
  1027. if resp.StatusCode != http.StatusBadRequest {
  1028. t.Error("Expected 400 Bad Request, not", resp.Status)
  1029. }
  1030. }
  1031. func testConfigPost(data io.Reader) (*http.Response, error) {
  1032. baseURL, cancel, err := startHTTP(apiCfg)
  1033. if err != nil {
  1034. return nil, err
  1035. }
  1036. defer cancel()
  1037. cli := &http.Client{
  1038. Timeout: time.Second,
  1039. }
  1040. req, _ := http.NewRequest("POST", baseURL+"/rest/system/config", data)
  1041. req.Header.Set("X-API-Key", testAPIKey)
  1042. return cli.Do(req)
  1043. }
  1044. func TestHostCheck(t *testing.T) {
  1045. t.Parallel()
  1046. // An API service bound to localhost should reject non-localhost host Headers
  1047. cfg := newMockedConfig()
  1048. cfg.GUIReturns(config.GUIConfiguration{RawAddress: "127.0.0.1:0"})
  1049. baseURL, cancel, err := startHTTP(cfg)
  1050. if err != nil {
  1051. t.Fatal(err)
  1052. }
  1053. defer cancel()
  1054. // A normal HTTP get to the localhost-bound service should succeed
  1055. resp, err := http.Get(baseURL)
  1056. if err != nil {
  1057. t.Fatal(err)
  1058. }
  1059. resp.Body.Close()
  1060. if resp.StatusCode != http.StatusOK {
  1061. t.Error("Regular HTTP get: expected 200 OK, not", resp.Status)
  1062. }
  1063. // A request with a suspicious Host header should fail
  1064. req, _ := http.NewRequest("GET", baseURL, nil)
  1065. req.Host = "example.com"
  1066. resp, err = http.DefaultClient.Do(req)
  1067. if err != nil {
  1068. t.Fatal(err)
  1069. }
  1070. resp.Body.Close()
  1071. if resp.StatusCode != http.StatusForbidden {
  1072. t.Error("Suspicious Host header: expected 403 Forbidden, not", resp.Status)
  1073. }
  1074. // A request with an explicit "localhost:8384" Host header should pass
  1075. req, _ = http.NewRequest("GET", baseURL, nil)
  1076. req.Host = "localhost:8384"
  1077. resp, err = http.DefaultClient.Do(req)
  1078. if err != nil {
  1079. t.Fatal(err)
  1080. }
  1081. resp.Body.Close()
  1082. if resp.StatusCode != http.StatusOK {
  1083. t.Error("Explicit localhost:8384: expected 200 OK, not", resp.Status)
  1084. }
  1085. // A request with an explicit "localhost" Host header (no port) should pass
  1086. req, _ = http.NewRequest("GET", baseURL, nil)
  1087. req.Host = "localhost"
  1088. resp, err = http.DefaultClient.Do(req)
  1089. if err != nil {
  1090. t.Fatal(err)
  1091. }
  1092. resp.Body.Close()
  1093. if resp.StatusCode != http.StatusOK {
  1094. t.Error("Explicit localhost: expected 200 OK, not", resp.Status)
  1095. }
  1096. // A server with InsecureSkipHostCheck set behaves differently
  1097. cfg = newMockedConfig()
  1098. cfg.GUIReturns(config.GUIConfiguration{
  1099. RawAddress: "127.0.0.1:0",
  1100. InsecureSkipHostCheck: true,
  1101. })
  1102. baseURL, cancel, err = startHTTP(cfg)
  1103. if err != nil {
  1104. t.Fatal(err)
  1105. }
  1106. defer cancel()
  1107. // A request with a suspicious Host header should be allowed
  1108. req, _ = http.NewRequest("GET", baseURL, nil)
  1109. req.Host = "example.com"
  1110. resp, err = http.DefaultClient.Do(req)
  1111. if err != nil {
  1112. t.Fatal(err)
  1113. }
  1114. resp.Body.Close()
  1115. if resp.StatusCode != http.StatusOK {
  1116. t.Error("Incorrect host header, check disabled: expected 200 OK, not", resp.Status)
  1117. }
  1118. if !testing.Short() {
  1119. // A server bound to a wildcard address also doesn't do the check
  1120. cfg = newMockedConfig()
  1121. cfg.GUIReturns(config.GUIConfiguration{
  1122. RawAddress: "0.0.0.0:0",
  1123. })
  1124. baseURL, cancel, err = startHTTP(cfg)
  1125. if err != nil {
  1126. t.Fatal(err)
  1127. }
  1128. defer cancel()
  1129. // A request with a suspicious Host header should be allowed
  1130. req, _ = http.NewRequest("GET", baseURL, nil)
  1131. req.Host = "example.com"
  1132. resp, err = http.DefaultClient.Do(req)
  1133. if err != nil {
  1134. t.Fatal(err)
  1135. }
  1136. resp.Body.Close()
  1137. if resp.StatusCode != http.StatusOK {
  1138. t.Error("Incorrect host header, wildcard bound: expected 200 OK, not", resp.Status)
  1139. }
  1140. }
  1141. // This should all work over IPv6 as well
  1142. if runningInContainer() {
  1143. // Working IPv6 in Docker can't be taken for granted.
  1144. return
  1145. }
  1146. cfg = newMockedConfig()
  1147. cfg.GUIReturns(config.GUIConfiguration{
  1148. RawAddress: "[::1]:0",
  1149. })
  1150. baseURL, cancel, err = startHTTP(cfg)
  1151. if err != nil {
  1152. t.Fatal(err)
  1153. }
  1154. defer cancel()
  1155. // A normal HTTP get to the localhost-bound service should succeed
  1156. resp, err = http.Get(baseURL)
  1157. if err != nil {
  1158. t.Fatal(err)
  1159. }
  1160. resp.Body.Close()
  1161. if resp.StatusCode != http.StatusOK {
  1162. t.Error("Regular HTTP get (IPv6): expected 200 OK, not", resp.Status)
  1163. }
  1164. // A request with a suspicious Host header should fail
  1165. req, _ = http.NewRequest("GET", baseURL, nil)
  1166. req.Host = "example.com"
  1167. resp, err = http.DefaultClient.Do(req)
  1168. if err != nil {
  1169. t.Fatal(err)
  1170. }
  1171. resp.Body.Close()
  1172. if resp.StatusCode != http.StatusForbidden {
  1173. t.Error("Suspicious Host header (IPv6): expected 403 Forbidden, not", resp.Status)
  1174. }
  1175. // A request with an explicit "localhost:8384" Host header should pass
  1176. req, _ = http.NewRequest("GET", baseURL, nil)
  1177. req.Host = "localhost:8384"
  1178. resp, err = http.DefaultClient.Do(req)
  1179. if err != nil {
  1180. t.Fatal(err)
  1181. }
  1182. resp.Body.Close()
  1183. if resp.StatusCode != http.StatusOK {
  1184. t.Error("Explicit localhost:8384 (IPv6): expected 200 OK, not", resp.Status)
  1185. }
  1186. }
  1187. func TestAddressIsLocalhost(t *testing.T) {
  1188. t.Parallel()
  1189. testcases := []struct {
  1190. address string
  1191. result bool
  1192. }{
  1193. // These are all valid localhost addresses
  1194. {"localhost", true},
  1195. {"LOCALHOST", true},
  1196. {"localhost.", true},
  1197. {"::1", true},
  1198. {"127.0.0.1", true},
  1199. {"127.23.45.56", true},
  1200. {"localhost:8080", true},
  1201. {"LOCALHOST:8000", true},
  1202. {"localhost.:8080", true},
  1203. {"[::1]:8080", true},
  1204. {"127.0.0.1:8080", true},
  1205. {"127.23.45.56:8080", true},
  1206. {"www.localhost", true},
  1207. {"www.localhost:8080", true},
  1208. // These are all non-localhost addresses
  1209. {"example.com", false},
  1210. {"example.com:8080", false},
  1211. {"localhost.com", false},
  1212. {"localhost.com:8080", false},
  1213. {"192.0.2.10", false},
  1214. {"192.0.2.10:8080", false},
  1215. {"0.0.0.0", false},
  1216. {"0.0.0.0:8080", false},
  1217. {"::", false},
  1218. {"[::]:8080", false},
  1219. {":8080", false},
  1220. }
  1221. for _, tc := range testcases {
  1222. result := addressIsLocalhost(tc.address)
  1223. if result != tc.result {
  1224. t.Errorf("addressIsLocalhost(%q)=%v, expected %v", tc.address, result, tc.result)
  1225. }
  1226. }
  1227. }
  1228. func TestAccessControlAllowOriginHeader(t *testing.T) {
  1229. t.Parallel()
  1230. baseURL, cancel, err := startHTTP(apiCfg)
  1231. if err != nil {
  1232. t.Fatal(err)
  1233. }
  1234. defer cancel()
  1235. cli := &http.Client{
  1236. Timeout: time.Second,
  1237. }
  1238. req, _ := http.NewRequest("GET", baseURL+"/rest/system/status", nil)
  1239. req.Header.Set("X-API-Key", testAPIKey)
  1240. resp, err := cli.Do(req)
  1241. if err != nil {
  1242. t.Fatal(err)
  1243. }
  1244. resp.Body.Close()
  1245. if resp.StatusCode != http.StatusOK {
  1246. t.Fatal("GET on /rest/system/status should succeed, not", resp.Status)
  1247. }
  1248. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  1249. t.Fatal("GET on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  1250. }
  1251. }
  1252. func TestOptionsRequest(t *testing.T) {
  1253. t.Parallel()
  1254. baseURL, cancel, err := startHTTP(apiCfg)
  1255. if err != nil {
  1256. t.Fatal(err)
  1257. }
  1258. defer cancel()
  1259. cli := &http.Client{
  1260. Timeout: time.Second,
  1261. }
  1262. req, _ := http.NewRequest("OPTIONS", baseURL+"/rest/system/status", nil)
  1263. resp, err := cli.Do(req)
  1264. if err != nil {
  1265. t.Fatal(err)
  1266. }
  1267. resp.Body.Close()
  1268. if resp.StatusCode != http.StatusNoContent {
  1269. t.Fatal("OPTIONS on /rest/system/status should succeed, not", resp.Status)
  1270. }
  1271. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  1272. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  1273. }
  1274. if resp.Header.Get("Access-Control-Allow-Methods") != "GET, POST, PUT, PATCH, DELETE, OPTIONS" {
  1275. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS' header")
  1276. }
  1277. if resp.Header.Get("Access-Control-Allow-Headers") != "Content-Type, X-API-Key" {
  1278. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Headers: Content-Type, X-API-KEY' header")
  1279. }
  1280. }
  1281. func TestEventMasks(t *testing.T) {
  1282. t.Parallel()
  1283. cfg := newMockedConfig()
  1284. defSub := new(eventmocks.BufferedSubscription)
  1285. diskSub := new(eventmocks.BufferedSubscription)
  1286. svc := New(protocol.LocalDeviceID, cfg, "", "syncthing", nil, defSub, diskSub, events.NoopLogger, nil, nil, nil, nil, nil, nil, false).(*service)
  1287. defer os.Remove(token)
  1288. if mask := svc.getEventMask(""); mask != DefaultEventMask {
  1289. t.Errorf("incorrect default mask %x != %x", int64(mask), int64(DefaultEventMask))
  1290. }
  1291. expected := events.FolderSummary | events.LocalChangeDetected
  1292. if mask := svc.getEventMask("FolderSummary,LocalChangeDetected"); mask != expected {
  1293. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  1294. }
  1295. expected = 0
  1296. if mask := svc.getEventMask("WeirdEvent,something else that doesn't exist"); mask != expected {
  1297. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  1298. }
  1299. if res := svc.getEventSub(DefaultEventMask); res != defSub {
  1300. t.Errorf("should have returned the given default event sub")
  1301. }
  1302. if res := svc.getEventSub(DiskEventMask); res != diskSub {
  1303. t.Errorf("should have returned the given disk event sub")
  1304. }
  1305. if res := svc.getEventSub(events.LocalIndexUpdated); res == nil || res == defSub || res == diskSub {
  1306. t.Errorf("should have returned a valid, non-default event sub")
  1307. }
  1308. }
  1309. func TestBrowse(t *testing.T) {
  1310. t.Parallel()
  1311. pathSep := string(os.PathSeparator)
  1312. ffs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?nostfolder=true")
  1313. _ = ffs.Mkdir("dir", 0o755)
  1314. _ = fs.WriteFile(ffs, "file", []byte("hello"), 0o644)
  1315. _ = ffs.Mkdir("MiXEDCase", 0o755)
  1316. // We expect completion to return the full path to the completed
  1317. // directory, with an ending slash.
  1318. dirPath := "dir" + pathSep
  1319. mixedCaseDirPath := "MiXEDCase" + pathSep
  1320. cases := []struct {
  1321. current string
  1322. returns []string
  1323. }{
  1324. // The directory without slash is completed to one with slash.
  1325. {"dir", []string{"dir" + pathSep}},
  1326. // With slash it's completed to its contents.
  1327. // Dirs are given pathSeps.
  1328. // Files are not returned.
  1329. {"", []string{mixedCaseDirPath, dirPath}},
  1330. // Globbing is automatic based on prefix.
  1331. {"d", []string{dirPath}},
  1332. {"di", []string{dirPath}},
  1333. {"dir", []string{dirPath}},
  1334. {"f", nil},
  1335. {"q", nil},
  1336. // Globbing is case-insensitive
  1337. {"mixed", []string{mixedCaseDirPath}},
  1338. }
  1339. for _, tc := range cases {
  1340. ret := browseFiles(ffs, tc.current)
  1341. if !slices.Equal(ret, tc.returns) {
  1342. t.Errorf("browseFiles(%q) => %q, expected %q", tc.current, ret, tc.returns)
  1343. }
  1344. }
  1345. }
  1346. func TestPrefixMatch(t *testing.T) {
  1347. t.Parallel()
  1348. cases := []struct {
  1349. s string
  1350. prefix string
  1351. expected int
  1352. }{
  1353. {"aaaA", "aaa", matchExact},
  1354. {"AAAX", "BBB", noMatch},
  1355. {"AAAX", "aAa", matchCaseIns},
  1356. {"äÜX", "äü", matchCaseIns},
  1357. }
  1358. for _, tc := range cases {
  1359. ret := checkPrefixMatch(tc.s, tc.prefix)
  1360. if ret != tc.expected {
  1361. t.Errorf("checkPrefixMatch(%q, %q) => %v, expected %v", tc.s, tc.prefix, ret, tc.expected)
  1362. }
  1363. }
  1364. }
  1365. func TestShouldRegenerateCertificate(t *testing.T) {
  1366. // Self signed certificates expiring in less than a month are errored so we
  1367. // can regenerate in time.
  1368. crt, err := tlsutil.NewCertificateInMemory("foo.example.com", 29)
  1369. if err != nil {
  1370. t.Fatal(err)
  1371. }
  1372. if err := shouldRegenerateCertificate(crt); err == nil {
  1373. t.Error("expected expiry error")
  1374. }
  1375. // Certificates with at least 31 days of life left are fine.
  1376. crt, err = tlsutil.NewCertificateInMemory("foo.example.com", 31)
  1377. if err != nil {
  1378. t.Fatal(err)
  1379. }
  1380. if err := shouldRegenerateCertificate(crt); err != nil {
  1381. t.Error("expected no error:", err)
  1382. }
  1383. if build.IsDarwin {
  1384. // Certificates with too long an expiry time are not allowed on macOS
  1385. crt, err = tlsutil.NewCertificateInMemory("foo.example.com", 1000)
  1386. if err != nil {
  1387. t.Fatal(err)
  1388. }
  1389. if err := shouldRegenerateCertificate(crt); err == nil {
  1390. t.Error("expected expiry error")
  1391. }
  1392. }
  1393. }
  1394. func TestConfigChanges(t *testing.T) {
  1395. t.Parallel()
  1396. const testAPIKey = "foobarbaz"
  1397. cfg := config.Configuration{
  1398. GUI: config.GUIConfiguration{
  1399. RawAddress: "127.0.0.1:0",
  1400. RawUseTLS: false,
  1401. APIKey: testAPIKey,
  1402. },
  1403. }
  1404. tmpFile, err := os.CreateTemp("", "syncthing-testConfig-")
  1405. if err != nil {
  1406. panic(err)
  1407. }
  1408. defer os.Remove(tmpFile.Name())
  1409. w := config.Wrap(tmpFile.Name(), cfg, protocol.LocalDeviceID, events.NoopLogger)
  1410. tmpFile.Close()
  1411. cfgCtx, cfgCancel := context.WithCancel(context.Background())
  1412. go w.Serve(cfgCtx)
  1413. defer cfgCancel()
  1414. baseURL, cancel, err := startHTTP(w)
  1415. if err != nil {
  1416. t.Fatal("Unexpected error from getting base URL:", err)
  1417. }
  1418. defer cancel()
  1419. cli := &http.Client{
  1420. Timeout: time.Minute,
  1421. }
  1422. do := func(req *http.Request, status int) *http.Response {
  1423. t.Helper()
  1424. req.Header.Set("X-API-Key", testAPIKey)
  1425. resp, err := cli.Do(req)
  1426. if err != nil {
  1427. t.Fatal(err)
  1428. }
  1429. if resp.StatusCode != status {
  1430. t.Errorf("Expected status %v, got %v", status, resp.StatusCode)
  1431. }
  1432. return resp
  1433. }
  1434. mod := func(method, path string, data interface{}) {
  1435. t.Helper()
  1436. bs, err := json.Marshal(data)
  1437. if err != nil {
  1438. t.Fatal(err)
  1439. }
  1440. req, _ := http.NewRequest(method, baseURL+path, bytes.NewReader(bs))
  1441. do(req, http.StatusOK).Body.Close()
  1442. }
  1443. get := func(path string) *http.Response {
  1444. t.Helper()
  1445. req, _ := http.NewRequest(http.MethodGet, baseURL+path, nil)
  1446. return do(req, http.StatusOK)
  1447. }
  1448. dev1Path := "/rest/config/devices/" + dev1.String()
  1449. // Create device
  1450. mod(http.MethodPut, "/rest/config/devices", []config.DeviceConfiguration{{DeviceID: dev1}})
  1451. // Check its there
  1452. get(dev1Path).Body.Close()
  1453. // Modify just a single attribute
  1454. mod(http.MethodPatch, dev1Path, map[string]bool{"Paused": true})
  1455. // Check that attribute
  1456. resp := get(dev1Path)
  1457. var dev config.DeviceConfiguration
  1458. if err := unmarshalTo(resp.Body, &dev); err != nil {
  1459. t.Fatal(err)
  1460. }
  1461. if !dev.Paused {
  1462. t.Error("Expected device to be paused")
  1463. }
  1464. folder2Path := "/rest/config/folders/folder2"
  1465. // Create a folder and add another
  1466. mod(http.MethodPut, "/rest/config/folders", []config.FolderConfiguration{{ID: "folder1", Path: "folder1"}})
  1467. mod(http.MethodPut, folder2Path, config.FolderConfiguration{ID: "folder2", Path: "folder2"})
  1468. // Check they are there
  1469. get("/rest/config/folders/folder1").Body.Close()
  1470. get(folder2Path).Body.Close()
  1471. // Modify just a single attribute
  1472. mod(http.MethodPatch, folder2Path, map[string]bool{"Paused": true})
  1473. // Check that attribute
  1474. resp = get(folder2Path)
  1475. var folder config.FolderConfiguration
  1476. if err := unmarshalTo(resp.Body, &folder); err != nil {
  1477. t.Fatal(err)
  1478. }
  1479. if !dev.Paused {
  1480. t.Error("Expected folder to be paused")
  1481. }
  1482. // Delete folder2
  1483. req, _ := http.NewRequest(http.MethodDelete, baseURL+folder2Path, nil)
  1484. do(req, http.StatusOK)
  1485. // Check folder1 is still there and folder2 gone
  1486. get("/rest/config/folders/folder1").Body.Close()
  1487. req, _ = http.NewRequest(http.MethodGet, baseURL+folder2Path, nil)
  1488. do(req, http.StatusNotFound)
  1489. mod(http.MethodPatch, "/rest/config/options", map[string]int{"maxSendKbps": 50})
  1490. resp = get("/rest/config/options")
  1491. var opts config.OptionsConfiguration
  1492. if err := unmarshalTo(resp.Body, &opts); err != nil {
  1493. t.Fatal(err)
  1494. }
  1495. if opts.MaxSendKbps != 50 {
  1496. t.Error("Expected 50 for MaxSendKbps, got", opts.MaxSendKbps)
  1497. }
  1498. }
  1499. func TestSanitizedHostname(t *testing.T) {
  1500. cases := []struct {
  1501. in, out string
  1502. }{
  1503. {"foo.BAR-baz", "foo.bar-baz"},
  1504. {"~.~-Min 1:a Räksmörgås-dator 😀😎 ~.~-", "min1araksmorgas-dator"},
  1505. {"Vicenç-PC", "vicenc-pc"},
  1506. {"~.~-~.~-", ""},
  1507. {"", ""},
  1508. }
  1509. for _, tc := range cases {
  1510. res, err := sanitizedHostname(tc.in)
  1511. if tc.out == "" && err == nil {
  1512. t.Errorf("%q should cause error", tc.in)
  1513. } else if res != tc.out {
  1514. t.Errorf("%q => %q, expected %q", tc.in, res, tc.out)
  1515. }
  1516. }
  1517. }
  1518. // runningInContainer returns true if we are inside Docker or LXC. It might
  1519. // be prone to false negatives if things change in the future, but likely
  1520. // not false positives.
  1521. func runningInContainer() bool {
  1522. if !build.IsLinux {
  1523. return false
  1524. }
  1525. bs, err := os.ReadFile("/proc/1/cgroup")
  1526. if err != nil {
  1527. return false
  1528. }
  1529. if bytes.Contains(bs, []byte("/docker/")) {
  1530. return true
  1531. }
  1532. if bytes.Contains(bs, []byte("/lxc/")) {
  1533. return true
  1534. }
  1535. return false
  1536. }