Skip to content

Commit c5bbdea

Browse files
authored
Handle failed token test without leaking token (#1138)
If the line `token_info = token_response[token]` fails with a `KeyError` (due to an error response from lichess), the user's token will be printed in the error message. Use `token_response.get()` to prevent this. Also, use `.get()` on the resulting `token_info` to print a more useful error message.
1 parent ef2f735 commit c5bbdea

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

lib/lichess.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ def __init__(self, token: str, url: str, version: str, logging_level: int, max_r
113113

114114
# Confirm that the OAuth token has the proper permission to play on lichess
115115
token_response = cast(TOKEN_TESTS_TYPE, self.api_post("token_test", data=token))
116-
token_info = token_response[token]
116+
token_info = token_response.get(token)
117117

118118
if not token_info:
119-
raise RuntimeError("Token in config file is not recognized by lichess. "
120-
"Please check that it was copied correctly into your configuration file.")
119+
raise RuntimeError("There was an error in retrieving information about the bot's token. "
120+
"Please check that it was copied correctly into your configuration file "
121+
"and try again.")
121122

122-
scopes = token_info["scopes"]
123+
scopes = token_info.get("scopes", "")
123124
if "bot:play" not in scopes.split(","):
124125
raise RuntimeError("Please use an API access token for your bot that "
125126
'has the scope "Play games with the bot API (bot:play)". '

0 commit comments

Comments
 (0)