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

202 lines
6.5 KiB
Go

package opa_test
import (
"bytes"
"context"
"image"
"image/png"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync/atomic"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/opencloud-eu/opencloud/pkg/log"
"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() {
newEngine := func(source string) (engine.Engine, string) {
path := filepath.Join(GinkgoT().TempDir(), "policy.rego")
Expect(os.WriteFile(path, []byte(source), 0o600)).To(Succeed())
e, err := opa.NewOPA(10*time.Second, log.NopLogger(), config.Engine{Policies: []string{path}}, nil)
Expect(err).ToNot(HaveOccurred())
return e, path
}
Context("when the policy files are read", func() {
var path string
install := func(name string) {
source, err := os.ReadFile(filepath.Join("testdata", "rules", name))
Expect(err).ToNot(HaveOccurred())
Expect(os.WriteFile(path, source, 0o600)).To(Succeed())
}
start := func(policies ...string) engine.Engine {
e, err := opa.NewOPA(10*time.Second, log.NopLogger(), config.Engine{Policies: policies}, nil)
Expect(err).ToNot(HaveOccurred())
return e
}
decide := func(e engine.Engine) bool {
granted, err := e.Evaluate(context.Background(), "data.rules.granted", engine.Environment{})
Expect(err).ToNot(HaveOccurred())
return granted
}
BeforeEach(func() {
path = filepath.Join(GinkgoT().TempDir(), "rules.rego")
install("granted.rego")
})
It("keeps the rule set a rewrite replaced", func() {
e := start(path)
Expect(decide(e)).To(BeTrue())
install("denied.rego")
Expect(decide(e)).To(BeTrue(), "a rewrite only takes effect on restart")
})
It("keeps the rule set after the file disappeared", func() {
e := start(path)
Expect(os.Remove(path)).To(Succeed())
Expect(decide(e)).To(BeTrue())
})
It("refuses to start on a broken policy", func() {
install("broken.rego")
_, err := opa.NewOPA(10*time.Second, log.NopLogger(), config.Engine{Policies: []string{path}}, nil)
Expect(err).To(HaveOccurred())
})
It("refuses to start when a policy is missing", func() {
Expect(os.Remove(path)).To(Succeed())
_, err := opa.NewOPA(10*time.Second, log.NopLogger(), config.Engine{Policies: []string{path}}, nil)
Expect(err).To(HaveOccurred())
})
It("takes a directory as a policy path", func() {
dir := GinkgoT().TempDir()
source, err := os.ReadFile(filepath.Join("testdata", "rules", "granted.rego"))
Expect(err).ToNot(HaveOccurred())
Expect(os.WriteFile(filepath.Join(dir, "policy.rego"), source, 0o600)).To(Succeed())
Expect(decide(start(dir))).To(BeTrue())
})
})
Context("across evaluations", func() {
It("carries no downloaded content into the next one", func() {
buf := new(bytes.Buffer)
Expect(png.Encode(buf, image.NewRGBA(image.Rect(0, 0, 1, 1)))).To(Succeed())
// same url, different content per request.
var served atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
body := []byte("plain text, not an image")
if served.Add(1) == 1 {
body = buf.Bytes()
}
_, err := w.Write(body)
Expect(err).ToNot(HaveOccurred())
}))
defer srv.Close()
e, _ := newEngine("package isolation\n\nimport future.keywords.if\n\ndefault granted := true\n\ngranted = false if {\n body := opencloud.resource.download(input.resource.url)\n opencloud.mimetype.detect(body) == \"image/png\"\n}\n")
env := engine.Environment{Resource: engine.Resource{URL: srv.URL}}
first, err := e.Evaluate(context.Background(), "data.isolation.granted", env)
Expect(err).ToNot(HaveOccurred())
Expect(first).To(BeFalse(), "png is served first and has to be denied")
second, err := e.Evaluate(context.Background(), "data.isolation.granted", env)
Expect(err).ToNot(HaveOccurred())
Expect(second).To(BeTrue(), "text is served second, seeing the png again means the memo leaked")
Expect(served.Load()).To(BeEquivalentTo(2), "the second evaluation has to fetch again")
})
It("judges each one by its own input", func() {
e, _ := newEngine("package isolation\n\nimport future.keywords.if\n\ndefault granted := true\n\ngranted = false if {\n endswith(input.resource.name, \".exe\")\n}\n")
for _, tc := range []struct {
name string
want bool
}{
{"notes.txt", true},
{"virus.exe", false},
{"notes.txt", true},
{"virus.exe", false},
} {
granted, err := e.Evaluate(context.Background(), "data.isolation.granted", engine.Environment{
Resource: engine.Resource{Name: tc.name},
})
Expect(err).ToNot(HaveOccurred())
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)
}
})
})
})