Skip to content

fix: guard against empty Build.Image in ImageNameWithDigest#3905

Open
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/image-name-with-digest-empty-guard
Open

fix: guard against empty Build.Image in ImageNameWithDigest#3905
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/image-name-with-digest-empty-guard

Conversation

@Elvand-Lie

@Elvand-Lie Elvand-Lie commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Description

ImageNameWithDigest in pkg/functions/function.go does not validate that 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), causing confusing downstream name.ParseReference errors.

Added early return for empty image.

Changes

  • pkg/functions/function.go, line 866: added if image == "" { return "" } guard

Reproduction

f := fn.Function{Build: fn.BuildSpec{Image: ""}}
result := f.ImageNameWithDigest("sha256:abc123")
// Before fix: "@sha256:abc123" (invalid OCI reference)
// After fix:  "" (safe empty string)

Verification

  • go vet ./pkg/functions: clean
  • go test ./pkg/functions/ -run TestFunction_ImageWithDigest -count=1: all 5 tests pass

Fixes #3902

ImageNameWithDigest did not validate that Build.Image is non-empty.
When called with a non-empty digest but empty image name, it produced
the malformed OCI reference @sha256:... (missing image name prefix),
causing confusing downstream parse errors in name.ParseReference.

Added early return for empty image.

Fixes knative#3902
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos June 17, 2026 20:50
@knative-prow

knative-prow Bot commented Jun 17, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Elvand-Lie
Once this PR has been reviewed and has the lgtm label, please assign matejvasek for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added size/XS 🤖 PR changes 0-9 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels Jun 17, 2026
@knative-prow

knative-prow Bot commented Jun 17, 2026

Copy link
Copy Markdown

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@Elvand-Lie

Copy link
Copy Markdown
Contributor Author

Verification Evidence

1. go vet: clean

$ go vet ./pkg/functions
# (no output, clean)

2. Before vs After

f := fn.Function{Build: fn.BuildSpec{Image: ""}}

// BEFORE fix:
f.ImageNameWithDigest("sha256:abc123")
// produces "@sha256:abc123", which is an INVALID OCI reference (no image name before @)
// Downstream: name.ParseReference("@sha256:abc123") fails with "could not parse reference"

// AFTER fix:
f.ImageNameWithDigest("sha256:abc123")
// produces "", a safe empty string that callers can check and handle gracefully

3. Code path trace showing how you hit this bug

client.go:Push()
  calls pusher.Push(), which returns imageDigest
  then client.go:1275 sets f.Build.Image = f.ImageNameWithDigest(imageDigest)
    if Build.Image was "" (interrupted build, corrupt func.yaml),
    this produces "@sha256:..." which fails all subsequent operations

4. Existing test suite, all pass

$ go test ./pkg/functions/ -run TestFunction_ImageWithDigest -count=1 -v
=== RUN   TestFunction_ImageWithDigest
=== RUN   TestFunction_ImageWithDigest/Full_path_with_port
=== RUN   TestFunction_ImageWithDigest/Path_with_namespace
=== RUN   TestFunction_ImageWithDigest/Just_image_name
=== RUN   TestFunction_ImageWithDigest/Full_path_with_port_and_SHA256_Digest
=== RUN   TestFunction_ImageWithDigest/Full_path_with_port_and_SHA256_Digest_with_empty_ImageDigest
--- PASS: TestFunction_ImageWithDigest (0.00s)
PASS
ok  	knative.dev/func/pkg/functions	0.394s

5. The fix (3 lines)

 image := f.Build.Image
+if image == "" {
+    return ""
+}

Minimal and defensive. No behavior change for any non-empty image path.

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.68%. Comparing base (f72ecda) to head (03daf3f).

Files with missing lines Patch % Lines
pkg/functions/function.go 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3905      +/-   ##
==========================================
- Coverage   53.93%   51.68%   -2.25%     
==========================================
  Files         200      200              
  Lines       23681    23682       +1     
==========================================
- Hits        12772    12240     -532     
- Misses       9674    10301     +627     
+ Partials     1235     1141      -94     
Flag Coverage Δ
e2e 33.42% <0.00%> (-0.01%) ⬇️
e2e go 29.38% <0.00%> (-0.01%) ⬇️
e2e node 25.70% <0.00%> (-0.01%) ⬇️
e2e python 29.71% <0.00%> (-0.01%) ⬇️
e2e quarkus 25.83% <0.00%> (-0.01%) ⬇️
e2e rust 25.30% <0.00%> (-0.01%) ⬇️
e2e springboot ?
e2e typescript 25.82% <0.00%> (-0.01%) ⬇️
e2e-config-ci 26.90% <0.00%> (+<0.01%) ⬆️
integration ?
unit macos-14 43.04% <0.00%> (-0.01%) ⬇️
unit macos-latest 43.04% <0.00%> (-0.01%) ⬇️
unit ubuntu-24.04-arm 43.34% <0.00%> (-0.02%) ⬇️
unit ubuntu-latest 43.89% <0.00%> (-0.01%) ⬇️
unit windows-latest 43.10% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/XS 🤖 PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageNameWithDigest produces invalid OCI reference when Build.Image is empty

1 participant