{"record":{"id":"eee632a6373e6017","repo":"docker/cli","slug":"context-name-q-is-invalid-names-are-validated-ag","errorCode":null,"errorMessage":"context name %q is invalid, names are validated against regexp %q","messagePattern":"context name %q is invalid, names are validated against regexp %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/context/store/store.go","lineNumber":224,"sourceCode":"// GetStorageInfo returns the paths where the Metadata and TLS data are stored\n// for the context.\nfunc (s *ContextStore) GetStorageInfo(contextName string) StorageInfo {\n\treturn StorageInfo{\n\t\tMetadataPath: s.meta.contextDir(contextdirOf(contextName)),\n\t\tTLSPath:      s.tls.contextDir(contextName),\n\t}\n}\n\n// ValidateContextName checks a context name is valid.\nfunc ValidateContextName(name string) error {\n\tif name == \"\" {\n\t\treturn errors.New(\"context name cannot be empty\")\n\t}\n\tif name == \"default\" {\n\t\treturn errors.New(`\"default\" is a reserved context name`)\n\t}\n\tif !isValidName(name) {\n\t\treturn fmt.Errorf(\"context name %q is invalid, names are validated against regexp %q\", name, validNameFormat)\n\t}\n\treturn nil\n}\n\n// validNameFormat is used as part of errors for invalid context-names.\n// We should consider making this less technical (\"must start with \"a-z\",\n// and only consist of alphanumeric characters and separators\").\nconst validNameFormat = `^[a-zA-Z0-9][a-zA-Z0-9_.+-]+$`\n\n// isValidName checks if the context-name is valid (\"^[a-zA-Z0-9][a-zA-Z0-9_.+-]+$\").\n//\n// Names must start with an alphanumeric character (a-zA-Z0-9), followed by\n// alphanumeric or separators (\"_\", \".\", \"+\", \"-\").\nfunc isValidName(s string) bool {\n\tif len(s) < 2 || !isAlphaNum(s[0]) {\n\t\treturn false\n\t}\n","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/context/store/store.go#L206-L242","documentation":"Returned by ValidateContextName (used during context create/import) when the name fails `^[a-zA-Z0-9][a-zA-Z0-9_.+-]+$`: it must start with an alphanumeric character, be at least 2 characters, and contain only alphanumerics plus `_ . + -`. Empty names and the reserved name 'default' are also rejected (with their own messages).","triggerScenarios":"Calling `docker context create`/import with a name such as 'my context' (space), 'a' (too short, <2 chars), '-foo' (leading separator), 'my@ctx' (@ not allowed), 'café' (non-ASCII), or 'default' (reserved).","commonSituations":"Names generated from CI variables containing spaces or slashes; leading dash/number sign; reserved 'default'; Unicode names; names with colons or @.","solutions":["Choose a name starting with a letter/digit, 2+ characters, containing only `[a-zA-Z0-9_.+-]`.","Avoid 'default' and characters like space, slash, colon, @, and any non-ASCII rune.","Validate the name with the same regex before calling the API."],"exampleFix":"// before\nif err := store.ValidateContextName(name); err != nil { ... }\n// name = \"my context\" -> rejected\n\n// after: sanitize before validating\nname = regexp.MustCompile(`[^a-zA-Z0-9_.+-]`).ReplaceAllString(name, \"-\")\nif err := store.ValidateContextName(name); err != nil { return err }","handlingStrategy":"validation","validationCode":"var contextNameRE = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.+-]+$`)\n\nfunc validContextName(name string) bool {\n    return name != \"\" && name != \"default\" && contextNameRE.MatchString(name)\n}\n\nif !validContextName(name) {\n    return fmt.Errorf(\"invalid context name %q\", name)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate context names from a safe charset ([a-zA-Z0-9_.+-]), starting alphanumeric.","Sanitize external inputs (CI vars, hostnames) before using as a context name.","Reserve 'default' — never try to create it."],"tags":["context","validation","naming","user-input"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}