ImageNameWithDigest produces invalid OCI reference when Build.Image is empty

Open Beginner friendly
#3,902 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

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.

Description

lifecycle/stale

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).

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

// 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:

image := f.Build.Image
if image == "" {
    return ""
}
Dominant language
Go
Stars
365
Forks
223
Avg merge
2d 3h
Merged PRs (30d)
25

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from knative/func

All issues in knative/func

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.