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
2 changes: 2 additions & 0 deletions pkg/loop/cmd/loopinstall/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ plugins:

The `installPath` is relative to the root of the downloaded module but it can also be an absolute path like: `github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/cmd/chainlink-cosmos`. The absolute path will be stripped to the relative path (it is supported for backwards compatibility).

By default the installed binary is named after the base of `installPath` (or of `moduleURI` when `installPath` is the module root). When the package that builds the binary is not named after the binary itself, set `binaryName` to override the installed name; the build target stays whatever `installPath` points at.

The `libs` field is an array of strings representing directory paths, which can include glob patterns for library files that need to be included with the plugin. Docker build will use these paths to copy the libraries into the final container image.

## Private Repository Access
Expand Down
7 changes: 7 additions & 0 deletions pkg/loop/cmd/loopinstall/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ func downloadAndInstallPlugin(pluginType string, pluginIdx int, plugin PluginDef
binaryName = filepath.Base(filepath.Clean(moduleURI))
}

// An explicit binaryName decouples the package that builds the binary from
// the name it is installed under.
if plugin.BinaryName != "" {
binaryName = plugin.BinaryName
}

// Determine output directory (GOBIN, or GOPATH/bin, or $HOME/go/bin).
outputDir := os.Getenv("GOBIN")
if outputDir == "" {
Expand Down Expand Up @@ -337,6 +343,7 @@ func writeBuildManifest(tasks []PluginInstallTask, outputFile string) error {
ModuleURI: task.Plugin.ModuleURI,
GitRef: task.Plugin.GitRef,
InstallPath: task.Plugin.InstallPath,
BinaryName: task.Plugin.BinaryName,
}

if len(task.Plugin.Libs) > 0 {
Expand Down
59 changes: 59 additions & 0 deletions pkg/loop/cmd/loopinstall/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,23 @@ func TestValidationFunctions(t *testing.T) {
"github.com/example/module\nls",
},
},
{
name: "validateBinaryName",
validator: validateBinaryName,
valid: []string{
"cron",
"cronv2",
"chainlink-evm",
"tool.v2",
},
invalid: []string{
"cron/../../etc",
"../escape",
"cron;rm -rf /",
"cron\nls",
"",
},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -727,6 +744,48 @@ func TestDownloadAndInstallPlugin(t *testing.T) {
},
expectError: false,
},
{
name: "binary name override",
plugin: PluginDef{
ModuleURI: "github.com/example/capabilities/scheduler/cron",
GitRef: "v1.0.0",
InstallPath: "./legacy", // Package dir is not named after the binary
BinaryName: "cron",
},
defaults: DefaultsConfig{},
mockDownload: func(cmd *exec.Cmd) error {
if stdout, ok := cmd.Stdout.(*bytes.Buffer); ok {
parts := strings.Split("github.com/example/capabilities/scheduler/cron", "/")
moduleDir := filepath.Join(append([]string{tempDir, "modules"}, parts...)...)
fmt.Fprintf(stdout, `{"Dir":"%s"}`, moduleDir)
}
return nil
},
mockBuild: func(cmd *exec.Cmd) error {
if len(cmd.Args) < 2 {
return fmt.Errorf("install command has too few arguments: %v", cmd.Args)
}
// args: build -o <outputPath> [flags...] <installArg>
outIdx := -1
for i, a := range cmd.Args {
if a == "-o" {
outIdx = i
break
}
}
if outIdx == -1 || outIdx+1 >= len(cmd.Args) {
return fmt.Errorf("expected -o <path> in install command: %v", cmd.Args)
}
if base := filepath.Base(cmd.Args[outIdx+1]); base != "cron" {
return fmt.Errorf("expected output binary %q, got %q", "cron", base)
}
if packageArg := cmd.Args[len(cmd.Args)-1]; packageArg != "./legacy" {
return fmt.Errorf("expected install package %q, got %q", "./legacy", packageArg)
}
return nil
},
expectError: false,
},
}

for _, tc := range tests {
Expand Down
7 changes: 7 additions & 0 deletions pkg/loop/cmd/loopinstall/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type PluginDef struct {
Libs []string `yaml:"libs"`
Flags string `yaml:"flags,omitempty"`
EnvVars []string `yaml:"envvars,omitempty"`
// BinaryName overrides the name of the installed binary. By default the
// name is the base of installPath (or of moduleURI when installPath is the
// module root), which breaks when the package that builds the binary is
// not named after the binary itself (e.g. "./legacy" building the binary
// a node looks up as "cron").
BinaryName string `yaml:"binaryName,omitempty"`
}

// ModDownloadResult represents the JSON directory (dir) output from 'go mod download -json'
Expand Down Expand Up @@ -55,4 +61,5 @@ type PluginManifest struct {
GitRef string `json:"gitRef"`
InstallPath string `json:"installPath"`
Libs []string `json:"libs,omitempty"`
BinaryName string `json:"binaryName,omitempty"`
}
9 changes: 9 additions & 0 deletions pkg/loop/cmd/loopinstall/plugins.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ plugins:
gitRef: "9a780650af4708e4bd9b75495feff2c5b4054e46"
installPath: "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/cmd/chainlink-starknet"
libs: []

legacy-cron:
# Example of a binary name override: the package that builds the binary is
# not named after it, so the installed name is set explicitly.
- moduleURI: "github.com/smartcontractkit/capabilities/scheduler/cron"
gitRef: "9777ffcac0b966d2488cb1e63e155da8b98bd8d6"
installPath: "./legacy"
binaryName: "cron"
flags: "-tags timetzdata"
14 changes: 14 additions & 0 deletions pkg/loop/cmd/loopinstall/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func (plugin PluginDef) Validate() error {
if err := validateInstallPath(plugin.InstallPath); err != nil {
return err
}
if plugin.BinaryName != "" {
if err := validateBinaryName(plugin.BinaryName); err != nil {
return err
}
}

return nil
}
Expand Down Expand Up @@ -49,6 +54,15 @@ func validateInstallPath(path string) error {
return nil
}

// validateBinaryName ensures an overridden binary name is a plain file name,
// so it cannot escape the output directory or smuggle path separators.
func validateBinaryName(name string) error {
if !regexp.MustCompile(`^[\w.-]+$`).MatchString(name) {
return fmt.Errorf("invalid binary name: %s", name)
}
return nil
}

// validateGoFlags ensures flags are safe and prevents command injection
func validateGoFlags(flags string) error {
// Check for potentially dangerous characters or substrings that could enable command injection
Expand Down
Loading