knative / knative/func

ImageNameWithDigest produces invalid OCI reference when Build.Image is empty

Open Beginner friendly
#3,902 1 comment 0 reactions 0 assignees View on GitHub
lifecycle/stale
Dominant language
Go
Stars
365
Forks
223
Avg merge
2d 3h
Merged PRs (30d)
25

Description

## Description
`ImageNameWithDigest` in `pkg/functions/function.go` does not validate that `f.Build.Image` is non-empty before processing. When called with a non-empty `newDigest` but an empty image name, it produces the malformed OCI reference `@sha256:...` (missing the image name prefix).

## Steps to Reproduce
This occurs when `Push` returns a digest but the function's `Build.Image` was never set (e.g., if the build process was interrupted or the function was loaded from a corrupt state).

```go
f := fn.Function{Build: fn.BuildSpec{Image: ""}}
result := f.ImageNameWithDigest("sha256:abc123def...")
fmt.Println(result) // "@sha256:abc123def..." -- invalid
```

## Expected Behavior
The function should return an empty string when `Build.Image` is empty, rather than producing a malformed reference.

## Actual Behavior
Returns `@sha256:abc123...` which fails `name.ParseReference` downstream with a confusing "could not parse reference" error.

## Relevant Code
```go
// pkg/functions/function.go:860-880
func (f Function) ImageNameWithDigest(newDigest string) string {
if newDigest == "" {
return f.Build.Image
}
image := f.Build.Image // no empty check!
// ...
lastSlashIdx := strings.LastIndexAny(image, "/") // -1 for empty string
// produces "@sha256:..." with no image name prefix
}
```

## Suggested Fix
Add an early return for empty image:
```go
image := f.Build.Image
if image == "" {
return ""
}
```

Contributor guide

Open the contributing guide

Research direction

Start in pkg/functions/function.go:860-880 at Function.ImageNameWithDigest and review how an empty Build.Image reaches reference construction. Verify that an empty image with a non-empty digest produces an empty result, while the existing empty-digest behavior remains unchanged; run the relevant package tests and add a focused regression test if the package test layout supports it.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.