api_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. "encoding/json"
  11. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "net"
  15. "net/http"
  16. "net/http/httptest"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. "testing"
  22. "time"
  23. "github.com/d4l3k/messagediff"
  24. "github.com/syncthing/syncthing/lib/config"
  25. "github.com/syncthing/syncthing/lib/events"
  26. "github.com/syncthing/syncthing/lib/fs"
  27. "github.com/syncthing/syncthing/lib/locations"
  28. "github.com/syncthing/syncthing/lib/model"
  29. "github.com/syncthing/syncthing/lib/protocol"
  30. "github.com/syncthing/syncthing/lib/sync"
  31. "github.com/syncthing/syncthing/lib/ur"
  32. "github.com/thejerf/suture"
  33. )
  34. var (
  35. confDir = filepath.Join("testdata", "config")
  36. token = filepath.Join(confDir, "csrftokens.txt")
  37. )
  38. func TestMain(m *testing.M) {
  39. orig := locations.GetBaseDir(locations.ConfigBaseDir)
  40. locations.SetBaseDir(locations.ConfigBaseDir, confDir)
  41. exitCode := m.Run()
  42. locations.SetBaseDir(locations.ConfigBaseDir, orig)
  43. os.Exit(exitCode)
  44. }
  45. func TestCSRFToken(t *testing.T) {
  46. defer os.Remove(token)
  47. max := 250
  48. int := 5
  49. if testing.Short() {
  50. max = 20
  51. int = 2
  52. }
  53. t1 := newCsrfToken()
  54. t2 := newCsrfToken()
  55. t3 := newCsrfToken()
  56. if !validCsrfToken(t3) {
  57. t.Fatal("t3 should be valid")
  58. }
  59. for i := 0; i < max; i++ {
  60. if i%int == 0 {
  61. // t1 and t2 should remain valid by virtue of us checking them now
  62. // and then.
  63. if !validCsrfToken(t1) {
  64. t.Fatal("t1 should be valid at iteration", i)
  65. }
  66. if !validCsrfToken(t2) {
  67. t.Fatal("t2 should be valid at iteration", i)
  68. }
  69. }
  70. // The newly generated token is always valid
  71. t4 := newCsrfToken()
  72. if !validCsrfToken(t4) {
  73. t.Fatal("t4 should be valid at iteration", i)
  74. }
  75. }
  76. if validCsrfToken(t3) {
  77. t.Fatal("t3 should have expired by now")
  78. }
  79. }
  80. func TestStopAfterBrokenConfig(t *testing.T) {
  81. cfg := config.Configuration{
  82. GUI: config.GUIConfiguration{
  83. RawAddress: "127.0.0.1:0",
  84. RawUseTLS: false,
  85. },
  86. }
  87. w := config.Wrap("/dev/null", cfg, events.NoopLogger)
  88. srv := New(protocol.LocalDeviceID, w, "", "syncthing", nil, nil, nil, events.NoopLogger, nil, nil, nil, nil, nil, nil, nil, nil, false).(*service)
  89. defer os.Remove(token)
  90. srv.started = make(chan string)
  91. sup := suture.New("test", suture.Spec{
  92. PassThroughPanics: true,
  93. })
  94. sup.Add(srv)
  95. sup.ServeBackground()
  96. <-srv.started
  97. // Service is now running, listening on a random port on localhost. Now we
  98. // request a config change to a completely invalid listen address. The
  99. // commit will fail and the service will be in a broken state.
  100. newCfg := config.Configuration{
  101. GUI: config.GUIConfiguration{
  102. RawAddress: "totally not a valid address",
  103. RawUseTLS: false,
  104. },
  105. }
  106. if err := srv.VerifyConfiguration(cfg, newCfg); err == nil {
  107. t.Fatal("Verify config should have failed")
  108. }
  109. // Nonetheless, it should be fine to Stop() it without panic.
  110. sup.Stop()
  111. }
  112. func TestAssetsDir(t *testing.T) {
  113. // For any given request to $FILE, we should return the first found of
  114. // - assetsdir/$THEME/$FILE
  115. // - compiled in asset $THEME/$FILE
  116. // - assetsdir/default/$FILE
  117. // - compiled in asset default/$FILE
  118. // The asset map contains compressed assets, so create a couple of gzip compressed assets here.
  119. buf := new(bytes.Buffer)
  120. gw := gzip.NewWriter(buf)
  121. gw.Write([]byte("default"))
  122. gw.Close()
  123. def := buf.Bytes()
  124. buf = new(bytes.Buffer)
  125. gw = gzip.NewWriter(buf)
  126. gw.Write([]byte("foo"))
  127. gw.Close()
  128. foo := buf.Bytes()
  129. e := &staticsServer{
  130. theme: "foo",
  131. mut: sync.NewRWMutex(),
  132. assetDir: "testdata",
  133. assets: map[string][]byte{
  134. "foo/a": foo, // overridden in foo/a
  135. "foo/b": foo,
  136. "default/a": def, // overridden in default/a (but foo/a takes precedence)
  137. "default/b": def, // overridden in default/b (but foo/b takes precedence)
  138. "default/c": def,
  139. },
  140. }
  141. s := httptest.NewServer(e)
  142. defer s.Close()
  143. // assetsdir/foo/a exists, overrides compiled in
  144. expectURLToContain(t, s.URL+"/a", "overridden-foo")
  145. // foo/b is compiled in, default/b is overridden, return compiled in
  146. expectURLToContain(t, s.URL+"/b", "foo")
  147. // only exists as compiled in default/c so use that
  148. expectURLToContain(t, s.URL+"/c", "default")
  149. // only exists as overridden default/d so use that
  150. expectURLToContain(t, s.URL+"/d", "overridden-default")
  151. }
  152. func expectURLToContain(t *testing.T, url, exp string) {
  153. res, err := http.Get(url)
  154. if err != nil {
  155. t.Error(err)
  156. return
  157. }
  158. if res.StatusCode != 200 {
  159. t.Errorf("Got %s instead of 200 OK", res.Status)
  160. return
  161. }
  162. data, err := ioutil.ReadAll(res.Body)
  163. res.Body.Close()
  164. if err != nil {
  165. t.Error(err)
  166. return
  167. }
  168. if string(data) != exp {
  169. t.Errorf("Got %q instead of %q on %q", data, exp, url)
  170. return
  171. }
  172. }
  173. func TestDirNames(t *testing.T) {
  174. names := dirNames("testdata")
  175. expected := []string{"config", "default", "foo", "testfolder"}
  176. if diff, equal := messagediff.PrettyDiff(expected, names); !equal {
  177. t.Errorf("Unexpected dirNames return: %#v\n%s", names, diff)
  178. }
  179. }
  180. type httpTestCase struct {
  181. URL string // URL to check
  182. Code int // Expected result code
  183. Type string // Expected content type
  184. Prefix string // Expected result prefix
  185. Timeout time.Duration // Defaults to a second
  186. }
  187. func TestAPIServiceRequests(t *testing.T) {
  188. const testAPIKey = "foobarbaz"
  189. cfg := new(mockedConfig)
  190. cfg.gui.APIKey = testAPIKey
  191. baseURL, err := startHTTP(cfg)
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. cases := []httpTestCase{
  196. // /rest/db
  197. {
  198. URL: "/rest/db/completion?device=" + protocol.LocalDeviceID.String() + "&folder=default",
  199. Code: 200,
  200. Type: "application/json",
  201. Prefix: "{",
  202. },
  203. {
  204. URL: "/rest/db/file?folder=default&file=something",
  205. Code: 404,
  206. },
  207. {
  208. URL: "/rest/db/ignores?folder=default",
  209. Code: 200,
  210. Type: "application/json",
  211. Prefix: "{",
  212. },
  213. {
  214. URL: "/rest/db/need?folder=default",
  215. Code: 200,
  216. Type: "application/json",
  217. Prefix: "{",
  218. },
  219. {
  220. URL: "/rest/db/status?folder=default",
  221. Code: 200,
  222. Type: "application/json",
  223. Prefix: "{",
  224. },
  225. {
  226. URL: "/rest/db/browse?folder=default",
  227. Code: 200,
  228. Type: "application/json",
  229. Prefix: "null",
  230. },
  231. // /rest/stats
  232. {
  233. URL: "/rest/stats/device",
  234. Code: 200,
  235. Type: "application/json",
  236. Prefix: "null",
  237. },
  238. {
  239. URL: "/rest/stats/folder",
  240. Code: 200,
  241. Type: "application/json",
  242. Prefix: "null",
  243. },
  244. // /rest/svc
  245. {
  246. URL: "/rest/svc/deviceid?id=" + protocol.LocalDeviceID.String(),
  247. Code: 200,
  248. Type: "application/json",
  249. Prefix: "{",
  250. },
  251. {
  252. URL: "/rest/svc/lang",
  253. Code: 200,
  254. Type: "application/json",
  255. Prefix: "[",
  256. },
  257. {
  258. URL: "/rest/svc/report",
  259. Code: 200,
  260. Type: "application/json",
  261. Prefix: "{",
  262. Timeout: 5 * time.Second,
  263. },
  264. // /rest/system
  265. {
  266. URL: "/rest/system/browse?current=~",
  267. Code: 200,
  268. Type: "application/json",
  269. Prefix: "[",
  270. },
  271. {
  272. URL: "/rest/system/config",
  273. Code: 200,
  274. Type: "application/json",
  275. Prefix: "{",
  276. },
  277. {
  278. URL: "/rest/system/config/insync",
  279. Code: 200,
  280. Type: "application/json",
  281. Prefix: "{",
  282. },
  283. {
  284. URL: "/rest/system/connections",
  285. Code: 200,
  286. Type: "application/json",
  287. Prefix: "null",
  288. },
  289. {
  290. URL: "/rest/system/discovery",
  291. Code: 200,
  292. Type: "application/json",
  293. Prefix: "{",
  294. },
  295. {
  296. URL: "/rest/system/error?since=0",
  297. Code: 200,
  298. Type: "application/json",
  299. Prefix: "{",
  300. },
  301. {
  302. URL: "/rest/system/ping",
  303. Code: 200,
  304. Type: "application/json",
  305. Prefix: "{",
  306. },
  307. {
  308. URL: "/rest/system/status",
  309. Code: 200,
  310. Type: "application/json",
  311. Prefix: "{",
  312. },
  313. {
  314. URL: "/rest/system/version",
  315. Code: 200,
  316. Type: "application/json",
  317. Prefix: "{",
  318. },
  319. {
  320. URL: "/rest/system/debug",
  321. Code: 200,
  322. Type: "application/json",
  323. Prefix: "{",
  324. },
  325. {
  326. URL: "/rest/system/log?since=0",
  327. Code: 200,
  328. Type: "application/json",
  329. Prefix: "{",
  330. },
  331. {
  332. URL: "/rest/system/log.txt?since=0",
  333. Code: 200,
  334. Type: "text/plain",
  335. Prefix: "",
  336. },
  337. }
  338. for _, tc := range cases {
  339. t.Log("Testing", tc.URL, "...")
  340. testHTTPRequest(t, baseURL, tc, testAPIKey)
  341. }
  342. }
  343. // testHTTPRequest tries the given test case, comparing the result code,
  344. // content type, and result prefix.
  345. func testHTTPRequest(t *testing.T, baseURL string, tc httpTestCase, apikey string) {
  346. timeout := time.Second
  347. if tc.Timeout > 0 {
  348. timeout = tc.Timeout
  349. }
  350. cli := &http.Client{
  351. Timeout: timeout,
  352. }
  353. req, err := http.NewRequest("GET", baseURL+tc.URL, nil)
  354. if err != nil {
  355. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  356. return
  357. }
  358. req.Header.Set("X-API-Key", apikey)
  359. resp, err := cli.Do(req)
  360. if err != nil {
  361. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  362. return
  363. }
  364. defer resp.Body.Close()
  365. if resp.StatusCode != tc.Code {
  366. t.Errorf("Get on %s should have returned status code %d, not %s", tc.URL, tc.Code, resp.Status)
  367. return
  368. }
  369. ct := resp.Header.Get("Content-Type")
  370. if !strings.HasPrefix(ct, tc.Type) {
  371. t.Errorf("The content type on %s should be %q, not %q", tc.URL, tc.Type, ct)
  372. return
  373. }
  374. data, err := ioutil.ReadAll(resp.Body)
  375. if err != nil {
  376. t.Errorf("Unexpected error reading %s: %v", tc.URL, err)
  377. return
  378. }
  379. if !bytes.HasPrefix(data, []byte(tc.Prefix)) {
  380. t.Errorf("Returned data from %s does not have prefix %q: %s", tc.URL, tc.Prefix, data)
  381. return
  382. }
  383. }
  384. func TestHTTPLogin(t *testing.T) {
  385. cfg := new(mockedConfig)
  386. cfg.gui.User = "üser"
  387. cfg.gui.Password = "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq" // bcrypt of "räksmörgås" in UTF-8
  388. baseURL, err := startHTTP(cfg)
  389. if err != nil {
  390. t.Fatal(err)
  391. }
  392. // Verify rejection when not using authorization
  393. req, _ := http.NewRequest("GET", baseURL, nil)
  394. resp, err := http.DefaultClient.Do(req)
  395. if err != nil {
  396. t.Fatal(err)
  397. }
  398. if resp.StatusCode != http.StatusUnauthorized {
  399. t.Errorf("Unexpected non-401 return code %d for unauthed request", resp.StatusCode)
  400. }
  401. // Verify that incorrect password is rejected
  402. req.SetBasicAuth("üser", "rksmrgs")
  403. resp, err = http.DefaultClient.Do(req)
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. if resp.StatusCode != http.StatusUnauthorized {
  408. t.Errorf("Unexpected non-401 return code %d for incorrect password", resp.StatusCode)
  409. }
  410. // Verify that incorrect username is rejected
  411. req.SetBasicAuth("user", "räksmörgås") // string literals in Go source code are in UTF-8
  412. resp, err = http.DefaultClient.Do(req)
  413. if err != nil {
  414. t.Fatal(err)
  415. }
  416. if resp.StatusCode != http.StatusUnauthorized {
  417. t.Errorf("Unexpected non-401 return code %d for incorrect username", resp.StatusCode)
  418. }
  419. // Verify that UTF-8 auth works
  420. req.SetBasicAuth("üser", "räksmörgås") // string literals in Go source code are in UTF-8
  421. resp, err = http.DefaultClient.Do(req)
  422. if err != nil {
  423. t.Fatal(err)
  424. }
  425. if resp.StatusCode != http.StatusOK {
  426. t.Errorf("Unexpected non-200 return code %d for authed request (UTF-8)", resp.StatusCode)
  427. }
  428. // Verify that ISO-8859-1 auth
  429. req.SetBasicAuth("\xfcser", "r\xe4ksm\xf6rg\xe5s") // escaped ISO-8859-1
  430. resp, err = http.DefaultClient.Do(req)
  431. if err != nil {
  432. t.Fatal(err)
  433. }
  434. if resp.StatusCode != http.StatusOK {
  435. t.Errorf("Unexpected non-200 return code %d for authed request (ISO-8859-1)", resp.StatusCode)
  436. }
  437. }
  438. func startHTTP(cfg *mockedConfig) (string, error) {
  439. m := new(mockedModel)
  440. assetDir := "../../gui"
  441. eventSub := new(mockedEventSub)
  442. diskEventSub := new(mockedEventSub)
  443. discoverer := new(mockedCachingMux)
  444. connections := new(mockedConnections)
  445. errorLog := new(mockedLoggerRecorder)
  446. systemLog := new(mockedLoggerRecorder)
  447. cpu := new(mockedCPUService)
  448. addrChan := make(chan string)
  449. // Instantiate the API service
  450. urService := ur.New(cfg, m, connections, false)
  451. summaryService := model.NewFolderSummaryService(cfg, m, protocol.LocalDeviceID, events.NoopLogger)
  452. svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, summaryService, errorLog, systemLog, cpu, nil, false).(*service)
  453. defer os.Remove(token)
  454. svc.started = addrChan
  455. // Actually start the API service
  456. supervisor := suture.New("API test", suture.Spec{
  457. PassThroughPanics: true,
  458. })
  459. supervisor.Add(svc)
  460. supervisor.ServeBackground()
  461. // Make sure the API service is listening, and get the URL to use.
  462. addr := <-addrChan
  463. tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
  464. if err != nil {
  465. return "", fmt.Errorf("Weird address from API service: %v", err)
  466. }
  467. host, _, _ := net.SplitHostPort(cfg.gui.RawAddress)
  468. if host == "" || host == "0.0.0.0" {
  469. host = "127.0.0.1"
  470. }
  471. baseURL := fmt.Sprintf("http://%s", net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port)))
  472. return baseURL, nil
  473. }
  474. func TestCSRFRequired(t *testing.T) {
  475. const testAPIKey = "foobarbaz"
  476. cfg := new(mockedConfig)
  477. cfg.gui.APIKey = testAPIKey
  478. baseURL, err := startHTTP(cfg)
  479. if err != nil {
  480. t.Fatal("Unexpected error from getting base URL:", err)
  481. }
  482. cli := &http.Client{
  483. Timeout: time.Second,
  484. }
  485. // Getting the base URL (i.e. "/") should succeed.
  486. resp, err := cli.Get(baseURL)
  487. if err != nil {
  488. t.Fatal("Unexpected error from getting base URL:", err)
  489. }
  490. resp.Body.Close()
  491. if resp.StatusCode != http.StatusOK {
  492. t.Fatal("Getting base URL should succeed, not", resp.Status)
  493. }
  494. // Find the returned CSRF token for future use
  495. var csrfTokenName, csrfTokenValue string
  496. for _, cookie := range resp.Cookies() {
  497. if strings.HasPrefix(cookie.Name, "CSRF-Token") {
  498. csrfTokenName = cookie.Name
  499. csrfTokenValue = cookie.Value
  500. break
  501. }
  502. }
  503. // Calling on /rest without a token should fail
  504. resp, err = cli.Get(baseURL + "/rest/system/config")
  505. if err != nil {
  506. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  507. }
  508. resp.Body.Close()
  509. if resp.StatusCode != http.StatusForbidden {
  510. t.Fatal("Getting /rest/system/config without CSRF token should fail, not", resp.Status)
  511. }
  512. // Calling on /rest with a token should succeed
  513. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  514. req.Header.Set("X-"+csrfTokenName, csrfTokenValue)
  515. resp, err = cli.Do(req)
  516. if err != nil {
  517. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  518. }
  519. resp.Body.Close()
  520. if resp.StatusCode != http.StatusOK {
  521. t.Fatal("Getting /rest/system/config with CSRF token should succeed, not", resp.Status)
  522. }
  523. // Calling on /rest with the API key should succeed
  524. req, _ = http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  525. req.Header.Set("X-API-Key", testAPIKey)
  526. resp, err = cli.Do(req)
  527. if err != nil {
  528. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  529. }
  530. resp.Body.Close()
  531. if resp.StatusCode != http.StatusOK {
  532. t.Fatal("Getting /rest/system/config with API key should succeed, not", resp.Status)
  533. }
  534. }
  535. func TestRandomString(t *testing.T) {
  536. const testAPIKey = "foobarbaz"
  537. cfg := new(mockedConfig)
  538. cfg.gui.APIKey = testAPIKey
  539. baseURL, err := startHTTP(cfg)
  540. if err != nil {
  541. t.Fatal(err)
  542. }
  543. cli := &http.Client{
  544. Timeout: time.Second,
  545. }
  546. // The default should be to return a 32 character random string
  547. for _, url := range []string{"/rest/svc/random/string", "/rest/svc/random/string?length=-1", "/rest/svc/random/string?length=yo"} {
  548. req, _ := http.NewRequest("GET", baseURL+url, nil)
  549. req.Header.Set("X-API-Key", testAPIKey)
  550. resp, err := cli.Do(req)
  551. if err != nil {
  552. t.Fatal(err)
  553. }
  554. var res map[string]string
  555. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  556. t.Fatal(err)
  557. }
  558. if len(res["random"]) != 32 {
  559. t.Errorf("Expected 32 random characters, got %q of length %d", res["random"], len(res["random"]))
  560. }
  561. }
  562. // We can ask for a different length if we like
  563. req, _ := http.NewRequest("GET", baseURL+"/rest/svc/random/string?length=27", nil)
  564. req.Header.Set("X-API-Key", testAPIKey)
  565. resp, err := cli.Do(req)
  566. if err != nil {
  567. t.Fatal(err)
  568. }
  569. var res map[string]string
  570. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  571. t.Fatal(err)
  572. }
  573. if len(res["random"]) != 27 {
  574. t.Errorf("Expected 27 random characters, got %q of length %d", res["random"], len(res["random"]))
  575. }
  576. }
  577. func TestConfigPostOK(t *testing.T) {
  578. cfg := bytes.NewBuffer([]byte(`{
  579. "version": 15,
  580. "folders": [
  581. {
  582. "id": "foo",
  583. "path": "TestConfigPostOK"
  584. }
  585. ]
  586. }`))
  587. resp, err := testConfigPost(cfg)
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. if resp.StatusCode != http.StatusOK {
  592. t.Error("Expected 200 OK, not", resp.Status)
  593. }
  594. os.RemoveAll("TestConfigPostOK")
  595. }
  596. func TestConfigPostDupFolder(t *testing.T) {
  597. cfg := bytes.NewBuffer([]byte(`{
  598. "version": 15,
  599. "folders": [
  600. {"id": "foo"},
  601. {"id": "foo"}
  602. ]
  603. }`))
  604. resp, err := testConfigPost(cfg)
  605. if err != nil {
  606. t.Fatal(err)
  607. }
  608. if resp.StatusCode != http.StatusBadRequest {
  609. t.Error("Expected 400 Bad Request, not", resp.Status)
  610. }
  611. }
  612. func testConfigPost(data io.Reader) (*http.Response, error) {
  613. const testAPIKey = "foobarbaz"
  614. cfg := new(mockedConfig)
  615. cfg.gui.APIKey = testAPIKey
  616. baseURL, err := startHTTP(cfg)
  617. if err != nil {
  618. return nil, err
  619. }
  620. cli := &http.Client{
  621. Timeout: time.Second,
  622. }
  623. req, _ := http.NewRequest("POST", baseURL+"/rest/system/config", data)
  624. req.Header.Set("X-API-Key", testAPIKey)
  625. return cli.Do(req)
  626. }
  627. func TestHostCheck(t *testing.T) {
  628. // An API service bound to localhost should reject non-localhost host Headers
  629. cfg := new(mockedConfig)
  630. cfg.gui.RawAddress = "127.0.0.1:0"
  631. baseURL, err := startHTTP(cfg)
  632. if err != nil {
  633. t.Fatal(err)
  634. }
  635. // A normal HTTP get to the localhost-bound service should succeed
  636. resp, err := http.Get(baseURL)
  637. if err != nil {
  638. t.Fatal(err)
  639. }
  640. resp.Body.Close()
  641. if resp.StatusCode != http.StatusOK {
  642. t.Error("Regular HTTP get: expected 200 OK, not", resp.Status)
  643. }
  644. // A request with a suspicious Host header should fail
  645. req, _ := http.NewRequest("GET", baseURL, nil)
  646. req.Host = "example.com"
  647. resp, err = http.DefaultClient.Do(req)
  648. if err != nil {
  649. t.Fatal(err)
  650. }
  651. resp.Body.Close()
  652. if resp.StatusCode != http.StatusForbidden {
  653. t.Error("Suspicious Host header: expected 403 Forbidden, not", resp.Status)
  654. }
  655. // A request with an explicit "localhost:8384" Host header should pass
  656. req, _ = http.NewRequest("GET", baseURL, nil)
  657. req.Host = "localhost:8384"
  658. resp, err = http.DefaultClient.Do(req)
  659. if err != nil {
  660. t.Fatal(err)
  661. }
  662. resp.Body.Close()
  663. if resp.StatusCode != http.StatusOK {
  664. t.Error("Explicit localhost:8384: expected 200 OK, not", resp.Status)
  665. }
  666. // A request with an explicit "localhost" Host header (no port) should pass
  667. req, _ = http.NewRequest("GET", baseURL, nil)
  668. req.Host = "localhost"
  669. resp, err = http.DefaultClient.Do(req)
  670. if err != nil {
  671. t.Fatal(err)
  672. }
  673. resp.Body.Close()
  674. if resp.StatusCode != http.StatusOK {
  675. t.Error("Explicit localhost: expected 200 OK, not", resp.Status)
  676. }
  677. // A server with InsecureSkipHostCheck set behaves differently
  678. cfg = new(mockedConfig)
  679. cfg.gui.RawAddress = "127.0.0.1:0"
  680. cfg.gui.InsecureSkipHostCheck = true
  681. baseURL, err = startHTTP(cfg)
  682. if err != nil {
  683. t.Fatal(err)
  684. }
  685. // A request with a suspicious Host header should be allowed
  686. req, _ = http.NewRequest("GET", baseURL, nil)
  687. req.Host = "example.com"
  688. resp, err = http.DefaultClient.Do(req)
  689. if err != nil {
  690. t.Fatal(err)
  691. }
  692. resp.Body.Close()
  693. if resp.StatusCode != http.StatusOK {
  694. t.Error("Incorrect host header, check disabled: expected 200 OK, not", resp.Status)
  695. }
  696. // A server bound to a wildcard address also doesn't do the check
  697. cfg = new(mockedConfig)
  698. cfg.gui.RawAddress = "0.0.0.0:0"
  699. cfg.gui.InsecureSkipHostCheck = true
  700. baseURL, err = startHTTP(cfg)
  701. if err != nil {
  702. t.Fatal(err)
  703. }
  704. // A request with a suspicious Host header should be allowed
  705. req, _ = http.NewRequest("GET", baseURL, nil)
  706. req.Host = "example.com"
  707. resp, err = http.DefaultClient.Do(req)
  708. if err != nil {
  709. t.Fatal(err)
  710. }
  711. resp.Body.Close()
  712. if resp.StatusCode != http.StatusOK {
  713. t.Error("Incorrect host header, wildcard bound: expected 200 OK, not", resp.Status)
  714. }
  715. // This should all work over IPv6 as well
  716. cfg = new(mockedConfig)
  717. cfg.gui.RawAddress = "[::1]:0"
  718. baseURL, err = startHTTP(cfg)
  719. if err != nil {
  720. t.Fatal(err)
  721. }
  722. // A normal HTTP get to the localhost-bound service should succeed
  723. resp, err = http.Get(baseURL)
  724. if err != nil {
  725. t.Fatal(err)
  726. }
  727. resp.Body.Close()
  728. if resp.StatusCode != http.StatusOK {
  729. t.Error("Regular HTTP get (IPv6): expected 200 OK, not", resp.Status)
  730. }
  731. // A request with a suspicious Host header should fail
  732. req, _ = http.NewRequest("GET", baseURL, nil)
  733. req.Host = "example.com"
  734. resp, err = http.DefaultClient.Do(req)
  735. if err != nil {
  736. t.Fatal(err)
  737. }
  738. resp.Body.Close()
  739. if resp.StatusCode != http.StatusForbidden {
  740. t.Error("Suspicious Host header (IPv6): expected 403 Forbidden, not", resp.Status)
  741. }
  742. // A request with an explicit "localhost:8384" Host header should pass
  743. req, _ = http.NewRequest("GET", baseURL, nil)
  744. req.Host = "localhost:8384"
  745. resp, err = http.DefaultClient.Do(req)
  746. if err != nil {
  747. t.Fatal(err)
  748. }
  749. resp.Body.Close()
  750. if resp.StatusCode != http.StatusOK {
  751. t.Error("Explicit localhost:8384 (IPv6): expected 200 OK, not", resp.Status)
  752. }
  753. }
  754. func TestAddressIsLocalhost(t *testing.T) {
  755. testcases := []struct {
  756. address string
  757. result bool
  758. }{
  759. // These are all valid localhost addresses
  760. {"localhost", true},
  761. {"LOCALHOST", true},
  762. {"localhost.", true},
  763. {"::1", true},
  764. {"127.0.0.1", true},
  765. {"127.23.45.56", true},
  766. {"localhost:8080", true},
  767. {"LOCALHOST:8000", true},
  768. {"localhost.:8080", true},
  769. {"[::1]:8080", true},
  770. {"127.0.0.1:8080", true},
  771. {"127.23.45.56:8080", true},
  772. // These are all non-localhost addresses
  773. {"example.com", false},
  774. {"example.com:8080", false},
  775. {"localhost.com", false},
  776. {"localhost.com:8080", false},
  777. {"www.localhost", false},
  778. {"www.localhost:8080", false},
  779. {"192.0.2.10", false},
  780. {"192.0.2.10:8080", false},
  781. {"0.0.0.0", false},
  782. {"0.0.0.0:8080", false},
  783. {"::", false},
  784. {"[::]:8080", false},
  785. {":8080", false},
  786. }
  787. for _, tc := range testcases {
  788. result := addressIsLocalhost(tc.address)
  789. if result != tc.result {
  790. t.Errorf("addressIsLocalhost(%q)=%v, expected %v", tc.address, result, tc.result)
  791. }
  792. }
  793. }
  794. func TestAccessControlAllowOriginHeader(t *testing.T) {
  795. const testAPIKey = "foobarbaz"
  796. cfg := new(mockedConfig)
  797. cfg.gui.APIKey = testAPIKey
  798. baseURL, err := startHTTP(cfg)
  799. if err != nil {
  800. t.Fatal(err)
  801. }
  802. cli := &http.Client{
  803. Timeout: time.Second,
  804. }
  805. req, _ := http.NewRequest("GET", baseURL+"/rest/system/status", nil)
  806. req.Header.Set("X-API-Key", testAPIKey)
  807. resp, err := cli.Do(req)
  808. if err != nil {
  809. t.Fatal(err)
  810. }
  811. resp.Body.Close()
  812. if resp.StatusCode != http.StatusOK {
  813. t.Fatal("GET on /rest/system/status should succeed, not", resp.Status)
  814. }
  815. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  816. t.Fatal("GET on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  817. }
  818. }
  819. func TestOptionsRequest(t *testing.T) {
  820. const testAPIKey = "foobarbaz"
  821. cfg := new(mockedConfig)
  822. cfg.gui.APIKey = testAPIKey
  823. baseURL, err := startHTTP(cfg)
  824. if err != nil {
  825. t.Fatal(err)
  826. }
  827. cli := &http.Client{
  828. Timeout: time.Second,
  829. }
  830. req, _ := http.NewRequest("OPTIONS", baseURL+"/rest/system/status", nil)
  831. resp, err := cli.Do(req)
  832. if err != nil {
  833. t.Fatal(err)
  834. }
  835. resp.Body.Close()
  836. if resp.StatusCode != http.StatusNoContent {
  837. t.Fatal("OPTIONS on /rest/system/status should succeed, not", resp.Status)
  838. }
  839. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  840. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  841. }
  842. if resp.Header.Get("Access-Control-Allow-Methods") != "GET, POST" {
  843. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Methods: GET, POST' header")
  844. }
  845. if resp.Header.Get("Access-Control-Allow-Headers") != "Content-Type, X-API-Key" {
  846. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Headers: Content-Type, X-API-KEY' header")
  847. }
  848. }
  849. func TestEventMasks(t *testing.T) {
  850. cfg := new(mockedConfig)
  851. defSub := new(mockedEventSub)
  852. diskSub := new(mockedEventSub)
  853. svc := New(protocol.LocalDeviceID, cfg, "", "syncthing", nil, defSub, diskSub, events.NoopLogger, nil, nil, nil, nil, nil, nil, nil, nil, false).(*service)
  854. defer os.Remove(token)
  855. if mask := svc.getEventMask(""); mask != DefaultEventMask {
  856. t.Errorf("incorrect default mask %x != %x", int64(mask), int64(DefaultEventMask))
  857. }
  858. expected := events.FolderSummary | events.LocalChangeDetected
  859. if mask := svc.getEventMask("FolderSummary,LocalChangeDetected"); mask != expected {
  860. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  861. }
  862. expected = 0
  863. if mask := svc.getEventMask("WeirdEvent,something else that doesn't exist"); mask != expected {
  864. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  865. }
  866. if res := svc.getEventSub(DefaultEventMask); res != defSub {
  867. t.Errorf("should have returned the given default event sub")
  868. }
  869. if res := svc.getEventSub(DiskEventMask); res != diskSub {
  870. t.Errorf("should have returned the given disk event sub")
  871. }
  872. if res := svc.getEventSub(events.LocalIndexUpdated); res == nil || res == defSub || res == diskSub {
  873. t.Errorf("should have returned a valid, non-default event sub")
  874. }
  875. }
  876. func TestBrowse(t *testing.T) {
  877. pathSep := string(os.PathSeparator)
  878. tmpDir, err := ioutil.TempDir("", "syncthing")
  879. if err != nil {
  880. t.Fatal(err)
  881. }
  882. defer os.RemoveAll(tmpDir)
  883. if err := os.Mkdir(filepath.Join(tmpDir, "dir"), 0755); err != nil {
  884. t.Fatal(err)
  885. }
  886. if err := ioutil.WriteFile(filepath.Join(tmpDir, "file"), []byte("hello"), 0644); err != nil {
  887. t.Fatal(err)
  888. }
  889. if err := os.Mkdir(filepath.Join(tmpDir, "MiXEDCase"), 0755); err != nil {
  890. t.Fatal(err)
  891. }
  892. // We expect completion to return the full path to the completed
  893. // directory, with an ending slash.
  894. dirPath := filepath.Join(tmpDir, "dir") + pathSep
  895. mixedCaseDirPath := filepath.Join(tmpDir, "MiXEDCase") + pathSep
  896. cases := []struct {
  897. current string
  898. returns []string
  899. }{
  900. // The direcotory without slash is completed to one with slash.
  901. {tmpDir, []string{tmpDir + pathSep}},
  902. // With slash it's completed to its contents.
  903. // Dirs are given pathSeps.
  904. // Files are not returned.
  905. {tmpDir + pathSep, []string{mixedCaseDirPath, dirPath}},
  906. // Globbing is automatic based on prefix.
  907. {tmpDir + pathSep + "d", []string{dirPath}},
  908. {tmpDir + pathSep + "di", []string{dirPath}},
  909. {tmpDir + pathSep + "dir", []string{dirPath}},
  910. {tmpDir + pathSep + "f", nil},
  911. {tmpDir + pathSep + "q", nil},
  912. // Globbing is case-insensitve
  913. {tmpDir + pathSep + "mixed", []string{mixedCaseDirPath}},
  914. }
  915. for _, tc := range cases {
  916. ret := browseFiles(tc.current, fs.FilesystemTypeBasic)
  917. if !equalStrings(ret, tc.returns) {
  918. t.Errorf("browseFiles(%q) => %q, expected %q", tc.current, ret, tc.returns)
  919. }
  920. }
  921. }
  922. func TestPrefixMatch(t *testing.T) {
  923. cases := []struct {
  924. s string
  925. prefix string
  926. expected int
  927. }{
  928. {"aaaA", "aaa", matchExact},
  929. {"AAAX", "BBB", noMatch},
  930. {"AAAX", "aAa", matchCaseIns},
  931. {"äÜX", "äü", matchCaseIns},
  932. }
  933. for _, tc := range cases {
  934. ret := checkPrefixMatch(tc.s, tc.prefix)
  935. if ret != tc.expected {
  936. t.Errorf("checkPrefixMatch(%q, %q) => %v, expected %v", tc.s, tc.prefix, ret, tc.expected)
  937. }
  938. }
  939. }
  940. func equalStrings(a, b []string) bool {
  941. if len(a) != len(b) {
  942. return false
  943. }
  944. for i := range a {
  945. if a[i] != b[i] {
  946. return false
  947. }
  948. }
  949. return true
  950. }