-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Beats Management] Prevent timing attacks when checking auth tokens (#…
…19363) * Using crypto.timingSafeEqual() for comparing auth tokens * Prevent subtler timing attack in token comparison function * Introduce random delay after we try to find token in ES to mitigate timing attack * Remove random delay
- Loading branch information
1 parent
32beb20
commit 41ff71b
Showing
4 changed files
with
31 additions
and
2 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/beats/server/lib/crypto/are_tokens_equal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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'; | ||
|
||
const RANDOM_TOKEN_1 = 'b48c4bda384a40cb91c6eb9b8849e77f'; | ||
const RANDOM_TOKEN_2 = '80a3819e3cd64f4399f1d4886be7a08b'; | ||
|
||
export function areTokensEqual(token1, token2) { | ||
if ((typeof token1 !== 'string') || (typeof token2 !== 'string') || (token1.length !== token2.length)) { | ||
// This prevents a more subtle timing attack where we know already the tokens aren't going to | ||
// match but still we don't return fast. Instead we compare two pre-generated random tokens using | ||
// the same comparison algorithm that we would use to compare two equal-length tokens. | ||
return timingSafeEqual(Buffer.from(RANDOM_TOKEN_1, 'utf8'), Buffer.from(RANDOM_TOKEN_2, 'utf8')); | ||
} | ||
|
||
return timingSafeEqual(Buffer.from(token1, 'utf8'), Buffer.from(token2, 'utf8')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters