Ecosyste.ms: Advisories

An open API service providing security vulnerability metadata for many open source software ecosystems.

Security Advisories: GSA_kwCzR0hTQS1mNTI0LXJmMzMtMmpqcs4AAu1k

OPA Compiler: Bypass of WithUnsafeBuiltins using "with" keyword to mock functions

Impact

The Rego compiler provides a (deprecated) WithUnsafeBuiltins function, which allows users to provide a set of built-in functions that should be deemed unsafe — and as such rejected — by the compiler if encountered in the policy compilation stage. A bypass of this protection has been found, where the use of the with keyword to mock such a built-in function (a feature introduced in OPA v0.40.0), isn’t taken into account by WithUnsafeBuiltins.

The same method is exposed via rego.UnsafeBuiltins in the github.com/open-policy-agent/opa/rego package.

When provided e.g. the http.send built-in function to WithUnsafeBuiltins, the following policy would still compile, and call the http.send function with the arguments provided to the is_object function when evaluated:

package policy

foo := is_object({
    "method": "get", 
    "url": "https://www.openpolicyagent.org"
})

allow := r {
    r := foo with is_object as http.send
}

Both built-in functions and user provided (i.e. custom) functions are mockable using this construct.

In addition to http.send, the opa.runtime built-in function is commonly considered unsafe in integrations where policy provided by untrusted parties is evaluated, as it risks exposing configuration, or environment variables, potentially carrying sensitive information.

Affected Users

All of these conditions have to be met to create an adverse effect:

Additionally, the OPA Query API is affected:

Patches

v0.43.1, v0.44.0

Workarounds

The WithUnsafeBuiltins function has been considered deprecated since the introduction of the capabilities feature in OPA v0.23.0 . While the function was commented as deprecated, the format of the comment was however not following the convention for deprecated functions, and might not have been picked up by tooling like editors. This has now been fixed. Users are still encouraged to use the capabilities feature over the deprecated WithUnsafeBuiltins function.

If you cannot upgrade, consider using capabilities instead:

Code like this using the github.com/open-policy-agent/opa/ast package:

// VULNERABLE with OPA <= 0.43.0
unsafeBuiltins := map[string]struct{}{
	ast.HTTPSend.Name: struct{}{},
}
compiler := ast.NewCompiler().WithUnsafeBuiltins(unsafeBuiltins)

needs to be changed to this:

caps := ast.CapabilitiesForThisVersion()
var j int
for i, bi := range caps.Builtins {
	if bi.Name == ast.HTTPSend.Name {
		j = i
		break
	}
}
caps.Builtins[j] = caps.Builtins[len(caps.Builtins)-1] // put last element into position j
caps.Builtins = caps.Builtins[:len(caps.Builtins)-1]   // truncate slice

compiler := ast.NewCompiler().WithCapabilities(caps)

When using the github.com/open-policy-agent/opa/rego package:

// VULNERABLE with OPA <= 0.43.0
r := rego.New(
	// other options omitted
	rego.UnsafeBuiltins(map[string]struct{}{ast.HTTPSend.Name: struct{}{}}),
)

needs to be changed to

r := rego.New(
	// other options omitted
	rego.Capabilities(caps),
)

with caps defined above.

Note that in the process, some error messages will change: http.send in this example will no longer be "unsafe" and thus forbidden, but it will simply become an "unknown function".

References

For more information

If you have any questions or comments about this advisory:

Permalink: https://github.com/advisories/GHSA-f524-rf33-2jjr
JSON: https://advisories.ecosyste.ms/api/v1/advisories/GSA_kwCzR0hTQS1mNTI0LXJmMzMtMmpqcs4AAu1k
Source: GitHub Advisory Database
Origin: Unspecified
Severity: High
Classification: General
Published: over 1 year ago
Updated: 3 months ago


CVSS Score: 7.4
CVSS vector: CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:N

Identifiers: GHSA-f524-rf33-2jjr, CVE-2022-36085
References: Repository: https://github.com/open-policy-agent/opa
Blast Radius: 24.5

Affected Packages

go:github.com/open-policy-agent/opa
Dependent packages: 852
Dependent repositories: 2,039
Downloads:
Affected Version Ranges: >= 0.40.0, < 0.43.1
Fixed in: 0.43.1
All affected versions: 0.40.0, 0.41.0, 0.42.0, 0.42.1, 0.42.2, 0.43.0
All unaffected versions: 0.1.0, 0.2.0, 0.2.1, 0.2.2, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.4.7, 0.4.8, 0.4.9, 0.4.10, 0.5.0, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.5.6, 0.5.7, 0.5.8, 0.5.9, 0.5.10, 0.5.11, 0.5.12, 0.5.13, 0.6.0, 0.7.0, 0.7.1, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.10.0, 0.10.1, 0.10.2, 0.10.3, 0.10.4, 0.10.5, 0.10.6, 0.10.7, 0.11.0, 0.12.0, 0.12.1, 0.12.2, 0.13.0, 0.13.1, 0.13.2, 0.13.3, 0.13.4, 0.13.5, 0.14.0, 0.14.1, 0.14.2, 0.15.0, 0.15.1, 0.16.0, 0.16.1, 0.16.2, 0.17.0, 0.17.1, 0.17.2, 0.17.3, 0.18.0, 0.19.0, 0.19.1, 0.19.2, 0.20.0, 0.20.1, 0.20.2, 0.20.3, 0.20.4, 0.20.5, 0.21.0, 0.21.1, 0.22.0, 0.23.0, 0.23.1, 0.23.2, 0.24.0, 0.25.0, 0.25.1, 0.25.2, 0.26.0, 0.27.0, 0.27.1, 0.28.0, 0.29.0, 0.29.1, 0.29.2, 0.29.3, 0.29.4, 0.30.0, 0.30.1, 0.30.2, 0.31.0, 0.32.0, 0.32.1, 0.33.0, 0.33.1, 0.34.0, 0.34.1, 0.34.2, 0.35.0, 0.36.0, 0.36.1, 0.37.0, 0.37.1, 0.37.2, 0.38.0, 0.38.1, 0.39.0, 0.43.1, 0.44.0, 0.45.0, 0.46.0, 0.46.1, 0.46.2, 0.46.3, 0.47.0, 0.47.1, 0.47.2, 0.47.3, 0.47.4, 0.48.0, 0.49.0, 0.49.1, 0.49.2, 0.50.0, 0.50.1, 0.50.2, 0.51.0, 0.52.0, 0.53.0, 0.53.1, 0.54.0, 0.55.0, 0.56.0, 0.57.0, 0.57.1, 0.58.0, 0.59.0, 0.60.0, 0.61.0