Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions pkg/policies/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,27 @@ func decodeIfBase64Wasm(content []byte) []byte {
return content
}

// isURLPath checks if a path is an HTTP or HTTPS URL
func isURLPath(path string) bool {
scheme, _ := RefParts(path)
return scheme == httpScheme || scheme == httpsScheme
}

func loadPolicyScript(spec *v1.PolicySpecV2, basePath string) ([]byte, error) {
var content []byte
var err error
switch source := spec.GetSource().(type) {
case *v1.PolicySpecV2_Embedded:
content = []byte(source.Embedded)
case *v1.PolicySpecV2_Path:
// path relative to policy folder
scriptPath := filepath.Join(filepath.Dir(basePath), source.Path)
var scriptPath string
// If the path is a URL, use it directly. Otherwise, resolve it relative to basePath
if isURLPath(source.Path) {
scriptPath = source.Path
} else {
// path relative to policy folder
scriptPath = filepath.Join(filepath.Dir(basePath), source.Path)
}
content, err = blob.LoadFileOrURL(scriptPath)
if err != nil {
return nil, fmt.Errorf("loading policy content: %w", err)
Expand All @@ -747,8 +759,14 @@ func loadLegacyPolicyScript(spec *v1.PolicySpec, basePath string) ([]byte, error
case *v1.PolicySpec_Embedded:
content = []byte(source.Embedded)
case *v1.PolicySpec_Path:
// path relative to policy folder
scriptPath := filepath.Join(filepath.Dir(basePath), source.Path)
var scriptPath string
// If the path is a URL, use it directly. Otherwise, resolve it relative to basePath
if isURLPath(source.Path) {
scriptPath = source.Path
} else {
// path relative to policy folder
scriptPath = filepath.Join(filepath.Dir(basePath), source.Path)
}
content, err = blob.LoadFileOrURL(scriptPath)
if err != nil {
return nil, fmt.Errorf("loading policy content: %w", err)
Expand Down
53 changes: 52 additions & 1 deletion pkg/policies/policies_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 The Chainloop Authors.
// Copyright 2024-2025 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1248,3 +1248,54 @@ func loadStatement(file string, s *suite.Suite) *intoto.Statement {

return &statement
}

func (s *testSuite) TestIsURLPath() {
cases := []struct {
name string
path string
expected bool
}{
{
name: "http URL",
path: "http://example.com/policy.rego",
expected: true,
},
{
name: "https URL",
path: "https://example.com/policy.rego",
expected: true,
},
{
name: "relative file path",
path: "policy.rego",
expected: false,
},
{
name: "absolute file path",
path: "/absolute/path/policy.rego",
expected: false,
},
{
name: "file scheme",
path: "file:///path/to/policy.rego",
expected: false,
},
{
name: "chainloop scheme",
path: "chainloop://provider/policy",
expected: false,
},
{
name: "empty path",
path: "",
expected: false,
},
}

for _, tc := range cases {
s.Run(tc.name, func() {
result := isURLPath(tc.path)
s.Equal(tc.expected, result)
})
}
}
Loading