|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package rego |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/chainloop-dev/chainloop/pkg/policies/engine" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +const httpSendPolicyTemplate = `package main |
| 29 | +
|
| 30 | +import rego.v1 |
| 31 | +
|
| 32 | +result := {"skipped": false, "violations": violations} |
| 33 | +
|
| 34 | +violations contains "request sent" if { |
| 35 | + http.send(%[1]s) |
| 36 | +} |
| 37 | +
|
| 38 | +matches_parameters if { |
| 39 | + http.send(%[1]s) |
| 40 | +} |
| 41 | +` |
| 42 | + |
| 43 | +func TestRego_HTTPSendRestrictedOptions(t *testing.T) { |
| 44 | + const allowedURL = `"url": "https://www.chainloop.dev"` |
| 45 | + |
| 46 | + testCases := []struct { |
| 47 | + name string |
| 48 | + request string |
| 49 | + wantErr string |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "tls_ca_cert_file", |
| 53 | + request: `{"method": "GET", ` + allowedURL + `, "tls_ca_cert_file": "/etc/ssl/ca.pem"}`, |
| 54 | + wantErr: `http.send: request option "tls_ca_cert_file" is not allowed`, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "tls_ca_cert_env_variable", |
| 58 | + request: `{"method": "GET", ` + allowedURL + `, "tls_ca_cert_env_variable": "CA_CERT"}`, |
| 59 | + wantErr: `http.send: request option "tls_ca_cert_env_variable" is not allowed`, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "tls_client_cert_file", |
| 63 | + request: `{"method": "GET", ` + allowedURL + `, "tls_client_cert_file": "/etc/ssl/cert.pem"}`, |
| 64 | + wantErr: `http.send: request option "tls_client_cert_file" is not allowed`, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "tls_client_cert_env_variable", |
| 68 | + request: `{"method": "GET", ` + allowedURL + `, "tls_client_cert_env_variable": "CLIENT_CERT"}`, |
| 69 | + wantErr: `http.send: request option "tls_client_cert_env_variable" is not allowed`, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "tls_client_key_file", |
| 73 | + request: `{"method": "GET", ` + allowedURL + `, "tls_client_key_file": "/etc/ssl/key.pem"}`, |
| 74 | + wantErr: `http.send: request option "tls_client_key_file" is not allowed`, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "tls_client_key_env_variable", |
| 78 | + request: `{"method": "GET", ` + allowedURL + `, "tls_client_key_env_variable": "CLIENT_KEY"}`, |
| 79 | + wantErr: `http.send: request option "tls_client_key_env_variable" is not allowed`, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "empty value is rejected", |
| 83 | + request: `{"method": "GET", ` + allowedURL + `, "tls_ca_cert_file": ""}`, |
| 84 | + wantErr: `http.send: request option "tls_ca_cert_file" is not allowed`, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "option key built at runtime", |
| 88 | + request: `object.union({"method": "GET", ` + allowedURL + `}, {concat("_", ["tls", "client", "key", "file"]): "/etc/ssl/key.pem"})`, |
| 89 | + wantErr: `http.send: request option "tls_client_key_file" is not allowed`, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "unix scheme with an allowed host", |
| 93 | + request: `{"method": "GET", "url": "unix://www.chainloop.dev/info?socket=%2Fvar%2Frun%2Fapp.sock"}`, |
| 94 | + wantErr: `http.send: url scheme "unix" is not allowed`, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "url without scheme", |
| 98 | + request: `{"method": "GET", "url": "www.chainloop.dev"}`, |
| 99 | + wantErr: `http.send: url scheme "" is not allowed`, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "upper case https scheme reaches http.send", |
| 103 | + request: `{"method": "GET", "url": "HTTPS://example.com"}`, |
| 104 | + wantErr: "http.send: disallowed host: example.com", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "request without restricted options reaches http.send", |
| 108 | + request: `{"method": "GET", "url": "https://example.com"}`, |
| 109 | + wantErr: "http.send: disallowed host: example.com", |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tc := range testCases { |
| 114 | + t.Run(tc.name, func(t *testing.T) { |
| 115 | + r := NewEngine() |
| 116 | + policy := &engine.Policy{ |
| 117 | + Name: "http-send", |
| 118 | + Source: fmt.Appendf(nil, httpSendPolicyTemplate, tc.request), |
| 119 | + } |
| 120 | + |
| 121 | + _, verifyErr := r.Verify(context.TODO(), policy, []byte(`{}`), nil) |
| 122 | + _, matchErr := r.MatchesParameters(context.TODO(), policy, nil, nil) |
| 123 | + |
| 124 | + for _, err := range []error{verifyErr, matchErr} { |
| 125 | + require.Error(t, err) |
| 126 | + assert.Contains(t, err.Error(), tc.wantErr) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestRego_HTTPSendPermissiveMode(t *testing.T) { |
| 133 | + // raise_error=false makes http.send return its own error in the response, |
| 134 | + // which shows that the request got past the restriction. |
| 135 | + const policySource = `package main |
| 136 | +
|
| 137 | +import rego.v1 |
| 138 | +
|
| 139 | +result := {"skipped": false, "violations": violations} |
| 140 | +
|
| 141 | +violations contains resp.error.message if { |
| 142 | + resp := http.send({ |
| 143 | + "method": "GET", |
| 144 | + "url": "unix://www.chainloop.dev/info?socket=%2Fnonexistent%2Fapp.sock", |
| 145 | + "tls_ca_cert_file": "/nonexistent/ca.pem", |
| 146 | + "raise_error": false, |
| 147 | + }) |
| 148 | +} |
| 149 | +` |
| 150 | + |
| 151 | + r := NewEngine(engine.WithOperatingMode(int32(EnvironmentModePermissive))) |
| 152 | + result, err := r.Verify(context.TODO(), &engine.Policy{Name: "http-send", Source: []byte(policySource)}, []byte(`{}`), nil) |
| 153 | + require.NoError(t, err) |
| 154 | + require.Len(t, result.Violations, 1) |
| 155 | + assert.Contains(t, result.Violations[0].Violation, "/nonexistent/ca.pem") |
| 156 | + assert.NotContains(t, result.Violations[0].Violation, "is not allowed") |
| 157 | +} |
0 commit comments