Skip to content

Commit 8aaf5e8

Browse files
committed
Defer cached artifact validation to checkCache
Construct artifacts.Artifact from the row without validation so a malformed content hash reaches newIntegrityChecks in checkCache, which clears the row and treats the request as a miss. Erroring at the DB boundary instead surfaced a 500 and left the bad row in place.
1 parent f76f4e0 commit 8aaf5e8

2 files changed

Lines changed: 14 additions & 104 deletions

File tree

‎internal/database/database_test.go‎

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"database/sql"
55
"os"
66
"path/filepath"
7-
"strings"
87
"testing"
98
"time"
109
)
@@ -309,83 +308,6 @@ func TestGetCachedArtifact(t *testing.T) {
309308
})
310309
}
311310

312-
func TestCachedArtifactRowArtifact(t *testing.T) {
313-
validRow := cachedArtifactRow{
314-
Ecosystem: "npm",
315-
StoragePath: "npm/example/1.0.0/example.tgz",
316-
ContentHash: sql.NullString{String: testContentHash, Valid: true},
317-
Size: sql.NullInt64{Int64: 0, Valid: true},
318-
}
319-
tests := []struct {
320-
name string
321-
row cachedArtifactRow
322-
versionPURL string
323-
wantErr string
324-
}{
325-
{name: "zero byte", row: validRow, versionPURL: "pkg:npm/example@1.0.0"},
326-
{
327-
name: "missing hash",
328-
row: cachedArtifactRow{Size: sql.NullInt64{Int64: 1, Valid: true}},
329-
versionPURL: "pkg:npm/example@1.0.0",
330-
wantErr: "content hash is missing",
331-
},
332-
{
333-
name: "malformed hash",
334-
row: cachedArtifactRow{
335-
ContentHash: sql.NullString{String: "not-a-hash", Valid: true},
336-
Size: sql.NullInt64{Int64: 1, Valid: true},
337-
},
338-
versionPURL: "pkg:npm/example@1.0.0",
339-
wantErr: "digest",
340-
},
341-
{
342-
name: "missing size",
343-
row: cachedArtifactRow{
344-
ContentHash: sql.NullString{String: testContentHash, Valid: true},
345-
},
346-
versionPURL: "pkg:npm/example@1.0.0",
347-
wantErr: "size is missing",
348-
},
349-
{
350-
name: "negative size",
351-
row: cachedArtifactRow{
352-
ContentHash: sql.NullString{String: testContentHash, Valid: true},
353-
Size: sql.NullInt64{Int64: -1, Valid: true},
354-
},
355-
versionPURL: "pkg:npm/example@1.0.0",
356-
wantErr: "size",
357-
},
358-
{name: "malformed PURL", row: validRow, versionPURL: "not-a-purl", wantErr: "PURL"},
359-
}
360-
361-
for _, test := range tests {
362-
t.Run(test.name, func(t *testing.T) {
363-
cached, err := test.row.artifact(test.versionPURL, "example.tgz")
364-
if test.wantErr != "" {
365-
if err == nil {
366-
t.Fatal("artifact() error = nil")
367-
}
368-
if !strings.Contains(err.Error(), test.wantErr) {
369-
t.Errorf("error = %q, want %q", err, test.wantErr)
370-
}
371-
if !strings.Contains(err.Error(), test.versionPURL) {
372-
t.Errorf("error = %q, want version PURL %q", err, test.versionPURL)
373-
}
374-
if !strings.Contains(err.Error(), "example.tgz") {
375-
t.Errorf("error = %q, want filename", err)
376-
}
377-
return
378-
}
379-
if err != nil {
380-
t.Fatalf("artifact() error = %v", err)
381-
}
382-
if err := cached.Artifact.Validate(); err != nil {
383-
t.Errorf("Artifact.Validate() error = %v", err)
384-
}
385-
})
386-
}
387-
}
388-
389311
func seedCachedArtifactTestData(t *testing.T, db *DB, packagePURL, versionPURL, filename string) {
390312
t.Helper()
391313

‎internal/database/queries.go‎

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (db *DB) GetCachedArtifact(packagePURL, versionPURL, filename string) (*Cac
245245
if err != nil {
246246
return nil, err
247247
}
248-
return row.artifact(versionPURL, filename)
248+
return row.artifact(versionPURL, filename), nil
249249
}
250250

251251
type cachedArtifactRow struct {
@@ -257,35 +257,23 @@ type cachedArtifactRow struct {
257257
Integrity sql.NullString `db:"integrity"`
258258
}
259259

260-
func (row cachedArtifactRow) artifact(versionPURL, filename string) (*CachedArtifact, error) {
261-
if !row.ContentHash.Valid || row.ContentHash.String == "" {
262-
return nil, fmt.Errorf("cached artifact for %q, filename %q: content hash is missing", versionPURL, filename)
263-
}
264-
if !row.Size.Valid {
265-
return nil, fmt.Errorf("cached artifact for %q, filename %q: size is missing", versionPURL, filename)
266-
}
267-
268-
mediaType := ""
269-
if row.ContentType.Valid {
270-
mediaType = row.ContentType.String
271-
}
272-
sharedArtifact, err := artifacts.New(
273-
versionPURL,
274-
digest.Digest("sha256:"+row.ContentHash.String),
275-
row.Size.Int64,
276-
filename,
277-
mediaType,
278-
)
279-
if err != nil {
280-
return nil, fmt.Errorf("invalid cached artifact for %q, filename %q: %w", versionPURL, filename, err)
281-
}
282-
260+
// artifact converts a cached artifact row to a CachedArtifact without
261+
// validation. A malformed hash or integrity value is handled by
262+
// checkCache, which clears the record and treats the request as a cache
263+
// miss so the client is served a fresh fetch instead of an error.
264+
func (row cachedArtifactRow) artifact(versionPURL, filename string) *CachedArtifact {
283265
return &CachedArtifact{
284266
Ecosystem: row.Ecosystem,
285267
StoragePath: row.StoragePath,
286-
Artifact: sharedArtifact,
287268
Integrity: row.Integrity,
288-
}, nil
269+
Artifact: artifacts.Artifact{
270+
PURL: versionPURL,
271+
Digest: digest.Digest("sha256:" + row.ContentHash.String),
272+
Size: row.Size.Int64,
273+
Filename: filename,
274+
MediaType: row.ContentType.String,
275+
},
276+
}
289277
}
290278

291279
func (db *DB) GetArtifactByPath(storagePath string) (*Artifact, error) {

0 commit comments

Comments
 (0)