Skip to content

fix: guard GetCredentialFunc against nil store and fix Ingest error cleanup#1115

Closed
TerryHowe wants to merge 5 commits into
oras-project:mainfrom
TerryHowe:fix/remote-bug-fixes
Closed

fix: guard GetCredentialFunc against nil store and fix Ingest error cleanup#1115
TerryHowe wants to merge 5 commits into
oras-project:mainfrom
TerryHowe:fix/remote-bug-fixes

Conversation

@TerryHowe

@TerryHowe TerryHowe commented Mar 9, 2026

Copy link
Copy Markdown
Member

Summary

This PR fixes two bugs in the registry/remote package and adds missing test coverage.

GetCredentialFunc nil store panic (registry/remote/auth.go): When GetCredentialFunc was called with a nil store, it would panic at runtime when the returned function tried to call methods on the nil store. The fix adds a nil guard that returns a function yielding EmptyCredential with no error, matching the documented semantics.

Ingest temp file leak on error (registry/remote/internal/ioutil/ioutil.go): The Ingest function creates a temp file and uses a deferred cleanup to remove it on error. However, the error paths for Chmod and io.Copy previously used bare return "", err returns which bypass the named return value ingestErr, so the deferred cleanup never triggered and temp files were leaked. The fix switches both error paths to assign ingestErr and use a plain return, ensuring the defer always sees the error and removes the temp file.

Typo fix (registry/remote/url.go): Fixed missing space in the buildRepositoryBlobMountURL comment (buildRepositoryBlobMountURLbuildsbuildRepositoryBlobMountURL builds).

Test plan

  • Added TestGetCredentialFunc_NilStore to verify nil store returns EmptyCredential without error
  • Added comprehensive tests for Ingest: success, name prefix, invalid dir, reader error with cleanup, empty content, and chmod error with cleanup
  • Unit tests pass (make test)

Use named return assignment instead of explicit return values so the
deferred os.Remove(path) receives the correct path rather than empty
string when Chmod or io.Copy fails.

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
A nil credentials.Store passed to GetCredentialFunc would cause a deferred
panic inside the returned closure at authentication time, making it hard to
diagnose. Add an early nil check that returns a no-op CredentialFunc yielding
EmptyCredential when store is nil.

Add TestGetCredentialFunc_NilStore to cover this case.

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
@codecov

codecov Bot commented Mar 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.30%. Comparing base (296eb2d) to head (13890f9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1115      +/-   ##
==========================================
+ Coverage   83.05%   83.30%   +0.24%     
==========================================
  Files          81       81              
  Lines        5684     5689       +5     
==========================================
+ Hits         4721     4739      +18     
+ Misses        595      581      -14     
- Partials      368      369       +1     

☔ View full report in Codecov by Sentry.
📢 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.

@TerryHowe TerryHowe added the v3 Things belongs to version 3.x label Apr 1, 2026
Use a package-level var to make the chmod call mockable, enabling a test
that exercises the permission-failure branch and verifies temp file cleanup.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
@TerryHowe TerryHowe changed the title fix: remote package bug fixes and ioutil cleanup fix: guard GetCredentialFunc against nil store and fix Ingest error cleanup Apr 1, 2026
@TerryHowe

Copy link
Copy Markdown
Member Author

Splitting this into two focused PRs since the fixes are independent:

Closing in favor of those.

@TerryHowe TerryHowe closed this May 23, 2026
TerryHowe added a commit that referenced this pull request May 24, 2026
## Summary

A `nil` `credentials.Store` passed to `GetCredentialFunc` would cause a
deferred panic inside the returned closure at authentication time,
making it hard to diagnose. This PR adds an early nil check that returns
a no-op `CredentialFunc` yielding `EmptyCredential` when `store` is
`nil`, matching the documented semantics.

Also fixes a small typo in the `buildRepositoryBlobMountURL` doc comment
(missing space).

Split out from #1115 — the other half (Ingest temp-file cleanup) is in a
separate PR.

## Test plan
- [x] Added `TestGetCredentialFunc_NilStore` to verify nil store returns
`EmptyCredential` without error
- [x] Unit tests pass (`make test`)

---------

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
TerryHowe added a commit that referenced this pull request Jun 22, 2026
## Summary

`Ingest` in `registry/remote/internal/ioutil` creates a temp file and
uses a deferred cleanup to remove it on error. The error paths for
`Chmod` and `io.Copy` previously used bare `return "", err` returns,
which bypassed the named return value `ingestErr` — so the deferred
cleanup never triggered and temp files were leaked.

The fix switches both error paths to assign `ingestErr` and use a plain
`return`, ensuring the defer always sees the error and removes the temp
file.

A package-level `fileChmod` var is introduced so the chmod-failure
branch can be exercised in tests.

Split out from #1115 — the other half (GetCredentialFunc nil guard) is
in a separate PR.

## Test plan
- [x] Added comprehensive tests for `Ingest`: success, name prefix,
invalid dir, reader error with cleanup, empty content, and chmod error
with cleanup
- [x] Unit tests pass (`make test`)

---------

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v3 Things belongs to version 3.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant