Skip to content

Commit

Permalink
fix(datasource/docker): Validate digest value before calling API (#19780
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zharinov committed Jan 13, 2023
1 parent cd5c569 commit b00294f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
28 changes: 19 additions & 9 deletions lib/modules/datasource/docker/index.spec.ts
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
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
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);
}

0 comments on commit b00294f

Please sign in to comment.