Skip to content

Commit 5dcf485

Browse files
committed
loader: fix panic on invalid type in name field
Adjust the cast logic to be safe so that there isn't a panic if a non-string value is used as `name`. The file will fail validation with a type error this way. Signed-off-by: Milas Bowman <[email protected]>
1 parent 270da2d commit 5dcf485

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

loader/loader.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ func InvalidProjectNameErr(v string) error {
271271
)
272272
}
273273

274+
// projectName determines the canonical name to use for the project considering
275+
// the loader Options as well as `name` fields in Compose YAML fields (which
276+
// also support interpolation).
277+
//
278+
// TODO(milas): restructure loading so that we don't need to re-parse the YAML
279+
// here, as it's both wasteful and makes this code error-prone.
274280
func projectName(details types.ConfigDetails, opts *Options) (string, error) {
275281
projectName, projectNameImperativelySet := opts.GetProjectName()
276282

@@ -281,10 +287,22 @@ func projectName(details types.ConfigDetails, opts *Options) (string, error) {
281287
for _, configFile := range details.ConfigFiles {
282288
yml, err := ParseYAML(configFile.Content)
283289
if err != nil {
290+
// HACK: the way that loading is currently structured, this is
291+
// a duplicative parse just for the `name`. if it fails, we
292+
// give up but don't return the error, knowing that it'll get
293+
// caught downstream for us
284294
return "", nil
285295
}
286296
if val, ok := yml["name"]; ok && val != "" {
287-
pjNameFromConfigFile = yml["name"].(string)
297+
sVal, ok := val.(string)
298+
if !ok {
299+
// HACK: see above - this is a temporary parsed version
300+
// that hasn't been schema-validated, but we don't want
301+
// to be the ones to actually report that, so give up,
302+
// knowing that it'll get caught downstream for us
303+
return "", nil
304+
}
305+
pjNameFromConfigFile = sVal
288306
}
289307
}
290308
if !opts.SkipInterpolation {

loader/loader_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,3 +2337,9 @@ func TestDeviceWriteBps(t *testing.T) {
23372337
})
23382338

23392339
}
2340+
2341+
func TestInvalidProjectNameType(t *testing.T) {
2342+
p, err := loadYAML(`name: 123`)
2343+
assert.Error(t, err, "name must be a string")
2344+
assert.Assert(t, is.Nil(p))
2345+
}

0 commit comments

Comments
 (0)