Skip to content
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

electron: use timingSafeEqual to check token #7308

Merged
merged 1 commit into from
Mar 11, 2020
Merged
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
23 changes: 21 additions & 2 deletions packages/core/src/electron-node/token/electron-token-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as http from 'http';
import * as cookie from 'cookie';
import * as crypto from 'crypto';
import { injectable } from 'inversify';
import { ElectronSecurityToken } from '../../electron-common/electron-token';

Expand All @@ -41,8 +42,26 @@ export class ElectronTokenValidator {
return false;
}

isTokenValid(token: ElectronSecurityToken): boolean {
return typeof token === 'object' && token.value === this.electronSecurityToken!.value;
/**
* Validates a token.
*
* This method both checks the shape of the parsed token data and its actual value.
*
* @param token Parsed object sent by the client as the token.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isTokenValid(token: any): boolean {
// eslint-disable-next-line no-null/no-null
if (typeof token === 'object' && token !== null && typeof token.value === 'string') {
try {
const received = Buffer.from(token.value, 'utf8');
const expected = Buffer.from(this.electronSecurityToken!.value, 'utf8');
return received.byteLength === expected.byteLength && crypto.timingSafeEqual(received, expected);
} catch (error) {
console.error(error);
}
}
return false;
}

/**
Expand Down