Files
opencloud/services/policies/pkg/engine/engine.go
T
Jörn Friedrich Dreyer a69677cecf 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>
2026-11-30 12:31:02 +01:00

86 lines
2.5 KiB
Go

package engine
import (
"context"
"encoding/json"
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
v0 "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/policies/v0"
"google.golang.org/protobuf/encoding/protojson"
)
// Engine defines the granted handlers.
type Engine interface {
Evaluate(ctx context.Context, query string, env Environment) (bool, error)
}
type (
// Stage defines the used auth stage
Stage string
)
var (
// StagePP defines the post-processing stage
StagePP Stage = "pp"
// StageHTTP defines the http stage
StageHTTP Stage = "http"
)
// Resource contains resource information and is used as part of the evaluated environment.
type Resource struct {
ID provider.ResourceId `json:"resource_id"`
Name string `json:"name"`
URL string `json:"url"`
Size uint64 `json:"size"`
}
// Request contains request information and is used as part of the evaluated environment.
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
type Environment struct {
Stage Stage `json:"stage"`
User user.User `json:"user"`
Request Request `json:"request"`
Resource Resource `json:"resource"`
}
// NewEnvironmentFromPB converts a PBEnvironment to Environment.
func NewEnvironmentFromPB(pEnv *v0.Environment) (Environment, error) {
env := Environment{}
rData, err := protojson.Marshal(pEnv)
if err != nil {
return env, err
}
if err := json.Unmarshal(rData, &env); err != nil {
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
case v0.Stage_STAGE_PP:
env.Stage = StagePP
}
return env, nil
}