Files
opencloud/services/policies/pkg/engine/engine_test.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

62 lines
1.8 KiB
Go

package engine_test
import (
. "github.com/onsi/ginkgo/v2"
. "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() {
DescribeTable("NewEnvironmentFromPB",
func(incomingStage pMessage.Stage, outgoinStage engine.Stage) {
pEnv := &pMessage.Environment{
Stage: incomingStage,
}
env, err := engine.NewEnvironmentFromPB(pEnv)
Expect(err).ToNot(HaveOccurred())
Expect(env.Stage).To(Equal(outgoinStage))
},
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())
})
})