-
Notifications
You must be signed in to change notification settings - Fork 353
HCL: add a file() function #2642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/usr/bin/env docker agent run | ||
|
|
||
| agent "root" { | ||
| description = "Agent that loads its system prompt from a separate file" | ||
| model = "auto" | ||
|
|
||
| # The file() helper reads a text file and injects its contents here. | ||
| # Relative paths are resolved from this HCL file's directory. | ||
| instruction = file("instructions_from_file.md") | ||
|
|
||
| welcome_message = "Hi! My instructions were loaded from instructions_from_file.md" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| You are a helpful assistant. | ||
|
|
||
| When you answer: | ||
| - be concise | ||
| - prefer bullet points when listing steps | ||
| - ask for clarification only if necessary | ||
| - mention when you are making an assumption |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package hcl | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/zclconf/go-cty/cty" | ||
| "github.com/zclconf/go-cty/cty/function" | ||
| ) | ||
|
|
||
| func fileFunction(baseDir string) function.Function { | ||
| return function.New(&function.Spec{ | ||
| Params: []function.Parameter{{ | ||
| Name: "path", | ||
| Type: cty.String, | ||
| }}, | ||
| Type: function.StaticReturnType(cty.String), | ||
| Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { | ||
| path := args[0].AsString() | ||
|
|
||
| data, readPath, err := readFileForHCL(path, baseDir) | ||
| if err != nil { | ||
| return cty.NilVal, fmt.Errorf("reading file %q: %w", readPath, err) | ||
| } | ||
| return cty.StringVal(string(data)), nil | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func readFileForHCL(path, baseDir string) ([]byte, string, error) { | ||
| if baseDir == "" { | ||
| data, err := os.ReadFile(path) | ||
| return data, path, err | ||
| } | ||
| if !filepath.IsLocal(path) { | ||
| return nil, path, errors.New("path must be a local relative path inside the config directory") | ||
| } | ||
|
|
||
| root, err := os.OpenRoot(baseDir) | ||
| if err != nil { | ||
| return nil, baseDir, fmt.Errorf("opening config directory %q: %w", baseDir, err) | ||
| } | ||
| defer root.Close() | ||
|
|
||
| data, err := root.ReadFile(filepath.ToSlash(path)) | ||
| return data, filepath.Join(baseDir, path), err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package hcl | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/goccy/go-yaml" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestToYAML_FileFunction(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| instructionsPath := filepath.Join(dir, "instructions.txt") | ||
| require.NoError(t, os.WriteFile(instructionsPath, []byte("Line 1\nLine 2\n"), 0o644)) | ||
|
|
||
| src := []byte(` | ||
| agent "root" { | ||
| instruction = file("instructions.txt") | ||
| model = "auto" | ||
| } | ||
| `) | ||
|
|
||
| m, err := ToMap(src, filepath.Join(dir, "agent.hcl")) | ||
| require.NoError(t, err) | ||
|
|
||
| items := m["agents"].(yaml.MapSlice) | ||
| root := items[0].Value.(map[string]any) | ||
| assert.Equal(t, "Line 1\nLine 2\n", root["instruction"]) | ||
| } | ||
|
|
||
| func TestToYAML_FileFunctionMissingFile(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| src := []byte(` | ||
| agent "root" { | ||
| instruction = file("missing.txt") | ||
| model = "auto" | ||
| } | ||
| `) | ||
|
|
||
| _, err := ToMap(src, filepath.Join(dir, "agent.hcl")) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "reading file") | ||
| assert.Contains(t, err.Error(), "missing.txt") | ||
| } | ||
|
|
||
| func TestToYAML_FileFunctionRejectsTraversal(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| parent := t.TempDir() | ||
| dir := filepath.Join(parent, "config") | ||
| require.NoError(t, os.Mkdir(dir, 0o755)) | ||
| secret := filepath.Join(parent, "secret.txt") | ||
| require.NoError(t, os.WriteFile(secret, []byte("nope"), 0o644)) | ||
|
|
||
| src := []byte(` | ||
| agent "root" { | ||
| instruction = file("../secret.txt") | ||
| model = "auto" | ||
| } | ||
| `) | ||
|
|
||
| _, err := ToMap(src, filepath.Join(dir, "agent.hcl")) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "reading file") | ||
| assert.Contains(t, err.Error(), "../secret.txt") | ||
| assert.Contains(t, err.Error(), "local relative path") | ||
| } | ||
|
|
||
| func TestToYAML_FileFunctionRejectsSymlinkEscape(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| parent := t.TempDir() | ||
| dir := filepath.Join(parent, "config") | ||
| require.NoError(t, os.Mkdir(dir, 0o755)) | ||
|
|
||
| outside := filepath.Join(parent, "outside.txt") | ||
| require.NoError(t, os.WriteFile(outside, []byte("secret"), 0o644)) | ||
|
|
||
| link := filepath.Join(dir, "instructions.txt") | ||
| if err := os.Symlink("../outside.txt", link); err != nil { | ||
| t.Skipf("symlink not supported: %v", err) | ||
| } | ||
|
|
||
| src := []byte(` | ||
| agent "root" { | ||
| instruction = file("instructions.txt") | ||
| model = "auto" | ||
| } | ||
| `) | ||
|
|
||
| _, err := ToMap(src, filepath.Join(dir, "agent.hcl")) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "reading file") | ||
| assert.Contains(t, err.Error(), filepath.Join(dir, "instructions.txt")) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MEDIUM]
file()silently resolves paths relative to CWD whenfilenameis empty or has no directory componentTwo cases:
ToMap(src, "")→baseDir("")returns""→ thebaseDir != ""guard fails →os.ReadFileresolves relative paths against the process working directory.ToMap(src, "agent.hcl")→filepath.Dir("agent.hcl")returns"."→filepath.Join(".", path)still resolves against CWD.In both cases,
file("secret.txt")reads from wherever the binary was invoked, not from a stable, expected location. Consider requiringfilenameto be an absolute path, or documenting the CWD-relative behaviour explicitly.