Repository navigation
envsec: fix P0 secrets-hygiene bugs (name validation, file perms, --format) #614
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8e9bf9e
envsec: fix P0 secrets-hygiene bugs (name validation, file perms, --f…
claude 88bbfc8
specs/bug-scan: mark P0 #1, #2, #3, #6 as fixed
claude 46b89b1
filecache: check os.Remove error in deferred cleanup (errcheck)
claude 71c95ad
envsec, filecache: address review findings on permissions and truncation
claude 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,19 @@ | ||
| package envcli | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "go.jetify.com/envsec/pkg/envsec" | ||
| ) | ||
|
|
||
| // The --format flag's default must be a value that ValidateFormat accepts, | ||
| // otherwise a plain `envsec download .env` always fails. | ||
| func TestDownloadFormatDefaultIsValid(t *testing.T) { | ||
| flag := DownloadCmd().Flags().Lookup("format") | ||
| if flag == nil { | ||
| t.Fatal("download command has no --format flag") | ||
| } | ||
| if err := envsec.ValidateFormat(flag.DefValue); err != nil { | ||
| t.Errorf("default --format %q is rejected: %v", flag.DefValue, 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
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,45 @@ | ||
| package envsec | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestWriteSecretFilePermissions(t *testing.T) { | ||
| path := filepath.Join(t.TempDir(), ".env") | ||
|
|
||
| // An existing world-readable file should be tightened. | ||
| if err := os.WriteFile(path, []byte("OLD=1\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := writeSecretFile(path, []byte("NEW=1\n")); err != nil { | ||
| t.Fatalf("writeSecretFile() error = %v", err) | ||
| } | ||
|
|
||
| info, err := os.Stat(path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if perm := info.Mode().Perm(); perm != 0o600 { | ||
| t.Errorf("perm = %o, want 600", perm) | ||
| } | ||
| got, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != "NEW=1\n" { | ||
| t.Errorf("contents = %q, want %q", got, "NEW=1\n") | ||
| } | ||
| } | ||
|
|
||
| func TestValidateFormat(t *testing.T) { | ||
| for _, f := range []string{"", "json", "dotenv"} { | ||
| if err := ValidateFormat(f); err != nil { | ||
| t.Errorf("ValidateFormat(%q) = %v, want nil", f, err) | ||
| } | ||
| } | ||
| if err := ValidateFormat("yaml"); err == nil { | ||
| t.Error("ValidateFormat(\"yaml\") = nil, want error") | ||
| } | ||
| } | ||
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,28 @@ | ||
| package envsec | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestEnsureValidNames(t *testing.T) { | ||
| valid := []string{"A", "_A", "abc_123", "FOO_BAR"} | ||
| for _, name := range valid { | ||
| if err := ensureValidNames([]string{name}); err != nil { | ||
| t.Errorf("ensureValidNames(%q) = %v, want nil", name, err) | ||
| } | ||
| } | ||
|
|
||
| invalid := []string{ | ||
| "", | ||
| "1A", | ||
| "A/../../other-project/prod/KEY", | ||
| "PATH=/evil:/bin", | ||
| "A B", | ||
| "A-B", | ||
| "A\n", | ||
| "jetpack_foo", | ||
| } | ||
| for _, name := range invalid { | ||
| if err := ensureValidNames([]string{name}); err == nil { | ||
| t.Errorf("ensureValidNames(%q) = nil, want error", name) | ||
| } | ||
| } | ||
| } |
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
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,36 @@ | ||
| package filecache | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // writeFile atomically writes data to path with 0600 permissions. Cached | ||
| // values may contain credentials, so they must not be readable by other | ||
| // users. Writing to a temp file and renaming also replaces any existing file | ||
| // (and its possibly looser permissions) and avoids leaving a truncated file | ||
| // behind if the process dies mid-write. | ||
| func writeFile(path string, data []byte) error { | ||
| tmp, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path)+".tmp-*") | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
| tmpName := tmp.Name() | ||
| defer func() { _ = os.Remove(tmpName) }() // no-op after a successful rename | ||
|
|
||
| // CreateTemp already uses 0600, but be explicit. | ||
| if err := tmp.Chmod(0o600); err != nil { | ||
| _ = tmp.Close() | ||
| return errors.WithStack(err) | ||
| } | ||
| if _, err := tmp.Write(data); err != nil { | ||
| _ = tmp.Close() | ||
| return errors.WithStack(err) | ||
| } | ||
| if err := tmp.Close(); err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
| return errors.WithStack(os.Rename(tmpName, path)) | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.