Skip to content

Conversation

@allrob23
Copy link
Contributor

@allrob23 allrob23 commented Apr 21, 2025

Description

#1202

This PR enhances get_password_from_file by removing pre-check if statements and adding explicit error handling for cases like file not found or permission denied, e.g., mycli.main.PasswordFileError: Password file '/tmp/a.txt' is empty or contains only whitespace, improving security and clarity.

Checklist

  • I've added this contribution to the changelog.md.
  • I've added my name to the AUTHORS file (or it's already there).

mycli/main.py Outdated

return password_from_file
password = fp.readline().strip()
if not password:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this introduce a new constraint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to if not? If so, I thought it makes sense to raise an error when the content is empty

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, raising an error when the password is empty changes the functionality, which I suppose we should hold constant for this PR. Looking closer, I would even strip() the newline more precisely but again that is out of scope.

Because the empty string is a valid password, as are passwords containing whitespace. Whether they are advisable is a separate Q.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I updated the code.

mycli/main.py Outdated
raise PasswordFileError(f"Permission denied reading password file '{password_file}'") from None
except IsADirectoryError:
raise PasswordFileError(f"Path '{password_file}' is a directory, not a file") from None
except OSError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also advisable to catch RuntimeError ? I'm unsure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's useful, maybe I can leave this last exception generic to handle all other types of exceptions? what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your decision!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Contributor

@rolandwalker rolandwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@rolandwalker
Copy link
Contributor

@allrob23 I see this PR now has a merge conflict. Do you want me to fix that and merge, or take that on yourself?

@allrob23
Copy link
Contributor Author

@rolandwalker can you please do it? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants