Skip to content

Commit 508db95

Browse files
bdehamerCopilot
andauthored
fix: strip OCI image tag when pushing attestation to registry (#464)
* fix: strip :tag from discovered OCI subject names An OCI subject discovered from $GITHUB_ARTIFACTS_LIST can carry a :tag in its name (e.g. ghcr.io/owner/app:12345) when the producer captures a tagged reference. The registry-push path passes the subject name to @sigstore/oci's parseImageName, which only accepts a bare "registry/repository" reference and throws "Invalid image name" on any tag. Normalize OCI names by stripping a trailing :tag before dedup and registry push. The entry's digest already pins the exact image, so the tag is redundant. A registry port (e.g. localhost:5000/repo) is preserved by only stripping a colon that appears after the final path separator. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1f0a618f-2ff5-464e-9c70-e29acccf2c3d * refactor: strip OCI tag only at the registry-push boundary Preserve any `:tag` on discovered OCI subjects in the attestation subject and strip it only when attaching/pushing the attestation to the registry, where a bare `registry/repository` reference is required. The subject digest already pins the exact image. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1f0a618f-2ff5-464e-9c70-e29acccf2c3d * docs: drop README changes for OCI tag handling This is an internal push-boundary detail that doesn't need end-user docs. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1f0a618f-2ff5-464e-9c70-e29acccf2c3d * fix: don't strip digest separator in stripOCITag Guard stripOCITag against digest references (e.g. "…/app@sha256:…"): the ':' inside a digest is not a tag separator, so bail out when an '@' is present to avoid returning a corrupt reference. Add unit coverage for digest and tag+digest forms. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1f0a618f-2ff5-464e-9c70-e29acccf2c3d * refactor: reduce OCI ref to bare name for all tag/digest forms Rename stripOCITag -> bareImageName and strip both tag and digest suffixes so the registry-push boundary always receives a valid bare "registry/repository" reference, covering all four reference forms: foo/bar, foo/bar:tag, foo/bar@sha256:…, foo/bar:tag@sha256:… The previous "@"-guard merely returned digest references unchanged, which @sigstore/oci still rejects. A registry port (colon before the final path separator) is still preserved. Add unit coverage for the digest and port+digest forms. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1f0a618f-2ff5-464e-9c70-e29acccf2c3d --------- Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1f0a618f-2ff5-464e-9c70-e29acccf2c3d
1 parent dda48f2 commit 508db95

6 files changed

Lines changed: 392 additions & 177 deletions

File tree

‎__tests__/fixtures/mocks.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ export const TEST_SUBJECT_WITH_REGISTRY: Subject = {
203203
}
204204
}
205205

206+
export const TEST_SUBJECT_WITH_REGISTRY_TAG: Subject = {
207+
name: 'ghcr.io/test-owner/test-repo:v1.2.3',
208+
digest: {
209+
sha256: '7d070f6b64d9bcc530fe99cc21eaaa4b3c364e0b2d367d7735671fa202a03b32'
210+
}
211+
}
212+
206213
export const TEST_PREDICATE: Predicate = {
207214
type: 'https://example.com/predicate/v1',
208215
params: { foo: 'bar' }

‎__tests__/integration/attest.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
createGitHubContextMock,
55
createOctokitMock,
66
TEST_PREDICATE,
7-
TEST_SUBJECT_WITH_REGISTRY
7+
TEST_SUBJECT_WITH_REGISTRY,
8+
TEST_SUBJECT_WITH_REGISTRY_TAG
89
} from '../fixtures/mocks'
910

1011
import type { Attestation } from '@actions/attest'
@@ -139,6 +140,24 @@ describe('createAttestation', () => {
139140

140141
expect(mockAttachArtifactToImage).not.toHaveBeenCalled()
141142
})
143+
144+
it('should strip the image tag before pushing but keep it in the attestation subject', async () => {
145+
const subjects = [TEST_SUBJECT_WITH_REGISTRY_TAG]
146+
147+
await createAttestation(subjects, TEST_PREDICATE, pushOpts)
148+
149+
// The attestation subject retains the tagged reference...
150+
expect(mockAttest).toHaveBeenCalledWith(
151+
expect.objectContaining({ subjects })
152+
)
153+
// ...but the registry-push path receives the bare reference.
154+
expect(mockGetRegistryCredentials).toHaveBeenCalledWith(
155+
'ghcr.io/test-owner/test-repo'
156+
)
157+
expect(mockAttachArtifactToImage).toHaveBeenCalledWith(
158+
expect.objectContaining({ imageName: 'ghcr.io/test-owner/test-repo' })
159+
)
160+
})
142161
})
143162

