1
0

api_test.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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/thejerf/suture/v4"
  26. "github.com/syncthing/syncthing/lib/assets"
  27. "github.com/syncthing/syncthing/lib/build"
  28. "github.com/syncthing/syncthing/lib/config"
  29. connmocks "github.com/syncthing/syncthing/lib/connections/mocks"
  30. "github.com/syncthing/syncthing/lib/db"
  31. "github.com/syncthing/syncthing/lib/db/backend"
  32. discovermocks "github.com/syncthing/syncthing/lib/discover/mocks"
  33. "github.com/syncthing/syncthing/lib/events"
  34. eventmocks "github.com/syncthing/syncthing/lib/events/mocks"
  35. "github.com/syncthing/syncthing/lib/fs"
  36. "github.com/syncthing/syncthing/lib/locations"
  37. "github.com/syncthing/syncthing/lib/logger"
  38. loggermocks "github.com/syncthing/syncthing/lib/logger/mocks"
  39. "github.com/syncthing/syncthing/lib/model"
  40. modelmocks "github.com/syncthing/syncthing/lib/model/mocks"
  41. "github.com/syncthing/syncthing/lib/protocol"
  42. "github.com/syncthing/syncthing/lib/rand"
  43. "github.com/syncthing/syncthing/lib/svcutil"
  44. "github.com/syncthing/syncthing/lib/sync"
  45. "github.com/syncthing/syncthing/lib/tlsutil"
  46. "github.com/syncthing/syncthing/lib/ur"
  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. // Since running tests in parallel, the previous 1s timeout proved to be too short.
  404. // https://github.com/syncthing/syncthing/issues/9455
  405. timeout := 10 * time.Second
  406. if tc.Timeout > 0 {
  407. timeout = tc.Timeout
  408. }
  409. cli := &http.Client{
  410. Timeout: timeout,
  411. }
  412. req, err := http.NewRequest("GET", baseURL+tc.URL, nil)
  413. if err != nil {
  414. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  415. return
  416. }
  417. req.Header.Set("X-API-Key", apikey)
  418. resp, err := cli.Do(req)
  419. if err != nil {
  420. t.Errorf("Unexpected error requesting %s: %v", tc.URL, err)
  421. return
  422. }
  423. defer resp.Body.Close()
  424. if resp.StatusCode != tc.Code {
  425. t.Errorf("Get on %s should have returned status code %d, not %s", tc.URL, tc.Code, resp.Status)
  426. return
  427. }
  428. ct := resp.Header.Get("Content-Type")
  429. if !strings.HasPrefix(ct, tc.Type) {
  430. t.Errorf("The content type on %s should be %q, not %q", tc.URL, tc.Type, ct)
  431. return
  432. }
  433. data, err := io.ReadAll(resp.Body)
  434. if err != nil {
  435. t.Errorf("Unexpected error reading %s: %v", tc.URL, err)
  436. return
  437. }
  438. if !bytes.HasPrefix(data, []byte(tc.Prefix)) {
  439. t.Errorf("Returned data from %s does not have prefix %q: %s", tc.URL, tc.Prefix, data)
  440. return
  441. }
  442. }
  443. func getSessionCookie(cookies []*http.Cookie) (*http.Cookie, bool) {
  444. for _, cookie := range cookies {
  445. if cookie.MaxAge >= 0 && strings.HasPrefix(cookie.Name, "sessionid") {
  446. return cookie, true
  447. }
  448. }
  449. return nil, false
  450. }
  451. func hasSessionCookie(cookies []*http.Cookie) bool {
  452. _, ok := getSessionCookie(cookies)
  453. return ok
  454. }
  455. func hasDeleteSessionCookie(cookies []*http.Cookie) bool {
  456. for _, cookie := range cookies {
  457. if cookie.MaxAge < 0 && strings.HasPrefix(cookie.Name, "sessionid") {
  458. return true
  459. }
  460. }
  461. return false
  462. }
  463. func httpRequest(method string, url string, body any, basicAuthUsername, basicAuthPassword, xapikeyHeader, authorizationBearer, csrfTokenName, csrfTokenValue string, cookies []*http.Cookie, t *testing.T) *http.Response {
  464. t.Helper()
  465. var bodyReader io.Reader = nil
  466. if body != nil {
  467. bodyBytes, err := json.Marshal(body)
  468. if err != nil {
  469. t.Fatal(err)
  470. }
  471. bodyReader = bytes.NewReader(bodyBytes)
  472. }
  473. req, err := http.NewRequest(method, url, bodyReader)
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. if basicAuthUsername != "" || basicAuthPassword != "" {
  478. req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
  479. }
  480. if xapikeyHeader != "" {
  481. req.Header.Set("X-API-Key", xapikeyHeader)
  482. }
  483. if authorizationBearer != "" {
  484. req.Header.Set("Authorization", "Bearer "+authorizationBearer)
  485. }
  486. if csrfTokenName != "" && csrfTokenValue != "" {
  487. req.Header.Set("X-"+csrfTokenName, csrfTokenValue)
  488. }
  489. for _, cookie := range cookies {
  490. req.AddCookie(cookie)
  491. }
  492. client := http.Client{Timeout: 15 * time.Second}
  493. resp, err := client.Do(req)
  494. if err != nil {
  495. t.Fatal(err)
  496. }
  497. return resp
  498. }
  499. func httpGet(url string, basicAuthUsername, basicAuthPassword, xapikeyHeader, authorizationBearer string, cookies []*http.Cookie, t *testing.T) *http.Response {
  500. t.Helper()
  501. return httpRequest(http.MethodGet, url, nil, basicAuthUsername, basicAuthPassword, xapikeyHeader, authorizationBearer, "", "", cookies, t)
  502. }
  503. func httpPost(url string, body map[string]string, cookies []*http.Cookie, t *testing.T) *http.Response {
  504. t.Helper()
  505. return httpRequest(http.MethodPost, url, body, "", "", "", "", "", "", cookies, t)
  506. }
  507. func TestHTTPLogin(t *testing.T) {
  508. t.Parallel()
  509. httpGetBasicAuth := func(url string, username string, password string) *http.Response {
  510. t.Helper()
  511. return httpGet(url, username, password, "", "", nil, t)
  512. }
  513. httpGetXapikey := func(url string, xapikeyHeader string) *http.Response {
  514. t.Helper()
  515. return httpGet(url, "", "", xapikeyHeader, "", nil, t)
  516. }
  517. httpGetAuthorizationBearer := func(url string, bearer string) *http.Response {
  518. t.Helper()
  519. return httpGet(url, "", "", "", bearer, nil, t)
  520. }
  521. testWith := func(sendBasicAuthPrompt bool, expectedOkStatus int, expectedFailStatus int, path string) {
  522. cfg := newMockedConfig()
  523. cfg.GUIReturns(config.GUIConfiguration{
  524. User: "üser",
  525. Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8
  526. RawAddress: "127.0.0.1:0",
  527. APIKey: testAPIKey,
  528. SendBasicAuthPrompt: sendBasicAuthPrompt,
  529. })
  530. baseURL, cancel, err := startHTTP(cfg)
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. t.Cleanup(cancel)
  535. url := baseURL + path
  536. t.Run(fmt.Sprintf("%d path", expectedOkStatus), func(t *testing.T) {
  537. t.Run("no auth is rejected", func(t *testing.T) {
  538. t.Parallel()
  539. resp := httpGetBasicAuth(url, "", "")
  540. if resp.StatusCode != expectedFailStatus {
  541. t.Errorf("Unexpected non-%d return code %d for unauthed request", expectedFailStatus, resp.StatusCode)
  542. }
  543. if hasSessionCookie(resp.Cookies()) {
  544. t.Errorf("Unexpected session cookie for unauthed request")
  545. }
  546. })
  547. t.Run("incorrect password is rejected", func(t *testing.T) {
  548. t.Parallel()
  549. resp := httpGetBasicAuth(url, "üser", "rksmrgs")
  550. if resp.StatusCode != expectedFailStatus {
  551. t.Errorf("Unexpected non-%d return code %d for incorrect password", expectedFailStatus, resp.StatusCode)
  552. }
  553. if hasSessionCookie(resp.Cookies()) {
  554. t.Errorf("Unexpected session cookie for incorrect password")
  555. }
  556. })
  557. t.Run("incorrect username is rejected", func(t *testing.T) {
  558. t.Parallel()
  559. resp := httpGetBasicAuth(url, "user", "räksmörgås") // string literals in Go source code are in UTF-8
  560. if resp.StatusCode != expectedFailStatus {
  561. t.Errorf("Unexpected non-%d return code %d for incorrect username", expectedFailStatus, resp.StatusCode)
  562. }
  563. if hasSessionCookie(resp.Cookies()) {
  564. t.Errorf("Unexpected session cookie for incorrect username")
  565. }
  566. })
  567. t.Run("UTF-8 auth works", func(t *testing.T) {
  568. t.Parallel()
  569. resp := httpGetBasicAuth(url, "üser", "räksmörgås") // string literals in Go source code are in UTF-8
  570. if resp.StatusCode != expectedOkStatus {
  571. t.Errorf("Unexpected non-%d return code %d for authed request (UTF-8)", expectedOkStatus, resp.StatusCode)
  572. }
  573. if !hasSessionCookie(resp.Cookies()) {
  574. t.Errorf("Expected session cookie for authed request (UTF-8)")
  575. }
  576. })
  577. t.Run("Logout removes the session cookie", func(t *testing.T) {
  578. t.Parallel()
  579. resp := httpGetBasicAuth(url, "üser", "räksmörgås") // string literals in Go source code are in UTF-8
  580. if resp.StatusCode != expectedOkStatus {
  581. t.Errorf("Unexpected non-%d return code %d for authed request (UTF-8)", expectedOkStatus, resp.StatusCode)
  582. }
  583. if !hasSessionCookie(resp.Cookies()) {
  584. t.Errorf("Expected session cookie for authed request (UTF-8)")
  585. }
  586. logoutResp := httpPost(baseURL+"/rest/noauth/auth/logout", nil, resp.Cookies(), t)
  587. if !hasDeleteSessionCookie(logoutResp.Cookies()) {
  588. t.Errorf("Expected session cookie to be deleted for logout request")
  589. }
  590. })
  591. t.Run("Session cookie is invalid after logout", func(t *testing.T) {
  592. t.Parallel()
  593. loginResp := httpGetBasicAuth(url, "üser", "räksmörgås") // string literals in Go source code are in UTF-8
  594. if loginResp.StatusCode != expectedOkStatus {
  595. t.Errorf("Unexpected non-%d return code %d for authed request (UTF-8)", expectedOkStatus, loginResp.StatusCode)
  596. }
  597. if !hasSessionCookie(loginResp.Cookies()) {
  598. t.Errorf("Expected session cookie for authed request (UTF-8)")
  599. }
  600. resp := httpGet(url, "", "", "", "", loginResp.Cookies(), t)
  601. if resp.StatusCode != expectedOkStatus {
  602. t.Errorf("Unexpected non-%d return code %d for cookie-authed request (UTF-8)", expectedOkStatus, resp.StatusCode)
  603. }
  604. httpPost(baseURL+"/rest/noauth/auth/logout", nil, loginResp.Cookies(), t)
  605. resp = httpGet(url, "", "", "", "", loginResp.Cookies(), t)
  606. if resp.StatusCode != expectedFailStatus {
  607. t.Errorf("Expected session to be invalid (status %d) after logout, got status: %d", expectedFailStatus, resp.StatusCode)
  608. }
  609. })
  610. t.Run("ISO-8859-1 auth works", func(t *testing.T) {
  611. t.Parallel()
  612. resp := httpGetBasicAuth(url, "\xfcser", "r\xe4ksm\xf6rg\xe5s") // escaped ISO-8859-1
  613. if resp.StatusCode != expectedOkStatus {
  614. t.Errorf("Unexpected non-%d return code %d for authed request (ISO-8859-1)", expectedOkStatus, resp.StatusCode)
  615. }
  616. if !hasSessionCookie(resp.Cookies()) {
  617. t.Errorf("Expected session cookie for authed request (ISO-8859-1)")
  618. }
  619. })
  620. t.Run("bad X-API-Key is rejected", func(t *testing.T) {
  621. t.Parallel()
  622. resp := httpGetXapikey(url, testAPIKey+"X")
  623. if resp.StatusCode != expectedFailStatus {
  624. t.Errorf("Unexpected non-%d return code %d for bad API key", expectedFailStatus, resp.StatusCode)
  625. }
  626. if hasSessionCookie(resp.Cookies()) {
  627. t.Errorf("Unexpected session cookie for bad API key")
  628. }
  629. })
  630. t.Run("good X-API-Key is accepted", func(t *testing.T) {
  631. t.Parallel()
  632. resp := httpGetXapikey(url, testAPIKey)
  633. if resp.StatusCode != expectedOkStatus {
  634. t.Errorf("Unexpected non-%d return code %d for API key", expectedOkStatus, resp.StatusCode)
  635. }
  636. if hasSessionCookie(resp.Cookies()) {
  637. t.Errorf("Unexpected session cookie for API key")
  638. }
  639. })
  640. t.Run("bad Bearer is rejected", func(t *testing.T) {
  641. t.Parallel()
  642. resp := httpGetAuthorizationBearer(url, testAPIKey+"X")
  643. if resp.StatusCode != expectedFailStatus {
  644. t.Errorf("Unexpected non-%d return code %d for bad Authorization: Bearer", expectedFailStatus, resp.StatusCode)
  645. }
  646. if hasSessionCookie(resp.Cookies()) {
  647. t.Errorf("Unexpected session cookie for bad Authorization: Bearer")
  648. }
  649. })
  650. t.Run("good Bearer is accepted", func(t *testing.T) {
  651. t.Parallel()
  652. resp := httpGetAuthorizationBearer(url, testAPIKey)
  653. if resp.StatusCode != expectedOkStatus {
  654. t.Errorf("Unexpected non-%d return code %d for Authorization: Bearer", expectedOkStatus, resp.StatusCode)
  655. }
  656. if hasSessionCookie(resp.Cookies()) {
  657. t.Errorf("Unexpected session cookie for bad Authorization: Bearer")
  658. }
  659. })
  660. })
  661. }
  662. testWith(true, http.StatusOK, http.StatusOK, "/")
  663. testWith(true, http.StatusOK, http.StatusUnauthorized, "/meta.js")
  664. testWith(true, http.StatusNotFound, http.StatusUnauthorized, "/any-path/that/does/nooooooot/match-any/noauth-pattern")
  665. testWith(false, http.StatusOK, http.StatusOK, "/")
  666. testWith(false, http.StatusOK, http.StatusForbidden, "/meta.js")
  667. testWith(false, http.StatusNotFound, http.StatusForbidden, "/any-path/that/does/nooooooot/match-any/noauth-pattern")
  668. }
  669. func TestHtmlFormLogin(t *testing.T) {
  670. t.Parallel()
  671. cfg := newMockedConfig()
  672. cfg.GUIReturns(config.GUIConfiguration{
  673. User: "üser",
  674. Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8
  675. SendBasicAuthPrompt: false,
  676. })
  677. baseURL, cancel, err := startHTTP(cfg)
  678. if err != nil {
  679. t.Fatal(err)
  680. }
  681. t.Cleanup(cancel)
  682. loginUrl := baseURL + "/rest/noauth/auth/password"
  683. resourceUrl := baseURL + "/meta.js"
  684. resourceUrl404 := baseURL + "/any-path/that/does/nooooooot/match-any/noauth-pattern"
  685. performLogin := func(username string, password string) *http.Response {
  686. t.Helper()
  687. return httpPost(loginUrl, map[string]string{"username": username, "password": password}, nil, t)
  688. }
  689. performResourceRequest := func(url string, cookies []*http.Cookie) *http.Response {
  690. t.Helper()
  691. return httpGet(url, "", "", "", "", cookies, t)
  692. }
  693. testNoAuthPath := func(noAuthPath string) {
  694. t.Run("auth is not needed for "+noAuthPath, func(t *testing.T) {
  695. t.Parallel()
  696. resp := httpGet(baseURL+noAuthPath, "", "", "", "", nil, t)
  697. if resp.StatusCode != http.StatusOK {
  698. t.Errorf("Unexpected non-200 return code %d at %s", resp.StatusCode, noAuthPath)
  699. }
  700. if hasSessionCookie(resp.Cookies()) {
  701. t.Errorf("Unexpected session cookie at " + noAuthPath)
  702. }
  703. })
  704. }
  705. testNoAuthPath("/index.html")
  706. testNoAuthPath("/rest/svc/lang")
  707. t.Run("incorrect password is rejected with 403", func(t *testing.T) {
  708. t.Parallel()
  709. resp := performLogin("üser", "rksmrgs") // string literals in Go source code are in UTF-8
  710. if resp.StatusCode != http.StatusForbidden {
  711. t.Errorf("Unexpected non-403 return code %d for incorrect password", resp.StatusCode)
  712. }
  713. if hasSessionCookie(resp.Cookies()) {
  714. t.Errorf("Unexpected session cookie for incorrect password")
  715. }
  716. resp = performResourceRequest(resourceUrl, resp.Cookies())
  717. if resp.StatusCode != http.StatusForbidden {
  718. t.Errorf("Unexpected non-403 return code %d for incorrect password", resp.StatusCode)
  719. }
  720. })
  721. t.Run("incorrect username is rejected with 403", func(t *testing.T) {
  722. t.Parallel()
  723. resp := performLogin("user", "räksmörgås") // string literals in Go source code are in UTF-8
  724. if resp.StatusCode != http.StatusForbidden {
  725. t.Errorf("Unexpected non-403 return code %d for incorrect username", resp.StatusCode)
  726. }
  727. if hasSessionCookie(resp.Cookies()) {
  728. t.Errorf("Unexpected session cookie for incorrect username")
  729. }
  730. resp = performResourceRequest(resourceUrl, resp.Cookies())
  731. if resp.StatusCode != http.StatusForbidden {
  732. t.Errorf("Unexpected non-403 return code %d for incorrect username", resp.StatusCode)
  733. }
  734. })
  735. t.Run("UTF-8 auth works", func(t *testing.T) {
  736. t.Parallel()
  737. // JSON is always UTF-8, so ISO-8859-1 case is not applicable
  738. resp := performLogin("üser", "räksmörgås") // string literals in Go source code are in UTF-8
  739. if resp.StatusCode != http.StatusNoContent {
  740. t.Errorf("Unexpected non-204 return code %d for authed request (UTF-8)", resp.StatusCode)
  741. }
  742. resp = performResourceRequest(resourceUrl, resp.Cookies())
  743. if resp.StatusCode != http.StatusOK {
  744. t.Errorf("Unexpected non-200 return code %d for authed request (UTF-8)", resp.StatusCode)
  745. }
  746. })
  747. t.Run("Logout removes the session cookie", func(t *testing.T) {
  748. t.Parallel()
  749. // JSON is always UTF-8, so ISO-8859-1 case is not applicable
  750. resp := performLogin("üser", "räksmörgås") // string literals in Go source code are in UTF-8
  751. if resp.StatusCode != http.StatusNoContent {
  752. t.Errorf("Unexpected non-204 return code %d for authed request (UTF-8)", resp.StatusCode)
  753. }
  754. logoutResp := httpPost(baseURL+"/rest/noauth/auth/logout", nil, resp.Cookies(), t)
  755. if !hasDeleteSessionCookie(logoutResp.Cookies()) {
  756. t.Errorf("Expected session cookie to be deleted for logout request")
  757. }
  758. })
  759. t.Run("Session cookie is invalid after logout", func(t *testing.T) {
  760. t.Parallel()
  761. // JSON is always UTF-8, so ISO-8859-1 case is not applicable
  762. loginResp := performLogin("üser", "räksmörgås") // string literals in Go source code are in UTF-8
  763. if loginResp.StatusCode != http.StatusNoContent {
  764. t.Errorf("Unexpected non-204 return code %d for authed request (UTF-8)", loginResp.StatusCode)
  765. }
  766. resp := performResourceRequest(resourceUrl, loginResp.Cookies())
  767. if resp.StatusCode != http.StatusOK {
  768. t.Errorf("Unexpected non-200 return code %d for authed request (UTF-8)", resp.StatusCode)
  769. }
  770. httpPost(baseURL+"/rest/noauth/auth/logout", nil, loginResp.Cookies(), t)
  771. resp = performResourceRequest(resourceUrl, loginResp.Cookies())
  772. if resp.StatusCode != http.StatusForbidden {
  773. t.Errorf("Expected session to be invalid (status 403) after logout, got status: %d", resp.StatusCode)
  774. }
  775. })
  776. t.Run("form login is not applicable to other URLs", func(t *testing.T) {
  777. t.Parallel()
  778. resp := httpPost(baseURL+"/meta.js", map[string]string{"username": "üser", "password": "räksmörgås"}, nil, t)
  779. if resp.StatusCode != http.StatusForbidden {
  780. t.Errorf("Unexpected non-403 return code %d for incorrect form login URL", resp.StatusCode)
  781. }
  782. if hasSessionCookie(resp.Cookies()) {
  783. t.Errorf("Unexpected session cookie for incorrect form login URL")
  784. }
  785. })
  786. t.Run("invalid URL returns 403 before auth and 404 after auth", func(t *testing.T) {
  787. t.Parallel()
  788. resp := performResourceRequest(resourceUrl404, nil)
  789. if resp.StatusCode != http.StatusForbidden {
  790. t.Errorf("Unexpected non-403 return code %d for unauthed request", resp.StatusCode)
  791. }
  792. resp = performLogin("üser", "räksmörgås")
  793. if resp.StatusCode != http.StatusNoContent {
  794. t.Errorf("Unexpected non-204 return code %d for authed request", resp.StatusCode)
  795. }
  796. resp = performResourceRequest(resourceUrl404, resp.Cookies())
  797. if resp.StatusCode != http.StatusNotFound {
  798. t.Errorf("Unexpected non-404 return code %d for authed request", resp.StatusCode)
  799. }
  800. })
  801. }
  802. func TestApiCache(t *testing.T) {
  803. t.Parallel()
  804. cfg := newMockedConfig()
  805. cfg.GUIReturns(config.GUIConfiguration{
  806. RawAddress: "127.0.0.1:0",
  807. APIKey: testAPIKey,
  808. })
  809. baseURL, cancel, err := startHTTP(cfg)
  810. if err != nil {
  811. t.Fatal(err)
  812. }
  813. t.Cleanup(cancel)
  814. httpGet := func(url string, bearer string) *http.Response {
  815. return httpGet(url, "", "", "", bearer, nil, t)
  816. }
  817. t.Run("meta.js has no-cache headers", func(t *testing.T) {
  818. t.Parallel()
  819. url := baseURL + "/meta.js"
  820. resp := httpGet(url, testAPIKey)
  821. if resp.Header.Get("Cache-Control") != "max-age=0, no-cache, no-store" {
  822. t.Errorf("Expected no-cache headers at %s", url)
  823. }
  824. })
  825. t.Run("/rest/ has no-cache headers", func(t *testing.T) {
  826. t.Parallel()
  827. url := baseURL + "/rest/system/version"
  828. resp := httpGet(url, testAPIKey)
  829. if resp.Header.Get("Cache-Control") != "max-age=0, no-cache, no-store" {
  830. t.Errorf("Expected no-cache headers at %s", url)
  831. }
  832. })
  833. }
  834. func startHTTP(cfg config.Wrapper) (string, context.CancelFunc, error) {
  835. m := new(modelmocks.Model)
  836. assetDir := "../../gui"
  837. eventSub := new(eventmocks.BufferedSubscription)
  838. diskEventSub := new(eventmocks.BufferedSubscription)
  839. discoverer := new(discovermocks.Manager)
  840. connections := new(connmocks.Service)
  841. errorLog := new(loggermocks.Recorder)
  842. systemLog := new(loggermocks.Recorder)
  843. for _, l := range []*loggermocks.Recorder{errorLog, systemLog} {
  844. l.SinceReturns([]logger.Line{
  845. {
  846. When: time.Now(),
  847. Message: "Test message",
  848. },
  849. })
  850. }
  851. addrChan := make(chan string)
  852. mockedSummary := &modelmocks.FolderSummaryService{}
  853. mockedSummary.SummaryReturns(new(model.FolderSummary), nil)
  854. // Instantiate the API service
  855. urService := ur.New(cfg, m, connections, false)
  856. mdb, _ := db.NewLowlevel(backend.OpenMemory(), events.NoopLogger)
  857. kdb := db.NewMiscDataNamespace(mdb)
  858. svc := New(protocol.LocalDeviceID, cfg, assetDir, "syncthing", m, eventSub, diskEventSub, events.NoopLogger, discoverer, connections, urService, mockedSummary, errorLog, systemLog, false, kdb).(*service)
  859. svc.started = addrChan
  860. // Actually start the API service
  861. supervisor := suture.New("API test", suture.Spec{
  862. PassThroughPanics: true,
  863. })
  864. supervisor.Add(svc)
  865. ctx, cancel := context.WithCancel(context.Background())
  866. supervisor.ServeBackground(ctx)
  867. // Make sure the API service is listening, and get the URL to use.
  868. addr := <-addrChan
  869. tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
  870. if err != nil {
  871. cancel()
  872. return "", cancel, fmt.Errorf("weird address from API service: %w", err)
  873. }
  874. host, _, _ := net.SplitHostPort(cfg.GUI().RawAddress)
  875. if host == "" || host == "0.0.0.0" {
  876. host = "127.0.0.1"
  877. }
  878. baseURL := fmt.Sprintf("http://%s", net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port)))
  879. return baseURL, cancel, nil
  880. }
  881. func TestCSRFRequired(t *testing.T) {
  882. t.Parallel()
  883. baseURL, cancel, err := startHTTP(apiCfg)
  884. if err != nil {
  885. t.Fatal("Unexpected error from getting base URL:", err)
  886. }
  887. t.Cleanup(cancel)
  888. cli := &http.Client{
  889. Timeout: time.Minute,
  890. }
  891. // Getting the base URL (i.e. "/") should succeed.
  892. resp, err := cli.Get(baseURL)
  893. if err != nil {
  894. t.Fatal("Unexpected error from getting base URL:", err)
  895. }
  896. resp.Body.Close()
  897. if resp.StatusCode != http.StatusOK {
  898. t.Fatal("Getting base URL should succeed, not", resp.Status)
  899. }
  900. // Find the returned CSRF token for future use
  901. var csrfTokenName, csrfTokenValue string
  902. for _, cookie := range resp.Cookies() {
  903. if strings.HasPrefix(cookie.Name, "CSRF-Token") {
  904. csrfTokenName = cookie.Name
  905. csrfTokenValue = cookie.Value
  906. break
  907. }
  908. }
  909. if csrfTokenValue == "" {
  910. t.Fatal("Failed to initialize CSRF test: no CSRF cookie returned from " + baseURL)
  911. }
  912. t.Run("/rest without a token should fail", func(t *testing.T) {
  913. t.Parallel()
  914. resp, err := cli.Get(baseURL + "/rest/system/config")
  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.StatusForbidden {
  920. t.Fatal("Getting /rest/system/config without CSRF token should fail, not", resp.Status)
  921. }
  922. })
  923. t.Run("/rest with a token should succeed", func(t *testing.T) {
  924. t.Parallel()
  925. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  926. req.Header.Set("X-"+csrfTokenName, csrfTokenValue)
  927. resp, err := cli.Do(req)
  928. if err != nil {
  929. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  930. }
  931. resp.Body.Close()
  932. if resp.StatusCode != http.StatusOK {
  933. t.Fatal("Getting /rest/system/config with CSRF token should succeed, not", resp.Status)
  934. }
  935. })
  936. t.Run("/rest with an incorrect API key should fail, X-API-Key version", func(t *testing.T) {
  937. t.Parallel()
  938. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  939. req.Header.Set("X-API-Key", testAPIKey+"X")
  940. resp, err := cli.Do(req)
  941. if err != nil {
  942. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  943. }
  944. resp.Body.Close()
  945. if resp.StatusCode != http.StatusForbidden {
  946. t.Fatal("Getting /rest/system/config with incorrect API token should fail, not", resp.Status)
  947. }
  948. })
  949. t.Run("/rest with an incorrect API key should fail, Bearer auth version", func(t *testing.T) {
  950. t.Parallel()
  951. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  952. req.Header.Set("Authorization", "Bearer "+testAPIKey+"X")
  953. resp, err := cli.Do(req)
  954. if err != nil {
  955. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  956. }
  957. resp.Body.Close()
  958. if resp.StatusCode != http.StatusForbidden {
  959. t.Fatal("Getting /rest/system/config with incorrect API token should fail, not", resp.Status)
  960. }
  961. })
  962. t.Run("/rest with the API key should succeed", func(t *testing.T) {
  963. t.Parallel()
  964. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  965. req.Header.Set("X-API-Key", testAPIKey)
  966. resp, err := cli.Do(req)
  967. if err != nil {
  968. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  969. }
  970. resp.Body.Close()
  971. if resp.StatusCode != http.StatusOK {
  972. t.Fatal("Getting /rest/system/config with API key should succeed, not", resp.Status)
  973. }
  974. })
  975. t.Run("/rest with the API key as a bearer token should succeed", func(t *testing.T) {
  976. t.Parallel()
  977. req, _ := http.NewRequest("GET", baseURL+"/rest/system/config", nil)
  978. req.Header.Set("Authorization", "Bearer "+testAPIKey)
  979. resp, err := cli.Do(req)
  980. if err != nil {
  981. t.Fatal("Unexpected error from getting /rest/system/config:", err)
  982. }
  983. resp.Body.Close()
  984. if resp.StatusCode != http.StatusOK {
  985. t.Fatal("Getting /rest/system/config with API key should succeed, not", resp.Status)
  986. }
  987. })
  988. }
  989. func TestRandomString(t *testing.T) {
  990. t.Parallel()
  991. baseURL, cancel, err := startHTTP(apiCfg)
  992. if err != nil {
  993. t.Fatal(err)
  994. }
  995. defer cancel()
  996. cli := &http.Client{
  997. Timeout: time.Second,
  998. }
  999. // The default should be to return a 32 character random string
  1000. for _, url := range []string{"/rest/svc/random/string", "/rest/svc/random/string?length=-1", "/rest/svc/random/string?length=yo"} {
  1001. req, _ := http.NewRequest("GET", baseURL+url, nil)
  1002. req.Header.Set("X-API-Key", testAPIKey)
  1003. resp, err := cli.Do(req)
  1004. if err != nil {
  1005. t.Fatal(err)
  1006. }
  1007. var res map[string]string
  1008. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  1009. t.Fatal(err)
  1010. }
  1011. if len(res["random"]) != 32 {
  1012. t.Errorf("Expected 32 random characters, got %q of length %d", res["random"], len(res["random"]))
  1013. }
  1014. }
  1015. // We can ask for a different length if we like
  1016. req, _ := http.NewRequest("GET", baseURL+"/rest/svc/random/string?length=27", nil)
  1017. req.Header.Set("X-API-Key", testAPIKey)
  1018. resp, err := cli.Do(req)
  1019. if err != nil {
  1020. t.Fatal(err)
  1021. }
  1022. var res map[string]string
  1023. if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
  1024. t.Fatal(err)
  1025. }
  1026. if len(res["random"]) != 27 {
  1027. t.Errorf("Expected 27 random characters, got %q of length %d", res["random"], len(res["random"]))
  1028. }
  1029. }
  1030. func TestConfigPostOK(t *testing.T) {
  1031. t.Parallel()
  1032. cfg := bytes.NewBuffer([]byte(`{
  1033. "version": 15,
  1034. "folders": [
  1035. {
  1036. "id": "foo",
  1037. "path": "TestConfigPostOK"
  1038. }
  1039. ]
  1040. }`))
  1041. resp, err := testConfigPost(cfg)
  1042. if err != nil {
  1043. t.Fatal(err)
  1044. }
  1045. if resp.StatusCode != http.StatusOK {
  1046. t.Error("Expected 200 OK, not", resp.Status)
  1047. }
  1048. os.RemoveAll("TestConfigPostOK")
  1049. }
  1050. func TestConfigPostDupFolder(t *testing.T) {
  1051. t.Parallel()
  1052. cfg := bytes.NewBuffer([]byte(`{
  1053. "version": 15,
  1054. "folders": [
  1055. {"id": "foo"},
  1056. {"id": "foo"}
  1057. ]
  1058. }`))
  1059. resp, err := testConfigPost(cfg)
  1060. if err != nil {
  1061. t.Fatal(err)
  1062. }
  1063. if resp.StatusCode != http.StatusBadRequest {
  1064. t.Error("Expected 400 Bad Request, not", resp.Status)
  1065. }
  1066. }
  1067. func testConfigPost(data io.Reader) (*http.Response, error) {
  1068. baseURL, cancel, err := startHTTP(apiCfg)
  1069. if err != nil {
  1070. return nil, err
  1071. }
  1072. defer cancel()
  1073. cli := &http.Client{
  1074. Timeout: time.Second,
  1075. }
  1076. req, _ := http.NewRequest("POST", baseURL+"/rest/system/config", data)
  1077. req.Header.Set("X-API-Key", testAPIKey)
  1078. return cli.Do(req)
  1079. }
  1080. func TestHostCheck(t *testing.T) {
  1081. t.Parallel()
  1082. // An API service bound to localhost should reject non-localhost host Headers
  1083. cfg := newMockedConfig()
  1084. cfg.GUIReturns(config.GUIConfiguration{RawAddress: "127.0.0.1:0"})
  1085. baseURL, cancel, err := startHTTP(cfg)
  1086. if err != nil {
  1087. t.Fatal(err)
  1088. }
  1089. defer cancel()
  1090. // A normal HTTP get to the localhost-bound service should succeed
  1091. resp, err := http.Get(baseURL)
  1092. if err != nil {
  1093. t.Fatal(err)
  1094. }
  1095. resp.Body.Close()
  1096. if resp.StatusCode != http.StatusOK {
  1097. t.Error("Regular HTTP get: expected 200 OK, not", resp.Status)
  1098. }
  1099. // A request with a suspicious Host header should fail
  1100. req, _ := http.NewRequest("GET", baseURL, nil)
  1101. req.Host = "example.com"
  1102. resp, err = http.DefaultClient.Do(req)
  1103. if err != nil {
  1104. t.Fatal(err)
  1105. }
  1106. resp.Body.Close()
  1107. if resp.StatusCode != http.StatusForbidden {
  1108. t.Error("Suspicious Host header: expected 403 Forbidden, not", resp.Status)
  1109. }
  1110. // A request with an explicit "localhost:8384" Host header should pass
  1111. req, _ = http.NewRequest("GET", baseURL, nil)
  1112. req.Host = "localhost:8384"
  1113. resp, err = http.DefaultClient.Do(req)
  1114. if err != nil {
  1115. t.Fatal(err)
  1116. }
  1117. resp.Body.Close()
  1118. if resp.StatusCode != http.StatusOK {
  1119. t.Error("Explicit localhost:8384: expected 200 OK, not", resp.Status)
  1120. }
  1121. // A request with an explicit "localhost" Host header (no port) should pass
  1122. req, _ = http.NewRequest("GET", baseURL, nil)
  1123. req.Host = "localhost"
  1124. resp, err = http.DefaultClient.Do(req)
  1125. if err != nil {
  1126. t.Fatal(err)
  1127. }
  1128. resp.Body.Close()
  1129. if resp.StatusCode != http.StatusOK {
  1130. t.Error("Explicit localhost: expected 200 OK, not", resp.Status)
  1131. }
  1132. // A server with InsecureSkipHostCheck set behaves differently
  1133. cfg = newMockedConfig()
  1134. cfg.GUIReturns(config.GUIConfiguration{
  1135. RawAddress: "127.0.0.1:0",
  1136. InsecureSkipHostCheck: true,
  1137. })
  1138. baseURL, cancel, err = startHTTP(cfg)
  1139. if err != nil {
  1140. t.Fatal(err)
  1141. }
  1142. defer cancel()
  1143. // A request with a suspicious Host header should be allowed
  1144. req, _ = http.NewRequest("GET", baseURL, nil)
  1145. req.Host = "example.com"
  1146. resp, err = http.DefaultClient.Do(req)
  1147. if err != nil {
  1148. t.Fatal(err)
  1149. }
  1150. resp.Body.Close()
  1151. if resp.StatusCode != http.StatusOK {
  1152. t.Error("Incorrect host header, check disabled: expected 200 OK, not", resp.Status)
  1153. }
  1154. if !testing.Short() {
  1155. // A server bound to a wildcard address also doesn't do the check
  1156. cfg = newMockedConfig()
  1157. cfg.GUIReturns(config.GUIConfiguration{
  1158. RawAddress: "0.0.0.0:0",
  1159. })
  1160. baseURL, cancel, err = startHTTP(cfg)
  1161. if err != nil {
  1162. t.Fatal(err)
  1163. }
  1164. defer cancel()
  1165. // A request with a suspicious Host header should be allowed
  1166. req, _ = http.NewRequest("GET", baseURL, nil)
  1167. req.Host = "example.com"
  1168. resp, err = http.DefaultClient.Do(req)
  1169. if err != nil {
  1170. t.Fatal(err)
  1171. }
  1172. resp.Body.Close()
  1173. if resp.StatusCode != http.StatusOK {
  1174. t.Error("Incorrect host header, wildcard bound: expected 200 OK, not", resp.Status)
  1175. }
  1176. }
  1177. // This should all work over IPv6 as well
  1178. if runningInContainer() {
  1179. // Working IPv6 in Docker can't be taken for granted.
  1180. return
  1181. }
  1182. cfg = newMockedConfig()
  1183. cfg.GUIReturns(config.GUIConfiguration{
  1184. RawAddress: "[::1]:0",
  1185. })
  1186. baseURL, cancel, err = startHTTP(cfg)
  1187. if err != nil {
  1188. t.Fatal(err)
  1189. }
  1190. defer cancel()
  1191. // A normal HTTP get to the localhost-bound service should succeed
  1192. resp, err = http.Get(baseURL)
  1193. if err != nil {
  1194. t.Fatal(err)
  1195. }
  1196. resp.Body.Close()
  1197. if resp.StatusCode != http.StatusOK {
  1198. t.Error("Regular HTTP get (IPv6): expected 200 OK, not", resp.Status)
  1199. }
  1200. // A request with a suspicious Host header should fail
  1201. req, _ = http.NewRequest("GET", baseURL, nil)
  1202. req.Host = "example.com"
  1203. resp, err = http.DefaultClient.Do(req)
  1204. if err != nil {
  1205. t.Fatal(err)
  1206. }
  1207. resp.Body.Close()
  1208. if resp.StatusCode != http.StatusForbidden {
  1209. t.Error("Suspicious Host header (IPv6): expected 403 Forbidden, not", resp.Status)
  1210. }
  1211. // A request with an explicit "localhost:8384" Host header should pass
  1212. req, _ = http.NewRequest("GET", baseURL, nil)
  1213. req.Host = "localhost:8384"
  1214. resp, err = http.DefaultClient.Do(req)
  1215. if err != nil {
  1216. t.Fatal(err)
  1217. }
  1218. resp.Body.Close()
  1219. if resp.StatusCode != http.StatusOK {
  1220. t.Error("Explicit localhost:8384 (IPv6): expected 200 OK, not", resp.Status)
  1221. }
  1222. }
  1223. func TestAddressIsLocalhost(t *testing.T) {
  1224. t.Parallel()
  1225. testcases := []struct {
  1226. address string
  1227. result bool
  1228. }{
  1229. // These are all valid localhost addresses
  1230. {"localhost", true},
  1231. {"LOCALHOST", true},
  1232. {"localhost.", true},
  1233. {"::1", true},
  1234. {"127.0.0.1", true},
  1235. {"127.23.45.56", true},
  1236. {"localhost:8080", true},
  1237. {"LOCALHOST:8000", true},
  1238. {"localhost.:8080", true},
  1239. {"[::1]:8080", true},
  1240. {"127.0.0.1:8080", true},
  1241. {"127.23.45.56:8080", true},
  1242. {"www.localhost", true},
  1243. {"www.localhost:8080", true},
  1244. // These are all non-localhost addresses
  1245. {"example.com", false},
  1246. {"example.com:8080", false},
  1247. {"localhost.com", false},
  1248. {"localhost.com:8080", false},
  1249. {"192.0.2.10", false},
  1250. {"192.0.2.10:8080", false},
  1251. {"0.0.0.0", false},
  1252. {"0.0.0.0:8080", false},
  1253. {"::", false},
  1254. {"[::]:8080", false},
  1255. {":8080", false},
  1256. }
  1257. for _, tc := range testcases {
  1258. result := addressIsLocalhost(tc.address)
  1259. if result != tc.result {
  1260. t.Errorf("addressIsLocalhost(%q)=%v, expected %v", tc.address, result, tc.result)
  1261. }
  1262. }
  1263. }
  1264. func TestAccessControlAllowOriginHeader(t *testing.T) {
  1265. t.Parallel()
  1266. baseURL, cancel, err := startHTTP(apiCfg)
  1267. if err != nil {
  1268. t.Fatal(err)
  1269. }
  1270. defer cancel()
  1271. cli := &http.Client{
  1272. Timeout: time.Second,
  1273. }
  1274. req, _ := http.NewRequest("GET", baseURL+"/rest/system/status", nil)
  1275. req.Header.Set("X-API-Key", testAPIKey)
  1276. resp, err := cli.Do(req)
  1277. if err != nil {
  1278. t.Fatal(err)
  1279. }
  1280. resp.Body.Close()
  1281. if resp.StatusCode != http.StatusOK {
  1282. t.Fatal("GET on /rest/system/status should succeed, not", resp.Status)
  1283. }
  1284. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  1285. t.Fatal("GET on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  1286. }
  1287. }
  1288. func TestOptionsRequest(t *testing.T) {
  1289. t.Parallel()
  1290. baseURL, cancel, err := startHTTP(apiCfg)
  1291. if err != nil {
  1292. t.Fatal(err)
  1293. }
  1294. defer cancel()
  1295. cli := &http.Client{
  1296. Timeout: time.Second,
  1297. }
  1298. req, _ := http.NewRequest("OPTIONS", baseURL+"/rest/system/status", nil)
  1299. resp, err := cli.Do(req)
  1300. if err != nil {
  1301. t.Fatal(err)
  1302. }
  1303. resp.Body.Close()
  1304. if resp.StatusCode != http.StatusNoContent {
  1305. t.Fatal("OPTIONS on /rest/system/status should succeed, not", resp.Status)
  1306. }
  1307. if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
  1308. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Origin: *' header")
  1309. }
  1310. if resp.Header.Get("Access-Control-Allow-Methods") != "GET, POST, PUT, PATCH, DELETE, OPTIONS" {
  1311. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS' header")
  1312. }
  1313. if resp.Header.Get("Access-Control-Allow-Headers") != "Content-Type, X-API-Key" {
  1314. t.Fatal("OPTIONS on /rest/system/status should return a 'Access-Control-Allow-Headers: Content-Type, X-API-KEY' header")
  1315. }
  1316. }
  1317. func TestEventMasks(t *testing.T) {
  1318. t.Parallel()
  1319. cfg := newMockedConfig()
  1320. defSub := new(eventmocks.BufferedSubscription)
  1321. diskSub := new(eventmocks.BufferedSubscription)
  1322. mdb, _ := db.NewLowlevel(backend.OpenMemory(), events.NoopLogger)
  1323. kdb := db.NewMiscDataNamespace(mdb)
  1324. svc := New(protocol.LocalDeviceID, cfg, "", "syncthing", nil, defSub, diskSub, events.NoopLogger, nil, nil, nil, nil, nil, nil, false, kdb).(*service)
  1325. if mask := svc.getEventMask(""); mask != DefaultEventMask {
  1326. t.Errorf("incorrect default mask %x != %x", int64(mask), int64(DefaultEventMask))
  1327. }
  1328. expected := events.FolderSummary | events.LocalChangeDetected
  1329. if mask := svc.getEventMask("FolderSummary,LocalChangeDetected"); mask != expected {
  1330. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  1331. }
  1332. expected = 0
  1333. if mask := svc.getEventMask("WeirdEvent,something else that doesn't exist"); mask != expected {
  1334. t.Errorf("incorrect parsed mask %x != %x", int64(mask), int64(expected))
  1335. }
  1336. if res := svc.getEventSub(DefaultEventMask); res != defSub {
  1337. t.Errorf("should have returned the given default event sub")
  1338. }
  1339. if res := svc.getEventSub(DiskEventMask); res != diskSub {
  1340. t.Errorf("should have returned the given disk event sub")
  1341. }
  1342. if res := svc.getEventSub(events.LocalIndexUpdated); res == nil || res == defSub || res == diskSub {
  1343. t.Errorf("should have returned a valid, non-default event sub")
  1344. }
  1345. }
  1346. func TestBrowse(t *testing.T) {
  1347. t.Parallel()
  1348. pathSep := string(os.PathSeparator)
  1349. ffs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?nostfolder=true")
  1350. _ = ffs.Mkdir("dir", 0o755)
  1351. _ = fs.WriteFile(ffs, "file", []byte("hello"), 0o644)
  1352. _ = ffs.Mkdir("MiXEDCase", 0o755)
  1353. // We expect completion to return the full path to the completed
  1354. // directory, with an ending slash.
  1355. dirPath := "dir" + pathSep
  1356. mixedCaseDirPath := "MiXEDCase" + pathSep
  1357. cases := []struct {
  1358. current string
  1359. returns []string
  1360. }{
  1361. // The directory without slash is completed to one with slash.
  1362. {"dir", []string{"dir" + pathSep}},
  1363. // With slash it's completed to its contents.
  1364. // Dirs are given pathSeps.
  1365. // Files are not returned.
  1366. {"", []string{mixedCaseDirPath, dirPath}},
  1367. // Globbing is automatic based on prefix.
  1368. {"d", []string{dirPath}},
  1369. {"di", []string{dirPath}},
  1370. {"dir", []string{dirPath}},
  1371. {"f", nil},
  1372. {"q", nil},
  1373. // Globbing is case-insensitive
  1374. {"mixed", []string{mixedCaseDirPath}},
  1375. }
  1376. for _, tc := range cases {
  1377. ret := browseFiles(ffs, tc.current)
  1378. if !slices.Equal(ret, tc.returns) {
  1379. t.Errorf("browseFiles(%q) => %q, expected %q", tc.current, ret, tc.returns)
  1380. }
  1381. }
  1382. }
  1383. func TestPrefixMatch(t *testing.T) {
  1384. t.Parallel()
  1385. cases := []struct {
  1386. s string
  1387. prefix string
  1388. expected int
  1389. }{
  1390. {"aaaA", "aaa", matchExact},
  1391. {"AAAX", "BBB", noMatch},
  1392. {"AAAX", "aAa", matchCaseIns},
  1393. {"äÜX", "äü", matchCaseIns},
  1394. }
  1395. for _, tc := range cases {
  1396. ret := checkPrefixMatch(tc.s, tc.prefix)
  1397. if ret != tc.expected {
  1398. t.Errorf("checkPrefixMatch(%q, %q) => %v, expected %v", tc.s, tc.prefix, ret, tc.expected)
  1399. }
  1400. }
  1401. }
  1402. func TestShouldRegenerateCertificate(t *testing.T) {
  1403. // Self signed certificates expiring in less than a month are errored so we
  1404. // can regenerate in time.
  1405. crt, err := tlsutil.NewCertificateInMemory("foo.example.com", 29)
  1406. if err != nil {
  1407. t.Fatal(err)
  1408. }
  1409. if err := shouldRegenerateCertificate(crt); err == nil {
  1410. t.Error("expected expiry error")
  1411. }
  1412. // Certificates with at least 31 days of life left are fine.
  1413. crt, err = tlsutil.NewCertificateInMemory("foo.example.com", 31)
  1414. if err != nil {
  1415. t.Fatal(err)
  1416. }
  1417. if err := shouldRegenerateCertificate(crt); err != nil {
  1418. t.Error("expected no error:", err)
  1419. }
  1420. if build.IsDarwin {
  1421. // Certificates with too long an expiry time are not allowed on macOS
  1422. crt, err = tlsutil.NewCertificateInMemory("foo.example.com", 1000)
  1423. if err != nil {
  1424. t.Fatal(err)
  1425. }
  1426. if err := shouldRegenerateCertificate(crt); err == nil {
  1427. t.Error("expected expiry error")
  1428. }
  1429. }
  1430. }
  1431. func TestConfigChanges(t *testing.T) {
  1432. t.Parallel()
  1433. const testAPIKey = "foobarbaz"
  1434. cfg := config.Configuration{
  1435. GUI: config.GUIConfiguration{
  1436. RawAddress: "127.0.0.1:0",
  1437. RawUseTLS: false,
  1438. APIKey: testAPIKey,
  1439. },
  1440. }
  1441. tmpFile, err := os.CreateTemp("", "syncthing-testConfig-")
  1442. if err != nil {
  1443. panic(err)
  1444. }
  1445. defer os.Remove(tmpFile.Name())
  1446. w := config.Wrap(tmpFile.Name(), cfg, protocol.LocalDeviceID, events.NoopLogger)
  1447. tmpFile.Close()
  1448. cfgCtx, cfgCancel := context.WithCancel(context.Background())
  1449. go w.Serve(cfgCtx)
  1450. defer cfgCancel()
  1451. baseURL, cancel, err := startHTTP(w)
  1452. if err != nil {
  1453. t.Fatal("Unexpected error from getting base URL:", err)
  1454. }
  1455. defer cancel()
  1456. cli := &http.Client{
  1457. Timeout: time.Minute,
  1458. }
  1459. do := func(req *http.Request, status int) *http.Response {
  1460. t.Helper()
  1461. req.Header.Set("X-API-Key", testAPIKey)
  1462. resp, err := cli.Do(req)
  1463. if err != nil {
  1464. t.Fatal(err)
  1465. }
  1466. if resp.StatusCode != status {
  1467. t.Errorf("Expected status %v, got %v", status, resp.StatusCode)
  1468. }
  1469. return resp
  1470. }
  1471. mod := func(method, path string, data interface{}) {
  1472. t.Helper()
  1473. bs, err := json.Marshal(data)
  1474. if err != nil {
  1475. t.Fatal(err)
  1476. }
  1477. req, _ := http.NewRequest(method, baseURL+path, bytes.NewReader(bs))
  1478. do(req, http.StatusOK).Body.Close()
  1479. }
  1480. get := func(path string) *http.Response {
  1481. t.Helper()
  1482. req, _ := http.NewRequest(http.MethodGet, baseURL+path, nil)
  1483. return do(req, http.StatusOK)
  1484. }
  1485. dev1Path := "/rest/config/devices/" + dev1.String()
  1486. // Create device
  1487. mod(http.MethodPut, "/rest/config/devices", []config.DeviceConfiguration{{DeviceID: dev1}})
  1488. // Check its there
  1489. get(dev1Path).Body.Close()
  1490. // Modify just a single attribute
  1491. mod(http.MethodPatch, dev1Path, map[string]bool{"Paused": true})
  1492. // Check that attribute
  1493. resp := get(dev1Path)
  1494. var dev config.DeviceConfiguration
  1495. if err := unmarshalTo(resp.Body, &dev); err != nil {
  1496. t.Fatal(err)
  1497. }
  1498. if !dev.Paused {
  1499. t.Error("Expected device to be paused")
  1500. }
  1501. folder2Path := "/rest/config/folders/folder2"
  1502. // Create a folder and add another
  1503. mod(http.MethodPut, "/rest/config/folders", []config.FolderConfiguration{{ID: "folder1", Path: "folder1"}})
  1504. mod(http.MethodPut, folder2Path, config.FolderConfiguration{ID: "folder2", Path: "folder2"})
  1505. // Check they are there
  1506. get("/rest/config/folders/folder1").Body.Close()
  1507. get(folder2Path).Body.Close()
  1508. // Modify just a single attribute
  1509. mod(http.MethodPatch, folder2Path, map[string]bool{"Paused": true})
  1510. // Check that attribute
  1511. resp = get(folder2Path)
  1512. var folder config.FolderConfiguration
  1513. if err := unmarshalTo(resp.Body, &folder); err != nil {
  1514. t.Fatal(err)
  1515. }
  1516. if !dev.Paused {
  1517. t.Error("Expected folder to be paused")
  1518. }
  1519. // Delete folder2
  1520. req, _ := http.NewRequest(http.MethodDelete, baseURL+folder2Path, nil)
  1521. do(req, http.StatusOK)
  1522. // Check folder1 is still there and folder2 gone
  1523. get("/rest/config/folders/folder1").Body.Close()
  1524. req, _ = http.NewRequest(http.MethodGet, baseURL+folder2Path, nil)
  1525. do(req, http.StatusNotFound)
  1526. mod(http.MethodPatch, "/rest/config/options", map[string]int{"maxSendKbps": 50})
  1527. resp = get("/rest/config/options")
  1528. var opts config.OptionsConfiguration
  1529. if err := unmarshalTo(resp.Body, &opts); err != nil {
  1530. t.Fatal(err)
  1531. }
  1532. if opts.MaxSendKbps != 50 {
  1533. t.Error("Expected 50 for MaxSendKbps, got", opts.MaxSendKbps)
  1534. }
  1535. }
  1536. func TestSanitizedHostname(t *testing.T) {
  1537. cases := []struct {
  1538. in, out string
  1539. }{
  1540. {"foo.BAR-baz", "foo.bar-baz"},
  1541. {"~.~-Min 1:a Räksmörgås-dator 😀😎 ~.~-", "min1araksmorgas-dator"},
  1542. {"Vicenç-PC", "vicenc-pc"},
  1543. {"~.~-~.~-", ""},
  1544. {"", ""},
  1545. }
  1546. for _, tc := range cases {
  1547. res, err := sanitizedHostname(tc.in)
  1548. if tc.out == "" && err == nil {
  1549. t.Errorf("%q should cause error", tc.in)
  1550. } else if res != tc.out {
  1551. t.Errorf("%q => %q, expected %q", tc.in, res, tc.out)
  1552. }
  1553. }
  1554. }
  1555. // runningInContainer returns true if we are inside Docker or LXC. It might
  1556. // be prone to false negatives if things change in the future, but likely
  1557. // not false positives.
  1558. func runningInContainer() bool {
  1559. if !build.IsLinux {
  1560. return false
  1561. }
  1562. bs, err := os.ReadFile("/proc/1/cgroup")
  1563. if err != nil {
  1564. return false
  1565. }
  1566. if bytes.Contains(bs, []byte("/docker/")) {
  1567. return true
  1568. }
  1569. if bytes.Contains(bs, []byte("/lxc/")) {
  1570. return true
  1571. }
  1572. return false
  1573. }