* chore(deps): consolidate dependency and security bumps Dependabot PRs applied: - #12878 qs 6.15.2 -> 6.16.0 (web/web-runtime) - #13013 alpine 3.24.1 -> 3.24.2 - #13061 actions/checkout -> v7.0.1 (missed instance in k8s.yml) - #13062 actions/setup-node -> v7.0.0 (missed instance in k8s.yml) - #13075 simple-git 3.36.0 -> 4.0.1 (security) - #13076 katex 0.16.45 -> 0.18.2+ (security) - #13077 dompurify 3.4.13 -> 3.4.16 (security) - #13079 go deps minor-and-patch group, 18 updates (opa 1.19.0 -> 1.21.1, otel 1.46 -> 1.47, bleve, govips, grpc-gateway, lico, tablewriter, logrus, gjson, x/exp, x/image, genproto, etc.) Skipped (already satisfied on master): #12513, #12624, #12625, #12633 Manual security bumps via pnpm.overrides (no dependabot PR existed): - axios -> 1.20.0 - smol-toml -> 1.9.0 - postcss -> 8.5.23 - postcss-selector-parser -> 7.1.6 - colord -> 2.9.4 - unhead -> 2.1.13 - markdown-it -> 14.3.1 - esbuild -> 0.28.1 - brace-expansion 5.0.11 -> 5.0.12 Not fixable: sprintf-js and elliptic have no patched version released upstream yet; left as-is. go build and pnpm install verified clean. * chore(deps): fix residual transitive vulns found by trivy trivy flagged duplicate/stale resolutions that survived the earlier override pass: dompurify (3.4.12 -> 3.4.16), qs (6.14.2 -> 6.16.0), and fast-uri (3.1.7 -> 3.1.8, CVE-2026-86472). Added/bumped pnpm.overrides accordingly. trivy fs scan of web/pnpm-lock.yaml now clean. * chore(deps): bump reva to bac0e1ee144ab853a81c6b02142032f102dd245a Pins github.com/owncloud/reva/v2 to bac0e1ee144ab853a81c6b02142032f102dd245a, pulling in its go-playground/locales and huandu/xstrings version bumps. Appends this PR to changelog/unreleased/bump-reva.md. go build ./... verified clean after go mod vendor resync. * fix(ci): revert unhead override, breaks web build * revert: keep k8s.yml actions/checkout and setup-node pinned
177 lines
4.3 KiB
Go
177 lines
4.3 KiB
Go
// Copyright 2016 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package topdown
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"github.com/open-policy-agent/opa/v1/ast"
|
|
"github.com/open-policy-agent/opa/v1/topdown/builtins"
|
|
)
|
|
|
|
const (
|
|
none uint64 = 1 << (10 * iota)
|
|
ki
|
|
mi
|
|
gi
|
|
ti
|
|
pi
|
|
ei
|
|
|
|
kb uint64 = 1000
|
|
mb = kb * 1000
|
|
gb = mb * 1000
|
|
tb = gb * 1000
|
|
pb = tb * 1000
|
|
eb = pb * 1000
|
|
)
|
|
|
|
func parseNumBytesError(msg string) error {
|
|
return fmt.Errorf("%s: %s", ast.UnitsParseBytes.Name, msg)
|
|
}
|
|
|
|
func errBytesUnitNotRecognized(unit string) error {
|
|
return parseNumBytesError("byte unit " + unit + " not recognized")
|
|
}
|
|
|
|
var (
|
|
errBytesValueNoAmount = parseNumBytesError("no byte amount provided")
|
|
errBytesValueNumConv = parseNumBytesError("could not parse byte amount to a number")
|
|
errBytesValueIncludesSpaces = parseNumBytesError("spaces not allowed in resource strings")
|
|
errBytesExponentTooLarge = parseNumBytesError("exponent too large")
|
|
)
|
|
|
|
// maxExponentDigits limits the number of digits allowed in the exponent of
|
|
// scientific notation input.
|
|
const maxExponentDigits = 6
|
|
|
|
func builtinNumBytes(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
|
var m big.Float
|
|
|
|
raw, err := builtins.StringOperand(operands[0].Value, 1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s := formatString(raw)
|
|
|
|
if strings.Contains(s, " ") {
|
|
return errBytesValueIncludesSpaces
|
|
}
|
|
|
|
num, unit, err := extractNumAndUnit(s)
|
|
if err != nil {
|
|
return errBytesExponentTooLarge
|
|
}
|
|
if num == "" {
|
|
return errBytesValueNoAmount
|
|
}
|
|
|
|
switch unit {
|
|
case "":
|
|
m.SetUint64(none)
|
|
case "kb", "k":
|
|
m.SetUint64(kb)
|
|
case "kib", "ki":
|
|
m.SetUint64(ki)
|
|
case "mb", "m":
|
|
m.SetUint64(mb)
|
|
case "mib", "mi":
|
|
m.SetUint64(mi)
|
|
case "gb", "g":
|
|
m.SetUint64(gb)
|
|
case "gib", "gi":
|
|
m.SetUint64(gi)
|
|
case "tb", "t":
|
|
m.SetUint64(tb)
|
|
case "tib", "ti":
|
|
m.SetUint64(ti)
|
|
case "pb", "p":
|
|
m.SetUint64(pb)
|
|
case "pib", "pi":
|
|
m.SetUint64(pi)
|
|
case "eb", "e":
|
|
m.SetUint64(eb)
|
|
case "eib", "ei":
|
|
m.SetUint64(ei)
|
|
default:
|
|
return errBytesUnitNotRecognized(unit)
|
|
}
|
|
|
|
numFloat, ok := new(big.Float).SetString(num)
|
|
if !ok {
|
|
return errBytesValueNumConv
|
|
}
|
|
|
|
var total big.Int
|
|
numFloat.Mul(numFloat, &m).Int(&total)
|
|
return iter(ast.NewTerm(builtins.IntToNumber(&total)))
|
|
}
|
|
|
|
// Makes the string lower case and removes quotation marks
|
|
func formatString(s ast.String) string {
|
|
return strings.ReplaceAll(strings.ToLower(string(s)), "\"", "")
|
|
}
|
|
|
|
// Splits the string into a number string à la "10" or "10.2" and a unit
|
|
// string à la "gb" or "MiB" or "foo". Either can be an empty string
|
|
// (error handling is provided elsewhere).
|
|
// Returns an error if the exponent in scientific notation exceeds
|
|
// maxExponentDigits digits.
|
|
func extractNumAndUnit(s string) (string, string, error) {
|
|
isNum := func(r rune) bool {
|
|
return unicode.IsDigit(r) || r == '.'
|
|
}
|
|
|
|
firstNonNumIdx := -1
|
|
for idx := 0; idx < len(s); idx++ {
|
|
r := rune(s[idx])
|
|
// Identify the first non-numeric character, marking the boundary between the number and the unit.
|
|
if !isNum(r) && r != 'e' && r != 'E' && r != '+' && r != '-' {
|
|
firstNonNumIdx = idx
|
|
break
|
|
}
|
|
if r == 'e' || r == 'E' {
|
|
// Check if the next character is a valid digit or +/- for scientific notation
|
|
if idx == len(s)-1 || (!unicode.IsDigit(rune(s[idx+1])) && rune(s[idx+1]) != '+' && rune(s[idx+1]) != '-') {
|
|
firstNonNumIdx = idx
|
|
break
|
|
}
|
|
// Skip the next character if it is '+' or '-'
|
|
if idx+1 < len(s) && (s[idx+1] == '+' || s[idx+1] == '-') {
|
|
idx++
|
|
}
|
|
|
|
// Count the digits in the exponent and reject if too large.
|
|
expStart := idx + 1
|
|
expEnd := expStart
|
|
for expEnd < len(s) && unicode.IsDigit(rune(s[expEnd])) {
|
|
expEnd++
|
|
}
|
|
if expEnd-expStart > maxExponentDigits {
|
|
return "", "", errors.New("exponent too large")
|
|
}
|
|
}
|
|
}
|
|
|
|
if firstNonNumIdx == -1 { // only digits, '.', or valid scientific notation
|
|
return s, "", nil
|
|
}
|
|
if firstNonNumIdx == 0 { // only units (starts with non-digit)
|
|
return "", s, nil
|
|
}
|
|
|
|
// Return the number and the rest as the unit
|
|
return s[:firstNonNumIdx], s[firstNonNumIdx:], nil
|
|
}
|
|
|
|
func init() {
|
|
RegisterBuiltinFunc(ast.UnitsParseBytes.Name, builtinNumBytes)
|
|
}
|