Skip to content

Commit 016704a

Browse files
authored
fix: add check for missing FROM instructions in Dockerfile parsing (#950)
* Add defensive check for existance of From instruction in Dockerfile * Better wording in test desc
1 parent c8efeb6 commit 016704a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/spec-node/dockerfileUtils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ export function ensureDockerfileHasFinalStageName(dockerfile: string, defaultLas
227227

228228
// Find the last line that starts with "FROM" (possibly preceeded by white-space)
229229
const fromLines = [...dockerfile.matchAll(findFromLines)];
230+
if (fromLines.length === 0) {
231+
throw new Error('Error parsing Dockerfile: Dockerfile contains no FROM instructions');
232+
}
233+
230234
const lastFromLineMatch = fromLines[fromLines.length - 1];
231235
const lastFromLine = lastFromLineMatch.groups?.line as string;
232236

src/test/dockerfileUtils.test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { imageMetadataLabel, internalGetImageBuildInfoFromDockerfile } from '../spec-node/imageMetadata';
33
import { ensureDockerfileHasFinalStageName, extractDockerfile, findBaseImage, findUserStatement, supportsBuildContexts } from '../spec-node/dockerfileUtils';
44
import { ImageDetails } from '../spec-shutdown/dockerUtils';
@@ -143,6 +143,16 @@ RUN another command
143143
});
144144
});
145145
});
146+
147+
describe('without any from stage (invalid Dockerfile)', () => {
148+
it('should throw a descriptive error', () => {
149+
const dockerfile = `
150+
RUN some command
151+
`;
152+
expect(() => ensureDockerfileHasFinalStageName(dockerfile, 'placeholder')).to.throw('Error parsing Dockerfile: Dockerfile contains no FROM instructions');
153+
});
154+
});
155+
146156
});
147157

148158
describe('getImageBuildInfo', () => {

0 commit comments

Comments
 (0)