{"record":{"id":"57dfe3c122b1bdac","repo":"docker/cli","slug":"the-password-stdin-option-requires-username-to","errorCode":null,"errorMessage":"the --password-stdin option requires --username to be set","messagePattern":"the --password-stdin option requires --username to be set","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/registry/login.go","lineNumber":80,"sourceCode":"\tflags := cmd.Flags()\n\n\tflags.StringVarP(&opts.user, \"username\", \"u\", \"\", \"Username\")\n\tflags.StringVarP(&opts.password, \"password\", \"p\", \"\", `Password or Personal Access Token (PAT), or \"-\" to read from stdin`)\n\tflags.BoolVar(&opts.passwordStdin, \"password-stdin\", false, \"Take the Password or Personal Access Token (PAT) from stdin\")\n\n\treturn cmd\n}\n\n// verifyLoginFlags validates flags set on the command.\n//\n// TODO(thaJeztah); combine with verifyLoginOptions, but this requires rewrites of many tests.\nfunc verifyLoginFlags(flags *pflag.FlagSet, opts loginOptions) error {\n\tif flags.Changed(\"password-stdin\") || opts.password == \"-\" {\n\t\tif flags.Changed(\"password\") && opts.password != \"-\" {\n\t\t\treturn errors.New(\"conflicting options: cannot specify both --password and --password-stdin\")\n\t\t}\n\t\tif !flags.Changed(\"username\") {\n\t\t\treturn errors.New(\"the --password-stdin option requires --username to be set\")\n\t\t}\n\t}\n\tif flags.Changed(\"username\") && opts.user == \"\" {\n\t\treturn errors.New(\"username is empty\")\n\t}\n\tif flags.Changed(\"password\") && opts.password == \"\" {\n\t\treturn errors.New(\"password is empty\")\n\t}\n\treturn nil\n}\n\n// readSecretFromStdin reads the secret from r and returns it as a string.\n// It trims terminal line-endings (LF, CRLF, or CR), which may be added when\n// inputting interactively or piping input. The value is otherwise treated as\n// opaque, preserving any other whitespace, including newlines, per [NIST SP 800-63B §5.1.1.2].\n// Note that trimming whitespace may still happen elsewhere (see [NIST SP 800-63B (revision 4) §3.1.1.2]);\n//\n// > Verifiers **MAY** make limited allowances for mistyping (e.g., removing","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/registry/login.go#L62-L98","documentation":"Thrown by verifyLoginFlags when --password-stdin is set (or --password is '-') but --username was not provided. Reading the password from stdin is non-interactive, so a username must be supplied explicitly via the flag.","triggerScenarios":"Running 'echo $TOKEN | docker login --password-stdin' without -u (login.go lines 75, 79-81).","commonSituations":"Forgetting the -u flag in CI scripts; assuming a stored username will be reused when piping a password.","solutions":["Add the --username (-u) flag.","Use the full form: 'printf %s \"$TOKEN\" | docker login -u <user> --password-stdin'."],"exampleFix":"# before\nprintf '%s' \"$TOKEN\" | docker login --password-stdin\n# after\nprintf '%s' \"$TOKEN\" | docker login -u myuser --password-stdin","handlingStrategy":"validation","validationCode":"// Require a username whenever stdin password mode is used\nif (passwordStdin || password == \"-\") && strings.TrimSpace(username) == \"\" {\n    return fmt.Errorf(\"--password-stdin requires --username\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always include -u when using --password-stdin.","Template CI commands with a mandatory username variable.","Fail fast in scripts if the username variable is empty."],"tags":["registry","login","auth","docker","go","cli","flags"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}