144163
describe('storage record creation', () => {
@@ -166,6 +185,20 @@ describe('createAttestation', () => {
166185
expect(result.storageRecordIds).toEqual([12345])
167186
})
168187

188+
it('should use the tag-stripped name in the storage record', async () => {
189+
const subjects = [TEST_SUBJECT_WITH_REGISTRY_TAG]
190+
191+
await createAttestation(subjects, TEST_PREDICATE, storageOpts)
192+
193+
expect(mockCreateStorageRecord).toHaveBeenCalledWith(
194+
expect.objectContaining({ name: 'ghcr.io/test-owner/test-repo' }),
195+
expect.objectContaining({
196+
registryUrl: 'https://ghcr.io'
197+
}),
198+
expect.anything()
199+
)
200+
})
201+
169202
it('should omit version from storage record when subjectVersion is empty', async () => {
170203
const subjects = [TEST_SUBJECT_WITH_REGISTRY]
171204
const opts = { ...storageOpts, subjectVersion: '' }

‎__tests__/unit/artifacts.test.ts‎

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import path from 'path'
44
import {
55
errorMessage,
66
readArtifactsList,
7-
parseArtifactsList
7+
parseArtifactsList,
8+
bareImageName
89
} from '../../src/artifacts'
910

1011
const ENV_KEY = 'GITHUB_ARTIFACTS_LIST'
@@ -707,6 +708,109 @@ describe('parseArtifactsList', () => {
707708
})
708709
})
709710

711+
describe('OCI tag preservation', () => {
712+
const wrap = (subjects: unknown[]): string =>
713+
JSON.stringify({ version: 1, subjects })
714+
715+
it('should preserve a trailing :tag in the OCI subject name', () => {
716+
const result = parseArtifactsList(
717+
wrap([
718+
{
719+
name: 'ghcr.io/owner/app:12345',
720+
kind: 'oci',
721+
digest: `sha256:${'a'.repeat(64)}`
722+
}
723+
])
724+
)
725+
726+
expect(result).toHaveLength(1)
727+
expect(result[0].name).toBe('ghcr.io/owner/app:12345')
728+
})
729+
730+
it('should preserve the tag while lowercasing when downcaseOCI is true', () => {
731+
const result = parseArtifactsList(
732+
wrap([
733+
{
734+
name: 'ghcr.io/Owner/App:v1',
735+
kind: 'oci',
736+
digest: `sha256:${'a'.repeat(64)}`
737+
}
738+
]),
739+
{ downcaseOCI: true }
740+
)
741+
742+
expect(result).toHaveLength(1)
743+
expect(result[0].name).toBe('ghcr.io/owner/app:v1')
744+
})
745+
746+
it('should treat differently-tagged OCI names as distinct subjects', () => {
747+
const result = parseArtifactsList(
748+
wrap([
749+
{
750+
name: 'ghcr.io/owner/app:v1',
751+
kind: 'oci',
752+
digest: `sha256:${'a'.repeat(64)}`
753+
},
754+
{
755+
name: 'ghcr.io/owner/app:v2',
756+
kind: 'oci',
757+
digest: `sha256:${'a'.repeat(64)}`
758+
}
759+
])
760+
)
761+
762+
expect(result).toHaveLength(2)
763+
expect(result[0].name).toBe('ghcr.io/owner/app:v1')
764+
expect(result[1].name).toBe('ghcr.io/owner/app:v2')
765+
})
766+
})
767+
768+
describe('bareImageName', () => {
769+
const DIGEST = `sha256:${'a'.repeat(64)}`
770+
771+
it('should leave a bare reference unchanged', () => {
772+
expect(bareImageName('ghcr.io/owner/app')).toBe('ghcr.io/owner/app')
773+
})
774+
775+
it('should strip a trailing :tag', () => {
776+
expect(bareImageName('ghcr.io/owner/app:12345')).toBe('ghcr.io/owner/app')
777+
})
778+
779+
it('should strip a digest suffix', () => {
780+
expect(bareImageName(`ghcr.io/owner/app@${DIGEST}`)).toBe(
781+
'ghcr.io/owner/app'
782+
)
783+
})
784+
785+
it('should strip both a tag and a digest suffix', () => {
786+
expect(bareImageName(`ghcr.io/owner/app:v1@${DIGEST}`)).toBe(
787+
'ghcr.io/owner/app'
788+
)
789+
})
790+
791+
it('should preserve a registry port when no tag is present', () => {
792+
expect(bareImageName('localhost:5000/owner/app')).toBe(
793+
'localhost:5000/owner/app'
794+
)
795+
})
796+
797+
it('should strip the tag but keep a registry port', () => {
798+
expect(bareImageName('localhost:5000/owner/app:12345')).toBe(
799+
'localhost:5000/owner/app'
800+
)
801+
})
802+
803+
it('should strip a digest but keep a registry port', () => {
804+
expect(bareImageName(`localhost:5000/owner/app@${DIGEST}`)).toBe(
805+
'localhost:5000/owner/app'
806+
)
807+
})
808+
809+
it('should strip a bare "name:tag" reference with no registry path', () => {
810+
expect(bareImageName('app:v1')).toBe('app')
811+
})
812+
})
813+
710814
describe('requireSingleOCI option', () => {
711815
const wrap = (subjects: unknown[]): string =>
712816
JSON.stringify({ version: 1, subjects })

0 commit comments

Comments
 (0)