Skip to content

Commit ee562b8

Browse files
committed
Client: explicitly set passwordMode boolean flag
Instead of checking the value of the password variable to see if it is null vs empty, especially since we sometimes reassign to it.
1 parent b233226 commit ee562b8

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

Client/Program.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ void printVersion()
295295
// It instead requires --password=PASSWORD or --password:PASSWORD or -pPASSWORD
296296
var bareArguments = parseOptions.Parse(args);
297297

298-
if (bareArguments.Count > 0 && password == string.Empty)
298+
var passwordMode = password == string.Empty; // instead of null
299+
if (bareArguments.Count > 0 && passwordMode)
299300
{
300301
// Check if this was the standalone password
301302
var possibles = new[] { "-p", "--password", "/p", "/password" };
302-
303303
for (int i = 0; i < args.Count - 1; ++i)
304304
{
305305
if (!possibles.Contains(args[i]))
@@ -383,16 +383,15 @@ void printVersion()
383383
Help(Console.Error, "A path to the secrets store is required!", command, parseOptions);
384384
}
385385

386-
if (keyfile == null && password == null)
386+
if (keyfile == null && !passwordMode)
387387
{
388+
// Changed: Default to password mode instead of erroring out
388389
// Help(Console.Error, "Must specify either --password or --keyfile!", command, parseOptions);
389-
390-
// Default to password mode instead of erroring out
391390
password = string.Empty;
391+
passwordMode = true;
392392
}
393393

394-
// We need to differentiate between null (not set) and empty (empty)
395-
if (password == string.Empty && (string.IsNullOrEmpty(keyfile) || !File.Exists(keyfile)))
394+
if (passwordMode)
396395
{
397396
if (command == "create")
398397
{
@@ -423,13 +422,13 @@ void printVersion()
423422
// Handle parameters specific to certain commands
424423
if (command == "create")
425424
{
426-
if (password is null && string.IsNullOrWhiteSpace(keyfile))
425+
if (!passwordMode && string.IsNullOrWhiteSpace(keyfile))
427426
{
427+
// This block should no longer be reachable!
428428
Console.Error.WriteLine("A newly created store must have one or both of --password and --keyfile specified");
429429
Environment.Exit(1);
430430
}
431-
if (!string.IsNullOrWhiteSpace(password) && File.Exists(keyfile)
432-
&& new FileInfo(keyfile).Length > 0)
431+
if (passwordMode && File.Exists(keyfile) && new FileInfo(keyfile).Length > 0)
433432
{
434433
Confirm($"Overwrite the existing contents of the key file at {keyfile} " +
435434
"with a key derived from the provided password? [yes/no]: ");
@@ -451,9 +450,9 @@ void printVersion()
451450

452451
using (sman)
453452
{
454-
if (!string.IsNullOrEmpty(password))
453+
if (passwordMode)
455454
{
456-
sman.LoadKeyFromPassword(password);
455+
sman.LoadKeyFromPassword(password!);
457456
try
458457
{
459458
sman.ValidateSentinel();
@@ -471,7 +470,7 @@ void printVersion()
471470
{
472471
Confirm($"Overwrite existing store at {path}? [yes/no]: ");
473472
}
474-
if (password == null)
473+
if (!passwordMode)
475474
{
476475
ExitIfNullOrEmpty(keyfile, "A keyfile path is required if no password is provided");
477476

@@ -492,12 +491,12 @@ void printVersion()
492491
keyCreated = keyfile;
493492
}
494493
}
495-
else if (password == null && keyfile != null)
494+
else if (!passwordMode && keyfile != null)
496495
{
497496
sman.LoadKeyFromFile(keyfile);
498497
}
499498

500-
if (password is null && keyfile is not null)
499+
if (!passwordMode && keyfile is not null)
501500
{
502501
try
503502
{
@@ -512,7 +511,7 @@ void printVersion()
512511
// We store --export-key to its own variable so that it doesn't override our defaulting to password
513512
// mode if no key file was specified. After we've decided on whether or not to use a password above,
514513
// we can now coalesce the two values.
515-
if (keyCreated is null && keyExport is not null && keyExport != keyfile)
514+
if (keyExport is not null && (keyfile is null || keyExport != keyfile))
516515
{
517516
sman.ExportKey(keyExport);
518517
keyCreated = keyExport;

0 commit comments

Comments
 (0)