2013-02-28 23:30:16 +00:00
|
|
|
/*
|
|
|
|
Copyright 2013 The Camlistore Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2013-09-19 07:13:23 +00:00
|
|
|
"strconv"
|
2013-02-28 23:30:16 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"camlistore.org/pkg/auth"
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-02-28 23:30:16 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/blobserver/gethandler"
|
|
|
|
"camlistore.org/pkg/httputil"
|
2017-03-24 00:45:34 +00:00
|
|
|
"camlistore.org/pkg/index"
|
2013-02-28 23:30:16 +00:00
|
|
|
"camlistore.org/pkg/schema"
|
2015-12-01 16:19:49 +00:00
|
|
|
"go4.org/jsonconfig"
|
2013-02-28 23:30:16 +00:00
|
|
|
)
|
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
type responseType int
|
|
|
|
|
|
|
|
const (
|
2015-12-28 21:53:29 +00:00
|
|
|
badRequest responseType = 400
|
|
|
|
unauthorizedRequest responseType = 401
|
2013-09-24 08:38:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type errorCode int
|
|
|
|
|
|
|
|
const (
|
2014-09-01 06:46:37 +00:00
|
|
|
noError errorCode = iota
|
|
|
|
assembleNonTransitive
|
2013-09-24 08:38:38 +00:00
|
|
|
invalidMethod
|
|
|
|
invalidURL
|
|
|
|
invalidVia
|
|
|
|
shareBlobInvalid
|
|
|
|
shareBlobTooLarge
|
|
|
|
shareExpired
|
2017-03-24 00:45:34 +00:00
|
|
|
shareDeleted
|
2013-09-24 08:38:38 +00:00
|
|
|
shareFetchFailed
|
|
|
|
shareReadFailed
|
|
|
|
shareTargetInvalid
|
|
|
|
shareNotTransitive
|
|
|
|
viaChainFetchFailed
|
|
|
|
viaChainInvalidLink
|
|
|
|
viaChainReadFailed
|
|
|
|
)
|
|
|
|
|
2015-12-28 21:53:29 +00:00
|
|
|
var errorCodeStr = [...]string{
|
|
|
|
noError: "noError",
|
|
|
|
assembleNonTransitive: "assembleNonTransitive",
|
|
|
|
invalidMethod: "invalidMethod",
|
|
|
|
invalidURL: "invalidURL",
|
|
|
|
invalidVia: "invalidVia",
|
|
|
|
shareBlobInvalid: "shareBlobInvalid",
|
|
|
|
shareBlobTooLarge: "shareBlobTooLarge",
|
|
|
|
shareExpired: "shareExpired",
|
2017-03-24 00:45:34 +00:00
|
|
|
shareDeleted: "shareDeleted",
|
2015-12-28 21:53:29 +00:00
|
|
|
shareFetchFailed: "shareFetchFailed",
|
|
|
|
shareReadFailed: "shareReadFailed",
|
|
|
|
shareTargetInvalid: "shareTargetInvalid",
|
|
|
|
shareNotTransitive: "shareNotTransitive",
|
|
|
|
viaChainFetchFailed: "viaChainFetchFailed",
|
|
|
|
viaChainInvalidLink: "viaChainInvalidLink",
|
|
|
|
viaChainReadFailed: "viaChainReadFailed",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec errorCode) String() string {
|
|
|
|
if ec < 0 || int(ec) >= len(errorCodeStr) || errorCodeStr[ec] == "" {
|
|
|
|
return fmt.Sprintf("ErrCode#%d", int(ec))
|
|
|
|
}
|
|
|
|
return errorCodeStr[ec]
|
|
|
|
}
|
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
type shareError struct {
|
|
|
|
code errorCode
|
|
|
|
response responseType
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *shareError) Error() string {
|
2015-12-28 21:53:29 +00:00
|
|
|
return fmt.Sprintf("share: %v (code=%v, type=%v)", e.message, e.code, e.response)
|
2013-09-24 08:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unauthorized(code errorCode, format string, args ...interface{}) *shareError {
|
|
|
|
return &shareError{
|
|
|
|
code: code, response: unauthorizedRequest, message: fmt.Sprintf(format, args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 23:30:16 +00:00
|
|
|
const fetchFailureDelay = 200 * time.Millisecond
|
|
|
|
|
|
|
|
// ShareHandler handles the requests for "share" (and shared) blobs.
|
|
|
|
type shareHandler struct {
|
2014-03-14 19:11:08 +00:00
|
|
|
fetcher blob.Fetcher
|
2017-03-24 00:45:34 +00:00
|
|
|
idx *index.Index // for knowledge about share claim deletions
|
2015-12-28 21:53:29 +00:00
|
|
|
log bool
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterHandlerConstructor("share", newShareFromConfig)
|
|
|
|
}
|
|
|
|
|
2015-12-28 21:53:29 +00:00
|
|
|
func newShareFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (http.Handler, error) {
|
2013-02-28 23:30:16 +00:00
|
|
|
blobRoot := conf.RequiredString("blobRoot")
|
|
|
|
if blobRoot == "" {
|
|
|
|
return nil, errors.New("No blobRoot defined for share handler")
|
|
|
|
}
|
2017-03-24 00:45:34 +00:00
|
|
|
indexPrefix := conf.RequiredString("index")
|
2015-12-28 21:53:29 +00:00
|
|
|
if err := conf.Validate(); err != nil {
|
|
|
|
return nil, err
|
2013-09-19 07:13:23 +00:00
|
|
|
}
|
2013-02-28 23:30:16 +00:00
|
|
|
|
2013-09-21 08:54:07 +00:00
|
|
|
bs, err := ld.GetStorage(blobRoot)
|
2013-02-28 23:30:16 +00:00
|
|
|
if err != nil {
|
2015-12-28 21:53:29 +00:00
|
|
|
return nil, fmt.Errorf("failed to get share handler's storage at %q: %v", blobRoot, err)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2014-03-14 19:11:08 +00:00
|
|
|
fetcher, ok := bs.(blob.Fetcher)
|
2013-02-28 23:30:16 +00:00
|
|
|
if !ok {
|
2017-12-10 09:13:00 +00:00
|
|
|
return nil, errors.New("share handler's storage not a Fetcher")
|
2015-12-28 21:53:29 +00:00
|
|
|
}
|
2017-03-24 00:45:34 +00:00
|
|
|
|
|
|
|
// Should we use the search handler instead (and add a method to access
|
|
|
|
// its index.IsDeleted method)? I think it's ok to use the index Handler
|
|
|
|
// directly, as long as we lock properly.
|
|
|
|
indexHandler, err := ld.GetHandler(indexPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("share handler config references unknown handler %q", indexPrefix)
|
|
|
|
}
|
|
|
|
indexer, ok := indexHandler.(*index.Index)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("share handler config references invalid indexer %q (actually a %T)", indexPrefix, indexHandler)
|
|
|
|
}
|
|
|
|
|
2015-12-28 21:53:29 +00:00
|
|
|
sh := &shareHandler{
|
|
|
|
fetcher: fetcher,
|
2017-03-24 00:45:34 +00:00
|
|
|
idx: indexer,
|
2015-12-28 21:53:29 +00:00
|
|
|
log: true,
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2015-12-28 21:53:29 +00:00
|
|
|
return sh, nil
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 21:53:29 +00:00
|
|
|
var timeSleep = time.Sleep // for tests
|
|
|
|
|
2013-02-28 23:30:16 +00:00
|
|
|
// Unauthenticated user. Be paranoid.
|
2017-03-24 00:45:34 +00:00
|
|
|
func (h *shareHandler) handleGetViaSharing(rw http.ResponseWriter, req *http.Request,
|
|
|
|
blobRef blob.Ref) error {
|
2013-09-15 19:12:26 +00:00
|
|
|
if !httputil.IsGet(req) {
|
2013-09-24 08:38:38 +00:00
|
|
|
return &shareError{code: invalidMethod, response: badRequest, message: "Invalid method"}
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 21:53:29 +00:00
|
|
|
rw.Header().Set("Access-Control-Allow-Origin", "*")
|
2014-09-01 06:46:37 +00:00
|
|
|
|
2013-02-28 23:30:16 +00:00
|
|
|
viaPathOkay := false
|
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
if !viaPathOkay {
|
|
|
|
// Insert a delay, to hide timing attacks probing
|
|
|
|
// for the existence of blobs.
|
|
|
|
sleep := fetchFailureDelay - (time.Now().Sub(startTime))
|
2015-12-28 21:53:29 +00:00
|
|
|
timeSleep(sleep)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
}()
|
2013-08-04 02:54:30 +00:00
|
|
|
viaBlobs := make([]blob.Ref, 0)
|
2013-02-28 23:30:16 +00:00
|
|
|
if via := req.FormValue("via"); via != "" {
|
|
|
|
for _, vs := range strings.Split(via, ",") {
|
2013-08-04 02:54:30 +00:00
|
|
|
if br, ok := blob.Parse(vs); ok {
|
|
|
|
viaBlobs = append(viaBlobs, br)
|
|
|
|
} else {
|
2013-09-24 08:38:38 +00:00
|
|
|
return &shareError{code: invalidVia, response: badRequest, message: "Malformed blobref in via param"}
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
fetchChain := make([]blob.Ref, 0)
|
2013-02-28 23:30:16 +00:00
|
|
|
fetchChain = append(fetchChain, viaBlobs...)
|
|
|
|
fetchChain = append(fetchChain, blobRef)
|
2013-09-21 16:51:13 +00:00
|
|
|
isTransitive := false
|
2013-02-28 23:30:16 +00:00
|
|
|
for i, br := range fetchChain {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
2017-03-24 00:45:34 +00:00
|
|
|
if h.idx != nil {
|
|
|
|
h.idx.RLock()
|
|
|
|
isDeleted := h.idx.IsDeleted(br)
|
|
|
|
h.idx.RUnlock()
|
|
|
|
if isDeleted {
|
|
|
|
return unauthorized(shareDeleted, "Share was deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file, size, err := h.fetcher.Fetch(br)
|
2013-02-28 23:30:16 +00:00
|
|
|
if err != nil {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(shareFetchFailed, "Fetch chain 0 of %s failed: %v", br, err)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if size > schema.MaxSchemaBlobSize {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(shareBlobTooLarge, "Fetch chain 0 of %s too large", br)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2013-03-08 16:59:26 +00:00
|
|
|
blob, err := schema.BlobFromReader(br, file)
|
|
|
|
if err != nil {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(shareReadFailed, "Can't create a blob from %v: %v", br, err)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2013-03-08 16:59:26 +00:00
|
|
|
share, ok := blob.AsShare()
|
|
|
|
if !ok {
|
2015-12-28 21:53:29 +00:00
|
|
|
return unauthorized(shareBlobInvalid, "Fetch chain 0 of %s wasn't a valid Share (is %q)", br, blob.Type())
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2013-09-09 00:46:40 +00:00
|
|
|
if share.IsExpired() {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(shareExpired, "Share is expired")
|
2013-09-09 00:46:40 +00:00
|
|
|
}
|
2013-03-08 16:59:26 +00:00
|
|
|
if len(fetchChain) > 1 && fetchChain[1].String() != share.Target().String() {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(shareTargetInvalid,
|
|
|
|
"Fetch chain 0->1 (%s -> %q) unauthorized, expected hop to %q",
|
|
|
|
br, fetchChain[1], share.Target())
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2013-09-21 16:51:13 +00:00
|
|
|
isTransitive = share.IsTransitive()
|
|
|
|
if len(fetchChain) > 2 && !isTransitive {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(shareNotTransitive, "Share is not transitive")
|
2013-09-21 08:54:07 +00:00
|
|
|
}
|
2013-02-28 23:30:16 +00:00
|
|
|
case len(fetchChain) - 1:
|
|
|
|
// Last one is fine (as long as its path up to here has been proven, and it's
|
|
|
|
// not the first thing in the chain)
|
|
|
|
continue
|
|
|
|
default:
|
2017-03-24 00:45:34 +00:00
|
|
|
rc, _, err := h.fetcher.Fetch(br)
|
2013-02-28 23:30:16 +00:00
|
|
|
if err != nil {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(viaChainFetchFailed, "Fetch chain %d of %s failed: %v", i, br, err)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2015-12-28 21:53:29 +00:00
|
|
|
defer rc.Close()
|
|
|
|
lr := io.LimitReader(rc, schema.MaxSchemaBlobSize)
|
2013-02-28 23:30:16 +00:00
|
|
|
slurpBytes, err := ioutil.ReadAll(lr)
|
|
|
|
if err != nil {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(viaChainReadFailed,
|
|
|
|
"Fetch chain %d of %s failed in slurp: %v", i, br, err)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2017-09-10 13:28:34 +00:00
|
|
|
sought := fetchChain[i+1]
|
|
|
|
if !bytesHaveSchemaLink(br, slurpBytes, sought) {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(viaChainInvalidLink,
|
2017-09-10 13:28:34 +00:00
|
|
|
"Fetch chain %d of %s failed; no reference to %s", i, br, sought)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 07:13:23 +00:00
|
|
|
if assemble, _ := strconv.ParseBool(req.FormValue("assemble")); assemble {
|
2013-09-21 16:51:13 +00:00
|
|
|
if !isTransitive {
|
2013-09-24 08:38:38 +00:00
|
|
|
return unauthorized(assembleNonTransitive, "Cannot assemble non-transitive share")
|
2013-09-21 16:51:13 +00:00
|
|
|
}
|
2013-09-19 07:13:23 +00:00
|
|
|
dh := &DownloadHandler{
|
2017-03-24 00:45:34 +00:00
|
|
|
Fetcher: h.fetcher,
|
2013-09-19 07:13:23 +00:00
|
|
|
// TODO(aa): It would be nice to specify a local cache here, as the UI handler does.
|
|
|
|
}
|
pkg/server: add files "zipper" to DownloadHandler
We want to add a feature for clients (the web UI), where they can select
a bunch of files and ask the server for a zip archive of all these files.
This CL modifies the DownloadHandler so it does exactly that upon
reception of a POST request with a query parameter of the form
files=sha1-foo,sha1-bar,sha1-baz
This CL also adds a new button to the contextual sidebar of the web UI,
that takes care of sending the download request to the server.
known limitations: only permanodes with file as camliContent are
accepted as a valid selection (i.e. no sets, or static-dirs, etc) for
now.
Implementation detail:
We're creating an ephemeral DOM form on the fly to send the request.
The reason is: if we sent it as a Go http request, we'd have to read
the response manually and then we'd have no way of writing it to disk.
If we did it with an xhr, we could write the response to disk by
creating a File or Blob and then using URL.createObjectURL(), but we'd
have to keep the response in memory while doing so, which is
unacceptable for large enough archives.
Fixes #899
Change-Id: I104f7c5bd10ab3369e28d33752380dd12b5b3e6b
2017-03-06 20:02:19 +00:00
|
|
|
dh.ServeFile(rw, req, blobRef)
|
2013-09-19 07:13:23 +00:00
|
|
|
} else {
|
2017-03-24 00:45:34 +00:00
|
|
|
gethandler.ServeBlobRef(rw, req, blobRef, h.fetcher)
|
2013-09-19 07:13:23 +00:00
|
|
|
}
|
2013-09-21 16:51:13 +00:00
|
|
|
viaPathOkay = true
|
2013-09-24 08:38:38 +00:00
|
|
|
return nil
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 08:38:38 +00:00
|
|
|
func (h *shareHandler) serveHTTP(rw http.ResponseWriter, req *http.Request) error {
|
|
|
|
var err error
|
2013-09-19 07:13:23 +00:00
|
|
|
pathSuffix := httputil.PathSuffix(req)
|
2013-09-24 08:38:38 +00:00
|
|
|
if len(pathSuffix) == 0 {
|
|
|
|
// This happens during testing because we don't go through PrefixHandler
|
|
|
|
pathSuffix = strings.TrimLeft(req.URL.Path, "/")
|
|
|
|
}
|
2013-09-19 07:13:23 +00:00
|
|
|
pathParts := strings.SplitN(pathSuffix, "/", 2)
|
|
|
|
blobRef, ok := blob.Parse(pathParts[0])
|
2013-08-04 02:54:30 +00:00
|
|
|
if !ok {
|
2013-09-24 08:38:38 +00:00
|
|
|
err = &shareError{code: invalidURL, response: badRequest,
|
|
|
|
message: fmt.Sprintf("Malformed share pathSuffix: %s", pathSuffix)}
|
|
|
|
} else {
|
2017-03-24 00:45:34 +00:00
|
|
|
err = h.handleGetViaSharing(rw, req, blobRef)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2013-09-24 08:38:38 +00:00
|
|
|
if se, ok := err.(*shareError); ok {
|
|
|
|
switch se.response {
|
|
|
|
case badRequest:
|
|
|
|
httputil.BadRequestError(rw, err.Error())
|
|
|
|
case unauthorizedRequest:
|
2015-12-28 21:53:29 +00:00
|
|
|
if h.log {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2013-09-24 08:38:38 +00:00
|
|
|
auth.SendUnauthorized(rw, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *shareHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
h.serveHTTP(rw, req)
|
2013-02-28 23:30:16 +00:00
|
|
|
}
|
2015-12-28 21:53:29 +00:00
|
|
|
|
|
|
|
// bytesHaveSchemaLink reports whether bb is a valid Camlistore schema
|
|
|
|
// blob and has target somewhere in a schema field used to represent a
|
|
|
|
// Merkle-tree-ish file or directory.
|
|
|
|
func bytesHaveSchemaLink(br blob.Ref, bb []byte, target blob.Ref) bool {
|
|
|
|
// Fast path for no:
|
|
|
|
if !bytes.Contains(bb, []byte(target.String())) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
b, err := schema.BlobFromReader(br, bytes.NewReader(bb))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
typ := b.Type()
|
|
|
|
switch typ {
|
2016-05-11 21:58:15 +00:00
|
|
|
case "file", "bytes":
|
2015-12-28 21:53:29 +00:00
|
|
|
for _, bp := range b.ByteParts() {
|
|
|
|
if bp.BlobRef.Valid() {
|
2016-05-11 21:58:15 +00:00
|
|
|
if bp.BlobRef == target {
|
|
|
|
return true
|
|
|
|
}
|
2015-12-28 21:53:29 +00:00
|
|
|
}
|
|
|
|
if bp.BytesRef.Valid() {
|
2016-05-11 21:58:15 +00:00
|
|
|
if bp.BytesRef == target {
|
|
|
|
return true
|
|
|
|
}
|
2015-12-28 21:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "directory":
|
|
|
|
if d, ok := b.DirectoryEntries(); ok {
|
|
|
|
return d == target
|
|
|
|
}
|
|
|
|
case "static-set":
|
|
|
|
for _, m := range b.StaticSetMembers() {
|
|
|
|
if m == target {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|