Merge branch '20602-controller-qos'
[arvados.git] / services / keep-web / handler.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepweb
6
7 import (
8         "encoding/json"
9         "errors"
10         "fmt"
11         "html"
12         "html/template"
13         "io"
14         "net/http"
15         "net/url"
16         "os"
17         "sort"
18         "strconv"
19         "strings"
20         "sync"
21
22         "git.arvados.org/arvados.git/lib/cmd"
23         "git.arvados.org/arvados.git/lib/webdavfs"
24         "git.arvados.org/arvados.git/sdk/go/arvados"
25         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
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/httpserver"
29         "git.arvados.org/arvados.git/sdk/go/keepclient"
30         "github.com/sirupsen/logrus"
31         "golang.org/x/net/webdav"
32 )
33
34 type handler struct {
35         Cache     cache
36         Cluster   *arvados.Cluster
37         setupOnce sync.Once
38 }
39
40 var urlPDHDecoder = strings.NewReplacer(" ", "+", "-", "+")
41
42 var notFoundMessage = "Not Found"
43 var unauthorizedMessage = "401 Unauthorized\n\nA valid Arvados token must be provided to access this resource."
44
45 // parseCollectionIDFromURL returns a UUID or PDH if s is a UUID or a
46 // PDH (even if it is a PDH with "+" replaced by " " or "-");
47 // otherwise "".
48 func parseCollectionIDFromURL(s string) string {
49         if arvadosclient.UUIDMatch(s) {
50                 return s
51         }
52         if pdh := urlPDHDecoder.Replace(s); arvadosclient.PDHMatch(pdh) {
53                 return pdh
54         }
55         return ""
56 }
57
58 func (h *handler) setup() {
59         keepclient.DefaultBlockCache.MaxBlocks = h.Cluster.Collections.WebDAVCache.MaxBlockEntries
60 }
61
62 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
63         json.NewEncoder(w).Encode(struct{ Version string }{cmd.Version.String()})
64 }
65
66 type errorWithHTTPStatus interface {
67         HTTPStatus() int
68 }
69
70 // updateOnSuccess wraps httpserver.ResponseWriter. If the handler
71 // sends an HTTP header indicating success, updateOnSuccess first
72 // calls the provided update func. If the update func fails, an error
73 // response is sent (using the error's HTTP status or 500 if none),
74 // and the status code and body sent by the handler are ignored (all
75 // response writes return the update error).
76 type updateOnSuccess struct {
77         httpserver.ResponseWriter
78         logger     logrus.FieldLogger
79         update     func() error
80         sentHeader bool
81         err        error
82 }
83
84 func (uos *updateOnSuccess) Write(p []byte) (int, error) {
85         if !uos.sentHeader {
86                 uos.WriteHeader(http.StatusOK)
87         }
88         if uos.err != nil {
89                 return 0, uos.err
90         }
91         return uos.ResponseWriter.Write(p)
92 }
93
94 func (uos *updateOnSuccess) WriteHeader(code int) {
95         if !uos.sentHeader {
96                 uos.sentHeader = true
97                 if code >= 200 && code < 400 {
98                         if uos.err = uos.update(); uos.err != nil {
99                                 code := http.StatusInternalServerError
100                                 if he := errorWithHTTPStatus(nil); errors.As(uos.err, &he) {
101                                         code = he.HTTPStatus()
102                                 }
103                                 uos.logger.WithError(uos.err).Errorf("update() returned %T error, changing response to HTTP %d", uos.err, code)
104                                 http.Error(uos.ResponseWriter, uos.err.Error(), code)
105                                 return
106                         }
107                 }
108         }
109         uos.ResponseWriter.WriteHeader(code)
110 }
111
112 var (
113         corsAllowHeadersHeader = strings.Join([]string{
114                 "Authorization", "Content-Type", "Range",
115                 // WebDAV request headers:
116                 "Depth", "Destination", "If", "Lock-Token", "Overwrite", "Timeout", "Cache-Control",
117         }, ", ")
118         writeMethod = map[string]bool{
119                 "COPY":      true,
120                 "DELETE":    true,
121                 "LOCK":      true,
122                 "MKCOL":     true,
123                 "MOVE":      true,
124                 "PROPPATCH": true,
125                 "PUT":       true,
126                 "RMCOL":     true,
127                 "UNLOCK":    true,
128         }
129         webdavMethod = map[string]bool{
130                 "COPY":      true,
131                 "DELETE":    true,
132                 "LOCK":      true,
133                 "MKCOL":     true,
134                 "MOVE":      true,
135                 "OPTIONS":   true,
136                 "PROPFIND":  true,
137                 "PROPPATCH": true,
138                 "PUT":       true,
139                 "RMCOL":     true,
140                 "UNLOCK":    true,
141         }
142         browserMethod = map[string]bool{
143                 "GET":  true,
144                 "HEAD": true,
145                 "POST": true,
146         }
147         // top-level dirs to serve with siteFS
148         siteFSDir = map[string]bool{
149                 "":      true, // root directory
150                 "by_id": true,
151                 "users": true,
152         }
153 )
154
155 func stripDefaultPort(host string) string {
156         // Will consider port 80 and port 443 to be the same vhost.  I think that's fine.
157         u := &url.URL{Host: host}
158         if p := u.Port(); p == "80" || p == "443" {
159                 return strings.ToLower(u.Hostname())
160         } else {
161                 return strings.ToLower(host)
162         }
163 }
164
165 // CheckHealth implements service.Handler.
166 func (h *handler) CheckHealth() error {
167         return nil
168 }
169
170 // Done implements service.Handler.
171 func (h *handler) Done() <-chan struct{} {
172         return nil
173 }
174
175 // ServeHTTP implements http.Handler.
176 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
177         h.setupOnce.Do(h.setup)
178
179         if xfp := r.Header.Get("X-Forwarded-Proto"); xfp != "" && xfp != "http" {
180                 r.URL.Scheme = xfp
181         }
182
183         w := httpserver.WrapResponseWriter(wOrig)
184
185         if method := r.Header.Get("Access-Control-Request-Method"); method != "" && r.Method == "OPTIONS" {
186                 if !browserMethod[method] && !webdavMethod[method] {
187                         w.WriteHeader(http.StatusMethodNotAllowed)
188                         return
189                 }
190                 w.Header().Set("Access-Control-Allow-Headers", corsAllowHeadersHeader)
191                 w.Header().Set("Access-Control-Allow-Methods", "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
192                 w.Header().Set("Access-Control-Allow-Origin", "*")
193                 w.Header().Set("Access-Control-Max-Age", "86400")
194                 return
195         }
196
197         if !browserMethod[r.Method] && !webdavMethod[r.Method] {
198                 w.WriteHeader(http.StatusMethodNotAllowed)
199                 return
200         }
201
202         if r.Header.Get("Origin") != "" {
203                 // Allow simple cross-origin requests without user
204                 // credentials ("user credentials" as defined by CORS,
205                 // i.e., cookies, HTTP authentication, and client-side
206                 // SSL certificates. See
207                 // http://www.w3.org/TR/cors/#user-credentials).
208                 w.Header().Set("Access-Control-Allow-Origin", "*")
209                 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
210         }
211
212         if h.serveS3(w, r) {
213                 return
214         }
215
216         webdavPrefix := ""
217         arvPath := r.URL.Path
218         if prefix := r.Header.Get("X-Webdav-Prefix"); prefix != "" {
219                 // Enable a proxy (e.g., container log handler in
220                 // controller) to satisfy a request for path
221                 // "/foo/bar/baz.txt" using content from
222                 // "//abc123-4.internal/bar/baz.txt", by adding a
223                 // request header "X-Webdav-Prefix: /foo"
224                 if !strings.HasPrefix(arvPath, prefix) {
225                         http.Error(w, "X-Webdav-Prefix header is not a prefix of the requested path", http.StatusBadRequest)
226                         return
227                 }
228                 arvPath = r.URL.Path[len(prefix):]
229                 if arvPath == "" {
230                         arvPath = "/"
231                 }
232                 w.Header().Set("Vary", "X-Webdav-Prefix, "+w.Header().Get("Vary"))
233                 webdavPrefix = prefix
234         }
235         pathParts := strings.Split(arvPath[1:], "/")
236
237         var stripParts int
238         var collectionID string
239         var tokens []string
240         var reqTokens []string
241         var pathToken bool
242         var attachment bool
243         var useSiteFS bool
244         credentialsOK := h.Cluster.Collections.TrustAllContent
245         reasonNotAcceptingCredentials := ""
246
247         if r.Host != "" && stripDefaultPort(r.Host) == stripDefaultPort(h.Cluster.Services.WebDAVDownload.ExternalURL.Host) {
248                 credentialsOK = true
249                 attachment = true
250         } else if r.FormValue("disposition") == "attachment" {
251                 attachment = true
252         }
253
254         if !credentialsOK {
255                 reasonNotAcceptingCredentials = fmt.Sprintf("vhost %q does not specify a single collection ID or match Services.WebDAVDownload.ExternalURL %q, and Collections.TrustAllContent is false",
256                         r.Host, h.Cluster.Services.WebDAVDownload.ExternalURL)
257         }
258
259         if collectionID = arvados.CollectionIDFromDNSName(r.Host); collectionID != "" {
260                 // http://ID.collections.example/PATH...
261                 credentialsOK = true
262         } else if r.URL.Path == "/status.json" {
263                 h.serveStatus(w, r)
264                 return
265         } else if siteFSDir[pathParts[0]] {
266                 useSiteFS = true
267         } else if len(pathParts) >= 1 && strings.HasPrefix(pathParts[0], "c=") {
268                 // /c=ID[/PATH...]
269                 collectionID = parseCollectionIDFromURL(pathParts[0][2:])
270                 stripParts = 1
271         } else if len(pathParts) >= 2 && pathParts[0] == "collections" {
272                 if len(pathParts) >= 4 && pathParts[1] == "download" {
273                         // /collections/download/ID/TOKEN/PATH...
274                         collectionID = parseCollectionIDFromURL(pathParts[2])
275                         tokens = []string{pathParts[3]}
276                         stripParts = 4
277                         pathToken = true
278                 } else {
279                         // /collections/ID/PATH...
280                         collectionID = parseCollectionIDFromURL(pathParts[1])
281                         stripParts = 2
282                         // This path is only meant to work for public
283                         // data. Tokens provided with the request are
284                         // ignored.
285                         credentialsOK = false
286                         reasonNotAcceptingCredentials = "the '/collections/UUID/PATH' form only works for public data"
287                 }
288         }
289
290         forceReload := false
291         if cc := r.Header.Get("Cache-Control"); strings.Contains(cc, "no-cache") || strings.Contains(cc, "must-revalidate") {
292                 forceReload = true
293         }
294
295         if credentialsOK {
296                 reqTokens = auth.CredentialsFromRequest(r).Tokens
297         }
298
299         formToken := r.FormValue("api_token")
300         origin := r.Header.Get("Origin")
301         cors := origin != "" && !strings.HasSuffix(origin, "://"+r.Host)
302         safeAjax := cors && (r.Method == http.MethodGet || r.Method == http.MethodHead)
303         safeAttachment := attachment && r.URL.Query().Get("api_token") == ""
304         if formToken == "" {
305                 // No token to use or redact.
306         } else if safeAjax || safeAttachment {
307                 // If this is a cross-origin request, the URL won't
308                 // appear in the browser's address bar, so
309                 // substituting a clipboard-safe URL is pointless.
310                 // Redirect-with-cookie wouldn't work anyway, because
311                 // it's not safe to allow third-party use of our
312                 // cookie.
313                 //
314                 // If we're supplying an attachment, we don't need to
315                 // convert POST to GET to avoid the "really resubmit
316                 // form?" problem, so provided the token isn't
317                 // embedded in the URL, there's no reason to do
318                 // redirect-with-cookie in this case either.
319                 reqTokens = append(reqTokens, formToken)
320         } else if browserMethod[r.Method] {
321                 // If this is a page view, and the client provided a
322                 // token via query string or POST body, we must put
323                 // the token in an HttpOnly cookie, and redirect to an
324                 // equivalent URL with the query param redacted and
325                 // method = GET.
326                 h.seeOtherWithCookie(w, r, "", credentialsOK)
327                 return
328         }
329
330         targetPath := pathParts[stripParts:]
331         if tokens == nil && len(targetPath) > 0 && strings.HasPrefix(targetPath[0], "t=") {
332                 // http://ID.example/t=TOKEN/PATH...
333                 // /c=ID/t=TOKEN/PATH...
334                 //
335                 // This form must only be used to pass scoped tokens
336                 // that give permission for a single collection. See
337                 // FormValue case above.
338                 tokens = []string{targetPath[0][2:]}
339                 pathToken = true
340                 targetPath = targetPath[1:]
341                 stripParts++
342         }
343
344         fsprefix := ""
345         if useSiteFS {
346                 if writeMethod[r.Method] {
347                         http.Error(w, webdavfs.ErrReadOnly.Error(), http.StatusMethodNotAllowed)
348                         return
349                 }
350                 if len(reqTokens) == 0 {
351                         w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
352                         http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
353                         return
354                 }
355                 tokens = reqTokens
356         } else if collectionID == "" {
357                 http.Error(w, notFoundMessage, http.StatusNotFound)
358                 return
359         } else {
360                 fsprefix = "by_id/" + collectionID + "/"
361         }
362
363         if src := r.Header.Get("X-Webdav-Source"); strings.HasPrefix(src, "/") && !strings.Contains(src, "//") && !strings.Contains(src, "/../") {
364                 fsprefix += src[1:]
365         }
366
367         if tokens == nil {
368                 tokens = reqTokens
369                 if h.Cluster.Users.AnonymousUserToken != "" {
370                         tokens = append(tokens, h.Cluster.Users.AnonymousUserToken)
371                 }
372         }
373
374         if len(targetPath) > 0 && targetPath[0] == "_" {
375                 // If a collection has a directory called "t=foo" or
376                 // "_", it can be served at
377                 // //collections.example/_/t=foo/ or
378                 // //collections.example/_/_/ respectively:
379                 // //collections.example/t=foo/ won't work because
380                 // t=foo will be interpreted as a token "foo".
381                 targetPath = targetPath[1:]
382                 stripParts++
383         }
384
385         dirOpenMode := os.O_RDONLY
386         if writeMethod[r.Method] {
387                 dirOpenMode = os.O_RDWR
388         }
389
390         var tokenValid bool
391         var tokenScopeProblem bool
392         var token string
393         var tokenUser *arvados.User
394         var sessionFS arvados.CustomFileSystem
395         var session *cachedSession
396         var collectionDir arvados.File
397         for _, token = range tokens {
398                 var statusErr errorWithHTTPStatus
399                 fs, sess, user, err := h.Cache.GetSession(token)
400                 if errors.As(err, &statusErr) && statusErr.HTTPStatus() == http.StatusUnauthorized {
401                         // bad token
402                         continue
403                 } else if err != nil {
404                         http.Error(w, "cache error: "+err.Error(), http.StatusInternalServerError)
405                         return
406                 }
407                 if token != h.Cluster.Users.AnonymousUserToken {
408                         tokenValid = true
409                 }
410                 f, err := fs.OpenFile(fsprefix, dirOpenMode, 0)
411                 if errors.As(err, &statusErr) &&
412                         statusErr.HTTPStatus() == http.StatusForbidden &&
413                         token != h.Cluster.Users.AnonymousUserToken {
414                         // collection id is outside scope of supplied
415                         // token
416                         tokenScopeProblem = true
417                         continue
418                 } else if os.IsNotExist(err) {
419                         // collection does not exist or is not
420                         // readable using this token
421                         continue
422                 } else if err != nil {
423                         http.Error(w, err.Error(), http.StatusInternalServerError)
424                         return
425                 }
426                 defer f.Close()
427
428                 collectionDir, sessionFS, session, tokenUser = f, fs, sess, user
429                 break
430         }
431         if forceReload && collectionDir != nil {
432                 err := collectionDir.Sync()
433                 if err != nil {
434                         if he := errorWithHTTPStatus(nil); errors.As(err, &he) {
435                                 http.Error(w, err.Error(), he.HTTPStatus())
436                         } else {
437                                 http.Error(w, err.Error(), http.StatusInternalServerError)
438                         }
439                         return
440                 }
441         }
442         if session == nil {
443                 if pathToken {
444                         // The URL is a "secret sharing link" that
445                         // didn't work out.  Asking the client for
446                         // additional credentials would just be
447                         // confusing.
448                         http.Error(w, notFoundMessage, http.StatusNotFound)
449                         return
450                 }
451                 if tokenValid {
452                         // The client provided valid token(s), but the
453                         // collection was not found.
454                         http.Error(w, notFoundMessage, http.StatusNotFound)
455                         return
456                 }
457                 if tokenScopeProblem {
458                         // The client provided a valid token but
459                         // fetching a collection returned 401, which
460                         // means the token scope doesn't permit
461                         // fetching that collection.
462                         http.Error(w, notFoundMessage, http.StatusForbidden)
463                         return
464                 }
465                 // The client's token was invalid (e.g., expired), or
466                 // the client didn't even provide one.  Redirect to
467                 // workbench2's login-and-redirect-to-download url if
468                 // this is a browser navigation request. (The redirect
469                 // flow can't preserve the original method if it's not
470                 // GET, and doesn't make sense if the UA is a
471                 // command-line tool, is trying to load an inline
472                 // image, etc.; in these cases, there's nothing we can
473                 // do, so return 401 unauthorized.)
474                 //
475                 // Note Sec-Fetch-Mode is sent by all non-EOL
476                 // browsers, except Safari.
477                 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode
478                 //
479                 // TODO(TC): This response would be confusing to
480                 // someone trying (anonymously) to download public
481                 // data that has been deleted.  Allow a referrer to
482                 // provide this context somehow?
483                 if r.Method == http.MethodGet && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
484                         target := url.URL(h.Cluster.Services.Workbench2.ExternalURL)
485                         redirkey := "redirectToPreview"
486                         if attachment {
487                                 redirkey = "redirectToDownload"
488                         }
489                         callback := "/c=" + collectionID + "/" + strings.Join(targetPath, "/")
490                         // target.RawQuery = url.Values{redirkey:
491                         // {target}}.Encode() would be the obvious
492                         // thing to do here, but wb2 doesn't decode
493                         // this as a query param -- it takes
494                         // everything after "${redirkey}=" as the
495                         // target URL. If we encode "/" as "%2F" etc.,
496                         // the redirect won't work.
497                         target.RawQuery = redirkey + "=" + callback
498                         w.Header().Add("Location", target.String())
499                         w.WriteHeader(http.StatusSeeOther)
500                         return
501                 }
502                 if !credentialsOK {
503                         http.Error(w, fmt.Sprintf("Authorization tokens are not accepted here: %v, and no anonymous user token is configured.", reasonNotAcceptingCredentials), http.StatusUnauthorized)
504                         return
505                 }
506                 // If none of the above cases apply, suggest the
507                 // user-agent (which is either a non-browser agent
508                 // like wget, or a browser that can't redirect through
509                 // a login flow) prompt the user for credentials.
510                 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
511                 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
512                 return
513         }
514
515         if r.Method == http.MethodGet || r.Method == http.MethodHead {
516                 targetfnm := fsprefix + strings.Join(pathParts[stripParts:], "/")
517                 if fi, err := sessionFS.Stat(targetfnm); err == nil && fi.IsDir() {
518                         if !strings.HasSuffix(r.URL.Path, "/") {
519                                 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
520                         } else {
521                                 h.serveDirectory(w, r, fi.Name(), sessionFS, targetfnm, !useSiteFS)
522                         }
523                         return
524                 }
525         }
526
527         var basename string
528         if len(targetPath) > 0 {
529                 basename = targetPath[len(targetPath)-1]
530         }
531         if arvadosclient.PDHMatch(collectionID) && writeMethod[r.Method] {
532                 http.Error(w, webdavfs.ErrReadOnly.Error(), http.StatusMethodNotAllowed)
533                 return
534         }
535         if !h.userPermittedToUploadOrDownload(r.Method, tokenUser) {
536                 http.Error(w, "Not permitted", http.StatusForbidden)
537                 return
538         }
539         h.logUploadOrDownload(r, session.arvadosclient, sessionFS, fsprefix+strings.Join(targetPath, "/"), nil, tokenUser)
540
541         if writeMethod[r.Method] {
542                 // Save the collection only if/when all
543                 // webdav->filesystem operations succeed --
544                 // and send a 500 error if the modified
545                 // collection can't be saved.
546                 //
547                 // Perform the write in a separate sitefs, so
548                 // concurrent read operations on the same
549                 // collection see the previous saved
550                 // state. After the write succeeds and the
551                 // collection record is updated, we reset the
552                 // session so the updates are visible in
553                 // subsequent read requests.
554                 client := session.client.WithRequestID(r.Header.Get("X-Request-Id"))
555                 sessionFS = client.SiteFileSystem(session.keepclient)
556                 writingDir, err := sessionFS.OpenFile(fsprefix, os.O_RDONLY, 0)
557                 if err != nil {
558                         http.Error(w, err.Error(), http.StatusInternalServerError)
559                         return
560                 }
561                 defer writingDir.Close()
562                 w = &updateOnSuccess{
563                         ResponseWriter: w,
564                         logger:         ctxlog.FromContext(r.Context()),
565                         update: func() error {
566                                 err := writingDir.Sync()
567                                 var te arvados.TransactionError
568                                 if errors.As(err, &te) {
569                                         err = te
570                                 }
571                                 if err != nil {
572                                         return err
573                                 }
574                                 // Sync the changes to the persistent
575                                 // sessionfs for this token.
576                                 snap, err := writingDir.Snapshot()
577                                 if err != nil {
578                                         return err
579                                 }
580                                 collectionDir.Splice(snap)
581                                 return nil
582                         }}
583         }
584         if r.Method == http.MethodGet {
585                 applyContentDispositionHdr(w, r, basename, attachment)
586         }
587         if webdavPrefix == "" {
588                 webdavPrefix = "/" + strings.Join(pathParts[:stripParts], "/")
589         }
590         wh := webdav.Handler{
591                 Prefix: webdavPrefix,
592                 FileSystem: &webdavfs.FS{
593                         FileSystem:    sessionFS,
594                         Prefix:        fsprefix,
595                         Writing:       writeMethod[r.Method],
596                         AlwaysReadEOF: r.Method == "PROPFIND",
597                 },
598                 LockSystem: webdavfs.NoLockSystem,
599                 Logger: func(r *http.Request, err error) {
600                         if err != nil && !os.IsNotExist(err) {
601                                 ctxlog.FromContext(r.Context()).WithError(err).Error("error reported by webdav handler")
602                         }
603                 },
604         }
605         wh.ServeHTTP(w, r)
606         if r.Method == http.MethodGet && w.WroteStatus() == http.StatusOK {
607                 wrote := int64(w.WroteBodyBytes())
608                 fnm := strings.Join(pathParts[stripParts:], "/")
609                 fi, err := wh.FileSystem.Stat(r.Context(), fnm)
610                 if err == nil && fi.Size() != wrote {
611                         var n int
612                         f, err := wh.FileSystem.OpenFile(r.Context(), fnm, os.O_RDONLY, 0)
613                         if err == nil {
614                                 n, err = f.Read(make([]byte, 1024))
615                                 f.Close()
616                         }
617                         ctxlog.FromContext(r.Context()).Errorf("stat.Size()==%d but only wrote %d bytes; read(1024) returns %d, %v", fi.Size(), wrote, n, err)
618                 }
619         }
620 }
621
622 var dirListingTemplate = `<!DOCTYPE HTML>
623 <HTML><HEAD>
624   <META name="robots" content="NOINDEX">
625   <TITLE>{{ .CollectionName }}</TITLE>
626   <STYLE type="text/css">
627     body {
628       margin: 1.5em;
629     }
630     pre {
631       background-color: #D9EDF7;
632       border-radius: .25em;
633       padding: .75em;
634       overflow: auto;
635     }
636     .footer p {
637       font-size: 82%;
638     }
639     ul {
640       padding: 0;
641     }
642     ul li {
643       font-family: monospace;
644       list-style: none;
645     }
646   </STYLE>
647 </HEAD>
648 <BODY>
649
650 <H1>{{ .CollectionName }}</H1>
651
652 <P>This collection of data files is being shared with you through
653 Arvados.  You can download individual files listed below.  To download
654 the entire directory tree with wget, try:</P>
655
656 <PRE>$ wget --mirror --no-parent --no-host --cut-dirs={{ .StripParts }} https://{{ .Request.Host }}{{ .Request.URL.Path }}</PRE>
657
658 <H2>File Listing</H2>
659
660 {{if .Files}}
661 <UL>
662 {{range .Files}}
663 {{if .IsDir }}
664   <LI>{{" " | printf "%15s  " | nbsp}}<A href="{{print "./" .Name}}/">{{.Name}}/</A></LI>
665 {{else}}
666   <LI>{{.Size | printf "%15d  " | nbsp}}<A href="{{print "./" .Name}}">{{.Name}}</A></LI>
667 {{end}}
668 {{end}}
669 </UL>
670 {{else}}
671 <P>(No files; this collection is empty.)</P>
672 {{end}}
673
674 <HR noshade>
675 <DIV class="footer">
676   <P>
677     About Arvados:
678     Arvados is a free and open source software bioinformatics platform.
679     To learn more, visit arvados.org.
680     Arvados is not responsible for the files listed on this page.
681   </P>
682 </DIV>
683
684 </BODY>
685 `
686
687 type fileListEnt struct {
688         Name  string
689         Size  int64
690         IsDir bool
691 }
692
693 func (h *handler) serveDirectory(w http.ResponseWriter, r *http.Request, collectionName string, fs http.FileSystem, base string, recurse bool) {
694         var files []fileListEnt
695         var walk func(string) error
696         if !strings.HasSuffix(base, "/") {
697                 base = base + "/"
698         }
699         walk = func(path string) error {
700                 dirname := base + path
701                 if dirname != "/" {
702                         dirname = strings.TrimSuffix(dirname, "/")
703                 }
704                 d, err := fs.Open(dirname)
705                 if err != nil {
706                         return err
707                 }
708                 ents, err := d.Readdir(-1)
709                 if err != nil {
710                         return err
711                 }
712                 for _, ent := range ents {
713                         if recurse && ent.IsDir() {
714                                 err = walk(path + ent.Name() + "/")
715                                 if err != nil {
716                                         return err
717                                 }
718                         } else {
719                                 files = append(files, fileListEnt{
720                                         Name:  path + ent.Name(),
721                                         Size:  ent.Size(),
722                                         IsDir: ent.IsDir(),
723                                 })
724                         }
725                 }
726                 return nil
727         }
728         if err := walk(""); err != nil {
729                 http.Error(w, "error getting directory listing: "+err.Error(), http.StatusInternalServerError)
730                 return
731         }
732
733         funcs := template.FuncMap{
734                 "nbsp": func(s string) template.HTML {
735                         return template.HTML(strings.Replace(s, " ", "&nbsp;", -1))
736                 },
737         }
738         tmpl, err := template.New("dir").Funcs(funcs).Parse(dirListingTemplate)
739         if err != nil {
740                 http.Error(w, "error parsing template: "+err.Error(), http.StatusInternalServerError)
741                 return
742         }
743         sort.Slice(files, func(i, j int) bool {
744                 return files[i].Name < files[j].Name
745         })
746         w.WriteHeader(http.StatusOK)
747         tmpl.Execute(w, map[string]interface{}{
748                 "CollectionName": collectionName,
749                 "Files":          files,
750                 "Request":        r,
751                 "StripParts":     strings.Count(strings.TrimRight(r.URL.Path, "/"), "/"),
752         })
753 }
754
755 func applyContentDispositionHdr(w http.ResponseWriter, r *http.Request, filename string, isAttachment bool) {
756         disposition := "inline"
757         if isAttachment {
758                 disposition = "attachment"
759         }
760         if strings.ContainsRune(r.RequestURI, '?') {
761                 // Help the UA realize that the filename is just
762                 // "filename.txt", not
763                 // "filename.txt?disposition=attachment".
764                 //
765                 // TODO(TC): Follow advice at RFC 6266 appendix D
766                 disposition += "; filename=" + strconv.QuoteToASCII(filename)
767         }
768         if disposition != "inline" {
769                 w.Header().Set("Content-Disposition", disposition)
770         }
771 }
772
773 func (h *handler) seeOtherWithCookie(w http.ResponseWriter, r *http.Request, location string, credentialsOK bool) {
774         if formToken := r.FormValue("api_token"); formToken != "" {
775                 if !credentialsOK {
776                         // It is not safe to copy the provided token
777                         // into a cookie unless the current vhost
778                         // (origin) serves only a single collection or
779                         // we are in TrustAllContent mode.
780                         http.Error(w, "cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)", http.StatusBadRequest)
781                         return
782                 }
783
784                 // The HttpOnly flag is necessary to prevent
785                 // JavaScript code (included in, or loaded by, a page
786                 // in the collection being served) from employing the
787                 // user's token beyond reading other files in the same
788                 // domain, i.e., same collection.
789                 //
790                 // The 303 redirect is necessary in the case of a GET
791                 // request to avoid exposing the token in the Location
792                 // bar, and in the case of a POST request to avoid
793                 // raising warnings when the user refreshes the
794                 // resulting page.
795                 http.SetCookie(w, &http.Cookie{
796                         Name:     "arvados_api_token",
797                         Value:    auth.EncodeTokenCookie([]byte(formToken)),
798                         Path:     "/",
799                         HttpOnly: true,
800                         SameSite: http.SameSiteLaxMode,
801                 })
802         }
803
804         // Propagate query parameters (except api_token) from
805         // the original request.
806         redirQuery := r.URL.Query()
807         redirQuery.Del("api_token")
808
809         u := r.URL
810         if location != "" {
811                 newu, err := u.Parse(location)
812                 if err != nil {
813                         http.Error(w, "error resolving redirect target: "+err.Error(), http.StatusInternalServerError)
814                         return
815                 }
816                 u = newu
817         }
818         redir := (&url.URL{
819                 Scheme:   r.URL.Scheme,
820                 Host:     r.Host,
821                 Path:     u.Path,
822                 RawQuery: redirQuery.Encode(),
823         }).String()
824
825         w.Header().Add("Location", redir)
826         w.WriteHeader(http.StatusSeeOther)
827         io.WriteString(w, `<A href="`)
828         io.WriteString(w, html.EscapeString(redir))
829         io.WriteString(w, `">Continue</A>`)
830 }
831
832 func (h *handler) userPermittedToUploadOrDownload(method string, tokenUser *arvados.User) bool {
833         var permitDownload bool
834         var permitUpload bool
835         if tokenUser != nil && tokenUser.IsAdmin {
836                 permitUpload = h.Cluster.Collections.WebDAVPermission.Admin.Upload
837                 permitDownload = h.Cluster.Collections.WebDAVPermission.Admin.Download
838         } else {
839                 permitUpload = h.Cluster.Collections.WebDAVPermission.User.Upload
840                 permitDownload = h.Cluster.Collections.WebDAVPermission.User.Download
841         }
842         if (method == "PUT" || method == "POST") && !permitUpload {
843                 // Disallow operations that upload new files.
844                 // Permit webdav operations that move existing files around.
845                 return false
846         } else if method == "GET" && !permitDownload {
847                 // Disallow downloading file contents.
848                 // Permit webdav operations like PROPFIND that retrieve metadata
849                 // but not file contents.
850                 return false
851         }
852         return true
853 }
854
855 func (h *handler) logUploadOrDownload(
856         r *http.Request,
857         client *arvadosclient.ArvadosClient,
858         fs arvados.CustomFileSystem,
859         filepath string,
860         collection *arvados.Collection,
861         user *arvados.User) {
862
863         log := ctxlog.FromContext(r.Context())
864         props := make(map[string]string)
865         props["reqPath"] = r.URL.Path
866         var useruuid string
867         if user != nil {
868                 log = log.WithField("user_uuid", user.UUID).
869                         WithField("user_full_name", user.FullName)
870                 useruuid = user.UUID
871         } else {
872                 useruuid = fmt.Sprintf("%s-tpzed-anonymouspublic", h.Cluster.ClusterID)
873         }
874         if collection == nil && fs != nil {
875                 collection, filepath = h.determineCollection(fs, filepath)
876         }
877         if collection != nil {
878                 log = log.WithField("collection_file_path", filepath)
879                 props["collection_file_path"] = filepath
880                 // h.determineCollection populates the collection_uuid
881                 // prop with the PDH, if this collection is being
882                 // accessed via PDH. For logging, we use a different
883                 // field depending on whether it's a UUID or PDH.
884                 if len(collection.UUID) > 32 {
885                         log = log.WithField("portable_data_hash", collection.UUID)
886                         props["portable_data_hash"] = collection.UUID
887                 } else {
888                         log = log.WithField("collection_uuid", collection.UUID)
889                         props["collection_uuid"] = collection.UUID
890                 }
891         }
892         if r.Method == "PUT" || r.Method == "POST" {
893                 log.Info("File upload")
894                 if h.Cluster.Collections.WebDAVLogEvents {
895                         go func() {
896                                 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
897                                         "object_uuid": useruuid,
898                                         "event_type":  "file_upload",
899                                         "properties":  props}}
900                                 err := client.Create("logs", lr, nil)
901                                 if err != nil {
902                                         log.WithError(err).Error("Failed to create upload log event on API server")
903                                 }
904                         }()
905                 }
906         } else if r.Method == "GET" {
907                 if collection != nil && collection.PortableDataHash != "" {
908                         log = log.WithField("portable_data_hash", collection.PortableDataHash)
909                         props["portable_data_hash"] = collection.PortableDataHash
910                 }
911                 log.Info("File download")
912                 if h.Cluster.Collections.WebDAVLogEvents {
913                         go func() {
914                                 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
915                                         "object_uuid": useruuid,
916                                         "event_type":  "file_download",
917                                         "properties":  props}}
918                                 err := client.Create("logs", lr, nil)
919                                 if err != nil {
920                                         log.WithError(err).Error("Failed to create download log event on API server")
921                                 }
922                         }()
923                 }
924         }
925 }
926
927 func (h *handler) determineCollection(fs arvados.CustomFileSystem, path string) (*arvados.Collection, string) {
928         target := strings.TrimSuffix(path, "/")
929         for cut := len(target); cut >= 0; cut = strings.LastIndexByte(target, '/') {
930                 target = target[:cut]
931                 fi, err := fs.Stat(target)
932                 if os.IsNotExist(err) {
933                         // creating a new file/dir, or download
934                         // destined to fail
935                         continue
936                 } else if err != nil {
937                         return nil, ""
938                 }
939                 switch src := fi.Sys().(type) {
940                 case *arvados.Collection:
941                         return src, strings.TrimPrefix(path[len(target):], "/")
942                 case *arvados.Group:
943                         return nil, ""
944                 default:
945                         if _, ok := src.(error); ok {
946                                 return nil, ""
947                         }
948                 }
949         }
950         return nil, ""
951 }