Skip to content

Commit 6b07a05

Browse files
authored
Allow periods in certificate names (#2133)
This change allows the certificate name to have a file extension.
1 parent 8c0e945 commit 6b07a05

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pkg/build/types/image_configuration.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import (
3535
)
3636

3737
// Regex for valid certificate names. Since the name of the certificate is used
38-
// as a filename, we restrict it to a safe subset of characters.
39-
var certNameRegex = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
38+
// as a filename, we restrict it to a safe subset of characters. Note the first
39+
// character must NOT be a period (.) to avoid creating hidden files and
40+
// directory traversal.
41+
var certNameRegex = regexp.MustCompile(`^[a-zA-Z0-9_-]+[a-zA-Z0-9_.-]*$`)
4042

4143
// Attempt to probe an upstream VCS URL if known.
4244
func (ic *ImageConfiguration) ProbeVCSUrl(ctx context.Context, imageConfigPath string) {

pkg/build/types/image_configuration_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func TestValidate(t *testing.T) {
279279
}},
280280
},
281281
},
282-
expectError: `configured additional certificate "trying/../../../to/break" has an invalid name, it must match ^[a-zA-Z0-9_-]+$`,
282+
expectError: `configured additional certificate "trying/../../../to/break" has an invalid name, it must match ^[a-zA-Z0-9_-]+[a-zA-Z0-9_.-]*$`,
283283
}, {
284284
name: "weird characters cert name",
285285
configuration: types.ImageConfiguration{
@@ -290,13 +290,38 @@ func TestValidate(t *testing.T) {
290290
}},
291291
},
292292
},
293-
expectError: `configured additional certificate "my-cert@123!" has an invalid name, it must match ^[a-zA-Z0-9_-]+$`,
293+
expectError: `configured additional certificate "my-cert@123!" has an invalid name, it must match ^[a-zA-Z0-9_-]+[a-zA-Z0-9_.-]*$`,
294+
}, {
295+
name: "cert name starting with period",
296+
configuration: types.ImageConfiguration{
297+
Certificates: &types.ImageCertificates{
298+
Additional: []types.AdditionalCertificateEntry{{
299+
Name: ".hidden-cert",
300+
Content: "test",
301+
}},
302+
},
303+
},
304+
expectError: `configured additional certificate ".hidden-cert" has an invalid name, it must match ^[a-zA-Z0-9_-]+[a-zA-Z0-9_.-]*$`,
305+
}, {
306+
name: "cert name with period is valid",
307+
configuration: types.ImageConfiguration{
308+
Certificates: &types.ImageCertificates{
309+
Additional: []types.AdditionalCertificateEntry{{
310+
Name: "my-cert.pem",
311+
Content: "test",
312+
}},
313+
},
314+
},
294315
}}
295316

296317
for _, tt := range tests {
297318
t.Run(tt.name, func(t *testing.T) {
298319
err := tt.configuration.Validate()
299-
require.EqualError(t, err, tt.expectError)
320+
if tt.expectError != "" {
321+
require.EqualError(t, err, tt.expectError)
322+
} else {
323+
require.NoError(t, err)
324+
}
300325
})
301326
}
302327
}

0 commit comments

Comments
 (0)