Skip to content

Commit

Permalink
Using crypto.timingSafeEqual() for comparing auth tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed May 23, 2018
1 parent 5886ebd commit a95f1e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions x-pack/plugins/beats/server/lib/crypto/are_tokens_equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { timingSafeEqual } from 'crypto';

export function areTokensEqual(token1, token2) {
return token1.length === token2.length
&& timingSafeEqual(Buffer.from(token1, 'utf8'), Buffer.from(token2, 'utf8'));
}
7 changes: 7 additions & 0 deletions x-pack/plugins/beats/server/lib/crypto/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { areTokensEqual } from './are_tokens_equal';
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { INDEX_NAMES } from '../../../common/constants';
import { callWithInternalUserFactory } from '../../lib/client';
import { wrapEsError } from '../../lib/error_wrappers';
import { areTokensEqual } from '../../lib/crypto';

async function getEnrollmentToken(callWithInternalUser, enrollmentToken) {
const params = {
Expand Down Expand Up @@ -80,7 +81,7 @@ export function registerEnrollBeatRoute(server) {
try {
const enrollmentToken = request.headers['kbn-beats-enrollment-token'];
const { token, expires_on: expiresOn } = await getEnrollmentToken(callWithInternalUser, enrollmentToken);
if (!token || token !== enrollmentToken) {
if (!token || !areTokensEqual(token, enrollmentToken)) {
return reply({ message: 'Invalid enrollment token' }).code(400);
}
if (moment(expiresOn).isBefore(moment())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { get } from 'lodash';
import { INDEX_NAMES } from '../../../common/constants';
import { callWithInternalUserFactory } from '../../lib/client';
import { wrapEsError } from '../../lib/error_wrappers';
import { areTokensEqual } from '../../lib/crypto';

async function getBeat(callWithInternalUser, beatId) {
const params = {
Expand Down Expand Up @@ -74,7 +75,7 @@ export function registerUpdateBeatRoute(server) {
return reply({ message: 'Beat not found' }).code(404);
}

const isAccessTokenValid = beat.access_token === request.headers['kbn-beats-access-token'];
const isAccessTokenValid = areTokensEqual(beat.access_token, request.headers['kbn-beats-access-token']);
if (!isAccessTokenValid) {
return reply({ message: 'Invalid access token' }).code(401);
}
Expand Down

0 comments on commit a95f1e0

Please sign in to comment.