Preserve artifact file identity and verify hashes

This commit is contained in:
2026-11-25 17:26:31 +00:00
parent fb6c1d3435
commit 3f5b2958cb
3 changed files with 29 additions and 3 deletions

View File

@@ -68,6 +68,9 @@ model-presence tests retain their behavior. No precision-conversion layer is add
## Stage 2: Preserve File Identity And Independently Check Hashes
**Status: complete.** Whitespace-sensitive file identities and fixed SHA-256
expectations are covered; the identity test failed with URI trimming in place.
**Findings:** 3 and 9.
**Files:** `internal/adapter/http/artifact_reader.go` and
`artifact_reader_test.go`.

View File

@@ -98,7 +98,7 @@ func (r *restrictedArtifactReader) readFileArtifact(ref promptkit.ArtifactRef) (
// resolveLexicalPath checks cleaned path containment without resolving symlinks.
func (r *restrictedArtifactReader) resolveLexicalPath(rawPath string) (string, error) {
cleanPath := filepath.Clean(strings.TrimSpace(rawPath))
cleanPath := filepath.Clean(rawPath)
candidate := cleanPath
if !filepath.IsAbs(cleanPath) {
candidate = filepath.Join(r.root, cleanPath)

View File

@@ -7,6 +7,7 @@ import (
"mime"
"os"
"path/filepath"
"runtime"
"testing"
"go.maximumdirect.net/promptkit"
@@ -82,11 +83,33 @@ func TestRestrictedArtifactReaderReadsContainedFiles(t *testing.T) {
if artifact.ContentType != expectedContentType {
t.Fatalf("unexpected artifact content type: got %q, want %q", artifact.ContentType, expectedContentType)
}
if artifact.Hash != artifactHash([]byte("allowed")) {
if artifact.Hash != "eabc01f12ec3e7cb6db0ada0f8f37323b0cfe6d08a2a73479e7d5b62d7e63529" {
t.Fatalf("unexpected artifact hash: %q", artifact.Hash)
}
}
files := []struct{ name, body, hash string }{
{"file.txt", "plain", "a116c9ed46d6207734a43317d30fd88f52ac8634c37d904bbf4e41d865f90475"},
{" file.txt", "padded", "c7f9b538b93ce513f654b8d199e50252ae037c5bde542c132b04a42cd8b92ea0"},
}
if runtime.GOOS != "windows" {
files = append(files, struct{ name, body, hash string }{"file.txt ", "padded", "c7f9b538b93ce513f654b8d199e50252ae037c5bde542c132b04a42cd8b92ea0"})
}
for _, file := range files {
if err := os.WriteFile(filepath.Join(root, file.name), []byte(file.body), 0o644); err != nil {
t.Fatal(err)
}
}
for _, file := range files {
artifact, err := reader.Read(context.Background(), promptkit.File(file.name))
if err != nil {
t.Fatalf("read %q: %v", file.name, err)
}
if artifact.Name != file.name || artifact.URI != filepath.Join(root, file.name) || artifact.Size != int64(len(file.body)) || string(artifact.Body) != file.body || artifact.Hash != file.hash {
t.Errorf("incorrect identity for %q: %#v", file.name, artifact)
}
}
artifact, err := reader.Read(context.Background(), promptkit.File("input.unknown"))
if err != nil {
t.Fatalf("read unknown-extension path: %v", err)
@@ -140,7 +163,7 @@ func TestRestrictedArtifactReaderWithoutRootDeniesFiles(t *testing.T) {
if err != nil {
t.Fatalf("read inline artifact: %v", err)
}
if artifact.ContentType != fallbackArtifactContentType || string(artifact.Body) != "inline" || artifact.Hash != artifactHash([]byte("inline")) {
if artifact.ContentType != fallbackArtifactContentType || string(artifact.Body) != "inline" || artifact.Hash != "995cf20a9c45daaf0a2cc31e85c290032ced97aadbac6c9d625595f5ce0ed427" {
t.Fatalf("unexpected inline artifact: %#v", artifact)
}