api_test.go 46 KB

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