From 8ee1725e3247d6734467f39ddffb55d8dce4e176 Mon Sep 17 00:00:00 2001 From: krewi1 Date: Thu, 30 Jan 2025 12:35:35 +0100 Subject: [PATCH 1/2] Add defensive check for existance of From instruction in Dockerfile --- src/spec-node/dockerfileUtils.ts | 4 ++++ src/test/dockerfileUtils.test.ts | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/spec-node/dockerfileUtils.ts b/src/spec-node/dockerfileUtils.ts index 0f34476c7..2a07023aa 100644 --- a/src/spec-node/dockerfileUtils.ts +++ b/src/spec-node/dockerfileUtils.ts @@ -227,6 +227,10 @@ export function ensureDockerfileHasFinalStageName(dockerfile: string, defaultLas // Find the last line that starts with "FROM" (possibly preceeded by white-space) const fromLines = [...dockerfile.matchAll(findFromLines)]; + if (fromLines.length === 0) { + throw new Error('Error parsing Dockerfile: Dockerfile contains no FROM instructions'); + } + const lastFromLineMatch = fromLines[fromLines.length - 1]; const lastFromLine = lastFromLineMatch.groups?.line as string; diff --git a/src/test/dockerfileUtils.test.ts b/src/test/dockerfileUtils.test.ts index bd9489ddc..77c7cf6dc 100644 --- a/src/test/dockerfileUtils.test.ts +++ b/src/test/dockerfileUtils.test.ts @@ -1,4 +1,4 @@ -import { assert } from 'chai'; +import { assert, expect } from 'chai'; import { imageMetadataLabel, internalGetImageBuildInfoFromDockerfile } from '../spec-node/imageMetadata'; import { ensureDockerfileHasFinalStageName, extractDockerfile, findBaseImage, findUserStatement, supportsBuildContexts } from '../spec-node/dockerfileUtils'; import { ImageDetails } from '../spec-shutdown/dockerUtils'; @@ -143,6 +143,16 @@ RUN another command }); }); }); + + describe('without any from stage (invalid Dockerfile)', () => { + it('should throw an error', () => { + const dockerfile = ` +RUN some command +`; + expect(() => ensureDockerfileHasFinalStageName(dockerfile, 'placeholder')).to.throw('Error parsing Dockerfile: Dockerfile contains no FROM instructions'); + }); + }); + }); describe('getImageBuildInfo', () => { From 55c0b1feae58e67c15757743ade7851b2523f964 Mon Sep 17 00:00:00 2001 From: krewi1 Date: Thu, 30 Jan 2025 12:38:44 +0100 Subject: [PATCH 2/2] Better wording in test desc --- src/test/dockerfileUtils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/dockerfileUtils.test.ts b/src/test/dockerfileUtils.test.ts index 77c7cf6dc..979375b91 100644 --- a/src/test/dockerfileUtils.test.ts +++ b/src/test/dockerfileUtils.test.ts @@ -145,7 +145,7 @@ RUN another command }); describe('without any from stage (invalid Dockerfile)', () => { - it('should throw an error', () => { + it('should throw a descriptive error', () => { const dockerfile = ` RUN some command `;