Skip to content

Remove bot token from debug stream #1095

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

Merged
merged 2 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Bot API 4.8 (Extra Poll and Dice features).
- Allow custom MySQL port to be defined for tests.
- New static method `Entity::escapeMarkdownV2` for MarkdownV2.
- Remove bot token from debug http logs, this can be disabled by setting `TelegramLog::$remove_bot_token` parameter to `false`
### Changed
- [:exclamation:][unreleased-bc-static-method-entityescapemarkdown] Made `Entity::escapeMarkdown` static, to not require an `Entity` object.
### Deprecated
Expand Down
6 changes: 6 additions & 0 deletions doc/01-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Telegram API changes continuously and it often happens that the database schema
If you store the raw data you can import all updates on the newest table schema by simply using [this script](../utils/importFromLog.php).
Remember to always backup first!!

### Hiding API token from the log
By default, the API token is removed from the log, to prevent any mistaken leakage when posting logs online.
This behaviour can be changed by setting the appropriate variable:
``php
\Longman\TelegramBot\TelegramLog::$remove_bot_token = false;
```

[PSR-3]: https://www.php-fig.org/psr/psr-3
[PSR-3-providers]: https://packagist.org/providers/psr/log-implementation
Expand Down
15 changes: 14 additions & 1 deletion src/TelegramLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class TelegramLog
*/
protected static $debug_log_temp_stream_handle;

/**
* Remove bot token from debug stream
*
* @var bool
*/
public static $remove_bot_token = true;

/**
* Initialise logging.
*
Expand Down Expand Up @@ -85,7 +92,13 @@ public static function endDebugLogTempStream($message = '%s')
{
if (is_resource(self::$debug_log_temp_stream_handle)) {
rewind(self::$debug_log_temp_stream_handle);
self::debug(sprintf($message, stream_get_contents(self::$debug_log_temp_stream_handle)));
$stream_contents = stream_get_contents(self::$debug_log_temp_stream_handle);

if (self::$remove_bot_token) {
$stream_contents = preg_replace('/\/bot(\d+)\:[\w\-]+\//', '/botBOT_TOKEN_REMOVED/', $stream_contents);
}

self::debug(sprintf($message, $stream_contents));
fclose(self::$debug_log_temp_stream_handle);
self::$debug_log_temp_stream_handle = null;
}
Expand Down