api_test.go 29 KB

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