Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datasource/docker): Validate digest value before calling API #19780

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/modules/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,8 @@ describe('modules/datasource/docker/index', () => {
});

it('supports architecture-specific digest in OCI manifests with media type', async () => {
const currentDigest = 'some-image-digest';
const currentDigest =
'sha256:0101010101010101010101010101010101010101010101010101010101010101';

httpMock
.scope(authUrl)
Expand Down Expand Up @@ -855,7 +856,8 @@ describe('modules/datasource/docker/index', () => {
});

it('supports architecture-specific digest in OCI manifests without media type', async () => {
const currentDigest = 'some-image-digest';
const currentDigest =
'sha256:0101010101010101010101010101010101010101010101010101010101010101';

httpMock
.scope(authUrl)
Expand Down Expand Up @@ -1005,7 +1007,8 @@ describe('modules/datasource/docker/index', () => {
});

it('handles error while retrieving image config blob', async () => {
const currentDigest = 'some-image-digest';
const currentDigest =
'sha256:0101010101010101010101010101010101010101010101010101010101010101';

httpMock
.scope(authUrl)
Expand Down Expand Up @@ -1058,24 +1061,31 @@ describe('modules/datasource/docker/index', () => {
.scope(baseUrl)
.get('/', undefined, { badheaders: ['authorization'] })
.reply(200, { token: 'some-token' })
.head('/library/some-dep/manifests/some-digest')
.head(
'/library/some-dep/manifests/sha256:0101010101010101010101010101010101010101010101010101010101010101'
)
.reply(404, {});
httpMock
.scope(baseUrl)
.get('/', undefined, { badheaders: ['authorization'] })
.reply(200, '', {})
.head('/library/some-dep/manifests/some-new-value', undefined, {
badheaders: ['authorization'],
})
.head(
'/library/some-dep/manifests/sha256:fafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafa',
undefined,
{
badheaders: ['authorization'],
}
)
.reply(401);

const res = await getDigest(
{
datasource: 'docker',
depName: 'some-dep',
currentDigest: 'some-digest',
currentDigest:
'sha256:0101010101010101010101010101010101010101010101010101010101010101',
},
'some-new-value'
'sha256:fafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafa'
);
expect(res).toBeNull();
});
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
import { hasKey } from '../../../util/object';
import { regEx } from '../../../util/regex';
import { addSecretForSanitizing } from '../../../util/sanitize';
import { isDockerDigest } from '../../../util/string';
import {
ensurePathPrefix,
ensureTrailingSlash,
Expand Down Expand Up @@ -1018,7 +1019,7 @@ export class DockerDatasource extends Datasource {
let digest: string | null = null;
try {
let architecture: string | null | undefined = null;
if (currentDigest) {
if (currentDigest && isDockerDigest(currentDigest)) {
architecture = await this.getImageArchitecture(
registryHost,
dockerRepository,
Expand Down
4 changes: 4 additions & 0 deletions lib/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ export function looseEquals(
}
return a.localeCompare(b, undefined, { sensitivity: 'base' }) === 0;
}

export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
viceice marked this conversation as resolved.
Show resolved Hide resolved
}