1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
27 "git.arvados.org/arvados.git/lib/cmd"
28 "git.arvados.org/arvados.git/lib/ctrlctx"
29 "git.arvados.org/arvados.git/lib/webdavfs"
30 "git.arvados.org/arvados.git/sdk/go/arvados"
31 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
32 "git.arvados.org/arvados.git/sdk/go/auth"
33 "git.arvados.org/arvados.git/sdk/go/ctxlog"
34 "git.arvados.org/arvados.git/sdk/go/httpserver"
35 "github.com/gotd/contrib/http_range"
36 "github.com/sirupsen/logrus"
37 "golang.org/x/net/webdav"
42 Cluster *arvados.Cluster
45 fileEventLogs map[fileEventLog]time.Time
46 fileEventLogsMtx sync.Mutex
47 fileEventLogsNextTidy time.Time
49 s3SecretCache map[string]*cachedS3Secret
50 s3SecretCacheMtx sync.Mutex
51 s3SecretCacheNextTidy time.Time
53 dbConnector *ctrlctx.DBConnector
54 dbConnectorMtx sync.Mutex
57 var urlPDHDecoder = strings.NewReplacer(" ", "+", "-", "+")
59 var notFoundMessage = "Not Found"
60 var unauthorizedMessage = "401 Unauthorized\n\nA valid Arvados token must be provided to access this resource."
62 // parseCollectionIDFromURL returns a UUID or PDH if s is a UUID or a
63 // PDH (even if it is a PDH with "+" replaced by " " or "-");
65 func parseCollectionIDFromURL(s string) string {
66 if arvadosclient.UUIDMatch(s) {
69 if pdh := urlPDHDecoder.Replace(s); arvadosclient.PDHMatch(pdh) {
75 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
76 json.NewEncoder(w).Encode(struct{ Version string }{cmd.Version.String()})
79 type errorWithHTTPStatus interface {
83 // updateOnSuccess wraps httpserver.ResponseWriter. If the handler
84 // sends an HTTP header indicating success, updateOnSuccess first
85 // calls the provided update func. If the update func fails, an error
86 // response is sent (using the error's HTTP status or 500 if none),
87 // and the status code and body sent by the handler are ignored (all
88 // response writes return the update error).
89 type updateOnSuccess struct {
90 httpserver.ResponseWriter
91 logger logrus.FieldLogger
97 func (uos *updateOnSuccess) Write(p []byte) (int, error) {
99 uos.WriteHeader(http.StatusOK)
104 return uos.ResponseWriter.Write(p)
107 func (uos *updateOnSuccess) WriteHeader(code int) {
109 uos.sentHeader = true
110 if code >= 200 && code < 400 {
111 if uos.err = uos.update(); uos.err != nil {
112 code := http.StatusInternalServerError
113 if he := errorWithHTTPStatus(nil); errors.As(uos.err, &he) {
114 code = he.HTTPStatus()
116 uos.logger.WithError(uos.err).Errorf("update() returned %T error, changing response to HTTP %d", uos.err, code)
117 http.Error(uos.ResponseWriter, uos.err.Error(), code)
122 uos.ResponseWriter.WriteHeader(code)
126 corsAllowHeadersHeader = strings.Join([]string{
127 "Authorization", "Content-Type", "Range",
128 // WebDAV request headers:
129 "Depth", "Destination", "If", "Lock-Token", "Overwrite", "Timeout", "Cache-Control",
131 writeMethod = map[string]bool{
141 webdavMethod = map[string]bool{
154 browserMethod = map[string]bool{
159 // top-level dirs to serve with siteFS
160 siteFSDir = map[string]bool{
161 "": true, // root directory
167 func stripDefaultPort(host string) string {
168 // Will consider port 80 and port 443 to be the same vhost. I think that's fine.
169 u := &url.URL{Host: host}
170 if p := u.Port(); p == "80" || p == "443" {
171 return strings.ToLower(u.Hostname())
173 return strings.ToLower(host)
177 // CheckHealth implements service.Handler.
178 func (h *handler) CheckHealth() error {
182 // Done implements service.Handler.
183 func (h *handler) Done() <-chan struct{} {
187 func (h *handler) getDBConnector() *ctrlctx.DBConnector {
188 h.dbConnectorMtx.Lock()
189 defer h.dbConnectorMtx.Unlock()
190 if h.dbConnector == nil {
191 h.dbConnector = &ctrlctx.DBConnector{PostgreSQL: h.Cluster.PostgreSQL}
196 // ServeHTTP implements http.Handler.
197 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
198 if xfp := r.Header.Get("X-Forwarded-Proto"); xfp != "" && xfp != "http" {
202 httpserver.SetResponseLogFields(r.Context(), logrus.Fields{
203 "webdavDepth": r.Header.Get("Depth"),
204 "webdavDestination": r.Header.Get("Destination"),
205 "webdavOverwrite": r.Header.Get("Overwrite"),
208 wbuffer := newWriteBuffer(wOrig, int(h.Cluster.Collections.WebDAVOutputBuffer))
209 defer wbuffer.Close()
210 w := httpserver.WrapResponseWriter(responseWriter{
212 ResponseWriter: wOrig,
215 if r.Method == "OPTIONS" && ServeCORSPreflight(w, r.Header) {
219 if !browserMethod[r.Method] && !webdavMethod[r.Method] {
220 w.WriteHeader(http.StatusMethodNotAllowed)
224 if r.Header.Get("Origin") != "" {
225 // Allow simple cross-origin requests without user
226 // credentials ("user credentials" as defined by CORS,
227 // i.e., cookies, HTTP authentication, and client-side
228 // SSL certificates. See
229 // http://www.w3.org/TR/cors/#user-credentials).
230 w.Header().Set("Access-Control-Allow-Origin", "*")
231 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
238 // webdavPrefix is the leading portion of r.URL.Path that
239 // should be ignored by the webdav handler, if any.
241 // req "/c={id}/..." -> webdavPrefix "/c={id}"
242 // req "/by_id/..." -> webdavPrefix ""
244 // Note: in the code immediately below, we set webdavPrefix
245 // only if it was explicitly set by the client. Otherwise, it
246 // gets set later, after checking the request path for cases
247 // like "/c={id}/...".
249 arvPath := r.URL.Path
250 if prefix := r.Header.Get("X-Webdav-Prefix"); prefix != "" {
251 // Enable a proxy (e.g., container log handler in
252 // controller) to satisfy a request for path
253 // "/foo/bar/baz.txt" using content from
254 // "//abc123-4.internal/bar/baz.txt", by adding a
255 // request header "X-Webdav-Prefix: /foo"
256 if !strings.HasPrefix(arvPath, prefix) {
257 http.Error(w, "X-Webdav-Prefix header is not a prefix of the requested path", http.StatusBadRequest)
260 arvPath = r.URL.Path[len(prefix):]
264 w.Header().Set("Vary", "X-Webdav-Prefix, "+w.Header().Get("Vary"))
265 webdavPrefix = prefix
267 pathParts := strings.Split(arvPath[1:], "/")
270 var collectionID string
272 var reqTokens []string
276 credentialsOK := h.Cluster.Collections.TrustAllContent
277 reasonNotAcceptingCredentials := ""
279 if r.Host != "" && stripDefaultPort(r.Host) == stripDefaultPort(h.Cluster.Services.WebDAVDownload.ExternalURL.Host) {
282 } else if r.FormValue("disposition") == "attachment" {
287 reasonNotAcceptingCredentials = fmt.Sprintf("vhost %q does not specify a single collection ID or match Services.WebDAVDownload.ExternalURL %q, and Collections.TrustAllContent is false",
288 r.Host, h.Cluster.Services.WebDAVDownload.ExternalURL)
291 if collectionID = arvados.CollectionIDFromDNSName(r.Host); collectionID != "" {
292 // http://ID.collections.example/PATH...
294 } else if r.URL.Path == "/status.json" {
297 } else if siteFSDir[pathParts[0]] {
299 } else if len(pathParts) >= 1 && strings.HasPrefix(pathParts[0], "c=") {
301 collectionID = parseCollectionIDFromURL(pathParts[0][2:])
303 } else if len(pathParts) >= 2 && pathParts[0] == "collections" {
304 if len(pathParts) >= 4 && pathParts[1] == "download" {
305 // /collections/download/ID/TOKEN/PATH...
306 collectionID = parseCollectionIDFromURL(pathParts[2])
307 tokens = []string{pathParts[3]}
311 // /collections/ID/PATH...
312 collectionID = parseCollectionIDFromURL(pathParts[1])
314 // This path is only meant to work for public
315 // data. Tokens provided with the request are
317 credentialsOK = false
318 reasonNotAcceptingCredentials = "the '/collections/UUID/PATH' form only works for public data"
323 if cc := r.Header.Get("Cache-Control"); strings.Contains(cc, "no-cache") || strings.Contains(cc, "must-revalidate") {
328 reqTokens = auth.CredentialsFromRequest(r).Tokens
332 origin := r.Header.Get("Origin")
333 cors := origin != "" && !strings.HasSuffix(origin, "://"+r.Host)
334 safeAjax := cors && (r.Method == http.MethodGet || r.Method == http.MethodHead)
335 // Important distinction: safeAttachment checks whether api_token exists
336 // as a query parameter. haveFormTokens checks whether api_token exists
337 // as request form data *or* a query parameter. Different checks are
338 // necessary because both the request disposition and the location of
339 // the API token affect whether or not the request needs to be
340 // redirected. The different branch comments below explain further.
341 safeAttachment := attachment && !r.URL.Query().Has("api_token")
342 if formTokens, haveFormTokens := r.Form["api_token"]; !haveFormTokens {
343 // No token to use or redact.
344 } else if safeAjax || safeAttachment {
345 // If this is a cross-origin request, the URL won't
346 // appear in the browser's address bar, so
347 // substituting a clipboard-safe URL is pointless.
348 // Redirect-with-cookie wouldn't work anyway, because
349 // it's not safe to allow third-party use of our
352 // If we're supplying an attachment, we don't need to
353 // convert POST to GET to avoid the "really resubmit
354 // form?" problem, so provided the token isn't
355 // embedded in the URL, there's no reason to do
356 // redirect-with-cookie in this case either.
357 for _, tok := range formTokens {
358 reqTokens = append(reqTokens, tok)
360 } else if browserMethod[r.Method] {
361 // If this is a page view, and the client provided a
362 // token via query string or POST body, we must put
363 // the token in an HttpOnly cookie, and redirect to an
364 // equivalent URL with the query param redacted and
366 h.seeOtherWithCookie(w, r, "", credentialsOK)
370 targetPath := pathParts[stripParts:]
371 if tokens == nil && len(targetPath) > 0 && strings.HasPrefix(targetPath[0], "t=") {
372 // http://ID.example/t=TOKEN/PATH...
373 // /c=ID/t=TOKEN/PATH...
375 // This form must only be used to pass scoped tokens
376 // that give permission for a single collection. See
377 // FormValue case above.
378 tokens = []string{targetPath[0][2:]}
380 targetPath = targetPath[1:]
384 // fsprefix is the path from sitefs root to the sitefs
385 // directory (implicitly or explicitly) indicated by the
386 // leading / in the request path.
388 // Request "/by_id/..." -> fsprefix ""
389 // Request "/c={id}/..." -> fsprefix "/by_id/{id}/"
392 if writeMethod[r.Method] {
393 http.Error(w, webdavfs.ErrReadOnly.Error(), http.StatusMethodNotAllowed)
396 if len(reqTokens) == 0 {
397 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
398 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
402 } else if collectionID == "" {
403 http.Error(w, notFoundMessage, http.StatusNotFound)
406 fsprefix = "by_id/" + collectionID + "/"
409 if src := r.Header.Get("X-Webdav-Source"); strings.HasPrefix(src, "/") && !strings.Contains(src, "//") && !strings.Contains(src, "/../") {
410 // Clients (specifically, the container log gateway)
411 // use X-Webdav-Source to specify that although the
412 // request path (and other webdav fields in the
413 // request) refer to target "/abc", the intended
414 // target is actually
415 // "{x-webdav-source-value}/abc".
417 // This, combined with X-Webdav-Prefix, enables the
418 // container log gateway to effectively alter the
419 // target path when proxying a request, without
420 // needing to rewrite all the other webdav
421 // request/response fields that might mention the
428 if h.Cluster.Users.AnonymousUserToken != "" {
429 tokens = append(tokens, h.Cluster.Users.AnonymousUserToken)
433 if len(targetPath) > 0 && targetPath[0] == "_" {
434 // If a collection has a directory called "t=foo" or
435 // "_", it can be served at
436 // //collections.example/_/t=foo/ or
437 // //collections.example/_/_/ respectively:
438 // //collections.example/t=foo/ won't work because
439 // t=foo will be interpreted as a token "foo".
440 targetPath = targetPath[1:]
444 dirOpenMode := os.O_RDONLY
445 if writeMethod[r.Method] {
446 dirOpenMode = os.O_RDWR
450 var tokenScopeProblem bool
452 var tokenUser *arvados.User
453 var sessionFS arvados.CustomFileSystem
454 var targetFS arvados.FileSystem
455 var session *cachedSession
456 var collectionDir arvados.File
457 for _, token = range tokens {
458 var statusErr errorWithHTTPStatus
459 fs, sess, user, err := h.Cache.GetSession(token)
460 if errors.As(err, &statusErr) && statusErr.HTTPStatus() == http.StatusUnauthorized {
463 } else if err != nil {
464 http.Error(w, "cache error: "+err.Error(), http.StatusInternalServerError)
467 if token != h.Cluster.Users.AnonymousUserToken {
470 f, err := fs.OpenFile(fsprefix, dirOpenMode, 0)
471 if errors.As(err, &statusErr) &&
472 statusErr.HTTPStatus() == http.StatusForbidden &&
473 token != h.Cluster.Users.AnonymousUserToken {
474 // collection id is outside scope of supplied
476 tokenScopeProblem = true
479 } else if os.IsNotExist(err) {
480 // collection does not exist or is not
481 // readable using this token
484 } else if err != nil {
485 http.Error(w, err.Error(), http.StatusInternalServerError)
491 collectionDir, sessionFS, session, tokenUser = f, fs, sess, user
497 // The URL is a "secret sharing link" that
498 // didn't work out. Asking the client for
499 // additional credentials would just be
501 http.Error(w, notFoundMessage, http.StatusNotFound)
505 // The client provided valid token(s), but the
506 // collection was not found.
507 http.Error(w, notFoundMessage, http.StatusNotFound)
510 if tokenScopeProblem {
511 // The client provided a valid token but
512 // fetching a collection returned 401, which
513 // means the token scope doesn't permit
514 // fetching that collection.
515 http.Error(w, notFoundMessage, http.StatusForbidden)
518 // The client's token was invalid (e.g., expired), or
519 // the client didn't even provide one. Redirect to
520 // workbench2's login-and-redirect-to-download url if
521 // this is a browser navigation request. (The redirect
522 // flow can't preserve the original method if it's not
523 // GET, and doesn't make sense if the UA is a
524 // command-line tool, is trying to load an inline
525 // image, etc.; in these cases, there's nothing we can
526 // do, so return 401 unauthorized.)
528 // Note Sec-Fetch-Mode is sent by all non-EOL
529 // browsers, except Safari.
530 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode
532 // TODO(TC): This response would be confusing to
533 // someone trying (anonymously) to download public
534 // data that has been deleted. Allow a referrer to
535 // provide this context somehow?
536 if r.Method == http.MethodGet && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
537 target := url.URL(h.Cluster.Services.Workbench2.ExternalURL)
538 redirkey := "redirectToPreview"
540 redirkey = "redirectToDownload"
542 callback := "/c=" + collectionID + "/" + strings.Join(targetPath, "/")
543 query := url.Values{redirkey: {callback}}
544 queryString := query.Encode()
545 // Note: Encode (and QueryEscape function) turns space
546 // into plus sign (+) rather than %20 (the plus sign
547 // becomes %2B); that is the rule for web forms data
548 // sent in URL query part via GET, but we're not
549 // emulating forms here. Client JS APIs
550 // (URLSearchParam#get, decodeURIComponent) will
551 // decode %20, but while the former also expects the
552 // form-specific encoding, the latter doesn't.
553 // Encode() almost encodes everything; RFC 3986 3.4
554 // says "it is sometimes better for usability" to not
555 // encode / and ? when passing URI reference in query.
556 // This is also legal according to WHATWG URL spec and
557 // can be desirable for debugging webapp.
558 // We can let slash / appear in the encoded query, and
559 // equality-sign = too, but exempting ? is not very
561 // Plus-sign, hash, and ampersand are never exempt.
562 r := strings.NewReplacer("+", "%20", "%2F", "/", "%3D", "=")
563 target.RawQuery = r.Replace(queryString)
564 w.Header().Add("Location", target.String())
565 w.WriteHeader(http.StatusSeeOther)
569 http.Error(w, fmt.Sprintf("Authorization tokens are not accepted here: %v, and no anonymous user token is configured.", reasonNotAcceptingCredentials), http.StatusUnauthorized)
572 // If none of the above cases apply, suggest the
573 // user-agent (which is either a non-browser agent
574 // like wget, or a browser that can't redirect through
575 // a login flow) prompt the user for credentials.
576 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
577 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
581 // The first call to releaseSession() calls session.Release(),
582 // then subsequent calls are no-ops. This lets us use a defer
583 // call here to ensure it gets called in all code paths, and
584 // also call it inline (see below) in the cases where we want
585 // to release the lock before returning.
586 var releaseSessionOnce sync.Once
587 releaseSession := func() { releaseSessionOnce.Do(func() { session.Release() }) }
588 defer releaseSession()
590 colltarget := strings.Join(targetPath, "/")
591 colltarget = strings.TrimSuffix(colltarget, "/")
592 fstarget := fsprefix + colltarget
594 need, err := h.needSync(r.Context(), sessionFS, fstarget)
596 http.Error(w, err.Error(), http.StatusBadGateway)
602 err := collectionDir.Sync()
604 if he := errorWithHTTPStatus(nil); errors.As(err, &he) {
605 http.Error(w, err.Error(), he.HTTPStatus())
607 http.Error(w, err.Error(), http.StatusInternalServerError)
613 if r.Method == http.MethodGet || r.Method == http.MethodHead {
614 if fi, err := sessionFS.Stat(fstarget); err == nil && fi.IsDir() {
615 releaseSession() // because we won't be writing anything
616 if !strings.HasSuffix(r.URL.Path, "/") {
617 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
619 h.serveDirectory(w, r, fi.Name(), sessionFS, fstarget, !useSiteFS)
626 if len(targetPath) > 0 {
627 basename = targetPath[len(targetPath)-1]
629 if arvadosclient.PDHMatch(collectionID) && writeMethod[r.Method] {
630 http.Error(w, webdavfs.ErrReadOnly.Error(), http.StatusMethodNotAllowed)
633 if !h.userPermittedToUploadOrDownload(r.Method, tokenUser) {
634 http.Error(w, "Not permitted", http.StatusForbidden)
637 h.logUploadOrDownload(r, session.arvadosclient, sessionFS, fstarget, nil, tokenUser)
639 if webdavPrefix == "" && stripParts > 0 {
640 webdavPrefix = "/" + strings.Join(pathParts[:stripParts], "/")
643 writing := writeMethod[r.Method]
645 // We implement write operations by writing to a
646 // temporary collection, then applying the change to
647 // the real collection using the replace_files option
648 // in a collection update request. This lets us do
649 // the slow part (i.e., receive the file data from the
650 // client and write it to Keep) without worrying about
651 // side effects of other read/write operations.
653 // Collection update requests for a given collection
654 // are serialized by the controller, so we don't need
655 // to do any locking for that part either.
657 // collprefix is the subdirectory in the target
658 // collection which (according to X-Webdav-Source) we
659 // should pretend is "/" for this request.
660 collprefix := strings.TrimPrefix(fsprefix, "by_id/"+collectionID+"/")
661 if len(collprefix) == len(fsprefix) {
662 http.Error(w, "internal error: writing to anything other than /by_id/{collectionID}", http.StatusInternalServerError)
666 // Create a temporary collection filesystem for webdav
668 var tmpcoll arvados.Collection
669 client := session.client.WithRequestID(r.Header.Get("X-Request-Id"))
670 tmpfs, err := tmpcoll.FileSystem(client, session.keepclient)
672 http.Error(w, err.Error(), http.StatusInternalServerError)
675 snap, err := arvados.Snapshot(sessionFS, "by_id/"+collectionID+"/")
677 http.Error(w, "snapshot: "+err.Error(), http.StatusInternalServerError)
680 err = arvados.Splice(tmpfs, "/", snap)
682 http.Error(w, "splice: "+err.Error(), http.StatusInternalServerError)
687 fsprefix = collprefix
688 replace := make(map[string]string)
692 dsttarget, err := copyMoveDestination(r, webdavPrefix)
694 http.Error(w, err.Error(), http.StatusBadRequest)
698 srcspec := "current/" + colltarget
699 // RFC 4918 9.8.3: A COPY of "Depth: 0" only
700 // instructs that the collection and its
701 // properties, but not resources identified by
702 // its internal member URLs, are to be copied.
704 // ...meaning we will be creating an empty
707 // RFC 4918 9.9.2: A client MUST NOT submit a
708 // Depth header on a MOVE on a collection with
709 // any value but "infinity".
711 // ...meaning we only need to consider this
712 // case for COPY, not for MOVE.
713 if fi, err := tmpfs.Stat(colltarget); err == nil && fi.IsDir() && r.Method == "COPY" && r.Header.Get("Depth") == "0" {
714 srcspec = "manifest_text/"
717 replace[strings.TrimSuffix(dsttarget, "/")] = srcspec
718 if r.Method == "MOVE" {
719 replace["/"+colltarget] = ""
722 replace["/"+colltarget] = "manifest_text/"
724 if depth := r.Header.Get("Depth"); depth != "" && depth != "infinity" {
725 http.Error(w, "invalid depth header, see RFC 4918 9.6.1", http.StatusBadRequest)
728 replace["/"+colltarget] = ""
730 // changes will be applied by updateOnSuccess
732 case "LOCK", "UNLOCK", "PROPPATCH":
735 http.Error(w, "method missing", http.StatusInternalServerError)
739 // Save the collection only if/when all
740 // webdav->filesystem operations succeed using our
741 // temporary collection -- and send a 500 error if the
742 // updates can't be saved.
743 logger := ctxlog.FromContext(r.Context())
744 w = &updateOnSuccess{
747 update: func() error {
749 var snap *arvados.Subtree
751 if r.Method == "PUT" {
752 snap, err = arvados.Snapshot(tmpfs, colltarget)
754 return fmt.Errorf("snapshot tmpfs: %w", err)
756 tmpfs, err = (&arvados.Collection{}).FileSystem(client, session.keepclient)
757 err = arvados.Splice(tmpfs, "file", snap)
759 return fmt.Errorf("splice tmpfs: %w", err)
761 manifest, err = tmpfs.MarshalManifest(".")
763 return fmt.Errorf("marshal tmpfs: %w", err)
765 replace["/"+colltarget] = "manifest_text/file"
766 } else if len(replace) == 0 {
769 err = client.RequestAndDecode(nil, "PATCH", "arvados/v1/collections/"+collectionID, nil, map[string]interface{}{
770 "replace_files": replace,
771 "collection": map[string]interface{}{"manifest_text": manifest}})
772 var te arvados.TransactionError
773 if errors.As(err, &te) {
782 // When writing, we need to block session renewal
783 // until we're finished, in order to guarantee the
784 // effect of the write is visible in future responses.
785 // But if we're not writing, we can release the lock
786 // early. This enables us to keep renewing sessions
787 // and processing more requests even if a slow client
788 // takes a long time to download a large file.
792 if r.Method == http.MethodGet {
793 applyContentDispositionHdr(w, r, basename, attachment)
795 wh := &webdav.Handler{
796 Prefix: webdavPrefix,
797 FileSystem: &webdavfs.FS{
798 FileSystem: targetFS,
800 Writing: writeMethod[r.Method],
801 AlwaysReadEOF: r.Method == "PROPFIND",
803 LockSystem: webdavfs.NoLockSystem,
804 Logger: func(r *http.Request, err error) {
805 if err != nil && !os.IsNotExist(err) {
806 ctxlog.FromContext(r.Context()).WithError(err).Error("error reported by webdav handler")
810 h.metrics.track(wh, w, r)
811 if r.Method == http.MethodGet && w.WroteStatus() == http.StatusOK {
812 wrote := int64(w.WroteBodyBytes())
813 fi, err := wh.FileSystem.Stat(r.Context(), colltarget)
814 if err == nil && fi.Size() != wrote {
816 f, err := wh.FileSystem.OpenFile(r.Context(), colltarget, os.O_RDONLY, 0)
818 n, err = f.Read(make([]byte, 1024))
821 ctxlog.FromContext(r.Context()).Errorf("stat.Size()==%d but only wrote %d bytes; read(1024) returns %d, %v", fi.Size(), wrote, n, err)
826 var dirListingTemplate = `<!DOCTYPE HTML>
828 <META name="robots" content="NOINDEX">
829 <TITLE>{{ .CollectionName }}</TITLE>
830 <STYLE type="text/css">
835 background-color: #D9EDF7;
836 border-radius: .25em;
844 border: 1px solid #808080;
850 font-family: monospace;
857 <H1>{{ .CollectionName }}</H1>
859 <P>This collection of data files is being shared with you through
860 Arvados. You can download individual files listed below. To download
861 the entire directory tree with <CODE>wget</CODE>, try:</P>
863 <PRE id="wget-example">$ wget --mirror --no-parent --no-host --cut-dirs={{ .StripParts }} {{ .QuotedUrlForWget }}</PRE>
865 <H2>File Listing</H2>
871 <LI>{{" " | printf "%15s " | nbsp}}<A class="item" href="{{ .Href }}/">{{ .Name }}/</A></LI>
873 <LI>{{.Size | printf "%15d " | nbsp}}<A class="item" href="{{ .Href }}">{{ .Name }}</A></LI>
878 <P>(No files; this collection is empty.)</P>
885 Arvados is a free and open source software bioinformatics platform.
886 To learn more, visit arvados.org.
887 Arvados is not responsible for the files listed on this page.
895 type fileListEnt struct {
902 // Given a filesystem path like `foo/"bar baz"`, return an escaped
903 // (percent-encoded) relative path like `./foo/%22bar%20%baz%22`.
905 // Note the result may contain html-unsafe characters like '&'. These
906 // will be handled separately by the HTML templating engine as needed.
907 func relativeHref(path string) string {
908 u := &url.URL{Path: path}
909 return "./" + u.EscapedPath()
912 // Return a shell-quoted URL suitable for pasting to a command line
913 // ("wget ...") to repeat the given HTTP request.
914 func makeQuotedUrlForWget(r *http.Request) string {
915 scheme := r.Header.Get("X-Forwarded-Proto")
916 if scheme == "http" || scheme == "https" {
917 // use protocol reported by load balancer / proxy
918 } else if r.TLS != nil {
923 p := r.URL.EscapedPath()
924 // An escaped path may still contain single quote chars, which
925 // would interfere with our shell quoting. Avoid this by
926 // escaping them as %27.
927 return fmt.Sprintf("'%s://%s%s'", scheme, r.Host, strings.Replace(p, "'", "%27", -1))
930 func (h *handler) serveDirectory(w http.ResponseWriter, r *http.Request, collectionName string, fs http.FileSystem, base string, recurse bool) {
931 var files []fileListEnt
932 var walk func(string) error
933 if !strings.HasSuffix(base, "/") {
936 walk = func(path string) error {
937 dirname := base + path
939 dirname = strings.TrimSuffix(dirname, "/")
941 d, err := fs.Open(dirname)
945 ents, err := d.Readdir(-1)
949 for _, ent := range ents {
950 if recurse && ent.IsDir() {
951 err = walk(path + ent.Name() + "/")
956 listingName := path + ent.Name()
957 files = append(files, fileListEnt{
959 Href: relativeHref(listingName),
967 if err := walk(""); err != nil {
968 http.Error(w, "error getting directory listing: "+err.Error(), http.StatusInternalServerError)
972 funcs := template.FuncMap{
973 "nbsp": func(s string) template.HTML {
974 return template.HTML(strings.Replace(s, " ", " ", -1))
977 tmpl, err := template.New("dir").Funcs(funcs).Parse(dirListingTemplate)
979 http.Error(w, "error parsing template: "+err.Error(), http.StatusInternalServerError)
982 sort.Slice(files, func(i, j int) bool {
983 return files[i].Name < files[j].Name
985 w.WriteHeader(http.StatusOK)
986 tmpl.Execute(w, map[string]interface{}{
987 "CollectionName": collectionName,
990 "StripParts": strings.Count(strings.TrimRight(r.URL.Path, "/"), "/"),
991 "QuotedUrlForWget": makeQuotedUrlForWget(r),
995 func applyContentDispositionHdr(w http.ResponseWriter, r *http.Request, filename string, isAttachment bool) {
996 disposition := "inline"
998 disposition = "attachment"
1000 if strings.ContainsRune(r.RequestURI, '?') {
1001 // Help the UA realize that the filename is just
1002 // "filename.txt", not
1003 // "filename.txt?disposition=attachment".
1005 // TODO(TC): Follow advice at RFC 6266 appendix D
1006 disposition += "; filename=" + strconv.QuoteToASCII(filename)
1008 if disposition != "inline" {
1009 w.Header().Set("Content-Disposition", disposition)
1013 func (h *handler) seeOtherWithCookie(w http.ResponseWriter, r *http.Request, location string, credentialsOK bool) {
1014 if formTokens, haveFormTokens := r.Form["api_token"]; haveFormTokens {
1016 // It is not safe to copy the provided token
1017 // into a cookie unless the current vhost
1018 // (origin) serves only a single collection or
1019 // we are in TrustAllContent mode.
1020 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)
1024 // The HttpOnly flag is necessary to prevent
1025 // JavaScript code (included in, or loaded by, a page
1026 // in the collection being served) from employing the
1027 // user's token beyond reading other files in the same
1028 // domain, i.e., same collection.
1030 // The 303 redirect is necessary in the case of a GET
1031 // request to avoid exposing the token in the Location
1032 // bar, and in the case of a POST request to avoid
1033 // raising warnings when the user refreshes the
1035 for _, tok := range formTokens {
1039 http.SetCookie(w, &http.Cookie{
1040 Name: "arvados_api_token",
1041 Value: auth.EncodeTokenCookie([]byte(tok)),
1044 SameSite: http.SameSiteLaxMode,
1050 // Propagate query parameters (except api_token) from
1051 // the original request.
1052 redirQuery := r.URL.Query()
1053 redirQuery.Del("api_token")
1057 newu, err := u.Parse(location)
1059 http.Error(w, "error resolving redirect target: "+err.Error(), http.StatusInternalServerError)
1065 Scheme: r.URL.Scheme,
1068 RawQuery: redirQuery.Encode(),
1071 w.Header().Add("Location", redir)
1072 w.WriteHeader(http.StatusSeeOther)
1073 io.WriteString(w, `<A href="`)
1074 io.WriteString(w, html.EscapeString(redir))
1075 io.WriteString(w, `">Continue</A>`)
1078 func (h *handler) userPermittedToUploadOrDownload(method string, tokenUser *arvados.User) bool {
1079 var permitDownload bool
1080 var permitUpload bool
1081 if tokenUser != nil && tokenUser.IsAdmin {
1082 permitUpload = h.Cluster.Collections.WebDAVPermission.Admin.Upload
1083 permitDownload = h.Cluster.Collections.WebDAVPermission.Admin.Download
1085 permitUpload = h.Cluster.Collections.WebDAVPermission.User.Upload
1086 permitDownload = h.Cluster.Collections.WebDAVPermission.User.Download
1088 if (method == "PUT" || method == "POST") && !permitUpload {
1089 // Disallow operations that upload new files.
1090 // Permit webdav operations that move existing files around.
1092 } else if method == "GET" && !permitDownload {
1093 // Disallow downloading file contents.
1094 // Permit webdav operations like PROPFIND that retrieve metadata
1095 // but not file contents.
1101 // Parse the request's Destination header and return the destination
1102 // path relative to the current collection, i.e., with webdavPrefix
1104 func copyMoveDestination(r *http.Request, webdavPrefix string) (string, error) {
1105 dsturl, err := url.Parse(r.Header.Get("Destination"))
1109 if dsturl.Host != "" && dsturl.Host != r.Host {
1110 return "", errors.New("destination host mismatch")
1112 if webdavPrefix == "" {
1113 return dsturl.Path, nil
1115 dsttarget := strings.TrimPrefix(dsturl.Path, webdavPrefix)
1116 if len(dsttarget) == len(dsturl.Path) {
1117 return "", errors.New("destination path not supported")
1119 return dsttarget, nil
1122 // Check whether fstarget is in a collection whose PDH has changed
1123 // since it was last Sync()ed in sessionFS.
1125 // If fstarget doesn't exist, but would be in such a collection if it
1126 // did exist, return true.
1127 func (h *handler) needSync(ctx context.Context, sessionFS arvados.CustomFileSystem, fstarget string) (bool, error) {
1128 collection, _ := h.determineCollection(sessionFS, fstarget)
1129 if collection == nil || len(collection.UUID) != 27 || !strings.HasPrefix(collection.UUID, h.Cluster.ClusterID) {
1132 db, err := h.getDBConnector().GetDB(ctx)
1136 var currentPDH string
1137 err = db.QueryRowContext(ctx, `select portable_data_hash from collections where uuid=$1`, collection.UUID).Scan(¤tPDH)
1141 if currentPDH != collection.PortableDataHash {
1147 type fileEventLog struct {
1159 func newFileEventLog(
1163 collection *arvados.Collection,
1167 var eventType string
1170 eventType = "file_upload"
1172 eventType = "file_download"
1177 // We want to log the address of the proxy closest to keep-web—the last
1178 // value in the X-Forwarded-For list—or the client address if there is no
1180 var clientAddr string
1181 // 1. Build a slice of proxy addresses from X-Forwarded-For.
1182 xff := strings.Join(r.Header.Values("X-Forwarded-For"), ",")
1183 addrs := strings.Split(xff, ",")
1184 // 2. Reverse the slice so it's in our most preferred order for logging.
1185 slices.Reverse(addrs)
1186 // 3. Append the client address to that slice.
1187 if addr, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
1188 addrs = append(addrs, addr)
1190 // 4. Use the first valid address in the slice.
1191 for _, addr := range addrs {
1192 if ip := net.ParseIP(strings.TrimSpace(addr)); ip != nil {
1193 clientAddr = ip.String()
1198 ev := &fileEventLog{
1199 requestPath: r.URL.Path,
1200 eventType: eventType,
1201 clientAddr: clientAddr,
1206 ev.userUUID = user.UUID
1207 ev.userFullName = user.FullName
1209 ev.userUUID = fmt.Sprintf("%s-tpzed-anonymouspublic", h.Cluster.ClusterID)
1212 if collection != nil {
1213 ev.collFilePath = filepath
1214 // h.determineCollection populates the collection_uuid
1215 // prop with the PDH, if this collection is being
1216 // accessed via PDH. For logging, we use a different
1217 // field depending on whether it's a UUID or PDH.
1218 if len(collection.UUID) > 32 {
1219 ev.collPDH = collection.UUID
1221 ev.collPDH = collection.PortableDataHash
1222 ev.collUUID = collection.UUID
1229 func (ev *fileEventLog) shouldLogPDH() bool {
1230 return ev.eventType == "file_download" && ev.collPDH != ""
1233 func (ev *fileEventLog) asDict() arvadosclient.Dict {
1234 props := arvadosclient.Dict{
1235 "reqPath": ev.requestPath,
1236 "collection_uuid": ev.collUUID,
1237 "collection_file_path": ev.collFilePath,
1239 if ev.shouldLogPDH() {
1240 props["portable_data_hash"] = ev.collPDH
1242 return arvadosclient.Dict{
1243 "object_uuid": ev.userUUID,
1244 "event_type": ev.eventType,
1245 "properties": props,
1249 func (ev *fileEventLog) asFields() logrus.Fields {
1250 fields := logrus.Fields{
1251 "collection_file_path": ev.collFilePath,
1252 "collection_uuid": ev.collUUID,
1253 "user_uuid": ev.userUUID,
1255 if ev.shouldLogPDH() {
1256 fields["portable_data_hash"] = ev.collPDH
1258 if !strings.HasSuffix(ev.userUUID, "-tpzed-anonymouspublic") {
1259 fields["user_full_name"] = ev.userFullName
1264 func (h *handler) shouldLogEvent(
1265 event *fileEventLog,
1267 fileInfo os.FileInfo,
1272 } else if event.eventType != "file_download" ||
1273 h.Cluster.Collections.WebDAVLogDownloadInterval == 0 ||
1277 td := h.Cluster.Collections.WebDAVLogDownloadInterval.Duration()
1278 cutoff := t.Add(-td)
1280 h.fileEventLogsMtx.Lock()
1281 defer h.fileEventLogsMtx.Unlock()
1282 if h.fileEventLogs == nil {
1283 h.fileEventLogs = make(map[fileEventLog]time.Time)
1285 shouldLog := h.fileEventLogs[ev].Before(cutoff)
1287 // Go's http fs server evaluates http.Request.Header.Get("Range")
1288 // (as of Go 1.22) so we should do the same.
1289 // Don't worry about merging multiple headers, etc.
1290 ranges, err := http_range.ParseRange(req.Header.Get("Range"), fileInfo.Size())
1291 if ranges == nil || err != nil {
1292 // The Range header was either empty or malformed.
1293 // Err on the side of logging.
1296 // Log this request only if it requested the first byte
1297 // (our heuristic for "starting a new download").
1298 for _, reqRange := range ranges {
1299 if reqRange.Start == 0 {
1307 h.fileEventLogs[ev] = t
1309 if t.After(h.fileEventLogsNextTidy) {
1310 for key, logTime := range h.fileEventLogs {
1311 if logTime.Before(cutoff) {
1312 delete(h.fileEventLogs, key)
1315 h.fileEventLogsNextTidy = t.Add(td)
1320 func (h *handler) logUploadOrDownload(
1322 client *arvadosclient.ArvadosClient,
1323 fs arvados.CustomFileSystem,
1325 collection *arvados.Collection,
1328 var fileInfo os.FileInfo
1330 if collection == nil {
1331 collection, filepath = h.determineCollection(fs, filepath)
1333 if collection != nil {
1334 // It's okay to ignore this error because shouldLogEvent will
1335 // always return true if fileInfo == nil.
1336 fileInfo, _ = fs.Stat(path.Join("by_id", collection.UUID, filepath))
1339 event := newFileEventLog(h, r, filepath, collection, user, client.ApiToken)
1340 if !h.shouldLogEvent(event, r, fileInfo, time.Now()) {
1343 log := ctxlog.FromContext(r.Context()).WithFields(event.asFields())
1344 log.Info(strings.Replace(event.eventType, "file_", "File ", 1))
1345 if h.Cluster.Collections.WebDAVLogEvents {
1347 logReq := arvadosclient.Dict{"log": event.asDict()}
1348 err := client.Create("logs", logReq, nil)
1350 log.WithError(err).Errorf("Failed to create %s log event on API server", event.eventType)
1356 func (h *handler) determineCollection(fs arvados.CustomFileSystem, path string) (*arvados.Collection, string) {
1357 target := strings.TrimSuffix(path, "/")
1358 for cut := len(target); cut >= 0; cut = strings.LastIndexByte(target, '/') {
1359 target = target[:cut]
1360 fi, err := fs.Stat(target)
1361 if os.IsNotExist(err) {
1362 // creating a new file/dir, or download
1365 } else if err != nil {
1368 switch src := fi.Sys().(type) {
1369 case *arvados.Collection:
1370 return src, strings.TrimPrefix(path[len(target):], "/")
1371 case *arvados.Group:
1374 if _, ok := src.(error); ok {
1382 func ServeCORSPreflight(w http.ResponseWriter, header http.Header) bool {
1383 method := header.Get("Access-Control-Request-Method")
1387 if !browserMethod[method] && !webdavMethod[method] {
1388 w.WriteHeader(http.StatusMethodNotAllowed)
1391 w.Header().Set("Access-Control-Allow-Headers", corsAllowHeadersHeader)
1392 w.Header().Set("Access-Control-Allow-Methods", "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
1393 w.Header().Set("Access-Control-Allow-Origin", "*")
1394 w.Header().Set("Access-Control-Max-Age", "86400")