Skip to content

Commit 53d8e87

Browse files
almeidxkyranet
andauthored
fix: censor token in debug output (#8764)
Co-authored-by: Aura Román <kyradiscord@gmail.com>
1 parent fc10774 commit 53d8e87

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

packages/discord.js/src/client/Client.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,7 @@ class Client extends BaseClient {
214214
if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
215215
this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
216216
this.rest.setToken(token);
217-
this.emit(
218-
Events.Debug,
219-
`Provided token: ${token
220-
.split('.')
221-
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
222-
.join('.')}`,
223-
);
217+
this.emit(Events.Debug, `Provided token: ${this._censoredToken}`);
224218

225219
if (this.options.presence) {
226220
this.options.ws.presence = this.presence._parse(this.options.presence);
@@ -459,6 +453,21 @@ class Client extends BaseClient {
459453
});
460454
}
461455

456+
/**
457+
* Partially censored client token for debug logging purposes.
458+
* @type {?string}
459+
* @readonly
460+
* @private
461+
*/
462+
get _censoredToken() {
463+
if (!this.token) return null;
464+
465+
return this.token
466+
.split('.')
467+
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
468+
.join('.');
469+
}
470+
462471
/**
463472
* Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script
464473
* with the client as `this`.

packages/discord.js/src/client/websocket/WebSocketShard.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,12 @@ class WebSocketShard extends EventEmitter {
740740
*/
741741
_send(data) {
742742
if (this.connection?.readyState !== WebSocket.OPEN) {
743-
this.debug(`Tried to send packet '${JSON.stringify(data)}' but no WebSocket is available!`);
743+
this.debug(
744+
`Tried to send packet '${JSON.stringify(data).replaceAll(
745+
this.manager.client.token,
746+
this.manager.client._censoredToken,
747+
)}' but no WebSocket is available!`,
748+
);
744749
this.destroy({ closeCode: 4_000 });
745750
return;
746751
}

packages/discord.js/typings/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
766766
private presence: ClientPresence;
767767
private _eval(script: string): unknown;
768768
private _validateOptions(options: ClientOptions): void;
769+
private get _censoredToken(): string | null;
769770

770771
public application: If<Ready, ClientApplication>;
771772
public channels: ChannelManager;

0 commit comments

Comments
 (0)