Skip to content

Commit 701296f

Browse files
committed
fix(initdata): reject combined certs in extra_root_certificates entries
Each array entry must contain exactly one certificate; bundles are now caught early with a clear error naming the offending index. Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
1 parent d0c3af9 commit 701296f

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

cmd/initdata/common.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,15 @@ func extractCertsFromInitdata(data map[string]string) ([]certEntry, error) {
286286
if extra, ok := img["extra_root_certificates"].([]interface{}); ok {
287287
for i, c := range extra {
288288
if cert, ok := c.(string); ok && cert != "" {
289-
pemSources = append(pemSources, pemSource{cert, fmt.Sprintf("cdh.toml/image.extra_root_certificates[%d]", i)})
289+
source := fmt.Sprintf("cdh.toml/image.extra_root_certificates[%d]", i)
290+
certs, err := parsePEMCerts([]byte(cert))
291+
if err != nil {
292+
return nil, fmt.Errorf("%s: failed to parse: %w", source, err)
293+
}
294+
if len(certs) > 1 {
295+
return nil, fmt.Errorf("%s: contains %d certificates; each array entry must contain exactly one certificate", source, len(certs))
296+
}
297+
pemSources = append(pemSources, pemSource{cert, source})
290298
}
291299
}
292300
}

cmd/initdata/validate_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,44 @@ func TestRunValidate_WithBothCACerts(t *testing.T) {
132132
}
133133
}
134134

135+
func TestRunValidate_CombinedExtraRootCertRejected(t *testing.T) {
136+
_, _, pem1 := makeTestCACert(t)
137+
_, _, pem2 := makeTestCACert(t)
138+
combined := strings.ReplaceAll(string(append(pem1, pem2...)), "\n", "\\n")
139+
140+
tomlContent := `version = "0.1.0"
141+
algorithm = "sha256"
142+
143+
[data]
144+
"aa.toml" = '''
145+
[token_configs]
146+
[token_configs.kbs]
147+
url = "http://kbs.example.svc:8080"
148+
'''
149+
"cdh.toml" = '''
150+
[kbc]
151+
name = "cc_kbc"
152+
url = "http://kbs.example.svc:8080"
153+
154+
[image]
155+
extra_root_certificates = ["` + combined + `"]
156+
'''
157+
`
158+
dir := t.TempDir()
159+
path := dir + "/initdata.toml"
160+
_ = os.WriteFile(path, []byte(tomlContent), 0600)
161+
validateFile = path
162+
defer func() { validateFile = "" }()
163+
164+
stderr, err := runValidateStderr(t)
165+
if err == nil {
166+
t.Fatal("expected validation failure for combined extra_root_certificates entry")
167+
}
168+
if !strings.Contains(stderr, "extra_root_certificates[0]") || !strings.Contains(stderr, "exactly one certificate") {
169+
t.Errorf("expected 'extra_root_certificates[0]' and 'exactly one certificate' in stderr, got: %q", stderr)
170+
}
171+
}
172+
135173
func TestRunValidate_InvalidEmbeddedCert(t *testing.T) {
136174
validateFile = "testdata/invalid-leaf-no-san.toml"
137175
defer func() { validateFile = "" }()

0 commit comments

Comments
 (0)