api_test.go 46 KB

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