Skip to content

Commit 824d6e0

Browse files
committed
fix: remove overly-broad /init suffix detection in IsSystemdImage
Per review feedback, matching any path ending in /init is too aggressive since many entrypoint scripts are named 'init'. Now only matches explicit systemd paths: /sbin/init, /lib/systemd/systemd, /usr/lib/systemd/systemd
1 parent 5d805cc commit 824d6e0

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

lib/images/systemd.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package images
22

3-
import "strings"
4-
53
// IsSystemdImage checks if the image's CMD indicates it wants systemd as init.
64
// Detection is based on the effective command (entrypoint + cmd), not whether
75
// systemd is installed in the image.
@@ -10,17 +8,21 @@ import "strings"
108
// - /sbin/init
119
// - /lib/systemd/systemd
1210
// - /usr/lib/systemd/systemd
13-
// - Any path ending in /init
1411
func IsSystemdImage(entrypoint, cmd []string) bool {
15-
// Combine to get the actual command that will run
16-
effective := append(entrypoint, cmd...)
12+
// Combine to get the actual command that will run.
13+
// Create a new slice to avoid corrupting caller's backing array.
14+
effective := make([]string, 0, len(entrypoint)+len(cmd))
15+
effective = append(effective, entrypoint...)
16+
effective = append(effective, cmd...)
1717
if len(effective) == 0 {
1818
return false
1919
}
2020

2121
first := effective[0]
2222

23-
// Match specific systemd/init paths
23+
// Match specific systemd/init paths only.
24+
// We intentionally don't match generic */init paths since many entrypoint
25+
// scripts are named "init" and would be false positives.
2426
systemdPaths := []string{
2527
"/sbin/init",
2628
"/lib/systemd/systemd",
@@ -32,12 +34,6 @@ func IsSystemdImage(entrypoint, cmd []string) bool {
3234
}
3335
}
3436

35-
// Match any absolute path ending in /init (e.g., /usr/sbin/init)
36-
// Only match absolute paths to avoid false positives like "./init"
37-
if strings.HasPrefix(first, "/") && strings.HasSuffix(first, "/init") {
38-
return true
39-
}
40-
4137
return false
4238
}
4339

lib/images/systemd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func TestIsSystemdImage(t *testing.T) {
3838
expected: true,
3939
},
4040
{
41-
name: "path ending in /init",
41+
name: "path ending in /init should not match (too broad)",
4242
entrypoint: nil,
4343
cmd: []string{"/usr/sbin/init"},
44-
expected: true,
44+
expected: false,
4545
},
4646
{
4747
name: "regular command (nginx)",

0 commit comments

Comments
 (0)