15 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
16 "git.curoverse.com/arvados.git/sdk/go/auth"
17 "git.curoverse.com/arvados.git/sdk/go/httpserver"
18 "git.curoverse.com/arvados.git/sdk/go/keepclient"
24 clientPool = arvadosclient.MakeClientPool()
25 trustAllContent = false
26 anonymousTokens []string
27 attachmentOnlyHost = ""
31 flag.BoolVar(&trustAllContent, "trust-all-content", false,
32 "Serve non-public content from a single origin. Dangerous: read docs before using!")
33 flag.StringVar(&attachmentOnlyHost, "attachment-only-host", "",
34 "Accept credentials, and add \"Content-Disposition: attachment\" response headers, for requests at this hostname:port. Prohibiting inline display makes it possible to serve untrusted and non-public content from a single origin, i.e., without wildcard DNS or SSL.")
37 // return a UUID or PDH if s begins with a UUID or URL-encoded PDH;
38 // otherwise return "".
39 func parseCollectionIdFromDNSName(s string) string {
41 if i := strings.IndexRune(s, '.'); i >= 0 {
44 // Names like {uuid}--dl.example.com serve the same purpose as
45 // {uuid}.dl.example.com but can reduce cost/effort of using
46 // [additional] wildcard certificates.
47 if i := strings.Index(s, "--"); i >= 0 {
50 if arvadosclient.UUIDMatch(s) {
53 if pdh := strings.Replace(s, "-", "+", 1); arvadosclient.PDHMatch(pdh) {
59 var urlPDHDecoder = strings.NewReplacer(" ", "+", "-", "+")
61 // return a UUID or PDH if s is a UUID or a PDH (even if it is a PDH
62 // with "+" replaced by " " or "-"); otherwise return "".
63 func parseCollectionIdFromURL(s string) string {
64 if arvadosclient.UUIDMatch(s) {
67 if pdh := urlPDHDecoder.Replace(s); arvadosclient.PDHMatch(pdh) {
73 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
77 remoteAddr := r.RemoteAddr
78 if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
79 remoteAddr = xff + "," + remoteAddr
82 w := httpserver.WrapResponseWriter(wOrig)
85 statusCode = w.WroteStatus()
86 } else if w.WroteStatus() == 0 {
87 w.WriteHeader(statusCode)
88 } else if w.WroteStatus() != statusCode {
89 httpserver.Log(r.RemoteAddr, "WARNING",
90 fmt.Sprintf("Our status changed from %d to %d after we sent headers", w.WroteStatus(), statusCode))
93 statusText = http.StatusText(statusCode)
95 httpserver.Log(remoteAddr, statusCode, statusText, w.WroteBodyBytes(), r.Method, r.Host, r.URL.Path, r.URL.RawQuery)
98 if r.Method != "GET" && r.Method != "POST" {
99 statusCode, statusText = http.StatusMethodNotAllowed, r.Method
103 arv := clientPool.Get()
105 statusCode, statusText = http.StatusInternalServerError, "Pool failed: "+clientPool.Err().Error()
108 defer clientPool.Put(arv)
110 pathParts := strings.Split(r.URL.Path[1:], "/")
113 var targetPath []string
115 var reqTokens []string
118 credentialsOK := trustAllContent
120 if r.Host != "" && r.Host == attachmentOnlyHost {
123 } else if r.FormValue("disposition") == "attachment" {
127 if targetId = parseCollectionIdFromDNSName(r.Host); targetId != "" {
128 // http://ID.dl.example/PATH...
130 targetPath = pathParts
131 } else if len(pathParts) >= 2 && strings.HasPrefix(pathParts[0], "c=") {
133 targetId = parseCollectionIdFromURL(pathParts[0][2:])
134 targetPath = pathParts[1:]
135 } else if len(pathParts) >= 3 && pathParts[0] == "collections" {
136 if len(pathParts) >= 5 && pathParts[1] == "download" {
137 // /collections/download/ID/TOKEN/PATH...
138 targetId = pathParts[2]
139 tokens = []string{pathParts[3]}
140 targetPath = pathParts[4:]
143 // /collections/ID/PATH...
144 targetId = pathParts[1]
145 tokens = anonymousTokens
146 targetPath = pathParts[2:]
149 statusCode = http.StatusNotFound
152 if t := r.FormValue("api_token"); t != "" {
153 // The client provided an explicit token in the query
154 // string, or a form in POST body. We must put the
155 // token in an HttpOnly cookie, and redirect to the
156 // same URL with the query param redacted and method =
160 // It is not safe to copy the provided token
161 // into a cookie unless the current vhost
162 // (origin) serves only a single collection or
163 // we are in trustAllContent mode.
164 statusCode = http.StatusBadRequest
168 // The HttpOnly flag is necessary to prevent
169 // JavaScript code (included in, or loaded by, a page
170 // in the collection being served) from employing the
171 // user's token beyond reading other files in the same
172 // domain, i.e., same collection.
174 // The 303 redirect is necessary in the case of a GET
175 // request to avoid exposing the token in the Location
176 // bar, and in the case of a POST request to avoid
177 // raising warnings when the user refreshes the
180 http.SetCookie(w, &http.Cookie{
182 Value: auth.EncodeTokenCookie([]byte(t)),
184 Expires: time.Now().AddDate(10, 0, 0),
187 redir := (&url.URL{Host: r.Host, Path: r.URL.Path}).String()
189 w.Header().Add("Location", redir)
190 statusCode, statusText = http.StatusSeeOther, redir
191 w.WriteHeader(statusCode)
192 io.WriteString(w, `<A href="`)
193 io.WriteString(w, html.EscapeString(redir))
194 io.WriteString(w, `">Continue</A>`)
198 if tokens == nil && strings.HasPrefix(targetPath[0], "t=") {
199 // http://ID.example/t=TOKEN/PATH...
200 // /c=ID/t=TOKEN/PATH...
202 // This form must only be used to pass scoped tokens
203 // that give permission for a single collection. See
204 // FormValue case above.
205 tokens = []string{targetPath[0][2:]}
207 targetPath = targetPath[1:]
212 reqTokens = auth.NewCredentialsFromHTTPRequest(r).Tokens
214 tokens = append(reqTokens, anonymousTokens...)
217 if len(targetPath) > 0 && targetPath[0] == "_" {
218 // If a collection has a directory called "t=foo" or
219 // "_", it can be served at //dl.example/_/t=foo/ or
220 // //dl.example/_/_/ respectively: //dl.example/t=foo/
221 // won't work because t=foo will be interpreted as a
223 targetPath = targetPath[1:]
226 tokenResult := make(map[string]int)
227 collection := make(map[string]interface{})
229 for _, arv.ApiToken = range tokens {
230 err := arv.Get("collections", targetId, nil, &collection)
236 if srvErr, ok := err.(arvadosclient.APIServerError); ok {
237 switch srvErr.HttpStatusCode {
239 // Token broken or insufficient to
240 // retrieve collection
241 tokenResult[arv.ApiToken] = srvErr.HttpStatusCode
245 // Something more serious is wrong
246 statusCode, statusText = http.StatusInternalServerError, err.Error()
250 if pathToken || !credentialsOK {
251 // Either the URL is a "secret sharing link"
252 // that didn't work out (and asking the client
253 // for additional credentials would just be
254 // confusing), or we don't even accept
255 // credentials at this path.
256 statusCode = http.StatusNotFound
259 for _, t := range reqTokens {
260 if tokenResult[t] == 404 {
261 // The client provided valid token(s), but the
262 // collection was not found.
263 statusCode = http.StatusNotFound
267 // The client's token was invalid (e.g., expired), or
268 // the client didn't even provide one. Propagate the
269 // 401 to encourage the client to use a [different]
272 // TODO(TC): This response would be confusing to
273 // someone trying (anonymously) to download public
274 // data that has been deleted. Allow a referrer to
275 // provide this context somehow?
276 w.Header().Add("WWW-Authenticate", "Basic realm=\"dl\"")
277 statusCode = http.StatusUnauthorized
281 filename := strings.Join(targetPath, "/")
282 kc, err := keepclient.MakeKeepClient(arv)
284 statusCode, statusText = http.StatusInternalServerError, err.Error()
287 rdr, err := kc.CollectionFileReader(collection, filename)
288 if os.IsNotExist(err) {
289 statusCode = http.StatusNotFound
291 } else if err != nil {
292 statusCode, statusText = http.StatusBadGateway, err.Error()
297 // One or both of these can be -1 if not found:
298 basenamePos := strings.LastIndex(filename, "/")
299 extPos := strings.LastIndex(filename, ".")
300 if extPos > basenamePos {
301 // Now extPos is safely >= 0.
302 if t := mime.TypeByExtension(filename[extPos:]); t != "" {
303 w.Header().Set("Content-Type", t)
306 w.Header().Set("Content-Length", fmt.Sprintf("%d", rdr.Len()))
308 w.Header().Set("Content-Disposition", "attachment")
311 w.WriteHeader(http.StatusOK)
312 _, err = io.Copy(w, rdr)
314 statusCode, statusText = http.StatusBadGateway, err.Error()