From 05603560e6729fe577386031e3b2efe8765db10d Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 3 Apr 2022 14:47:10 +0400 Subject: [PATCH] Give a name to a magic number and document it --- src/errors.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index ad3348dd..be1adc40 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -758,11 +758,22 @@ pub(crate) fn hide_token(mut error: reqwest::Error) -> reqwest::Error { if let Some(token) = segment.and_then(|s| s.strip_prefix("bot")) { // make sure that what we are about to delete looks like a bot token if let Some((id, secret)) = token.split_once(':') { - if secret.len() >= 35 - && secret - .chars() - .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') - && id.chars().all(|c| c.is_ascii_digit()) + // The part before the : in the token is the id of the bot. + let id_character = |c: char| c.is_ascii_digit(); + + // The part after the : in the token is the secret. + // + // In all bot tokens we could find the secret is 35 characters long and is + // 0-9a-zA-Z_- only. + // + // It would be nice to research if TBA always has 35 character secrets or if it + // is just a coincidence. + const SECRET_LENGTH: usize = 35; + let secret_character = |c: char| c.is_ascii_alphanumeric() || c == '-' || c == '_'; + + if secret.len() >= SECRET_LENGTH + && id.chars().all(id_character) + && secret.chars().all(secret_character) { // found token, hide only the token let without_token =