17598: Handle comparison URLs with :80 or :443
[arvados.git] / services / keep-web / handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "context"
10         "fmt"
11         "html"
12         "io/ioutil"
13         "net/http"
14         "net/http/httptest"
15         "net/url"
16         "os"
17         "path/filepath"
18         "regexp"
19         "strings"
20         "time"
21
22         "git.arvados.org/arvados.git/lib/config"
23         "git.arvados.org/arvados.git/sdk/go/arvados"
24         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
25         "git.arvados.org/arvados.git/sdk/go/arvadostest"
26         "git.arvados.org/arvados.git/sdk/go/auth"
27         "git.arvados.org/arvados.git/sdk/go/ctxlog"
28         "git.arvados.org/arvados.git/sdk/go/keepclient"
29         "github.com/sirupsen/logrus"
30         check "gopkg.in/check.v1"
31 )
32
33 var _ = check.Suite(&UnitSuite{})
34
35 type UnitSuite struct {
36         Config *arvados.Config
37 }
38
39 func (s *UnitSuite) SetUpTest(c *check.C) {
40         ldr := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), ctxlog.TestLogger(c))
41         ldr.Path = "-"
42         cfg, err := ldr.Load()
43         c.Assert(err, check.IsNil)
44         s.Config = cfg
45 }
46
47 func (s *UnitSuite) TestCORSPreflight(c *check.C) {
48         h := handler{Config: newConfig(s.Config)}
49         u := mustParseURL("http://keep-web.example/c=" + arvadostest.FooCollection + "/foo")
50         req := &http.Request{
51                 Method:     "OPTIONS",
52                 Host:       u.Host,
53                 URL:        u,
54                 RequestURI: u.RequestURI(),
55                 Header: http.Header{
56                         "Origin":                        {"https://workbench.example"},
57                         "Access-Control-Request-Method": {"POST"},
58                 },
59         }
60
61         // Check preflight for an allowed request
62         resp := httptest.NewRecorder()
63         h.ServeHTTP(resp, req)
64         c.Check(resp.Code, check.Equals, http.StatusOK)
65         c.Check(resp.Body.String(), check.Equals, "")
66         c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
67         c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Equals, "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
68         c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Equals, "Authorization, Content-Type, Range, Depth, Destination, If, Lock-Token, Overwrite, Timeout")
69
70         // Check preflight for a disallowed request
71         resp = httptest.NewRecorder()
72         req.Header.Set("Access-Control-Request-Method", "MAKE-COFFEE")
73         h.ServeHTTP(resp, req)
74         c.Check(resp.Body.String(), check.Equals, "")
75         c.Check(resp.Code, check.Equals, http.StatusMethodNotAllowed)
76 }
77
78 func (s *UnitSuite) TestEmptyResponse(c *check.C) {
79         for _, trial := range []struct {
80                 dataExists    bool
81                 sendIMSHeader bool
82                 expectStatus  int
83                 logRegexp     string
84         }{
85                 // If we return no content due to a Keep read error,
86                 // we should emit a log message.
87                 {false, false, http.StatusOK, `(?ms).*only wrote 0 bytes.*`},
88
89                 // If we return no content because the client sent an
90                 // If-Modified-Since header, our response should be
91                 // 304, and we should not emit a log message.
92                 {true, true, http.StatusNotModified, ``},
93         } {
94                 c.Logf("trial: %+v", trial)
95                 arvadostest.StartKeep(2, true)
96                 if trial.dataExists {
97                         arv, err := arvadosclient.MakeArvadosClient()
98                         c.Assert(err, check.IsNil)
99                         arv.ApiToken = arvadostest.ActiveToken
100                         kc, err := keepclient.MakeKeepClient(arv)
101                         c.Assert(err, check.IsNil)
102                         _, _, err = kc.PutB([]byte("foo"))
103                         c.Assert(err, check.IsNil)
104                 }
105
106                 h := handler{Config: newConfig(s.Config)}
107                 u := mustParseURL("http://" + arvadostest.FooCollection + ".keep-web.example/foo")
108                 req := &http.Request{
109                         Method:     "GET",
110                         Host:       u.Host,
111                         URL:        u,
112                         RequestURI: u.RequestURI(),
113                         Header: http.Header{
114                                 "Authorization": {"Bearer " + arvadostest.ActiveToken},
115                         },
116                 }
117                 if trial.sendIMSHeader {
118                         req.Header.Set("If-Modified-Since", strings.Replace(time.Now().UTC().Format(time.RFC1123), "UTC", "GMT", -1))
119                 }
120
121                 var logbuf bytes.Buffer
122                 logger := logrus.New()
123                 logger.Out = &logbuf
124                 req = req.WithContext(ctxlog.Context(context.Background(), logger))
125
126                 resp := httptest.NewRecorder()
127                 h.ServeHTTP(resp, req)
128                 c.Check(resp.Code, check.Equals, trial.expectStatus)
129                 c.Check(resp.Body.String(), check.Equals, "")
130
131                 c.Log(logbuf.String())
132                 c.Check(logbuf.String(), check.Matches, trial.logRegexp)
133         }
134 }
135
136 func (s *UnitSuite) TestInvalidUUID(c *check.C) {
137         bogusID := strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + "-"
138         token := arvadostest.ActiveToken
139         for _, trial := range []string{
140                 "http://keep-web/c=" + bogusID + "/foo",
141                 "http://keep-web/c=" + bogusID + "/t=" + token + "/foo",
142                 "http://keep-web/collections/download/" + bogusID + "/" + token + "/foo",
143                 "http://keep-web/collections/" + bogusID + "/foo",
144                 "http://" + bogusID + ".keep-web/" + bogusID + "/foo",
145                 "http://" + bogusID + ".keep-web/t=" + token + "/" + bogusID + "/foo",
146         } {
147                 c.Log(trial)
148                 u := mustParseURL(trial)
149                 req := &http.Request{
150                         Method:     "GET",
151                         Host:       u.Host,
152                         URL:        u,
153                         RequestURI: u.RequestURI(),
154                 }
155                 resp := httptest.NewRecorder()
156                 cfg := newConfig(s.Config)
157                 cfg.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
158                 h := handler{Config: cfg}
159                 h.ServeHTTP(resp, req)
160                 c.Check(resp.Code, check.Equals, http.StatusNotFound)
161         }
162 }
163
164 func mustParseURL(s string) *url.URL {
165         r, err := url.Parse(s)
166         if err != nil {
167                 panic("parse URL: " + s)
168         }
169         return r
170 }
171
172 func (s *IntegrationSuite) TestVhost404(c *check.C) {
173         for _, testURL := range []string{
174                 arvadostest.NonexistentCollection + ".example.com/theperthcountyconspiracy",
175                 arvadostest.NonexistentCollection + ".example.com/t=" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
176         } {
177                 resp := httptest.NewRecorder()
178                 u := mustParseURL(testURL)
179                 req := &http.Request{
180                         Method:     "GET",
181                         URL:        u,
182                         RequestURI: u.RequestURI(),
183                 }
184                 s.testServer.Handler.ServeHTTP(resp, req)
185                 c.Check(resp.Code, check.Equals, http.StatusNotFound)
186                 c.Check(resp.Body.String(), check.Equals, notFoundMessage+"\n")
187         }
188 }
189
190 // An authorizer modifies an HTTP request to make use of the given
191 // token -- by adding it to a header, cookie, query param, or whatever
192 // -- and returns the HTTP status code we should expect from keep-web if
193 // the token is invalid.
194 type authorizer func(*http.Request, string) int
195
196 func (s *IntegrationSuite) TestVhostViaAuthzHeaderOAuth2(c *check.C) {
197         s.doVhostRequests(c, authzViaAuthzHeaderOAuth2)
198 }
199 func authzViaAuthzHeaderOAuth2(r *http.Request, tok string) int {
200         r.Header.Add("Authorization", "Bearer "+tok)
201         return http.StatusUnauthorized
202 }
203 func (s *IntegrationSuite) TestVhostViaAuthzHeaderBearer(c *check.C) {
204         s.doVhostRequests(c, authzViaAuthzHeaderBearer)
205 }
206 func authzViaAuthzHeaderBearer(r *http.Request, tok string) int {
207         r.Header.Add("Authorization", "Bearer "+tok)
208         return http.StatusUnauthorized
209 }
210
211 func (s *IntegrationSuite) TestVhostViaCookieValue(c *check.C) {
212         s.doVhostRequests(c, authzViaCookieValue)
213 }
214 func authzViaCookieValue(r *http.Request, tok string) int {
215         r.AddCookie(&http.Cookie{
216                 Name:  "arvados_api_token",
217                 Value: auth.EncodeTokenCookie([]byte(tok)),
218         })
219         return http.StatusUnauthorized
220 }
221
222 func (s *IntegrationSuite) TestVhostViaPath(c *check.C) {
223         s.doVhostRequests(c, authzViaPath)
224 }
225 func authzViaPath(r *http.Request, tok string) int {
226         r.URL.Path = "/t=" + tok + r.URL.Path
227         return http.StatusNotFound
228 }
229
230 func (s *IntegrationSuite) TestVhostViaQueryString(c *check.C) {
231         s.doVhostRequests(c, authzViaQueryString)
232 }
233 func authzViaQueryString(r *http.Request, tok string) int {
234         r.URL.RawQuery = "api_token=" + tok
235         return http.StatusUnauthorized
236 }
237
238 func (s *IntegrationSuite) TestVhostViaPOST(c *check.C) {
239         s.doVhostRequests(c, authzViaPOST)
240 }
241 func authzViaPOST(r *http.Request, tok string) int {
242         r.Method = "POST"
243         r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
244         r.Body = ioutil.NopCloser(strings.NewReader(
245                 url.Values{"api_token": {tok}}.Encode()))
246         return http.StatusUnauthorized
247 }
248
249 func (s *IntegrationSuite) TestVhostViaXHRPOST(c *check.C) {
250         s.doVhostRequests(c, authzViaPOST)
251 }
252 func authzViaXHRPOST(r *http.Request, tok string) int {
253         r.Method = "POST"
254         r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
255         r.Header.Add("Origin", "https://origin.example")
256         r.Body = ioutil.NopCloser(strings.NewReader(
257                 url.Values{
258                         "api_token":   {tok},
259                         "disposition": {"attachment"},
260                 }.Encode()))
261         return http.StatusUnauthorized
262 }
263
264 // Try some combinations of {url, token} using the given authorization
265 // mechanism, and verify the result is correct.
266 func (s *IntegrationSuite) doVhostRequests(c *check.C, authz authorizer) {
267         for _, hostPath := range []string{
268                 arvadostest.FooCollection + ".example.com/foo",
269                 arvadostest.FooCollection + "--collections.example.com/foo",
270                 arvadostest.FooCollection + "--collections.example.com/_/foo",
271                 arvadostest.FooCollectionPDH + ".example.com/foo",
272                 strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + "--collections.example.com/foo",
273                 arvadostest.FooBarDirCollection + ".example.com/dir1/foo",
274         } {
275                 c.Log("doRequests: ", hostPath)
276                 s.doVhostRequestsWithHostPath(c, authz, hostPath)
277         }
278 }
279
280 func (s *IntegrationSuite) doVhostRequestsWithHostPath(c *check.C, authz authorizer, hostPath string) {
281         for _, tok := range []string{
282                 arvadostest.ActiveToken,
283                 arvadostest.ActiveToken[:15],
284                 arvadostest.SpectatorToken,
285                 "bogus",
286                 "",
287         } {
288                 u := mustParseURL("http://" + hostPath)
289                 req := &http.Request{
290                         Method:     "GET",
291                         Host:       u.Host,
292                         URL:        u,
293                         RequestURI: u.RequestURI(),
294                         Header:     http.Header{},
295                 }
296                 failCode := authz(req, tok)
297                 req, resp := s.doReq(req)
298                 code, body := resp.Code, resp.Body.String()
299
300                 // If the initial request had a (non-empty) token
301                 // showing in the query string, we should have been
302                 // redirected in order to hide it in a cookie.
303                 c.Check(req.URL.String(), check.Not(check.Matches), `.*api_token=.+`)
304
305                 if tok == arvadostest.ActiveToken {
306                         c.Check(code, check.Equals, http.StatusOK)
307                         c.Check(body, check.Equals, "foo")
308                 } else {
309                         c.Check(code >= 400, check.Equals, true)
310                         c.Check(code < 500, check.Equals, true)
311                         if tok == arvadostest.SpectatorToken {
312                                 // Valid token never offers to retry
313                                 // with different credentials.
314                                 c.Check(code, check.Equals, http.StatusNotFound)
315                         } else {
316                                 // Invalid token can ask to retry
317                                 // depending on the authz method.
318                                 c.Check(code, check.Equals, failCode)
319                         }
320                         if code == 404 {
321                                 c.Check(body, check.Equals, notFoundMessage+"\n")
322                         } else {
323                                 c.Check(body, check.Equals, unauthorizedMessage+"\n")
324                         }
325                 }
326         }
327 }
328
329 func (s *IntegrationSuite) TestVhostPortMatch(c *check.C) {
330         for _, port := range []string{"80", "443", "8000"} {
331                 s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = fmt.Sprintf("download.example.com:%v", port)
332                 u := mustParseURL(fmt.Sprintf("http://download.example.com/by_id/%v/foo", arvadostest.FooCollection))
333                 req := &http.Request{
334                         Method:     "GET",
335                         Host:       u.Host,
336                         URL:        u,
337                         RequestURI: u.RequestURI(),
338                         Header:     http.Header{"Authorization": []string{"Bearer " + arvadostest.ActiveToken}},
339                 }
340                 req, resp := s.doReq(req)
341                 code, _ := resp.Code, resp.Body.String()
342
343                 if port == "8000" {
344                         c.Check(code, check.Equals, 401)
345                 } else {
346                         c.Check(code, check.Equals, 200)
347                 }
348         }
349 }
350
351 func (s *IntegrationSuite) doReq(req *http.Request) (*http.Request, *httptest.ResponseRecorder) {
352         resp := httptest.NewRecorder()
353         s.testServer.Handler.ServeHTTP(resp, req)
354         if resp.Code != http.StatusSeeOther {
355                 return req, resp
356         }
357         cookies := (&http.Response{Header: resp.Header()}).Cookies()
358         u, _ := req.URL.Parse(resp.Header().Get("Location"))
359         req = &http.Request{
360                 Method:     "GET",
361                 Host:       u.Host,
362                 URL:        u,
363                 RequestURI: u.RequestURI(),
364                 Header:     http.Header{},
365         }
366         for _, c := range cookies {
367                 req.AddCookie(c)
368         }
369         return s.doReq(req)
370 }
371
372 func (s *IntegrationSuite) TestVhostRedirectQueryTokenToCookie(c *check.C) {
373         s.testVhostRedirectTokenToCookie(c, "GET",
374                 arvadostest.FooCollection+".example.com/foo",
375                 "?api_token="+arvadostest.ActiveToken,
376                 "",
377                 "",
378                 http.StatusOK,
379                 "foo",
380         )
381 }
382
383 func (s *IntegrationSuite) TestSingleOriginSecretLink(c *check.C) {
384         s.testVhostRedirectTokenToCookie(c, "GET",
385                 "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.ActiveToken+"/foo",
386                 "",
387                 "",
388                 "",
389                 http.StatusOK,
390                 "foo",
391         )
392 }
393
394 // Bad token in URL is 404 Not Found because it doesn't make sense to
395 // retry the same URL with different authorization.
396 func (s *IntegrationSuite) TestSingleOriginSecretLinkBadToken(c *check.C) {
397         s.testVhostRedirectTokenToCookie(c, "GET",
398                 "example.com/c="+arvadostest.FooCollection+"/t=bogus/foo",
399                 "",
400                 "",
401                 "",
402                 http.StatusNotFound,
403                 notFoundMessage+"\n",
404         )
405 }
406
407 // Bad token in a cookie (even if it got there via our own
408 // query-string-to-cookie redirect) is, in principle, retryable at the
409 // same URL so it's 401 Unauthorized.
410 func (s *IntegrationSuite) TestVhostRedirectQueryTokenToBogusCookie(c *check.C) {
411         s.testVhostRedirectTokenToCookie(c, "GET",
412                 arvadostest.FooCollection+".example.com/foo",
413                 "?api_token=thisisabogustoken",
414                 "",
415                 "",
416                 http.StatusUnauthorized,
417                 unauthorizedMessage+"\n",
418         )
419 }
420
421 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSingleOriginError(c *check.C) {
422         s.testVhostRedirectTokenToCookie(c, "GET",
423                 "example.com/c="+arvadostest.FooCollection+"/foo",
424                 "?api_token="+arvadostest.ActiveToken,
425                 "",
426                 "",
427                 http.StatusBadRequest,
428                 "cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)\n",
429         )
430 }
431
432 // If client requests an attachment by putting ?disposition=attachment
433 // in the query string, and gets redirected, the redirect target
434 // should respond with an attachment.
435 func (s *IntegrationSuite) TestVhostRedirectQueryTokenRequestAttachment(c *check.C) {
436         resp := s.testVhostRedirectTokenToCookie(c, "GET",
437                 arvadostest.FooCollection+".example.com/foo",
438                 "?disposition=attachment&api_token="+arvadostest.ActiveToken,
439                 "",
440                 "",
441                 http.StatusOK,
442                 "foo",
443         )
444         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
445 }
446
447 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSiteFS(c *check.C) {
448         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
449         resp := s.testVhostRedirectTokenToCookie(c, "GET",
450                 "download.example.com/by_id/"+arvadostest.FooCollection+"/foo",
451                 "?api_token="+arvadostest.ActiveToken,
452                 "",
453                 "",
454                 http.StatusOK,
455                 "foo",
456         )
457         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
458 }
459
460 func (s *IntegrationSuite) TestPastCollectionVersionFileAccess(c *check.C) {
461         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
462         resp := s.testVhostRedirectTokenToCookie(c, "GET",
463                 "download.example.com/c="+arvadostest.WazVersion1Collection+"/waz",
464                 "?api_token="+arvadostest.ActiveToken,
465                 "",
466                 "",
467                 http.StatusOK,
468                 "waz",
469         )
470         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
471         resp = s.testVhostRedirectTokenToCookie(c, "GET",
472                 "download.example.com/by_id/"+arvadostest.WazVersion1Collection+"/waz",
473                 "?api_token="+arvadostest.ActiveToken,
474                 "",
475                 "",
476                 http.StatusOK,
477                 "waz",
478         )
479         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
480 }
481
482 func (s *IntegrationSuite) TestVhostRedirectQueryTokenTrustAllContent(c *check.C) {
483         s.testServer.Config.cluster.Collections.TrustAllContent = true
484         s.testVhostRedirectTokenToCookie(c, "GET",
485                 "example.com/c="+arvadostest.FooCollection+"/foo",
486                 "?api_token="+arvadostest.ActiveToken,
487                 "",
488                 "",
489                 http.StatusOK,
490                 "foo",
491         )
492 }
493
494 func (s *IntegrationSuite) TestVhostRedirectQueryTokenAttachmentOnlyHost(c *check.C) {
495         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "example.com:1234"
496
497         s.testVhostRedirectTokenToCookie(c, "GET",
498                 "example.com/c="+arvadostest.FooCollection+"/foo",
499                 "?api_token="+arvadostest.ActiveToken,
500                 "",
501                 "",
502                 http.StatusBadRequest,
503                 "cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)\n",
504         )
505
506         resp := s.testVhostRedirectTokenToCookie(c, "GET",
507                 "example.com:1234/c="+arvadostest.FooCollection+"/foo",
508                 "?api_token="+arvadostest.ActiveToken,
509                 "",
510                 "",
511                 http.StatusOK,
512                 "foo",
513         )
514         c.Check(resp.Header().Get("Content-Disposition"), check.Equals, "attachment")
515 }
516
517 func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie(c *check.C) {
518         s.testVhostRedirectTokenToCookie(c, "POST",
519                 arvadostest.FooCollection+".example.com/foo",
520                 "",
521                 "application/x-www-form-urlencoded",
522                 url.Values{"api_token": {arvadostest.ActiveToken}}.Encode(),
523                 http.StatusOK,
524                 "foo",
525         )
526 }
527
528 func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie404(c *check.C) {
529         s.testVhostRedirectTokenToCookie(c, "POST",
530                 arvadostest.FooCollection+".example.com/foo",
531                 "",
532                 "application/x-www-form-urlencoded",
533                 url.Values{"api_token": {arvadostest.SpectatorToken}}.Encode(),
534                 http.StatusNotFound,
535                 notFoundMessage+"\n",
536         )
537 }
538
539 func (s *IntegrationSuite) TestAnonymousTokenOK(c *check.C) {
540         s.testServer.Config.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
541         s.testVhostRedirectTokenToCookie(c, "GET",
542                 "example.com/c="+arvadostest.HelloWorldCollection+"/Hello%20world.txt",
543                 "",
544                 "",
545                 "",
546                 http.StatusOK,
547                 "Hello world\n",
548         )
549 }
550
551 func (s *IntegrationSuite) TestAnonymousTokenError(c *check.C) {
552         s.testServer.Config.cluster.Users.AnonymousUserToken = "anonymousTokenConfiguredButInvalid"
553         s.testVhostRedirectTokenToCookie(c, "GET",
554                 "example.com/c="+arvadostest.HelloWorldCollection+"/Hello%20world.txt",
555                 "",
556                 "",
557                 "",
558                 http.StatusNotFound,
559                 notFoundMessage+"\n",
560         )
561 }
562
563 func (s *IntegrationSuite) TestSpecialCharsInPath(c *check.C) {
564         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
565
566         client := s.testServer.Config.Client
567         client.AuthToken = arvadostest.ActiveToken
568         fs, err := (&arvados.Collection{}).FileSystem(&client, nil)
569         c.Assert(err, check.IsNil)
570         f, err := fs.OpenFile("https:\\\"odd' path chars", os.O_CREATE, 0777)
571         c.Assert(err, check.IsNil)
572         f.Close()
573         mtxt, err := fs.MarshalManifest(".")
574         c.Assert(err, check.IsNil)
575         var coll arvados.Collection
576         err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
577                 "collection": map[string]string{
578                         "manifest_text": mtxt,
579                 },
580         })
581         c.Assert(err, check.IsNil)
582
583         u, _ := url.Parse("http://download.example.com/c=" + coll.UUID + "/")
584         req := &http.Request{
585                 Method:     "GET",
586                 Host:       u.Host,
587                 URL:        u,
588                 RequestURI: u.RequestURI(),
589                 Header: http.Header{
590                         "Authorization": {"Bearer " + client.AuthToken},
591                 },
592         }
593         resp := httptest.NewRecorder()
594         s.testServer.Handler.ServeHTTP(resp, req)
595         c.Check(resp.Code, check.Equals, http.StatusOK)
596         c.Check(resp.Body.String(), check.Matches, `(?ms).*href="./https:%5c%22odd%27%20path%20chars"\S+https:\\&#34;odd&#39; path chars.*`)
597 }
598
599 func (s *IntegrationSuite) TestForwardSlashSubstitution(c *check.C) {
600         arv := arvados.NewClientFromEnv()
601         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
602         s.testServer.Config.cluster.Collections.ForwardSlashNameSubstitution = "{SOLIDUS}"
603         name := "foo/bar/baz"
604         nameShown := strings.Replace(name, "/", "{SOLIDUS}", -1)
605         nameShownEscaped := strings.Replace(name, "/", "%7bSOLIDUS%7d", -1)
606
607         client := s.testServer.Config.Client
608         client.AuthToken = arvadostest.ActiveToken
609         fs, err := (&arvados.Collection{}).FileSystem(&client, nil)
610         c.Assert(err, check.IsNil)
611         f, err := fs.OpenFile("filename", os.O_CREATE, 0777)
612         c.Assert(err, check.IsNil)
613         f.Close()
614         mtxt, err := fs.MarshalManifest(".")
615         c.Assert(err, check.IsNil)
616         var coll arvados.Collection
617         err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
618                 "collection": map[string]string{
619                         "manifest_text": mtxt,
620                         "name":          name,
621                         "owner_uuid":    arvadostest.AProjectUUID,
622                 },
623         })
624         c.Assert(err, check.IsNil)
625         defer arv.RequestAndDecode(&coll, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
626
627         base := "http://download.example.com/by_id/" + coll.OwnerUUID + "/"
628         for tryURL, expectRegexp := range map[string]string{
629                 base:                          `(?ms).*href="./` + nameShownEscaped + `/"\S+` + nameShown + `.*`,
630                 base + nameShownEscaped + "/": `(?ms).*href="./filename"\S+filename.*`,
631         } {
632                 u, _ := url.Parse(tryURL)
633                 req := &http.Request{
634                         Method:     "GET",
635                         Host:       u.Host,
636                         URL:        u,
637                         RequestURI: u.RequestURI(),
638                         Header: http.Header{
639                                 "Authorization": {"Bearer " + client.AuthToken},
640                         },
641                 }
642                 resp := httptest.NewRecorder()
643                 s.testServer.Handler.ServeHTTP(resp, req)
644                 c.Check(resp.Code, check.Equals, http.StatusOK)
645                 c.Check(resp.Body.String(), check.Matches, expectRegexp)
646         }
647 }
648
649 // XHRs can't follow redirect-with-cookie so they rely on method=POST
650 // and disposition=attachment (telling us it's acceptable to respond
651 // with content instead of a redirect) and an Origin header that gets
652 // added automatically by the browser (telling us it's desirable to do
653 // so).
654 func (s *IntegrationSuite) TestXHRNoRedirect(c *check.C) {
655         u, _ := url.Parse("http://example.com/c=" + arvadostest.FooCollection + "/foo")
656         req := &http.Request{
657                 Method:     "POST",
658                 Host:       u.Host,
659                 URL:        u,
660                 RequestURI: u.RequestURI(),
661                 Header: http.Header{
662                         "Origin":       {"https://origin.example"},
663                         "Content-Type": {"application/x-www-form-urlencoded"},
664                 },
665                 Body: ioutil.NopCloser(strings.NewReader(url.Values{
666                         "api_token":   {arvadostest.ActiveToken},
667                         "disposition": {"attachment"},
668                 }.Encode())),
669         }
670         resp := httptest.NewRecorder()
671         s.testServer.Handler.ServeHTTP(resp, req)
672         c.Check(resp.Code, check.Equals, http.StatusOK)
673         c.Check(resp.Body.String(), check.Equals, "foo")
674         c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
675
676         // GET + Origin header is representative of both AJAX GET
677         // requests and inline images via <IMG crossorigin="anonymous"
678         // src="...">.
679         u.RawQuery = "api_token=" + url.QueryEscape(arvadostest.ActiveTokenV2)
680         req = &http.Request{
681                 Method:     "GET",
682                 Host:       u.Host,
683                 URL:        u,
684                 RequestURI: u.RequestURI(),
685                 Header: http.Header{
686                         "Origin": {"https://origin.example"},
687                 },
688         }
689         resp = httptest.NewRecorder()
690         s.testServer.Handler.ServeHTTP(resp, req)
691         c.Check(resp.Code, check.Equals, http.StatusOK)
692         c.Check(resp.Body.String(), check.Equals, "foo")
693         c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
694 }
695
696 func (s *IntegrationSuite) testVhostRedirectTokenToCookie(c *check.C, method, hostPath, queryString, contentType, reqBody string, expectStatus int, expectRespBody string) *httptest.ResponseRecorder {
697         u, _ := url.Parse(`http://` + hostPath + queryString)
698         req := &http.Request{
699                 Method:     method,
700                 Host:       u.Host,
701                 URL:        u,
702                 RequestURI: u.RequestURI(),
703                 Header:     http.Header{"Content-Type": {contentType}},
704                 Body:       ioutil.NopCloser(strings.NewReader(reqBody)),
705         }
706
707         resp := httptest.NewRecorder()
708         defer func() {
709                 c.Check(resp.Code, check.Equals, expectStatus)
710                 c.Check(resp.Body.String(), check.Equals, expectRespBody)
711         }()
712
713         s.testServer.Handler.ServeHTTP(resp, req)
714         if resp.Code != http.StatusSeeOther {
715                 return resp
716         }
717         c.Check(resp.Body.String(), check.Matches, `.*href="http://`+regexp.QuoteMeta(html.EscapeString(hostPath))+`(\?[^"]*)?".*`)
718         cookies := (&http.Response{Header: resp.Header()}).Cookies()
719
720         u, _ = u.Parse(resp.Header().Get("Location"))
721         req = &http.Request{
722                 Method:     "GET",
723                 Host:       u.Host,
724                 URL:        u,
725                 RequestURI: u.RequestURI(),
726                 Header:     http.Header{},
727         }
728         for _, c := range cookies {
729                 req.AddCookie(c)
730         }
731
732         resp = httptest.NewRecorder()
733         s.testServer.Handler.ServeHTTP(resp, req)
734         c.Check(resp.Header().Get("Location"), check.Equals, "")
735         return resp
736 }
737
738 func (s *IntegrationSuite) TestDirectoryListingWithAnonymousToken(c *check.C) {
739         s.testServer.Config.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
740         s.testDirectoryListing(c)
741 }
742
743 func (s *IntegrationSuite) TestDirectoryListingWithNoAnonymousToken(c *check.C) {
744         s.testServer.Config.cluster.Users.AnonymousUserToken = ""
745         s.testDirectoryListing(c)
746 }
747
748 func (s *IntegrationSuite) testDirectoryListing(c *check.C) {
749         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
750         authHeader := http.Header{
751                 "Authorization": {"OAuth2 " + arvadostest.ActiveToken},
752         }
753         for _, trial := range []struct {
754                 uri      string
755                 header   http.Header
756                 expect   []string
757                 redirect string
758                 cutDirs  int
759         }{
760                 {
761                         uri:     strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + ".example.com/",
762                         header:  authHeader,
763                         expect:  []string{"dir1/foo", "dir1/bar"},
764                         cutDirs: 0,
765                 },
766                 {
767                         uri:     strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + ".example.com/dir1/",
768                         header:  authHeader,
769                         expect:  []string{"foo", "bar"},
770                         cutDirs: 1,
771                 },
772                 {
773                         // URLs of this form ignore authHeader, and
774                         // FooAndBarFilesInDirUUID isn't public, so
775                         // this returns 401.
776                         uri:    "download.example.com/collections/" + arvadostest.FooAndBarFilesInDirUUID + "/",
777                         header: authHeader,
778                         expect: nil,
779                 },
780                 {
781                         uri:     "download.example.com/users/active/foo_file_in_dir/",
782                         header:  authHeader,
783                         expect:  []string{"dir1/"},
784                         cutDirs: 3,
785                 },
786                 {
787                         uri:     "download.example.com/users/active/foo_file_in_dir/dir1/",
788                         header:  authHeader,
789                         expect:  []string{"bar"},
790                         cutDirs: 4,
791                 },
792                 {
793                         uri:     "download.example.com/",
794                         header:  authHeader,
795                         expect:  []string{"users/"},
796                         cutDirs: 0,
797                 },
798                 {
799                         uri:      "download.example.com/users",
800                         header:   authHeader,
801                         redirect: "/users/",
802                         expect:   []string{"active/"},
803                         cutDirs:  1,
804                 },
805                 {
806                         uri:     "download.example.com/users/",
807                         header:  authHeader,
808                         expect:  []string{"active/"},
809                         cutDirs: 1,
810                 },
811                 {
812                         uri:      "download.example.com/users/active",
813                         header:   authHeader,
814                         redirect: "/users/active/",
815                         expect:   []string{"foo_file_in_dir/"},
816                         cutDirs:  2,
817                 },
818                 {
819                         uri:     "download.example.com/users/active/",
820                         header:  authHeader,
821                         expect:  []string{"foo_file_in_dir/"},
822                         cutDirs: 2,
823                 },
824                 {
825                         uri:     "collections.example.com/collections/download/" + arvadostest.FooAndBarFilesInDirUUID + "/" + arvadostest.ActiveToken + "/",
826                         header:  nil,
827                         expect:  []string{"dir1/foo", "dir1/bar"},
828                         cutDirs: 4,
829                 },
830                 {
831                         uri:     "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/",
832                         header:  nil,
833                         expect:  []string{"dir1/foo", "dir1/bar"},
834                         cutDirs: 2,
835                 },
836                 {
837                         uri:     "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken,
838                         header:  nil,
839                         expect:  []string{"dir1/foo", "dir1/bar"},
840                         cutDirs: 2,
841                 },
842                 {
843                         uri:     "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID,
844                         header:  authHeader,
845                         expect:  []string{"dir1/foo", "dir1/bar"},
846                         cutDirs: 1,
847                 },
848                 {
849                         uri:      "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/dir1",
850                         header:   authHeader,
851                         redirect: "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/dir1/",
852                         expect:   []string{"foo", "bar"},
853                         cutDirs:  2,
854                 },
855                 {
856                         uri:     "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/_/dir1/",
857                         header:  authHeader,
858                         expect:  []string{"foo", "bar"},
859                         cutDirs: 3,
860                 },
861                 {
862                         uri:      arvadostest.FooAndBarFilesInDirUUID + ".example.com/dir1?api_token=" + arvadostest.ActiveToken,
863                         header:   authHeader,
864                         redirect: "/dir1/",
865                         expect:   []string{"foo", "bar"},
866                         cutDirs:  1,
867                 },
868                 {
869                         uri:    "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/theperthcountyconspiracydoesnotexist/",
870                         header: authHeader,
871                         expect: nil,
872                 },
873                 {
874                         uri:     "download.example.com/c=" + arvadostest.WazVersion1Collection,
875                         header:  authHeader,
876                         expect:  []string{"waz"},
877                         cutDirs: 1,
878                 },
879                 {
880                         uri:     "download.example.com/by_id/" + arvadostest.WazVersion1Collection,
881                         header:  authHeader,
882                         expect:  []string{"waz"},
883                         cutDirs: 2,
884                 },
885         } {
886                 comment := check.Commentf("HTML: %q => %q", trial.uri, trial.expect)
887                 resp := httptest.NewRecorder()
888                 u := mustParseURL("//" + trial.uri)
889                 req := &http.Request{
890                         Method:     "GET",
891                         Host:       u.Host,
892                         URL:        u,
893                         RequestURI: u.RequestURI(),
894                         Header:     copyHeader(trial.header),
895                 }
896                 s.testServer.Handler.ServeHTTP(resp, req)
897                 var cookies []*http.Cookie
898                 for resp.Code == http.StatusSeeOther {
899                         u, _ := req.URL.Parse(resp.Header().Get("Location"))
900                         req = &http.Request{
901                                 Method:     "GET",
902                                 Host:       u.Host,
903                                 URL:        u,
904                                 RequestURI: u.RequestURI(),
905                                 Header:     copyHeader(trial.header),
906                         }
907                         cookies = append(cookies, (&http.Response{Header: resp.Header()}).Cookies()...)
908                         for _, c := range cookies {
909                                 req.AddCookie(c)
910                         }
911                         resp = httptest.NewRecorder()
912                         s.testServer.Handler.ServeHTTP(resp, req)
913                 }
914                 if trial.redirect != "" {
915                         c.Check(req.URL.Path, check.Equals, trial.redirect, comment)
916                 }
917                 if trial.expect == nil {
918                         if s.testServer.Config.cluster.Users.AnonymousUserToken == "" {
919                                 c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
920                         } else {
921                                 c.Check(resp.Code, check.Equals, http.StatusNotFound, comment)
922                         }
923                 } else {
924                         c.Check(resp.Code, check.Equals, http.StatusOK, comment)
925                         for _, e := range trial.expect {
926                                 c.Check(resp.Body.String(), check.Matches, `(?ms).*href="./`+e+`".*`, comment)
927                         }
928                         c.Check(resp.Body.String(), check.Matches, `(?ms).*--cut-dirs=`+fmt.Sprintf("%d", trial.cutDirs)+` .*`, comment)
929                 }
930
931                 comment = check.Commentf("WebDAV: %q => %q", trial.uri, trial.expect)
932                 req = &http.Request{
933                         Method:     "OPTIONS",
934                         Host:       u.Host,
935                         URL:        u,
936                         RequestURI: u.RequestURI(),
937                         Header:     copyHeader(trial.header),
938                         Body:       ioutil.NopCloser(&bytes.Buffer{}),
939                 }
940                 resp = httptest.NewRecorder()
941                 s.testServer.Handler.ServeHTTP(resp, req)
942                 if trial.expect == nil {
943                         if s.testServer.Config.cluster.Users.AnonymousUserToken == "" {
944                                 c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
945                         } else {
946                                 c.Check(resp.Code, check.Equals, http.StatusNotFound, comment)
947                         }
948                 } else {
949                         c.Check(resp.Code, check.Equals, http.StatusOK, comment)
950                 }
951
952                 req = &http.Request{
953                         Method:     "PROPFIND",
954                         Host:       u.Host,
955                         URL:        u,
956                         RequestURI: u.RequestURI(),
957                         Header:     copyHeader(trial.header),
958                         Body:       ioutil.NopCloser(&bytes.Buffer{}),
959                 }
960                 resp = httptest.NewRecorder()
961                 s.testServer.Handler.ServeHTTP(resp, req)
962                 if trial.expect == nil {
963                         if s.testServer.Config.cluster.Users.AnonymousUserToken == "" {
964                                 c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
965                         } else {
966                                 c.Check(resp.Code, check.Equals, http.StatusNotFound, comment)
967                         }
968                 } else {
969                         c.Check(resp.Code, check.Equals, http.StatusMultiStatus, comment)
970                         for _, e := range trial.expect {
971                                 if strings.HasSuffix(e, "/") {
972                                         e = filepath.Join(u.Path, e) + "/"
973                                 } else {
974                                         e = filepath.Join(u.Path, e)
975                                 }
976                                 c.Check(resp.Body.String(), check.Matches, `(?ms).*<D:href>`+e+`</D:href>.*`, comment)
977                         }
978                 }
979         }
980 }
981
982 func (s *IntegrationSuite) TestDeleteLastFile(c *check.C) {
983         arv := arvados.NewClientFromEnv()
984         var newCollection arvados.Collection
985         err := arv.RequestAndDecode(&newCollection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
986                 "collection": map[string]string{
987                         "owner_uuid":    arvadostest.ActiveUserUUID,
988                         "manifest_text": ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt 0:3:bar.txt\n",
989                         "name":          "keep-web test collection",
990                 },
991                 "ensure_unique_name": true,
992         })
993         c.Assert(err, check.IsNil)
994         defer arv.RequestAndDecode(&newCollection, "DELETE", "arvados/v1/collections/"+newCollection.UUID, nil, nil)
995
996         var updated arvados.Collection
997         for _, fnm := range []string{"foo.txt", "bar.txt"} {
998                 s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "example.com"
999                 u, _ := url.Parse("http://example.com/c=" + newCollection.UUID + "/" + fnm)
1000                 req := &http.Request{
1001                         Method:     "DELETE",
1002                         Host:       u.Host,
1003                         URL:        u,
1004                         RequestURI: u.RequestURI(),
1005                         Header: http.Header{
1006                                 "Authorization": {"Bearer " + arvadostest.ActiveToken},
1007                         },
1008                 }
1009                 resp := httptest.NewRecorder()
1010                 s.testServer.Handler.ServeHTTP(resp, req)
1011                 c.Check(resp.Code, check.Equals, http.StatusNoContent)
1012
1013                 updated = arvados.Collection{}
1014                 err = arv.RequestAndDecode(&updated, "GET", "arvados/v1/collections/"+newCollection.UUID, nil, nil)
1015                 c.Check(err, check.IsNil)
1016                 c.Check(updated.ManifestText, check.Not(check.Matches), `(?ms).*\Q`+fnm+`\E.*`)
1017                 c.Logf("updated manifest_text %q", updated.ManifestText)
1018         }
1019         c.Check(updated.ManifestText, check.Equals, "")
1020 }
1021
1022 func (s *IntegrationSuite) TestHealthCheckPing(c *check.C) {
1023         s.testServer.Config.cluster.ManagementToken = arvadostest.ManagementToken
1024         authHeader := http.Header{
1025                 "Authorization": {"Bearer " + arvadostest.ManagementToken},
1026         }
1027
1028         resp := httptest.NewRecorder()
1029         u := mustParseURL("http://download.example.com/_health/ping")
1030         req := &http.Request{
1031                 Method:     "GET",
1032                 Host:       u.Host,
1033                 URL:        u,
1034                 RequestURI: u.RequestURI(),
1035                 Header:     authHeader,
1036         }
1037         s.testServer.Handler.ServeHTTP(resp, req)
1038
1039         c.Check(resp.Code, check.Equals, http.StatusOK)
1040         c.Check(resp.Body.String(), check.Matches, `{"health":"OK"}\n`)
1041 }
1042
1043 func (s *IntegrationSuite) TestFileContentType(c *check.C) {
1044         s.testServer.Config.cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1045
1046         client := s.testServer.Config.Client
1047         client.AuthToken = arvadostest.ActiveToken
1048         arv, err := arvadosclient.New(&client)
1049         c.Assert(err, check.Equals, nil)
1050         kc, err := keepclient.MakeKeepClient(arv)
1051         c.Assert(err, check.Equals, nil)
1052
1053         fs, err := (&arvados.Collection{}).FileSystem(&client, kc)
1054         c.Assert(err, check.IsNil)
1055
1056         trials := []struct {
1057                 filename    string
1058                 content     string
1059                 contentType string
1060         }{
1061                 {"picture.txt", "BMX bikes are small this year\n", "text/plain; charset=utf-8"},
1062                 {"picture.bmp", "BMX bikes are small this year\n", "image/x-ms-bmp"},
1063                 {"picture.jpg", "BMX bikes are small this year\n", "image/jpeg"},
1064                 {"picture1", "BMX bikes are small this year\n", "image/bmp"},            // content sniff; "BM" is the magic signature for .bmp
1065                 {"picture2", "Cars are small this year\n", "text/plain; charset=utf-8"}, // content sniff
1066         }
1067         for _, trial := range trials {
1068                 f, err := fs.OpenFile(trial.filename, os.O_CREATE|os.O_WRONLY, 0777)
1069                 c.Assert(err, check.IsNil)
1070                 _, err = f.Write([]byte(trial.content))
1071                 c.Assert(err, check.IsNil)
1072                 c.Assert(f.Close(), check.IsNil)
1073         }
1074         mtxt, err := fs.MarshalManifest(".")
1075         c.Assert(err, check.IsNil)
1076         var coll arvados.Collection
1077         err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1078                 "collection": map[string]string{
1079                         "manifest_text": mtxt,
1080                 },
1081         })
1082         c.Assert(err, check.IsNil)
1083
1084         for _, trial := range trials {
1085                 u, _ := url.Parse("http://download.example.com/by_id/" + coll.UUID + "/" + trial.filename)
1086                 req := &http.Request{
1087                         Method:     "GET",
1088                         Host:       u.Host,
1089                         URL:        u,
1090                         RequestURI: u.RequestURI(),
1091                         Header: http.Header{
1092                                 "Authorization": {"Bearer " + client.AuthToken},
1093                         },
1094                 }
1095                 resp := httptest.NewRecorder()
1096                 s.testServer.Handler.ServeHTTP(resp, req)
1097                 c.Check(resp.Code, check.Equals, http.StatusOK)
1098                 c.Check(resp.Header().Get("Content-Type"), check.Equals, trial.contentType)
1099                 c.Check(resp.Body.String(), check.Equals, trial.content)
1100         }
1101 }
1102
1103 func (s *IntegrationSuite) TestKeepClientBlockCache(c *check.C) {
1104         s.testServer.Config.cluster.Collections.WebDAVCache.MaxBlockEntries = 42
1105         c.Check(keepclient.DefaultBlockCache.MaxBlocks, check.Not(check.Equals), 42)
1106         u := mustParseURL("http://keep-web.example/c=" + arvadostest.FooCollection + "/t=" + arvadostest.ActiveToken + "/foo")
1107         req := &http.Request{
1108                 Method:     "GET",
1109                 Host:       u.Host,
1110                 URL:        u,
1111                 RequestURI: u.RequestURI(),
1112         }
1113         resp := httptest.NewRecorder()
1114         s.testServer.Handler.ServeHTTP(resp, req)
1115         c.Check(resp.Code, check.Equals, http.StatusOK)
1116         c.Check(keepclient.DefaultBlockCache.MaxBlocks, check.Equals, 42)
1117 }
1118
1119 func copyHeader(h http.Header) http.Header {
1120         hc := http.Header{}
1121         for k, v := range h {
1122                 hc[k] = append([]string(nil), v...)
1123         }
1124         return hc
1125 }