api_test.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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/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, 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.Bytes()
  128. buf = new(bytes.Buffer)
  129. gw = gzip.NewWriter(buf)
  130. gw.Write([]byte("foo"))
  131. gw.Close()
  132. foo := buf.Bytes()
  133. e := &staticsServer{
  134. theme: "foo",
  135. mut: sync.NewRWMutex(),
  136. assetDir: "testdata",
  137. assets: map[string][]byte{
  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, err := startHTTP(cfg)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. cases := []httpTestCase{
  202. // /rest/db
  203. {
  204. URL: "/rest/db/completion?device=" + protocol.LocalDeviceID.String() + "&folder=default",
  205. Code: 200,
  206. Type: "application/json",
  207. Prefix: "{",
  208. },
  209. {
  210. URL: "/rest/db/file?folder=default&file=something",
  211. Code: 404,
  212. },
  213. {
  214. URL: "/rest/db/ignores?folder=default",
  215. Code: 200,
  216. Type: "application/json",
  217. Prefix: "{",
  218. },
  219. {
  220. URL: "/rest/db/need?folder=default",
  221. Code: 200,
  222. Type: "application/json",
  223. Prefix: "{",
  224. },
  225. {
  226. URL: "/rest/db/status?folder=default",
  227. Code: 200,
  228. Type: "application/json",
  229. Prefix: "{",
  230. },
  231. {
  232. URL: "/rest/db/browse?folder=default",
  233. Code: 200,
  234. Type: "application/json",
  235. Prefix: "null",
  236. },
  237. // /rest/stats
  238. {
  239. URL: "/rest/stats/device",
  240. Code: 200,
  241. Type: "application/json",
  242. Prefix: "null",
  243. },
  244. {
  245. URL: "/rest/stats/folder",
  246. Code: 200,
  247. Type: "application/json",
  248. Prefix: "null",
  249. },
  250. // /rest/svc
  251. {
  252. URL: "/rest/svc/deviceid?id=" + protocol.LocalDeviceID.String(),
  253. Code: 200,
  254. Type: "application/json",
  255. Prefix: "{",
  256. },
  257. {
  258. URL: "/rest/svc/lang",
  259. Code: 200,
  260. Type: "application/json",
  261. Prefix: "[",
  262. },
  263. {
  264. URL: "/rest/svc/report",
  265. Code: 200,
  266. Type: "application/json",
  267. Prefix: "{",
  268. Timeout: 5 * time.Second,
  269. },
  270. // /rest/system
  271. {
  272. URL: "/rest/system/browse?current=~",
  273. Code: 200,
  274. Type: "application/json",
  275. Prefix: "[",
  276. },
  277. {
  278. URL: "/rest/system/config",
  279. Code: 200,
  280. Type: "application/json",
  281. Prefix: "{",
  282. },
  283. {
  284. URL: "/rest/system/config/insync",
  285. Code: 200,
  286. Type: "application/json",
  287. Prefix: "{",
  288. },
  289. {
  290. URL: "/rest/system/connections",
  291. Code: 200,
  292. Type: "application/json",
  293. Prefix: "null",
  294. },
  295. {
  296. URL: "/rest/system/discovery",
  297. Code: 200,
  298. Type: "application/json",
  299. Prefix: "{",
  300. },
  301. {
  302. URL: "/rest/system/error?since=0",
  303. Code: 200,
  304. Type: "application/json",
  305. Prefix: "{",
  306. },
  307. {
  308. URL: "/rest/system/ping",
  309. Code: 200,
  310. Type: "application/json",
  311. Prefix: "{",
  312. },
  313. {
  314. URL: "/rest/system/status",
  315. Code: 200,
  316. Type: "application/json",
  317. Prefix: "{",
  318. },
  319. {
  320. URL: "/rest/system/version",
  321. Code: 200,
  322. Type: "application/json",
  323. Prefix: "{",
  324. },
  325. {
  326. URL: "/rest/system/debug",
  327. Code: 200,
  328. Type: "application/json",
  329. Prefix: "{",
  330. },
  331. {
  332. URL: "/rest/system/log?since=0",
  333. Code: 200,
  334. Type: "application/json",
  335. Prefix: "{",
  336. },
  337. {
  338. URL: "/rest/system/log.txt?since=0",
  339. Code: 200,
  340. Type: "text/plain",
  341. Prefix: "",
  342. },
  343. }
  344. for _, tc := range cases {
  345. t.Log("Testing", tc.URL, "...")
  346. testHTTPRequest(t, baseURL, tc, testAPIKey)
  347. }
  348. }
  349. // testHTTPRequest tries the given test case, comparing the result code,
  350. // content type, and result prefix.
  351. func testHTTPRequest(t *testing.T, baseURL string, tc httpTestCase, apikey string) {
  352. timeout := time.Second
  353. if tc.Timeout > 0 {
  354. timeout = tc.Timeout
  355. }
  356. cli := &http.Client{
  357. Timeout: timeout,
  358. }
  359. req, err := http.NewRequest("GET", baseURL+tc.URL, nil)
  360. if err != nil {
  361. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  362. return
  363. }
  364. req.Header.Set("X-API-Key", apikey)
  365. resp, err := cli.Do(req)
  366. if err != nil {
  367. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  368. return
  369. }
  370. defer resp.Body.Close()
  371. if resp.StatusCode != tc.Code {
  372. t.Errorf("Get on %s should have returned status code %d, not %s", tc.URL, tc.Code, resp.Status)
  373. return
  374. }
  375. ct := resp.Header.Get("Content-Type")
  376. if !strings.HasPrefix(ct, tc.Type) {
  377. t.Errorf("The content type on %s should be %q, not %q", tc.URL, tc.Type, ct)
  378. return
  379. }
  380. data, err := ioutil.ReadAll(resp.Body)
  381. if err != nil {
  382. t.Errorf("Unexpected error reading %s: %v", tc.URL, err)
  383. return
  384. }
  385. if !bytes.HasPrefix(data, []byte(tc.Prefix)) {
  386. t.Errorf("Returned data from %s does not have prefix %q: %s", tc.URL, tc.Prefix, data)
  387. return
  388. }
  389. }
  390. func TestHTTPLogin(t *testing.T) {
  391. t.Parallel()
  392. cfg := new(mockedConfig)
  393. cfg.gui.User = "üser"
  394. cfg.gui.Password = "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq" // bcrypt of "räksmörgås" in UTF-8
  395. baseURL, err := startHTTP(cfg)
  396. if err != nil {
  397. t.Fatal(err)
  398. }
  399. // Verify rejection when not using authorization
  400. req, _ := http.NewRequest("GET", baseURL, nil)
  401. resp, err := http.DefaultClient.Do(req)
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. if resp.StatusCode != http.StatusUnauthorized {
  406. t.Errorf("Unexpected non-401 return code %d for unauthed request", resp.StatusCode)
  407. }
  408. // Verify that incorrect password is rejected
  409. req.SetBasicAuth("üser", "rksmrgs")
  410. resp, err = http.DefaultClient.Do(req)
  411. if err != nil {
  412. t.Fatal(err)
  413. }
  414. if resp.StatusCode != http.StatusUnauthorized {
  415. t.Errorf("Unexpected non-401 return code %d for incorrect password", resp.StatusCode)
  416. }
  417. // Verify that incorrect username is rejected
  418. req.SetBasicAuth("user", "räksmörgås") // string literals in Go source code are in UTF-8
  419. resp, err = http.DefaultClient.Do(req)
  420. if err != nil {
  421. t.Fatal(err)
  422. }
  423. if resp.StatusCode != http.StatusUnauthorized {
  424. t.Errorf("Unexpected non-401 return code %d for incorrect username", resp.StatusCode)
  425. }
  426. // Verify that UTF-8 auth works
  427. req.SetBasicAuth("üser", "räksmörgås") // string literals in Go source code are in UTF-8
  428. resp, err = http.DefaultClient.Do(req)
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. if resp.StatusCode != http.StatusOK {
  433. t.Errorf("Unexpected non-200 return code %d for authed request (UTF-8)", resp.StatusCode)
  434. }
  435. // Verify that ISO-8859-1 auth
  436. req.SetBasicAuth("\xfcser", "r\xe4ksm\xf6rg\xe5s") // escaped ISO-8859-1
  437. resp, err = http.DefaultClient.Do(req)
  438. if err != nil {
  439. t.Fatal(err)
  440. }
  441. if resp.StatusCode != http.StatusOK {
  442. t.Errorf("Unexpected non-200 return code %d for authed request (ISO-8859-1)", resp.StatusCode)
  443. }
  444. }
  445. func startHTTP(cfg *mockedConfig) (string, error) {
  446. m := new(mockedModel)
  447. assetDir := "../../gui"
  448. eventSub := new(mockedEventSub)
  449. diskEventSub := new(mockedEventSub)
  450. discoverer := new(mockedCachingMux)
  451. connections := new(mockedConnections)
  452. errorLog := new(mockedLoggerRecorder)
  453. systemLog := new(mockedLoggerRecorder)
  454. cpu := new(mockedCPUService)
  455. addrChan := make(chan string)
  456. // Instantiate the API service
  457. urService := ur.New(cfg, m, connections, false)
  458. summaryService := model.NewFolderSummaryService(cfg, m, protocol.LocalDeviceID, events.NoopLogger)
  459. svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, summaryService, errorLog, systemLog, cpu, 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. return "", fmt.Errorf("Weird address from API service: %v", err)
  473. }
  474. host, _, _ := net.SplitHostPort(cfg.gui.RawAddress)
  475. if host == "" || host == "0.0.0.0" {
  476. host = "127.0.0.1"
  477. }
  478. baseURL := fmt.Sprintf("http://%s", net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port)))
  479. return baseURL, nil
  480. }
  481. func TestCSRFRequired(t *testing.T) {
  482. t.Parallel()
  483. const testAPIKey = "foobarbaz"
  484. cfg := new(mockedConfig)
  485. cfg.gui.APIKey = testAPIKey
  486. baseURL, err := startHTTP(cfg)
  487. if err != nil {
  488. t.Fatal("Unexpected error from getting base URL:", err)
  489. }
  490. cli := &http.Client{
  491. Timeout: time.Second,
  492. }
  493. // Getting the base URL (i.e. "/") should succeed.
  494. resp, err := cli.Get(baseURL)
  495. if err != nil {
  496. t.Fatal("Unexpected error from getting base URL:", err)
  497. }
  498. resp.Body.Close()
  499. if resp.StatusCode != http.StatusOK {
  500. t.Fatal("Getting base URL should succeed, not", resp.Status)
  501. }
  502. // Find the returned CSRF token for future use
  503. var csrfTokenName, csrfTokenValue string
  504. for _, cookie := range resp.Cookies() {
  505. if strings.HasPrefix(cookie.Name, "CSRF-Token") {
  506. csrfTokenName = cookie.Name
  507. csrfTokenValue = cookie.Value
  508. break
  509. }
  510. }
  511. // Calling on /rest without a token should fail
  512. resp, err = cli.Get(baseURL + "/rest/system/config")
  513. if err != nil {
  514. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  515. }
  516. resp.Body.Close()
  517. if resp.StatusCode != http.StatusForbidden {
  518. t.Fatal("Getting /rest/system/config without CSRF token should fail, not", resp.Status)
  519. }
  520. // Calling on /rest with a token should succeed
  521. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  522. req.Header.Set("X-"+csrfTokenName, csrfTokenValue)
  523. resp, err = cli.Do(req)
  524. if err != nil {
  525. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  526. }
  527. resp.Body.Close()
  528. if resp.StatusCode != http.StatusOK {
  529. t.Fatal("Getting /rest/system/config with CSRF token should succeed, not", resp.Status)
  530. }
  531. // Calling on /rest with the API key should succeed
  532. req, _ = http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  533. req.Header.Set("X-API-Key", testAPIKey)
  534. resp, err = cli.Do(req)
  535. if err != nil {
  536. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  537. }
  538. resp.Body.Close()
  539. if resp.StatusCode != http.StatusOK {
  540. t.Fatal("Getting /rest/system/config with API key should succeed, not", resp.Status)
  541. }
  542. }
  543. func TestRandomString(t *testing.T) {
  544. t.Parallel()
  545. const testAPIKey = "foobarbaz"
  546. cfg := new(mockedConfig)
  547. cfg.gui.APIKey = testAPIKey
  548. baseURL, err := startHTTP(cfg)
  549. if err != nil {
  550. t.Fatal(err)
  551. }
  552. cli := &http.Client{
  553. Timeout: time.Second,
  554. }
  555. // The default should be to return a 32 character random string
  556. for _, url := range []string{"/rest/svc/random/string", "/rest/svc/random/string?length=-1", "/rest/svc/random/string?length=yo"} {
  557. req, _ := http.NewRequest("GET", baseURL+url, nil)
  558. req.Header.Set("X-API-Key", testAPIKey)
  559. resp, err := cli.Do(req)
  560. if err != nil {
  561. t.Fatal(err)
  562. }
  563. var res map[string]string
  564. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  565. t.Fatal(err)
  566. }
  567. if len(res["random"]) != 32 {
  568. t.Errorf("Expected 32 random characters, got %q of length %d", res["random"], len(res["random"]))
  569. }
  570. }
  571. // We can ask for a different length if we like
  572. req, _ := http.NewRequest("GET", baseURL+"/rest/svc/random/string?length=27", nil)
  573. req.Header.Set("X-API-Key", testAPIKey)
  574. resp, err := cli.Do(req)
  575. if err != nil {
  576. t.Fatal(err)
  577. }
  578. var res map[string]string
  579. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  580. t.Fatal(err)
  581. }
  582. if len(res["random"]) != 27 {
  583. t.Errorf("Expected 27 random characters, got %q of length %d", res["random"], len(res["random"]))
  584. }
  585. }
  586. func TestConfigPostOK(t *testing.T) {
  587. t.Parallel()
  588. cfg := bytes.NewBuffer([]byte(`{
  589. "version": 15,
  590. "folders": [
  591. {
  592. "id": "foo",
  593. "path": "TestConfigPostOK"
  594. }
  595. ]
  596. }`))
  597. resp, err := testConfigPost(cfg)
  598. if err != nil {
  599. t.Fatal(err)
  600. }
  601. if resp.StatusCode != http.StatusOK {
  602. t.Error("Expected 200 OK, not", resp.Status)
  603. }
  604. os.RemoveAll("TestConfigPostOK")
  605. }
  606. func TestConfigPostDupFolder(t *testing.T) {
  607. t.Parallel()
  608. cfg := bytes.NewBuffer([]byte(`{
  609. "version": 15,
  610. "folders": [
  611. {"id": "foo"},
  612. {"id": "foo"}
  613. ]
  614. }`))
  615. resp, err := testConfigPost(cfg)
  616. if err != nil {
  617. t.Fatal(err)
  618. }
  619. if resp.StatusCode != http.StatusBadRequest {
  620. t.Error("Expected 400 Bad Request, not", resp.Status)
  621. }
  622. }
  623. func testConfigPost(data io.Reader) (*http.Response, error) {
  624. const testAPIKey = "foobarbaz"
  625. cfg := new(mockedConfig)
  626. cfg.gui.APIKey = testAPIKey
  627. baseURL, err := startHTTP(cfg)
  628. if err != nil {
  629. return nil, err
  630. }
  631. cli := &http.Client{
  632. Timeout: time.Second,
  633. }
  634. req, _ := http.NewRequest("POST", baseURL+"/rest/system/config", data)
  635. req.Header.Set("X-API-Key", testAPIKey)
  636. return cli.Do(req)
  637. }
  638. func TestHostCheck(t *testing.T) {
  639. t.Parallel()
  640. // An API service bound to localhost should reject non-localhost host Headers
  641. cfg := new(mockedConfig)
  642. cfg.gui.RawAddress = "127.0.0.1:0"
  643. baseURL, err := startHTTP(cfg)
  644. if err != nil {
  645. t.Fatal(err)
  646. }
  647. // A normal HTTP get to the localhost-bound service should succeed
  648. resp, err := http.Get(baseURL)
  649. if err != nil {
  650. t.Fatal(err)
  651. }
  652. resp.Body.Close()
  653. if resp.StatusCode != http.StatusOK {
  654. t.Error("Regular HTTP get: expected 200 OK, not", resp.Status)
  655. }
  656. // A request with a suspicious Host header should fail
  657. req, _ := http.NewRequest("GET", baseURL, nil)
  658. req.Host = "example.com"
  659. resp, err = http.DefaultClient.Do(req)
  660. if err != nil {
  661. t.Fatal(err)
  662. }
  663. resp.Body.Close()
  664. if resp.StatusCode != http.StatusForbidden {
  665. t.Error("Suspicious Host header: expected 403 Forbidden, not", resp.Status)
  666. }
  667. // A request with an explicit "localhost:8384" Host header should pass
  668. req, _ = http.NewRequest("GET", baseURL, nil)
  669. req.Host = "localhost:8384"
  670. resp, err = http.DefaultClient.Do(req)
  671. if err != nil {
  672. t.Fatal(err)
  673. }
  674. resp.Body.Close()
  675. if resp.StatusCode != http.StatusOK {
  676. t.Error("Explicit localhost:8384: expected 200 OK, not", resp.Status)
  677. }
  678. // A request with an explicit "localhost" Host header (no port) should pass
  679. req, _ = http.NewRequest("GET", baseURL, nil)
  680. req.Host = "localhost"
  681. resp, err = http.DefaultClient.Do(req)
  682. if err != nil {
  683. t.Fatal(err)
  684. }
  685. resp.Body.Close()
  686. if resp.StatusCode != http.StatusOK {
  687. t.Error("Explicit localhost: expected 200 OK, not", resp.Status)
  688. }
  689. // A server with InsecureSkipHostCheck set behaves differently
  690. cfg = new(mockedConfig)
  691. cfg.gui.RawAddress = "127.0.0.1:0"
  692. cfg.gui.InsecureSkipHostCheck = true
  693. baseURL, err = startHTTP(cfg)
  694. if err != nil {
  695. t.Fatal(err)
  696. }
  697. // A request with a suspicious Host header should be allowed
  698. req, _ = http.NewRequest("GET", baseURL, nil)
  699. req.Host = "example.com"
  700. resp, err = http.DefaultClient.Do(req)
  701. if err != nil {
  702. t.Fatal(err)
  703. }
  704. resp.Body.Close()
  705. if resp.StatusCode != http.StatusOK {
  706. t.Error("Incorrect host header, check disabled: expected 200 OK, not", resp.Status)
  707. }
  708. // A server bound to a wildcard address also doesn't do the check
  709. cfg = new(mockedConfig)
  710. cfg.gui.RawAddress = "0.0.0.0:0"
  711. cfg.gui.InsecureSkipHostCheck = true
  712. baseURL, err = startHTTP(cfg)
  713. if err != nil {
  714. t.Fatal(err)
  715. }
  716. // A request with a suspicious Host header should be allowed
  717. req, _ = http.NewRequest("GET", baseURL, nil)
  718. req.Host = "example.com"
  719. resp, err = http.DefaultClient.Do(req)
  720. if err != nil {
  721. t.Fatal(err)
  722. }
  723. resp.Body.Close()
  724. if resp.StatusCode != http.StatusOK {
  725. t.Error("Incorrect host header, wildcard bound: expected 200 OK, not", resp.Status)
  726. }
  727. // This should all work over IPv6 as well
  728. if runningInContainer() {
  729. // Working IPv6 in Docker can't be taken for granted.
  730. return
  731. }
  732. cfg = new(mockedConfig)
  733. cfg.gui.RawAddress = "[::1]:0"
  734. baseURL, err = startHTTP(cfg)
  735. if err != nil {
  736. t.Fatal(err)
  737. }
  738. // A normal HTTP get to the localhost-bound service should succeed
  739. resp, err = http.Get(baseURL)
  740. if err != nil {
  741. t.Fatal(err)
  742. }
  743. resp.Body.Close()
  744. if resp.StatusCode != http.StatusOK {
  745. t.Error("Regular HTTP get (IPv6): expected 200 OK, not", resp.Status)
  746. }
  747. // A request with a suspicious Host header should fail
  748. req, _ = http.NewRequest("GET", baseURL, nil)
  749. req.Host = "example.com"
  750. resp, err = http.DefaultClient.Do(req)
  751. if err != nil {
  752. t.Fatal(err)
  753. }
  754. resp.Body.Close()
  755. if resp.StatusCode != http.StatusForbidden {
  756. t.Error("Suspicious Host header (IPv6): expected 403 Forbidden, not", resp.Status)
  757. }
  758. // A request with an explicit "localhost:8384" Host header should pass
  759. req, _ = http.NewRequest("GET", baseURL, nil)
  760. req.Host = "localhost:8384"
  761. resp, err = http.DefaultClient.Do(req)
  762. if err != nil {
  763. t.Fatal(err)
  764. }
  765. resp.Body.Close()
  766. if resp.StatusCode != http.StatusOK {
  767. t.Error("Explicit localhost:8384 (IPv6): expected 200 OK, not", resp.Status)
  768. }
  769. }
  770. func TestAddressIsLocalhost(t *testing.T) {
  771. t.Parallel()
  772. testcases := []struct {
  773. address string
  774. result bool
  775. }{
  776. // These are all valid localhost addresses
  777. {"localhost", true},
  778. {"LOCALHOST", true},
  779. {"localhost.", true},
  780. {"::1", true},
  781. {"127.0.0.1", true},
  782. {"127.23.45.56", true},
  783. {"localhost:8080", true},
  784. {"LOCALHOST:8000", true},
  785. {"localhost.:8080", true},
  786. {"[::1]:8080", true},
  787. {"127.0.0.1:8080", true},
  788. {"127.23.45.56:8080", true},
  789. // These are all non-localhost addresses
  790. {"example.com", false},
  791. {"example.com:8080", false},
  792. {"localhost.com", false},
  793. {"localhost.com:8080", false},
  794. {"www.localhost", false},
  795. {"www.localhost:8080", false},
  796. {"192.0.2.10", false},
  797. {"192.0.2.10:8080", false},
  798. {"0.0.0.0", false},
  799. {"0.0.0.0:8080", false},
  800. {"::", false},
  801. {"[::]:8080", false},
  802. {":8080", false},
  803. }
  804. for _, tc := range testcases {
  805. result := addressIsLocalhost(tc.address)
  806. if result != tc.result {
  807. t.Errorf("addressIsLocalhost(%q)=%v, expected %v", tc.address, result, tc.result)
  808. }
  809. }
  810. }
  811. func TestAccessControlAllowOriginHeader(t *testing.T) {
  812. t.Parallel()
  813. const testAPIKey = "foobarbaz"
  814. cfg := new(mockedConfig)
  815. cfg.gui.APIKey = testAPIKey
  816. baseURL, err := startHTTP(cfg)
  817. if err != nil {
  818. t.Fatal(err)
  819. }
  820. cli := &http.Client{
  821. Timeout: time.Second,
  822. }
  823. req, _ := http.NewRequest("GET", baseURL+"/rest/system/status", nil)
  824. req.Header.Set("X-API-Key", testAPIKey)
  825. resp, err := cli.Do(req)
  826. if err != nil {
  827. t.Fatal(err)
  828. }
  829. resp.Body.Close()
  830. if resp.StatusCode != http.StatusOK {
  831. t.Fatal("GET on /rest/system/status should succeed, not", resp.Status)
  832. }
  833. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  834. t.Fatal("GET on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  835. }
  836. }
  837. func TestOptionsRequest(t *testing.T) {
  838. t.Parallel()
  839. const testAPIKey = "foobarbaz"
  840. cfg := new(mockedConfig)
  841. cfg.gui.APIKey = testAPIKey
  842. baseURL, err := startHTTP(cfg)
  843. if err != nil {
  844. t.Fatal(err)
  845. }
  846. cli := &http.Client{
  847. Timeout: time.Second,
  848. }
  849. req, _ := http.NewRequest("OPTIONS", baseURL+"/rest/system/status", nil)
  850. resp, err := cli.Do(req)
  851. if err != nil {
  852. t.Fatal(err)
  853. }
  854. resp.Body.Close()
  855. if resp.StatusCode != http.StatusNoContent {
  856. t.Fatal("OPTIONS on /rest/system/status should succeed, not", resp.Status)
  857. }
  858. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  859. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  860. }
  861. if resp.Header.Get("Access-Control-Allow-Methods") != "GET, POST" {
  862. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Methods: GET, POST' header")
  863. }
  864. if resp.Header.Get("Access-Control-Allow-Headers") != "Content-Type, X-API-Key" {
  865. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Headers: Content-Type, X-API-KEY' header")
  866. }
  867. }
  868. func TestEventMasks(t *testing.T) {
  869. t.Parallel()
  870. cfg := new(mockedConfig)
  871. defSub := new(mockedEventSub)
  872. diskSub := new(mockedEventSub)
  873. svc := New(protocol.LocalDeviceID, cfg, "", "syncthing", nil, defSub, diskSub, events.NoopLogger, nil, nil, nil, nil, nil, nil, nil, nil, false).(*service)
  874. defer os.Remove(token)
  875. if mask := svc.getEventMask(""); mask != DefaultEventMask {
  876. t.Errorf("incorrect default mask %x != %x", int64(mask), int64(DefaultEventMask))
  877. }
  878. expected := events.FolderSummary | events.LocalChangeDetected
  879. if mask := svc.getEventMask("FolderSummary,LocalChangeDetected"); mask != expected {
  880. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  881. }
  882. expected = 0
  883. if mask := svc.getEventMask("WeirdEvent,something else that doesn't exist"); mask != expected {
  884. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  885. }
  886. if res := svc.getEventSub(DefaultEventMask); res != defSub {
  887. t.Errorf("should have returned the given default event sub")
  888. }
  889. if res := svc.getEventSub(DiskEventMask); res != diskSub {
  890. t.Errorf("should have returned the given disk event sub")
  891. }
  892. if res := svc.getEventSub(events.LocalIndexUpdated); res == nil || res == defSub || res == diskSub {
  893. t.Errorf("should have returned a valid, non-default event sub")
  894. }
  895. }
  896. func TestBrowse(t *testing.T) {
  897. t.Parallel()
  898. pathSep := string(os.PathSeparator)
  899. tmpDir, err := ioutil.TempDir("", "syncthing")
  900. if err != nil {
  901. t.Fatal(err)
  902. }
  903. defer os.RemoveAll(tmpDir)
  904. if err := os.Mkdir(filepath.Join(tmpDir, "dir"), 0755); err != nil {
  905. t.Fatal(err)
  906. }
  907. if err := ioutil.WriteFile(filepath.Join(tmpDir, "file"), []byte("hello"), 0644); err != nil {
  908. t.Fatal(err)
  909. }
  910. if err := os.Mkdir(filepath.Join(tmpDir, "MiXEDCase"), 0755); err != nil {
  911. t.Fatal(err)
  912. }
  913. // We expect completion to return the full path to the completed
  914. // directory, with an ending slash.
  915. dirPath := filepath.Join(tmpDir, "dir") + pathSep
  916. mixedCaseDirPath := filepath.Join(tmpDir, "MiXEDCase") + pathSep
  917. cases := []struct {
  918. current string
  919. returns []string
  920. }{
  921. // The direcotory without slash is completed to one with slash.
  922. {tmpDir, []string{tmpDir + pathSep}},
  923. // With slash it's completed to its contents.
  924. // Dirs are given pathSeps.
  925. // Files are not returned.
  926. {tmpDir + pathSep, []string{mixedCaseDirPath, dirPath}},
  927. // Globbing is automatic based on prefix.
  928. {tmpDir + pathSep + "d", []string{dirPath}},
  929. {tmpDir + pathSep + "di", []string{dirPath}},
  930. {tmpDir + pathSep + "dir", []string{dirPath}},
  931. {tmpDir + pathSep + "f", nil},
  932. {tmpDir + pathSep + "q", nil},
  933. // Globbing is case-insensitve
  934. {tmpDir + pathSep + "mixed", []string{mixedCaseDirPath}},
  935. }
  936. for _, tc := range cases {
  937. ret := browseFiles(tc.current, fs.FilesystemTypeBasic)
  938. if !equalStrings(ret, tc.returns) {
  939. t.Errorf("browseFiles(%q) => %q, expected %q", tc.current, ret, tc.returns)
  940. }
  941. }
  942. }
  943. func TestPrefixMatch(t *testing.T) {
  944. t.Parallel()
  945. cases := []struct {
  946. s string
  947. prefix string
  948. expected int
  949. }{
  950. {"aaaA", "aaa", matchExact},
  951. {"AAAX", "BBB", noMatch},
  952. {"AAAX", "aAa", matchCaseIns},
  953. {"äÜX", "äü", matchCaseIns},
  954. }
  955. for _, tc := range cases {
  956. ret := checkPrefixMatch(tc.s, tc.prefix)
  957. if ret != tc.expected {
  958. t.Errorf("checkPrefixMatch(%q, %q) => %v, expected %v", tc.s, tc.prefix, ret, tc.expected)
  959. }
  960. }
  961. }
  962. func equalStrings(a, b []string) bool {
  963. if len(a) != len(b) {
  964. return false
  965. }
  966. for i := range a {
  967. if a[i] != b[i] {
  968. return false
  969. }
  970. }
  971. return true
  972. }
  973. // runningInContainer returns true if we are inside Docker or LXC. It might
  974. // be prone to false negatives if things change in the future, but likely
  975. // not false positives.
  976. func runningInContainer() bool {
  977. if runtime.GOOS != "linux" {
  978. return false
  979. }
  980. bs, err := ioutil.ReadFile("/proc/1/cgroup")
  981. if err != nil {
  982. return false
  983. }
  984. if bytes.Contains(bs, []byte("/docker/")) {
  985. return true
  986. }
  987. if bytes.Contains(bs, []byte("/lxc/")) {
  988. return true
  989. }
  990. return false
  991. }