Skip to content

Commit e3c9903

Browse files
a-orenclaude
andcommitted
fix(providers): strip all leading flags from Dockerfile FROM lines
The parseFromImage function previously only stripped a single --flag token using a regex. Dockerfile syntax allows multiple flags before the image reference. Replace the single regex with a loop that skips all leading --flag tokens. TC-4977 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Assisted-by: Claude Code
1 parent 905c336 commit e3c9903

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

src/providers/oci_dockerfile.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ export function parseFromImage(manifestContent) {
4242
for (const line of lines) {
4343
const trimmed = line.trim()
4444
if (/^FROM\s+/i.test(trimmed)) {
45-
// Extract image ref: FROM [--platform=...] image [AS name]
46-
const withoutFrom = trimmed.replace(/^FROM\s+/i, '')
47-
// Skip optional --platform flag
48-
const withoutFlags = withoutFrom.replace(/^--\S+\s+/, '')
49-
// Take only the image part (before AS alias)
50-
const parts = withoutFlags.split(/\s+/)
51-
lastFrom = parts[0]
45+
// Extract image ref: FROM [--flag=val ...] image [AS name]
46+
const tokens = trimmed.replace(/^FROM\s+/i, '').split(/\s+/)
47+
// Skip all leading --flag tokens (e.g. --platform=linux/amd64)
48+
let i = 0
49+
while (i < tokens.length && tokens[i].startsWith('--')) {
50+
i++
51+
}
52+
lastFrom = tokens[i] || null
5253
}
5354
}
5455
if (!lastFrom) {

test/providers/oci_dockerfile.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,18 @@ suite('testing the Dockerfile/Containerfile data provider', () => {
5959
expect(parseFromImage(content)).to.equal('nginx:alpine')
6060
})
6161

62-
/** Verifies that --platform flags are skipped when parsing FROM lines. */
62+
/** Verifies that a single --platform flag is skipped when parsing FROM lines. */
6363
test('handles --platform flag', () => {
6464
const content = 'FROM --platform=linux/amd64 ubuntu:22.04\n'
6565
expect(parseFromImage(content)).to.equal('ubuntu:22.04')
6666
})
6767

68+
/** Verifies that multiple flags before the image reference are all skipped. */
69+
test('handles multiple flags before image', () => {
70+
const content = 'FROM --platform=linux/amd64 --some-flag=value ubuntu:22.04 AS base\n'
71+
expect(parseFromImage(content)).to.equal('ubuntu:22.04')
72+
})
73+
6874
/** Verifies that image references with digests are parsed correctly. */
6975
test('handles image with digest', () => {
7076
const content = 'FROM httpd@sha256:abc123\n'

0 commit comments

Comments
 (0)