api_test.go 29 KB

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