mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
feat(policies): leave unknown request lengths out of the policy input
content_length was 0 both for an empty body and for a body of unknown length, e.g. a PUT with chunked transfer encoding. A policy could not reject uploads that do not declare their size. The lengths are now proto3 optional fields and the proxy sets content_length whenever Go knows it, including 0 for an empty body. An unknown length leaves the key out of the policy input. Rules that compare a missing key do not apply, so existing limit rules keep working, and a policy can reject an undeclared length explicitly with `not "content_length" in object.keys(input.request)`. We use field presence instead of -1, because a negative sentinel would turn a request without lengths, e.g. in the postprocessing stage, into an empty body. related: https://github.com/opencloud-eu/opencloud/pull/3903#issuecomment-7273785513 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
1 parent
bd4325b75b
commit
a69677cecf
9 files changed
+143
-73
No files matched your search
@@ -223,10 +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, 0 if unknown
|
||||
ContentLength uint64 `protobuf:"varint,3,opt,name=content_length,json=contentLength,proto3" json:"content_length,omitempty"`
|
||||
// TUS Upload-Length header, the declared total size of an upload, 0 if absent
|
||||
UploadLength uint64 `protobuf:"varint,4,opt,name=upload_length,json=uploadLength,proto3" json:"upload_length,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() {
|
||||
@@ -276,15 +276,15 @@ func (x *Request) GetPath() string {
|
||||
}
|
||||
|
||||
func (x *Request) GetContentLength() uint64 {
|
||||
if x != nil {
|
||||
return x.ContentLength
|
||||
if x != nil && x.ContentLength != nil {
|
||||
return *x.ContentLength
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Request) GetUploadLength() uint64 {
|
||||
if x != nil {
|
||||
return x.UploadLength
|
||||
if x != nil && x.UploadLength != nil {
|
||||
return *x.UploadLength
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -505,40 +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, 0x81, 0x01, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06,
|
||||
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, 0x25, 0x0a, 0x0e, 0x63, 0x6f, 0x6e, 0x74,
|
||||
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,
|
||||
0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x12,
|
||||
0x23, 0x0a, 0x0d, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68,
|
||||
0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0c, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x4c, 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, 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,
|
||||
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, 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, 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, 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 (
|
||||
@@ -657,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{
|
||||
|
||||
@@ -138,12 +138,12 @@
|
||||
"contentLength": {
|
||||
"type": "string",
|
||||
"format": "uint64",
|
||||
"title": "Content-Length of the request body, 0 if unknown"
|
||||
"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, 0 if absent"
|
||||
"title": "TUS Upload-Length header, the declared total size of an upload, unset if absent"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -32,10 +32,10 @@ message Resource {
|
||||
message Request {
|
||||
string method = 1;
|
||||
string path = 2;
|
||||
// Content-Length of the request body, 0 if unknown
|
||||
uint64 content_length = 3;
|
||||
// TUS Upload-Length header, the declared total size of an upload, 0 if absent
|
||||
uint64 upload_length = 4;
|
||||
// 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 {
|
||||
|
||||
@@ -187,12 +187,13 @@ 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. Both are `0` if unknown. They allow rejecting uploads that are too big before any data is transferred:
|
||||
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
|
||||
|
||||
@@ -203,11 +204,22 @@ granted = false if {
|
||||
|
||||
# a plain PUT carries the whole file, a PATCH a chunk
|
||||
granted = false if {
|
||||
input.request.method == ["PUT", "PATCH"][_]
|
||||
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. The TUS concatenation extension can merge several smaller uploads into one file, so the actual file size has to be checked in postprocessing using `input.resource.size`.
|
||||
|
||||
## Rego Extensions
|
||||
|
||||
@@ -40,10 +40,11 @@ type Resource struct {
|
||||
type Request struct {
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
// ContentLength is the length of the request body, 0 if unknown.
|
||||
ContentLength uint64 `json:"content_length"`
|
||||
// UploadLength is the total size of an upload as declared by the TUS Upload-Length header, 0 if absent.
|
||||
UploadLength uint64 `json:"upload_length"`
|
||||
// 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
|
||||
@@ -68,8 +69,10 @@ func NewEnvironmentFromPB(pEnv *v0.Environment) (Environment, error) {
|
||||
}
|
||||
|
||||
// protojson encodes uint64 as strings with camelCase keys, so copy the lengths explicitly
|
||||
env.Request.ContentLength = pEnv.GetRequest().GetContentLength()
|
||||
env.Request.UploadLength = pEnv.GetRequest().GetUploadLength()
|
||||
if pReq := pEnv.GetRequest(); pReq != nil {
|
||||
env.Request.ContentLength = pReq.ContentLength
|
||||
env.Request.UploadLength = pReq.UploadLength
|
||||
}
|
||||
|
||||
switch pEnv.Stage {
|
||||
case v0.Stage_STAGE_HTTP:
|
||||
|
||||
@@ -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() {
|
||||
@@ -29,8 +30,8 @@ var _ = Describe("Engine", func() {
|
||||
Request: &pMessage.Request{
|
||||
Method: "POST",
|
||||
Path: "/remote.php/dav/spaces/some-space-id",
|
||||
ContentLength: 1024,
|
||||
UploadLength: 20000000000,
|
||||
ContentLength: proto.Uint64(1024),
|
||||
UploadLength: proto.Uint64(20000000000),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -38,7 +39,23 @@ var _ = Describe("Engine", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(env.Request.Method).To(Equal("POST"))
|
||||
Expect(env.Request.ContentLength).To(Equal(uint64(1024)))
|
||||
Expect(env.Request.UploadLength).To(Equal(uint64(20000000000)))
|
||||
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())
|
||||
})
|
||||
})
|
||||
@@ -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() {
|
||||
@@ -151,8 +152,28 @@ var _ = Describe("engine", func() {
|
||||
}
|
||||
})
|
||||
|
||||
It("compares the request lengths as numbers", func() {
|
||||
e, _ := newEngine("package size\n\nimport future.keywords.if\n\ndefault granted := true\n\ngranted = false if {\n input.request.upload_length > 10737418240\n}\n\ngranted = false if {\n input.request.method == [\"PUT\", \"PATCH\"][_]\n input.request.content_length > 10737418240\n}\n")
|
||||
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
|
||||
@@ -160,11 +181,14 @@ var _ = Describe("engine", func() {
|
||||
want bool
|
||||
}{
|
||||
{"no lengths", engine.Request{Method: "POST"}, true},
|
||||
{"tus creation below limit", engine.Request{Method: "POST", UploadLength: 10737418240}, true},
|
||||
{"tus creation above limit", engine.Request{Method: "POST", UploadLength: 20000000000}, false},
|
||||
{"put above limit", engine.Request{Method: "PUT", ContentLength: 20000000000}, false},
|
||||
{"patch above limit", engine.Request{Method: "PATCH", ContentLength: 20000000000}, false},
|
||||
{"patch below limit", engine.Request{Method: "PATCH", ContentLength: 104857600}, 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,
|
||||
|
||||
@@ -16,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/v2/pkg/ctx"
|
||||
"github.com/opencloud-eu/reva/v2/pkg/storagespace"
|
||||
@@ -74,11 +75,12 @@ 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
|
||||
if r.ContentLength > 0 {
|
||||
req.Environment.Request.ContentLength = uint64(r.ContentLength)
|
||||
// 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 = uploadLength
|
||||
req.Environment.Request.UploadLength = proto.Uint64(uploadLength)
|
||||
}
|
||||
|
||||
resource := &pMessage.Resource{}
|
||||
|
||||
@@ -20,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"
|
||||
@@ -115,15 +116,18 @@ func TestPolicies_EvaluationEnvironment_RequestLengths(t *testing.T) {
|
||||
name string
|
||||
method string
|
||||
body string
|
||||
chunked bool
|
||||
uploadLength string
|
||||
expectedContentLength uint64
|
||||
expectedUploadLength uint64
|
||||
expectedContentLength *uint64
|
||||
expectedUploadLength *uint64
|
||||
}{
|
||||
{name: "tus creation", method: http.MethodPost, uploadLength: "20000000000", expectedUploadLength: 20000000000},
|
||||
{name: "put", method: http.MethodPut, body: "some content", expectedContentLength: 12},
|
||||
{name: "patch", method: http.MethodPatch, body: "a chunk", expectedContentLength: 7},
|
||||
{name: "invalid upload length", method: http.MethodPost, uploadLength: "-1"},
|
||||
{name: "no lengths", method: http.MethodGet},
|
||||
{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 {
|
||||
@@ -145,6 +149,10 @@ func TestPolicies_EvaluationEnvironment_RequestLengths(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user