120 lines
4.0 KiB
Go
120 lines
4.0 KiB
Go
package machinelearning
|
|
|
|
import (
|
|
"errors"
|
|
"path"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
var ErrPolicy = errors.New("invalid machine learning policy")
|
|
var ErrNotAllowed = errors.New("machine learning scope is disabled, stale or excluded")
|
|
|
|
// Policy contains no credentials. Generation changes on every saved update,
|
|
// including disable/re-enable, so queued work cannot reuse old consent.
|
|
type Policy struct {
|
|
Generation string `json:"generation"`
|
|
Enabled bool `json:"enabled"`
|
|
ScopeUUID string `json:"scope_uuid"`
|
|
Provider string `json:"provider"`
|
|
Model string `json:"model"`
|
|
Dimensions int `json:"dimensions"`
|
|
Prefixes []string `json:"prefixes"`
|
|
Exclude []string `json:"exclude"`
|
|
AllowExternal bool `json:"allow_external"`
|
|
MaxBatchBytes int `json:"max_batch_bytes"`
|
|
DailyInputBytes int64 `json:"daily_input_bytes"`
|
|
}
|
|
|
|
// Canonical paths are decoded logical Wiki paths, not URLs or raw request URIs.
|
|
// Reject ambiguous encodings instead of silently broadening a configured scope.
|
|
func canonicalPath(p string) bool {
|
|
return utf8.ValidString(p) && len(p) <= 4096 && strings.HasPrefix(p, "/") && path.Clean(p) == p &&
|
|
!strings.ContainsAny(p, "%?#\\") && strings.IndexFunc(p, unicode.IsControl) < 0
|
|
}
|
|
|
|
func (p Policy) Validate() error {
|
|
if p.DailyInputBytes < 0 || p.DailyInputBytes > 1000000000 {
|
|
return ErrPolicy
|
|
}
|
|
if !validUUID(p.Generation) {
|
|
return ErrPolicy
|
|
}
|
|
// A new disabled configuration may have no model yet.
|
|
if !p.Enabled && p.Provider == "" && p.ScopeUUID == "" && p.Model == "" && p.Dimensions == 0 && len(p.Prefixes) == 0 && len(p.Exclude) == 0 && !p.AllowExternal && p.MaxBatchBytes == 0 {
|
|
return nil
|
|
}
|
|
if !validUUID(p.ScopeUUID) || p.Dimensions < 1 || p.Dimensions > 1536 || len(p.Prefixes) < 1 || len(p.Prefixes) > 64 || len(p.Exclude) > 64 || p.MaxBatchBytes < 1 || p.MaxBatchBytes > 96000 {
|
|
return ErrPolicy
|
|
}
|
|
switch p.Provider {
|
|
case "doc2vec":
|
|
if !modelHash.MatchString(p.Model) || p.AllowExternal {
|
|
return ErrPolicy
|
|
}
|
|
case "openai":
|
|
if p.Model != "text-embedding-3-small" || (p.Enabled && !p.AllowExternal) {
|
|
return ErrPolicy
|
|
}
|
|
default:
|
|
return ErrPolicy
|
|
}
|
|
for _, paths := range [][]string{p.Prefixes, p.Exclude} {
|
|
seen := map[string]bool{}
|
|
for _, prefix := range paths {
|
|
if !canonicalPath(prefix) || seen[prefix] {
|
|
return ErrPolicy
|
|
}
|
|
seen[prefix] = true
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func under(p, prefix string) bool {
|
|
return prefix == "/" || p == prefix || strings.HasPrefix(p, prefix+"/")
|
|
}
|
|
|
|
// Allows is an additional scope check, NOT document authorization. Callers must
|
|
// still check the current principal, revision, and learning cohort membership.
|
|
func (p Policy) Allows(logicalPath, generation string) bool {
|
|
if p.Validate() != nil || !p.Enabled || generation != p.Generation || !canonicalPath(logicalPath) {
|
|
return false
|
|
}
|
|
for _, prefix := range p.Exclude {
|
|
if under(logicalPath, prefix) {
|
|
return false
|
|
}
|
|
}
|
|
for _, prefix := range p.Prefixes {
|
|
if under(logicalPath, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Prepare binds model and consent to a freshly loaded policy rather than to
|
|
// caller-provided JSON. Inputs must be authorized chunks of this one document.
|
|
// The batch limit is not a daily budget or a monetary cost ceiling.
|
|
func (p Policy) Prepare(domain, logicalPath, generation string, inputs []Input) (Request, error) {
|
|
if !p.Allows(logicalPath, generation) {
|
|
return Request{}, ErrNotAllowed
|
|
}
|
|
bytes := 0
|
|
for _, input := range inputs {
|
|
bytes += len(input.Text)
|
|
if bytes > p.MaxBatchBytes {
|
|
return Request{}, ErrNotAllowed
|
|
}
|
|
}
|
|
req := Request{Protocol: "1", DomainUUID: domain, ScopeUUID: p.ScopeUUID, Provider: p.Provider, Model: p.Model, Dimensions: p.Dimensions, Inputs: inputs, AllowExternal: p.AllowExternal}
|
|
if err := validate(req); err != nil {
|
|
return Request{}, err
|
|
}
|
|
// Do not retain caller-owned slice memory across a queued operation.
|
|
req.Inputs = append([]Input(nil), inputs...)
|
|
return req, nil
|
|
}
|