Skip to content

Commit

Permalink
fix: censor token in debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Oct 17, 2022
1 parent caeb1cb commit 327b343
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
32 changes: 25 additions & 7 deletions packages/discord.js/src/client/Client.js
Expand Up @@ -152,6 +152,8 @@ class Client extends BaseClient {
this.token = null;
}

Object.defineProperty(this, '_censoredToken', { value: null, writable: true });

/**
* User that the client is logged in as
* @type {?ClientUser}
Expand Down Expand Up @@ -214,13 +216,7 @@ class Client extends BaseClient {
if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
this.rest.setToken(token);
this.emit(
Events.Debug,
`Provided token: ${token
.split('.')
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
.join('.')}`,
);
this.emit(Events.Debug, `Provided token: ${this._getCensoredToken()}`);

if (this.options.presence) {
this.options.ws.presence = this.presence._parse(this.options.presence);
Expand Down Expand Up @@ -256,6 +252,7 @@ class Client extends BaseClient {
this.sweepers.destroy();
this.ws.destroy();
this.token = null;
this._censoredToken = null;
this.rest.setToken(null);
}

Expand Down Expand Up @@ -470,6 +467,27 @@ class Client extends BaseClient {
return eval(script);
}

/**
* Partially censors the client token for debug logging purposes.
* @returns {?string}
* @private
*/
_getCensoredToken() {
if (!this.token) return null;

/**
* The cached censored client token
* @type {?string}
* @private
*/
this._censoredToken ??= this.token
.split('.')
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
.join('.');

return this._censoredToken;
}

/**
* Validates the client options.
* @param {ClientOptions} [options=this.options] Options to validate
Expand Down
7 changes: 6 additions & 1 deletion packages/discord.js/src/client/websocket/WebSocketShard.js
Expand Up @@ -740,7 +740,12 @@ class WebSocketShard extends EventEmitter {
*/
_send(data) {
if (this.connection?.readyState !== WebSocket.OPEN) {
this.debug(`Tried to send packet '${JSON.stringify(data)}' but no WebSocket is available!`);
this.debug(
`Tried to send packet '${JSON.stringify(data).replaceAll(
this.manager.client.token,
this.manager.client._getCensoredToken,
)}' but no WebSocket is available!`,
);
this.destroy({ closeCode: 4_000 });
return;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -762,9 +762,11 @@ export type If<T extends boolean, A, B = null> = T extends true ? A : T extends

export class Client<Ready extends boolean = boolean> extends BaseClient {
public constructor(options: ClientOptions);
private _censoredToken: string | null;
private actions: unknown;
private presence: ClientPresence;
private _eval(script: string): unknown;
private _getCensoredToken(): string | null;
private _validateOptions(options: ClientOptions): void;

public application: If<Ready, ClientApplication>;
Expand Down

0 comments on commit 327b343

Please sign in to comment.