-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safe refresh #31
Safe refresh #31
Conversation
unprintable characters in REFRESH_TOKEN.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for the contribution, @jwoehr! 🎉 It's always appreciated when someone takes the time to improve a project based on their own experience with the software! 👍
I left several comments (mostly related to formatting and style) but overall this is quite nice!
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
@Mandrenkov I'm not sure the rest of the test |
Hi @jwoehr, I think it depends what the intention of the sanity check is. Consider: settings = xcc.Settings(
REFRESH_TOKEN="j.w.t\n", HOST="example.com", PORT=80, TLS=False
)
match = r"The REFRESH_TOKEN setting contains non-printable character(s)\."
# Check that a ValueError is thrown since "\n" is not printable.
with pytest.raises(ValueError, match=match):
settings.save()
# Check that the .env file was not modified since there was a "\n" in the refresh token.
assert dotenv_values(env_file.name) == {
"XANADU_CLOUD_REFRESH_TOKEN": "j.w.t", # Note that the space at the end is deleted here.
"XANADU_CLOUD_HOST": "example.com",
"XANADU_CLOUD_PORT": "80",
"XANADU_CLOUD_TLS": "False",
} Here, we verify that the newline character is caught by the sanity check and the .env file is not modified. Of course, we can also expand the coverage of the test by parameterizing it: @pytest.mark.parametrize("refresh_token", [chr(0x0a), chr(0x20)]) Note, however, that What do you think about increasing the strictness of the sanity check by matching the JWT format exactly? |
Yes, that sounds better than dealing with it piecemeal. Is there a quick reference on the format? |
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Thanks for the reply, @jwoehr!
🙌
Yes! I would highly recommend taking a look at https://jwt.io/introduction (namely What is the JSON Web Token structure?). In a nutshell though, a JWT is three Base64-encoded strings concatenated with two periods (
I think most of the settings in the .env file could benefit from additional validation (e.g., checking that |
@Mandrenkov since the REFRESH_TOKEN is only manipulated by the user and by the affected code from the user side in its Base64Url form, then all that we have to check for is a character that's not legal in Base64URL, correct? |
|
I had to add |
- do check before starting save because save dict not ordered
I can't make the post-check in the test_settings.py routine test_save_bad_Base64URL work so I've commented it out.
|
Yeah, almost! We also need to account for the
Exactly!
Ah, so if you take a look at the implementation of |
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Co-authored-by: Mikhail Andrenkov <Mandrenkov@users.noreply.github.com>
Hmm now I get a different error in the test:
|
Right! We're no longer using the assert dotenv_values(env_file.name) ... with assert dotenv_values(xcc.Settings.Config.env_file) ... |
Excellent everything works now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you still need to run make format lint
. 🤔
Ah, I did run |
I fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @jwoehr! I think this PR is ready to merge. 🟢
Context:
It's possible to save bad refresh tokens by accidentally including non-printable characters when copying them.
Description of the Change:
Settings.save()
does a sanity check andraise
s on unprintables in the refresh token.Benefits:
Less surprise and mystery on a user error in performing a token refresh
Possible Drawbacks:
This works, but though I wrote a test, I have not been able to make the test work, could use some help on
test_settings.py
testtest_save_unprintable
Related GitHub Issues: