Skip to content

Commit

Permalink
fix(policies): Set proper host on policy engine (chainloop-dev#1403)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Rodriguez <[email protected]>
  • Loading branch information
javirln authored Oct 16, 2024
1 parent f1d4092 commit 0383e20
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/policies/engine/rego/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ var builtinFuncNotAllowed = []*ast.Builtin{
// allowedNetworkDomains is a list of network domains that are allowed for the compiler to access
// when using http.send built-in function
var allowedNetworkDomains = []string{
"chainloop.dev",
"cisa.gov",
"www.chainloop.dev",
"www.cisa.gov",
}

// Force interface
Expand Down
16 changes: 15 additions & 1 deletion pkg/policies/engine/rego/rego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestRego_VerifyInvalidPolicy(t *testing.T) {
})
}

func TestRego_WithRestrictiveMOde(t *testing.T) {
func TestRego_WithRestrictiveMode(t *testing.T) {
t.Run("forbidden functions", func(t *testing.T) {
regoContent, err := os.ReadFile("testfiles/restrictive_mode.rego")
require.NoError(t, err)
Expand Down Expand Up @@ -229,6 +229,20 @@ func TestRego_WithRestrictiveMOde(t *testing.T) {
assert.Contains(t, err.Error(), "eval_builtin_error: http.send: unallowed host: example.com")
assert.Len(t, violations, 0)
})

t.Run("allowed network requests", func(t *testing.T) {
regoContent, err := os.ReadFile("testfiles/restricted_mode_networking_allowed_host.rego")
require.NoError(t, err)

r := &Rego{}
policy := &engine.Policy{
Name: "policy",
Source: regoContent,
}

_, err = r.Verify(context.TODO(), policy, []byte(`{}`), nil)
assert.NoError(t, err)
})
}

func TestRego_WithPermissiveMode(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import rego.v1

violations contains msg if {
kev := http.send({"method": "GET", "url": "https://www.chainloop.dev", "cache": true}).body

msg := ""
}

0 comments on commit 0383e20

Please sign in to comment.