forked from open-policy-agent/conftest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent policy file overwrite on downloads (open-policy-agent#1039)
File existence check before downloading policies. Errors out and no overwrites. Maintains data integrity by preventing accidental policy overwrites. Added a test which verified the behaviour. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
- Loading branch information
1 parent
5b3e926
commit 163bdd8
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,62 @@ | ||
package downloader | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDownloadFailsWhenFileExists(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
// Create a file that would conflict with the download | ||
existingFile := filepath.Join(tmpDir, "policy.rego") | ||
if err := os.WriteFile(existingFile, []byte("existing content"), os.FileMode(0600)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Start a test HTTP server on an ephemeral port | ||
listener, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer listener.Close() | ||
|
||
server := &http.Server{ | ||
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
fmt.Fprint(w, "new content") | ||
}), | ||
ReadHeaderTimeout: 1 * time.Second, | ||
} | ||
errCh := make(chan error, 1) | ||
go func() { | ||
errCh <- server.Serve(listener) | ||
}() | ||
defer server.Close() | ||
|
||
// Try to download a policy file with the same name | ||
urls := []string{fmt.Sprintf("http://%s/policy.rego", listener.Addr().String())} | ||
downloadErr := Download(context.Background(), tmpDir, urls) | ||
|
||
// Verify that download fails with the expected error | ||
if downloadErr == nil { | ||
t.Error("Expected download to fail when file exists, but it succeeded") | ||
} | ||
if downloadErr != nil && !filepath.IsAbs(existingFile) { | ||
t.Errorf("Expected error message to contain absolute path, got: %v", downloadErr) | ||
} | ||
|
||
// Verify the original file is unchanged | ||
content, err := os.ReadFile(existingFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(content) != "existing content" { | ||
t.Error("Existing file was modified") | ||
} | ||
} |