diff --git a/protogen/gen/opencloud/messages/policies/v0/policies.pb.go b/protogen/gen/opencloud/messages/policies/v0/policies.pb.go index 2d95ac1574..7854caec34 100644 --- a/protogen/gen/opencloud/messages/policies/v0/policies.pb.go +++ b/protogen/gen/opencloud/messages/policies/v0/policies.pb.go @@ -223,6 +223,10 @@ type Request struct { Method string `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"` Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"` + // Content-Length of the request body, unset if unknown (e.g. chunked transfer encoding) + ContentLength *uint64 `protobuf:"varint,3,opt,name=content_length,json=contentLength,proto3,oneof" json:"content_length,omitempty"` + // TUS Upload-Length header, the declared total size of an upload, unset if absent + UploadLength *uint64 `protobuf:"varint,4,opt,name=upload_length,json=uploadLength,proto3,oneof" json:"upload_length,omitempty"` } func (x *Request) Reset() { @@ -271,6 +275,20 @@ func (x *Request) GetPath() string { return "" } +func (x *Request) GetContentLength() uint64 { + if x != nil && x.ContentLength != nil { + return *x.ContentLength + } + return 0 +} + +func (x *Request) GetUploadLength() uint64 { + if x != nil && x.UploadLength != nil { + return *x.UploadLength + } + return 0 +} + type Environment struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -487,35 +505,43 @@ var file_opencloud_messages_policies_v0_policies_proto_rawDesc = []byte{ 0x75, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6f, 0x70, 0x61, 0x71, 0x75, 0x65, 0x49, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x73, 0x70, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x70, 0x61, 0x63, 0x65, 0x49, 0x64, - 0x22, 0x35, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6d, - 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74, - 0x68, 0x6f, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x22, 0x8d, 0x02, 0x0a, 0x0b, 0x45, 0x6e, 0x76, 0x69, - 0x72, 0x6f, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x3b, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x67, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x25, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, - 0x75, 0x64, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x6f, 0x6c, 0x69, - 0x63, 0x69, 0x65, 0x73, 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x74, 0x61, 0x67, 0x65, 0x52, 0x05, 0x73, - 0x74, 0x61, 0x67, 0x65, 0x12, 0x38, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x6d, + 0x22, 0xb0, 0x01, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, + 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, + 0x74, 0x68, 0x6f, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x2a, 0x0a, 0x0e, 0x63, 0x6f, 0x6e, 0x74, + 0x65, 0x6e, 0x74, 0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, + 0x48, 0x00, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e, 0x67, 0x74, + 0x68, 0x88, 0x01, 0x01, 0x12, 0x28, 0x0a, 0x0d, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x6c, + 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x48, 0x01, 0x52, 0x0c, 0x75, + 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x88, 0x01, 0x01, 0x42, 0x11, + 0x0a, 0x0f, 0x5f, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, + 0x68, 0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x6c, 0x65, 0x6e, + 0x67, 0x74, 0x68, 0x22, 0x8d, 0x02, 0x0a, 0x0b, 0x45, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, + 0x65, 0x6e, 0x74, 0x12, 0x3b, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0e, 0x32, 0x25, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, 0x73, - 0x2e, 0x76, 0x30, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, 0x41, - 0x0a, 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x27, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x6d, 0x65, 0x73, 0x73, + 0x2e, 0x76, 0x30, 0x2e, 0x53, 0x74, 0x61, 0x67, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x67, 0x65, + 0x12, 0x38, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, + 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x73, 0x2e, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, 0x73, 0x2e, 0x76, 0x30, 0x2e, + 0x55, 0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, 0x41, 0x0a, 0x07, 0x72, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x27, 0x2e, 0x6f, 0x70, + 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, + 0x2e, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, 0x73, 0x2e, 0x76, 0x30, 0x2e, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x52, 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x44, 0x0a, + 0x08, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x28, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, 0x73, 0x2e, 0x76, 0x30, - 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x52, 0x07, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x12, 0x44, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x04, 0x20, - 0x01, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2e, - 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2e, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, - 0x73, 0x2e, 0x76, 0x30, 0x2e, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x08, 0x72, - 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x2a, 0x25, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x67, 0x65, - 0x12, 0x0c, 0x0a, 0x08, 0x53, 0x54, 0x41, 0x47, 0x45, 0x5f, 0x50, 0x50, 0x10, 0x00, 0x12, 0x0e, - 0x0a, 0x0a, 0x53, 0x54, 0x41, 0x47, 0x45, 0x5f, 0x48, 0x54, 0x54, 0x50, 0x10, 0x01, 0x42, 0x4f, - 0x5a, 0x4d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6f, 0x70, 0x65, - 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, - 0x6f, 0x75, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, 0x65, 0x6e, 0x2f, 0x67, 0x65, 0x6e, - 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, - 0x67, 0x65, 0x73, 0x2f, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, 0x73, 0x2f, 0x76, 0x30, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x2e, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x08, 0x72, 0x65, 0x73, 0x6f, 0x75, + 0x72, 0x63, 0x65, 0x2a, 0x25, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x67, 0x65, 0x12, 0x0c, 0x0a, 0x08, + 0x53, 0x54, 0x41, 0x47, 0x45, 0x5f, 0x50, 0x50, 0x10, 0x00, 0x12, 0x0e, 0x0a, 0x0a, 0x53, 0x54, + 0x41, 0x47, 0x45, 0x5f, 0x48, 0x54, 0x54, 0x50, 0x10, 0x01, 0x42, 0x4f, 0x5a, 0x4d, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, + 0x75, 0x64, 0x2d, 0x65, 0x75, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, 0x65, 0x6e, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x6f, 0x70, 0x65, + 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x2f, + 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x69, 0x65, 0x73, 0x2f, 0x76, 0x30, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( @@ -634,6 +660,7 @@ func file_opencloud_messages_policies_v0_policies_proto_init() { } } } + file_opencloud_messages_policies_v0_policies_proto_msgTypes[2].OneofWrappers = []interface{}{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/protogen/gen/opencloud/services/policies/v0/policies.swagger.json b/protogen/gen/opencloud/services/policies/v0/policies.swagger.json index 7b514d5346..aec42d4cc2 100644 --- a/protogen/gen/opencloud/services/policies/v0/policies.swagger.json +++ b/protogen/gen/opencloud/services/policies/v0/policies.swagger.json @@ -134,6 +134,16 @@ }, "path": { "type": "string" + }, + "contentLength": { + "type": "string", + "format": "uint64", + "title": "Content-Length of the request body, unset if unknown (e.g. chunked transfer encoding)" + }, + "uploadLength": { + "type": "string", + "format": "uint64", + "title": "TUS Upload-Length header, the declared total size of an upload, unset if absent" } } }, diff --git a/protogen/proto/opencloud/messages/policies/v0/policies.proto b/protogen/proto/opencloud/messages/policies/v0/policies.proto index 2d49a02d3c..3e30b2cac4 100644 --- a/protogen/proto/opencloud/messages/policies/v0/policies.proto +++ b/protogen/proto/opencloud/messages/policies/v0/policies.proto @@ -32,6 +32,10 @@ message Resource { message Request { string method = 1; string path = 2; + // Content-Length of the request body, unset if unknown (e.g. chunked transfer encoding) + optional uint64 content_length = 3; + // TUS Upload-Length header, the declared total size of an upload, unset if absent + optional uint64 upload_length = 4; } enum Stage { diff --git a/services/policies/README.md b/services/policies/README.md index 8d210022f9..05fd46f007 100644 --- a/services/policies/README.md +++ b/services/policies/README.md @@ -185,6 +185,43 @@ To identify available keys for OPA, you need to look at [`engine.go`](https://gi Note that which keys are available depends on the module it is used in. +### Request Lengths + +In the proxy module, `input.request.content_length` contains the `Content-Length` of the request body and `input.request.upload_length` the total size of an upload as declared by the TUS `Upload-Length` header. They allow rejecting uploads that are too big before any data is transferred: + +```rego +package proxy + +import future.keywords.if +import future.keywords.in + +default granted := true + +# TUS creation declares the total size of the upload +granted = false if { + input.request.upload_length > 10737418240 +} + +# a plain PUT carries the whole file, a PATCH a chunk +granted = false if { + input.request.method in ["PUT", "PATCH"] + input.request.content_length > 10737418240 +} +``` + +A key is missing from the input if the length is unknown, `content_length` for example when the body is sent with chunked transfer encoding and `upload_length` when the request has no `Upload-Length` header. A body that is known to be empty has a `content_length` of `0`. Rules that compare a missing key do not apply, so the rules above let requests with an unknown length pass. To reject a PUT that does not declare its length: + +```rego +granted = false if { + input.request.method == "PUT" + not "content_length" in object.keys(input.request) +} +``` + +Some WebDAV clients stream uploads with chunked transfer encoding, so such a rule rejects their uploads. A PATCH does not need it, the TUS server does not accept more data than the declared `Upload-Length`. + +This is only an early check based on what the client declares. To check the actual size of the file that was written, use `input.resource.size` in postprocessing. + ## Rego Extensions Besides the standard rego built-in functions, the following functions are added on top: diff --git a/services/policies/pkg/engine/engine.go b/services/policies/pkg/engine/engine.go index c40a81ac97..20b24d98ce 100644 --- a/services/policies/pkg/engine/engine.go +++ b/services/policies/pkg/engine/engine.go @@ -40,6 +40,11 @@ type Resource struct { type Request struct { Method string `json:"method"` Path string `json:"path"` + // ContentLength is the length of the request body, nil if unknown (e.g. chunked transfer encoding). + // It is left out of the policy input then, so policies can tell an unknown length from an empty body. + ContentLength *uint64 `json:"content_length,omitempty"` + // UploadLength is the total size of an upload as declared by the TUS Upload-Length header, nil if absent. + UploadLength *uint64 `json:"upload_length,omitempty"` } // Environment contains every data that is needed to decide if the request should pass or not @@ -63,6 +68,12 @@ func NewEnvironmentFromPB(pEnv *v0.Environment) (Environment, error) { return env, err } + // protojson encodes uint64 as strings with camelCase keys, so copy the lengths explicitly + if pReq := pEnv.GetRequest(); pReq != nil { + env.Request.ContentLength = pReq.ContentLength + env.Request.UploadLength = pReq.UploadLength + } + switch pEnv.Stage { case v0.Stage_STAGE_HTTP: env.Stage = StageHTTP diff --git a/services/policies/pkg/engine/engine_test.go b/services/policies/pkg/engine/engine_test.go index ea3095d418..60a15a1fa0 100644 --- a/services/policies/pkg/engine/engine_test.go +++ b/services/policies/pkg/engine/engine_test.go @@ -5,6 +5,7 @@ import ( . "github.com/onsi/gomega" pMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/policies/v0" "github.com/opencloud-eu/opencloud/services/policies/pkg/engine" + "google.golang.org/protobuf/proto" ) var _ = Describe("Engine", func() { @@ -22,4 +23,39 @@ var _ = Describe("Engine", func() { Entry("http stage", pMessage.Stage_STAGE_HTTP, engine.StageHTTP), Entry("pp stage", pMessage.Stage_STAGE_PP, engine.StagePP), ) + + It("NewEnvironmentFromPB keeps the request lengths", func() { + pEnv := &pMessage.Environment{ + Stage: pMessage.Stage_STAGE_HTTP, + Request: &pMessage.Request{ + Method: "POST", + Path: "/remote.php/dav/spaces/some-space-id", + ContentLength: proto.Uint64(1024), + UploadLength: proto.Uint64(20000000000), + }, + } + + env, err := engine.NewEnvironmentFromPB(pEnv) + Expect(err).ToNot(HaveOccurred()) + + Expect(env.Request.Method).To(Equal("POST")) + Expect(env.Request.ContentLength).To(HaveValue(Equal(uint64(1024)))) + Expect(env.Request.UploadLength).To(HaveValue(Equal(uint64(20000000000)))) + }) + + It("NewEnvironmentFromPB keeps unknown request lengths unset", func() { + pEnv := &pMessage.Environment{ + Stage: pMessage.Stage_STAGE_HTTP, + Request: &pMessage.Request{ + Method: "PUT", + ContentLength: proto.Uint64(0), + }, + } + + env, err := engine.NewEnvironmentFromPB(pEnv) + Expect(err).ToNot(HaveOccurred()) + + Expect(env.Request.ContentLength).To(HaveValue(BeZero())) + Expect(env.Request.UploadLength).To(BeNil()) + }) }) diff --git a/services/policies/pkg/engine/opa/engine_test.go b/services/policies/pkg/engine/opa/engine_test.go index c7e33d330e..94e0691561 100644 --- a/services/policies/pkg/engine/opa/engine_test.go +++ b/services/policies/pkg/engine/opa/engine_test.go @@ -19,6 +19,7 @@ import ( "github.com/opencloud-eu/opencloud/services/policies/pkg/config" "github.com/opencloud-eu/opencloud/services/policies/pkg/engine" "github.com/opencloud-eu/opencloud/services/policies/pkg/engine/opa" + "google.golang.org/protobuf/proto" ) var _ = Describe("engine", func() { @@ -150,5 +151,51 @@ var _ = Describe("engine", func() { Expect(granted).To(Equal(tc.want), "for %s", tc.name) } }) + + It("compares the request lengths as numbers and detects unknown lengths", func() { + e, _ := newEngine(`package size + +import future.keywords.if +import future.keywords.in + +default granted := true + +granted = false if { + input.request.upload_length > 10737418240 +} + +granted = false if { + input.request.method in ["PUT", "PATCH"] + input.request.content_length > 10737418240 +} + +granted = false if { + input.request.method == "PUT" + not "content_length" in object.keys(input.request) +} +`) + + for _, tc := range []struct { + name string + request engine.Request + want bool + }{ + {"no lengths", engine.Request{Method: "POST"}, true}, + {"tus creation below limit", engine.Request{Method: "POST", ContentLength: proto.Uint64(0), UploadLength: proto.Uint64(10737418240)}, true}, + {"tus creation above limit", engine.Request{Method: "POST", ContentLength: proto.Uint64(0), UploadLength: proto.Uint64(20000000000)}, false}, + {"put above limit", engine.Request{Method: "PUT", ContentLength: proto.Uint64(20000000000)}, false}, + {"empty put", engine.Request{Method: "PUT", ContentLength: proto.Uint64(0)}, true}, + {"put with unknown length", engine.Request{Method: "PUT"}, false}, + {"patch above limit", engine.Request{Method: "PATCH", ContentLength: proto.Uint64(20000000000)}, false}, + {"patch below limit", engine.Request{Method: "PATCH", ContentLength: proto.Uint64(104857600)}, true}, + {"patch with unknown length", engine.Request{Method: "PATCH"}, true}, + } { + granted, err := e.Evaluate(context.Background(), "data.size.granted", engine.Environment{ + Request: tc.request, + }) + Expect(err).ToNot(HaveOccurred()) + Expect(granted).To(Equal(tc.want), "for %s", tc.name) + } + }) }) }) diff --git a/services/proxy/pkg/middleware/policies.go b/services/proxy/pkg/middleware/policies.go index 919e8cdc96..c53fe2c570 100644 --- a/services/proxy/pkg/middleware/policies.go +++ b/services/proxy/pkg/middleware/policies.go @@ -5,6 +5,7 @@ import ( "net/http" "path" "path/filepath" + "strconv" "strings" "time" @@ -15,6 +16,7 @@ import ( tusd "github.com/tus/tusd/v2/pkg/handler" "go.opentelemetry.io/otel/trace" "google.golang.org/grpc/metadata" + "google.golang.org/protobuf/proto" revactx "github.com/opencloud-eu/reva/v3/pkg/ctx" "github.com/opencloud-eu/reva/v3/pkg/storagespace" @@ -72,6 +74,15 @@ func Policies(qs string, opts ...Option) func(next http.Handler) http.Handler { }, } + // the declared lengths allow policies to reject uploads before the data is transferred + // a length of -1 means unknown, e.g. chunked transfer encoding, so the field stays unset + if r.ContentLength >= 0 { + req.Environment.Request.ContentLength = proto.Uint64(uint64(r.ContentLength)) + } + if uploadLength, err := strconv.ParseUint(r.Header.Get(net.HeaderUploadLength), 10, 64); err == nil { + req.Environment.Request.UploadLength = proto.Uint64(uploadLength) + } + resource := &pMessage.Resource{} // tus diff --git a/services/proxy/pkg/middleware/policies_test.go b/services/proxy/pkg/middleware/policies_test.go index 3270b851f7..6f14dd5ad3 100644 --- a/services/proxy/pkg/middleware/policies_test.go +++ b/services/proxy/pkg/middleware/policies_test.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/http/httptest" + "strings" "testing" gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" @@ -19,6 +20,7 @@ import ( "github.com/stretchr/testify/mock" "go-micro.dev/v4/client" "google.golang.org/grpc" + "google.golang.org/protobuf/proto" pMessage "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/policies/v0" policiesPG "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/policies/v0" @@ -109,6 +111,58 @@ func TestPolicies_EvaluationEnvironment_Request(t *testing.T) { policiesMiddleware.ServeHTTP(responseRecorder, httptest.NewRequest(http.MethodDelete, "/whatever", nil)) } +func TestPolicies_EvaluationEnvironment_RequestLengths(t *testing.T) { + tests := []struct { + name string + method string + body string + chunked bool + uploadLength string + expectedContentLength *uint64 + expectedUploadLength *uint64 + }{ + {name: "tus creation", method: http.MethodPost, uploadLength: "20000000000", expectedContentLength: proto.Uint64(0), expectedUploadLength: proto.Uint64(20000000000)}, + {name: "put", method: http.MethodPut, body: "some content", expectedContentLength: proto.Uint64(12)}, + {name: "empty put", method: http.MethodPut, expectedContentLength: proto.Uint64(0)}, + {name: "chunked put", method: http.MethodPut, body: "some content", chunked: true}, + {name: "patch", method: http.MethodPatch, body: "a chunk", expectedContentLength: proto.Uint64(7)}, + {name: "invalid upload length", method: http.MethodPost, uploadLength: "-1", expectedContentLength: proto.Uint64(0)}, + {name: "no body", method: http.MethodGet, expectedContentLength: proto.Uint64(0)}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var g = NewWithT(t) + + policiesMiddleware, policiesProviderService, _ := prepare("any") + policiesProviderService.On("Evaluate", mock.Anything, mock.Anything, mock.Anything).Return( + func(_ context.Context, in *policiesPG.EvaluateRequest, _ ...client.CallOption) (*policiesPG.EvaluateResponse, error) { + g.Expect(in.Environment.Request.ContentLength).To(Equal(tt.expectedContentLength)) + g.Expect(in.Environment.Request.UploadLength).To(Equal(tt.expectedUploadLength)) + + return &policiesPG.EvaluateResponse{Result: false}, nil + }, + ).Once() + + var body io.Reader + if tt.body != "" { + body = strings.NewReader(tt.body) + } + request := httptest.NewRequest(tt.method, "/remote.php/dav/spaces/some-space-id/file.txt", body) + if tt.chunked { + request.ContentLength = -1 + request.TransferEncoding = []string{"chunked"} + } + if tt.uploadLength != "" { + request.Header.Set(net.HeaderUploadLength, tt.uploadLength) + } + + policiesMiddleware.ServeHTTP(httptest.NewRecorder(), request) + policiesProviderService.AssertExpectations(t) + }) + } +} + func TestPolicies_EvaluationEnvironment_Resource(t *testing.T) { var g = NewWithT(t)