1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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/auth"
26 "git.arvados.org/arvados.git/sdk/go/ctxlog"
27 "git.arvados.org/arvados.git/sdk/go/httpserver"
28 "git.arvados.org/arvados.git/sdk/go/keepclient"
29 "github.com/sirupsen/logrus"
30 "golang.org/x/net/webdav"
35 Cluster *arvados.Cluster
36 clientPool *arvadosclient.ClientPool
38 webdavLS webdav.LockSystem
41 var urlPDHDecoder = strings.NewReplacer(" ", "+", "-", "+")
43 var notFoundMessage = "Not Found"
44 var unauthorizedMessage = "401 Unauthorized\r\n\r\nA valid Arvados token must be provided to access this resource.\r\n"
46 // parseCollectionIDFromURL returns a UUID or PDH if s is a UUID or a
47 // PDH (even if it is a PDH with "+" replaced by " " or "-");
49 func parseCollectionIDFromURL(s string) string {
50 if arvadosclient.UUIDMatch(s) {
53 if pdh := urlPDHDecoder.Replace(s); arvadosclient.PDHMatch(pdh) {
59 func (h *handler) setup() {
60 // Errors will be handled at the client pool.
61 arv, _ := arvados.NewClientFromConfig(h.Cluster)
62 h.clientPool = arvadosclient.MakeClientPoolWith(arv)
64 keepclient.DefaultBlockCache.MaxBlocks = h.Cluster.Collections.WebDAVCache.MaxBlockEntries
66 // Even though we don't accept LOCK requests, every webdav
67 // handler must have a non-nil LockSystem.
68 h.webdavLS = &noLockSystem{}
71 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
72 json.NewEncoder(w).Encode(struct{ Version string }{version})
75 // updateOnSuccess wraps httpserver.ResponseWriter. If the handler
76 // sends an HTTP header indicating success, updateOnSuccess first
77 // calls the provided update func. If the update func fails, an error
78 // response is sent (using the error's HTTP status or 500 if none),
79 // and the status code and body sent by the handler are ignored (all
80 // response writes return the update error).
81 type updateOnSuccess struct {
82 httpserver.ResponseWriter
83 logger logrus.FieldLogger
89 func (uos *updateOnSuccess) Write(p []byte) (int, error) {
91 uos.WriteHeader(http.StatusOK)
96 return uos.ResponseWriter.Write(p)
99 func (uos *updateOnSuccess) WriteHeader(code int) {
101 uos.sentHeader = true
102 if code >= 200 && code < 400 {
103 if uos.err = uos.update(); uos.err != nil {
104 code := http.StatusInternalServerError
105 var he interface{ HTTPStatus() int }
106 if errors.As(uos.err, &he) {
107 code = he.HTTPStatus()
109 uos.logger.WithError(uos.err).Errorf("update() returned %T error, changing response to HTTP %d", uos.err, code)
110 http.Error(uos.ResponseWriter, uos.err.Error(), code)
115 uos.ResponseWriter.WriteHeader(code)
119 corsAllowHeadersHeader = strings.Join([]string{
120 "Authorization", "Content-Type", "Range",
121 // WebDAV request headers:
122 "Depth", "Destination", "If", "Lock-Token", "Overwrite", "Timeout",
124 writeMethod = map[string]bool{
135 webdavMethod = map[string]bool{
148 browserMethod = map[string]bool{
153 // top-level dirs to serve with siteFS
154 siteFSDir = map[string]bool{
155 "": true, // root directory
161 func stripDefaultPort(host string) string {
162 // Will consider port 80 and port 443 to be the same vhost. I think that's fine.
163 u := &url.URL{Host: host}
164 if p := u.Port(); p == "80" || p == "443" {
165 return strings.ToLower(u.Hostname())
167 return strings.ToLower(host)
171 // CheckHealth implements service.Handler.
172 func (h *handler) CheckHealth() error {
176 // Done implements service.Handler.
177 func (h *handler) Done() <-chan struct{} {
181 // ServeHTTP implements http.Handler.
182 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
183 h.setupOnce.Do(h.setup)
185 if xfp := r.Header.Get("X-Forwarded-Proto"); xfp != "" && xfp != "http" {
189 w := httpserver.WrapResponseWriter(wOrig)
191 if method := r.Header.Get("Access-Control-Request-Method"); method != "" && r.Method == "OPTIONS" {
192 if !browserMethod[method] && !webdavMethod[method] {
193 w.WriteHeader(http.StatusMethodNotAllowed)
196 w.Header().Set("Access-Control-Allow-Headers", corsAllowHeadersHeader)
197 w.Header().Set("Access-Control-Allow-Methods", "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
198 w.Header().Set("Access-Control-Allow-Origin", "*")
199 w.Header().Set("Access-Control-Max-Age", "86400")
203 if !browserMethod[r.Method] && !webdavMethod[r.Method] {
204 w.WriteHeader(http.StatusMethodNotAllowed)
208 if r.Header.Get("Origin") != "" {
209 // Allow simple cross-origin requests without user
210 // credentials ("user credentials" as defined by CORS,
211 // i.e., cookies, HTTP authentication, and client-side
212 // SSL certificates. See
213 // http://www.w3.org/TR/cors/#user-credentials).
214 w.Header().Set("Access-Control-Allow-Origin", "*")
215 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
222 pathParts := strings.Split(r.URL.Path[1:], "/")
225 var collectionID string
227 var reqTokens []string
231 credentialsOK := h.Cluster.Collections.TrustAllContent
232 reasonNotAcceptingCredentials := ""
234 if r.Host != "" && stripDefaultPort(r.Host) == stripDefaultPort(h.Cluster.Services.WebDAVDownload.ExternalURL.Host) {
237 } else if r.FormValue("disposition") == "attachment" {
242 reasonNotAcceptingCredentials = fmt.Sprintf("vhost %q does not specify a single collection ID or match Services.WebDAVDownload.ExternalURL %q, and Collections.TrustAllContent is false",
243 r.Host, h.Cluster.Services.WebDAVDownload.ExternalURL)
246 if collectionID = arvados.CollectionIDFromDNSName(r.Host); collectionID != "" {
247 // http://ID.collections.example/PATH...
249 } else if r.URL.Path == "/status.json" {
252 } else if siteFSDir[pathParts[0]] {
254 } else if len(pathParts) >= 1 && strings.HasPrefix(pathParts[0], "c=") {
256 collectionID = parseCollectionIDFromURL(pathParts[0][2:])
258 } else if len(pathParts) >= 2 && pathParts[0] == "collections" {
259 if len(pathParts) >= 4 && pathParts[1] == "download" {
260 // /collections/download/ID/TOKEN/PATH...
261 collectionID = parseCollectionIDFromURL(pathParts[2])
262 tokens = []string{pathParts[3]}
266 // /collections/ID/PATH...
267 collectionID = parseCollectionIDFromURL(pathParts[1])
269 // This path is only meant to work for public
270 // data. Tokens provided with the request are
272 credentialsOK = false
273 reasonNotAcceptingCredentials = "the '/collections/UUID/PATH' form only works for public data"
277 if collectionID == "" && !useSiteFS {
278 http.Error(w, notFoundMessage, http.StatusNotFound)
283 if cc := r.Header.Get("Cache-Control"); strings.Contains(cc, "no-cache") || strings.Contains(cc, "must-revalidate") {
288 reqTokens = auth.CredentialsFromRequest(r).Tokens
291 formToken := r.FormValue("api_token")
292 origin := r.Header.Get("Origin")
293 cors := origin != "" && !strings.HasSuffix(origin, "://"+r.Host)
294 safeAjax := cors && (r.Method == http.MethodGet || r.Method == http.MethodHead)
295 safeAttachment := attachment && r.URL.Query().Get("api_token") == ""
297 // No token to use or redact.
298 } else if safeAjax || safeAttachment {
299 // If this is a cross-origin request, the URL won't
300 // appear in the browser's address bar, so
301 // substituting a clipboard-safe URL is pointless.
302 // Redirect-with-cookie wouldn't work anyway, because
303 // it's not safe to allow third-party use of our
306 // If we're supplying an attachment, we don't need to
307 // convert POST to GET to avoid the "really resubmit
308 // form?" problem, so provided the token isn't
309 // embedded in the URL, there's no reason to do
310 // redirect-with-cookie in this case either.
311 reqTokens = append(reqTokens, formToken)
312 } else if browserMethod[r.Method] {
313 // If this is a page view, and the client provided a
314 // token via query string or POST body, we must put
315 // the token in an HttpOnly cookie, and redirect to an
316 // equivalent URL with the query param redacted and
318 h.seeOtherWithCookie(w, r, "", credentialsOK)
323 h.serveSiteFS(w, r, reqTokens, credentialsOK, attachment)
327 targetPath := pathParts[stripParts:]
328 if tokens == nil && len(targetPath) > 0 && strings.HasPrefix(targetPath[0], "t=") {
329 // http://ID.example/t=TOKEN/PATH...
330 // /c=ID/t=TOKEN/PATH...
332 // This form must only be used to pass scoped tokens
333 // that give permission for a single collection. See
334 // FormValue case above.
335 tokens = []string{targetPath[0][2:]}
337 targetPath = targetPath[1:]
343 if h.Cluster.Users.AnonymousUserToken != "" {
344 tokens = append(tokens, h.Cluster.Users.AnonymousUserToken)
350 http.Error(w, fmt.Sprintf("Authorization tokens are not accepted here: %v, and no anonymous user token is configured.", reasonNotAcceptingCredentials), http.StatusUnauthorized)
352 http.Error(w, fmt.Sprintf("No authorization token in request, and no anonymous user token is configured."), http.StatusUnauthorized)
357 if len(targetPath) > 0 && targetPath[0] == "_" {
358 // If a collection has a directory called "t=foo" or
359 // "_", it can be served at
360 // //collections.example/_/t=foo/ or
361 // //collections.example/_/_/ respectively:
362 // //collections.example/t=foo/ won't work because
363 // t=foo will be interpreted as a token "foo".
364 targetPath = targetPath[1:]
368 arv := h.clientPool.Get()
370 http.Error(w, "client pool error: "+h.clientPool.Err().Error(), http.StatusInternalServerError)
373 defer h.clientPool.Put(arv)
375 dirOpenMode := os.O_RDONLY
376 if writeMethod[r.Method] {
377 dirOpenMode = os.O_RDWR
380 validToken := make(map[string]bool)
382 var tokenUser *arvados.User
383 var sessionFS arvados.CustomFileSystem
384 var session *cachedSession
385 var collectionDir arvados.File
386 for _, token = range tokens {
387 var statusErr interface{ HTTPStatus() int }
388 fs, sess, user, err := h.Cache.GetSession(token)
389 if errors.As(err, &statusErr) && statusErr.HTTPStatus() == http.StatusUnauthorized {
392 } else if err != nil {
393 http.Error(w, "cache error: "+err.Error(), http.StatusInternalServerError)
396 f, err := fs.OpenFile("by_id/"+collectionID, dirOpenMode, 0)
397 if errors.As(err, &statusErr) && statusErr.HTTPStatus() == http.StatusForbidden {
398 // collection id is outside token scope
399 validToken[token] = true
402 validToken[token] = true
403 if os.IsNotExist(err) {
404 // collection does not exist or is not
405 // readable using this token
407 } else if err != nil {
408 http.Error(w, err.Error(), http.StatusInternalServerError)
413 collectionDir, sessionFS, session, tokenUser = f, fs, sess, user
417 err := collectionDir.Sync()
419 var statusErr interface{ HTTPStatus() int }
420 if errors.As(err, &statusErr) {
421 http.Error(w, err.Error(), statusErr.HTTPStatus())
423 http.Error(w, err.Error(), http.StatusInternalServerError)
429 if pathToken || !credentialsOK {
430 // Either the URL is a "secret sharing link"
431 // that didn't work out (and asking the client
432 // for additional credentials would just be
433 // confusing), or we don't even accept
434 // credentials at this path.
435 http.Error(w, notFoundMessage, http.StatusNotFound)
438 for _, t := range reqTokens {
440 // The client provided valid token(s),
441 // but the collection was not found.
442 http.Error(w, notFoundMessage, http.StatusNotFound)
446 // The client's token was invalid (e.g., expired), or
447 // the client didn't even provide one. Redirect to
448 // workbench2's login-and-redirect-to-download url if
449 // this is a browser navigation request. (The redirect
450 // flow can't preserve the original method if it's not
451 // GET, and doesn't make sense if the UA is a
452 // command-line tool, is trying to load an inline
453 // image, etc.; in these cases, there's nothing we can
454 // do, so return 401 unauthorized.)
456 // Note Sec-Fetch-Mode is sent by all non-EOL
457 // browsers, except Safari.
458 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode
460 // TODO(TC): This response would be confusing to
461 // someone trying (anonymously) to download public
462 // data that has been deleted. Allow a referrer to
463 // provide this context somehow?
464 if r.Method == http.MethodGet && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
465 target := url.URL(h.Cluster.Services.Workbench2.ExternalURL)
466 redirkey := "redirectToPreview"
468 redirkey = "redirectToDownload"
470 callback := "/c=" + collectionID + "/" + strings.Join(targetPath, "/")
471 // target.RawQuery = url.Values{redirkey:
472 // {target}}.Encode() would be the obvious
473 // thing to do here, but wb2 doesn't decode
474 // this as a query param -- it takes
475 // everything after "${redirkey}=" as the
476 // target URL. If we encode "/" as "%2F" etc.,
477 // the redirect won't work.
478 target.RawQuery = redirkey + "=" + callback
479 w.Header().Add("Location", target.String())
480 w.WriteHeader(http.StatusSeeOther)
482 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
483 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
489 if len(targetPath) > 0 {
490 basename = targetPath[len(targetPath)-1]
492 applyContentDispositionHdr(w, r, basename, attachment)
494 if arvadosclient.PDHMatch(collectionID) && writeMethod[r.Method] {
495 http.Error(w, errReadOnly.Error(), http.StatusMethodNotAllowed)
498 if !h.userPermittedToUploadOrDownload(r.Method, tokenUser) {
499 http.Error(w, "Not permitted", http.StatusForbidden)
502 h.logUploadOrDownload(r, session.arvadosclient, sessionFS, "by_id/"+collectionID+"/"+strings.Join(targetPath, "/"), nil, tokenUser)
504 if writeMethod[r.Method] {
505 // Save the collection only if/when all
506 // webdav->filesystem operations succeed --
507 // and send a 500 error if the modified
508 // collection can't be saved.
510 // Perform the write in a separate sitefs, so
511 // concurrent read operations on the same
512 // collection see the previous saved
513 // state. After the write succeeds and the
514 // collection record is updated, we reset the
515 // session so the updates are visible in
516 // subsequent read requests.
517 client := session.client.WithRequestID(r.Header.Get("X-Request-Id"))
518 sessionFS = client.SiteFileSystem(session.keepclient)
519 writingDir, err := sessionFS.OpenFile("by_id/"+collectionID, os.O_RDONLY, 0)
521 http.Error(w, err.Error(), http.StatusInternalServerError)
524 defer writingDir.Close()
525 w = &updateOnSuccess{
527 logger: ctxlog.FromContext(r.Context()),
528 update: func() error {
529 err := writingDir.Sync()
530 var te arvados.TransactionError
531 if errors.As(err, &te) {
537 // Sync the changes to the persistent
538 // sessionfs for this token.
539 snap, err := writingDir.Snapshot()
543 collectionDir.Splice(snap)
547 wh := webdav.Handler{
548 Prefix: "/" + strings.Join(pathParts[:stripParts], "/"),
549 FileSystem: &webdavFS{
551 prefix: "by_id/" + collectionID + "/",
552 writing: writeMethod[r.Method],
553 alwaysReadEOF: r.Method == "PROPFIND",
555 LockSystem: h.webdavLS,
556 Logger: func(r *http.Request, err error) {
558 ctxlog.FromContext(r.Context()).WithError(err).Error("error reported by webdav handler")
562 if r.Method == http.MethodGet || r.Method == http.MethodHead {
563 targetfnm := "by_id/" + collectionID + "/" + strings.Join(pathParts[stripParts:], "/")
564 if fi, err := sessionFS.Stat(targetfnm); err == nil && fi.IsDir() {
565 if !strings.HasSuffix(r.URL.Path, "/") {
566 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
568 h.serveDirectory(w, r, fi.Name(), sessionFS, targetfnm, true)
574 if r.Method == http.MethodGet && w.WroteStatus() == http.StatusOK {
575 wrote := int64(w.WroteBodyBytes())
576 fnm := strings.Join(pathParts[stripParts:], "/")
577 fi, err := wh.FileSystem.Stat(r.Context(), fnm)
578 if err == nil && fi.Size() != wrote {
580 f, err := wh.FileSystem.OpenFile(r.Context(), fnm, os.O_RDONLY, 0)
582 n, err = f.Read(make([]byte, 1024))
585 ctxlog.FromContext(r.Context()).Errorf("stat.Size()==%d but only wrote %d bytes; read(1024) returns %d, %v", fi.Size(), wrote, n, err)
590 func (h *handler) getClients(reqID, token string) (arv *arvadosclient.ArvadosClient, kc *keepclient.KeepClient, client *arvados.Client, release func(), err error) {
591 arv = h.clientPool.Get()
593 err = h.clientPool.Err()
596 release = func() { h.clientPool.Put(arv) }
598 kc, err = keepclient.MakeKeepClient(arv)
604 client = (&arvados.Client{
605 APIHost: arv.ApiServer,
606 AuthToken: arv.ApiToken,
607 Insecure: arv.ApiInsecure,
608 }).WithRequestID(reqID)
612 func (h *handler) serveSiteFS(w http.ResponseWriter, r *http.Request, tokens []string, credentialsOK, attachment bool) {
613 if len(tokens) == 0 {
614 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
615 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
618 if writeMethod[r.Method] {
619 http.Error(w, errReadOnly.Error(), http.StatusMethodNotAllowed)
623 fs, sess, user, err := h.Cache.GetSession(tokens[0])
625 http.Error(w, err.Error(), http.StatusInternalServerError)
628 f, err := fs.Open(r.URL.Path)
629 if os.IsNotExist(err) {
630 http.Error(w, err.Error(), http.StatusNotFound)
632 } else if err != nil {
633 http.Error(w, err.Error(), http.StatusInternalServerError)
637 if fi, err := f.Stat(); err == nil && fi.IsDir() && r.Method == "GET" {
638 if !strings.HasSuffix(r.URL.Path, "/") {
639 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
641 h.serveDirectory(w, r, fi.Name(), fs, r.URL.Path, false)
646 if !h.userPermittedToUploadOrDownload(r.Method, user) {
647 http.Error(w, "Not permitted", http.StatusForbidden)
650 h.logUploadOrDownload(r, sess.arvadosclient, fs, r.URL.Path, nil, user)
652 if r.Method == "GET" {
653 _, basename := filepath.Split(r.URL.Path)
654 applyContentDispositionHdr(w, r, basename, attachment)
656 wh := webdav.Handler{
657 FileSystem: &webdavFS{
659 writing: writeMethod[r.Method],
660 alwaysReadEOF: r.Method == "PROPFIND",
662 LockSystem: h.webdavLS,
663 Logger: func(_ *http.Request, err error) {
665 ctxlog.FromContext(r.Context()).WithError(err).Error("error reported by webdav handler")
672 var dirListingTemplate = `<!DOCTYPE HTML>
674 <META name="robots" content="NOINDEX">
675 <TITLE>{{ .CollectionName }}</TITLE>
676 <STYLE type="text/css">
681 background-color: #D9EDF7;
682 border-radius: .25em;
693 font-family: monospace;
700 <H1>{{ .CollectionName }}</H1>
702 <P>This collection of data files is being shared with you through
703 Arvados. You can download individual files listed below. To download
704 the entire directory tree with wget, try:</P>
706 <PRE>$ wget --mirror --no-parent --no-host --cut-dirs={{ .StripParts }} https://{{ .Request.Host }}{{ .Request.URL.Path }}</PRE>
708 <H2>File Listing</H2>
714 <LI>{{" " | printf "%15s " | nbsp}}<A href="{{print "./" .Name}}/">{{.Name}}/</A></LI>
716 <LI>{{.Size | printf "%15d " | nbsp}}<A href="{{print "./" .Name}}">{{.Name}}</A></LI>
721 <P>(No files; this collection is empty.)</P>
728 Arvados is a free and open source software bioinformatics platform.
729 To learn more, visit arvados.org.
730 Arvados is not responsible for the files listed on this page.
737 type fileListEnt struct {
743 func (h *handler) serveDirectory(w http.ResponseWriter, r *http.Request, collectionName string, fs http.FileSystem, base string, recurse bool) {
744 var files []fileListEnt
745 var walk func(string) error
746 if !strings.HasSuffix(base, "/") {
749 walk = func(path string) error {
750 dirname := base + path
752 dirname = strings.TrimSuffix(dirname, "/")
754 d, err := fs.Open(dirname)
758 ents, err := d.Readdir(-1)
762 for _, ent := range ents {
763 if recurse && ent.IsDir() {
764 err = walk(path + ent.Name() + "/")
769 files = append(files, fileListEnt{
770 Name: path + ent.Name(),
778 if err := walk(""); err != nil {
779 http.Error(w, "error getting directory listing: "+err.Error(), http.StatusInternalServerError)
783 funcs := template.FuncMap{
784 "nbsp": func(s string) template.HTML {
785 return template.HTML(strings.Replace(s, " ", " ", -1))
788 tmpl, err := template.New("dir").Funcs(funcs).Parse(dirListingTemplate)
790 http.Error(w, "error parsing template: "+err.Error(), http.StatusInternalServerError)
793 sort.Slice(files, func(i, j int) bool {
794 return files[i].Name < files[j].Name
796 w.WriteHeader(http.StatusOK)
797 tmpl.Execute(w, map[string]interface{}{
798 "CollectionName": collectionName,
801 "StripParts": strings.Count(strings.TrimRight(r.URL.Path, "/"), "/"),
805 func applyContentDispositionHdr(w http.ResponseWriter, r *http.Request, filename string, isAttachment bool) {
806 disposition := "inline"
808 disposition = "attachment"
810 if strings.ContainsRune(r.RequestURI, '?') {
811 // Help the UA realize that the filename is just
812 // "filename.txt", not
813 // "filename.txt?disposition=attachment".
815 // TODO(TC): Follow advice at RFC 6266 appendix D
816 disposition += "; filename=" + strconv.QuoteToASCII(filename)
818 if disposition != "inline" {
819 w.Header().Set("Content-Disposition", disposition)
823 func (h *handler) seeOtherWithCookie(w http.ResponseWriter, r *http.Request, location string, credentialsOK bool) {
824 if formToken := r.FormValue("api_token"); formToken != "" {
826 // It is not safe to copy the provided token
827 // into a cookie unless the current vhost
828 // (origin) serves only a single collection or
829 // we are in TrustAllContent mode.
830 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)
834 // The HttpOnly flag is necessary to prevent
835 // JavaScript code (included in, or loaded by, a page
836 // in the collection being served) from employing the
837 // user's token beyond reading other files in the same
838 // domain, i.e., same collection.
840 // The 303 redirect is necessary in the case of a GET
841 // request to avoid exposing the token in the Location
842 // bar, and in the case of a POST request to avoid
843 // raising warnings when the user refreshes the
845 http.SetCookie(w, &http.Cookie{
846 Name: "arvados_api_token",
847 Value: auth.EncodeTokenCookie([]byte(formToken)),
850 SameSite: http.SameSiteLaxMode,
854 // Propagate query parameters (except api_token) from
855 // the original request.
856 redirQuery := r.URL.Query()
857 redirQuery.Del("api_token")
861 newu, err := u.Parse(location)
863 http.Error(w, "error resolving redirect target: "+err.Error(), http.StatusInternalServerError)
869 Scheme: r.URL.Scheme,
872 RawQuery: redirQuery.Encode(),
875 w.Header().Add("Location", redir)
876 w.WriteHeader(http.StatusSeeOther)
877 io.WriteString(w, `<A href="`)
878 io.WriteString(w, html.EscapeString(redir))
879 io.WriteString(w, `">Continue</A>`)
882 func (h *handler) userPermittedToUploadOrDownload(method string, tokenUser *arvados.User) bool {
883 var permitDownload bool
884 var permitUpload bool
885 if tokenUser != nil && tokenUser.IsAdmin {
886 permitUpload = h.Cluster.Collections.WebDAVPermission.Admin.Upload
887 permitDownload = h.Cluster.Collections.WebDAVPermission.Admin.Download
889 permitUpload = h.Cluster.Collections.WebDAVPermission.User.Upload
890 permitDownload = h.Cluster.Collections.WebDAVPermission.User.Download
892 if (method == "PUT" || method == "POST") && !permitUpload {
893 // Disallow operations that upload new files.
894 // Permit webdav operations that move existing files around.
896 } else if method == "GET" && !permitDownload {
897 // Disallow downloading file contents.
898 // Permit webdav operations like PROPFIND that retrieve metadata
899 // but not file contents.
905 func (h *handler) logUploadOrDownload(
907 client *arvadosclient.ArvadosClient,
908 fs arvados.CustomFileSystem,
910 collection *arvados.Collection,
911 user *arvados.User) {
913 log := ctxlog.FromContext(r.Context())
914 props := make(map[string]string)
915 props["reqPath"] = r.URL.Path
918 log = log.WithField("user_uuid", user.UUID).
919 WithField("user_full_name", user.FullName)
922 useruuid = fmt.Sprintf("%s-tpzed-anonymouspublic", h.Cluster.ClusterID)
924 if collection == nil && fs != nil {
925 collection, filepath = h.determineCollection(fs, filepath)
927 if collection != nil {
928 log = log.WithField("collection_file_path", filepath)
929 props["collection_file_path"] = filepath
930 // h.determineCollection populates the collection_uuid
931 // prop with the PDH, if this collection is being
932 // accessed via PDH. For logging, we use a different
933 // field depending on whether it's a UUID or PDH.
934 if len(collection.UUID) > 32 {
935 log = log.WithField("portable_data_hash", collection.UUID)
936 props["portable_data_hash"] = collection.UUID
938 log = log.WithField("collection_uuid", collection.UUID)
939 props["collection_uuid"] = collection.UUID
942 if r.Method == "PUT" || r.Method == "POST" {
943 log.Info("File upload")
944 if h.Cluster.Collections.WebDAVLogEvents {
946 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
947 "object_uuid": useruuid,
948 "event_type": "file_upload",
949 "properties": props}}
950 err := client.Create("logs", lr, nil)
952 log.WithError(err).Error("Failed to create upload log event on API server")
956 } else if r.Method == "GET" {
957 if collection != nil && collection.PortableDataHash != "" {
958 log = log.WithField("portable_data_hash", collection.PortableDataHash)
959 props["portable_data_hash"] = collection.PortableDataHash
961 log.Info("File download")
962 if h.Cluster.Collections.WebDAVLogEvents {
964 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
965 "object_uuid": useruuid,
966 "event_type": "file_download",
967 "properties": props}}
968 err := client.Create("logs", lr, nil)
970 log.WithError(err).Error("Failed to create download log event on API server")
977 func (h *handler) determineCollection(fs arvados.CustomFileSystem, path string) (*arvados.Collection, string) {
978 target := strings.TrimSuffix(path, "/")
979 for cut := len(target); cut >= 0; cut = strings.LastIndexByte(target, '/') {
980 target = target[:cut]
981 fi, err := fs.Stat(target)
982 if os.IsNotExist(err) {
983 // creating a new file/dir, or download
986 } else if err != nil {
989 switch src := fi.Sys().(type) {
990 case *arvados.Collection:
991 return src, strings.TrimPrefix(path[len(target):], "/")
995 if _, ok := src.(error); ok {