Skip to content

Commit edd41f2

Browse files
authored
fix: eval in portableTimingSafeEqual (#227)
`eval` was added to help ensure that `portableTimingSafeEqual` is not optimized into a non-constant time function. However, the Content Security Policy `'unsafe-eval'` will flag this. It is better to remove this and risk such an optimization, then to force customers to weaken the Content Security Policy.
1 parent f937f2c commit edd41f2

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

modules/material-management/src/cryptographic_material.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,20 @@ const timingSafeEqual: (a: Uint8Array, b: Uint8Array) => boolean = (function ()
8282
/* https://codahale.com/a-lesson-in-timing-attacks/ */
8383
function portableTimingSafeEqual (a: Uint8Array, b: Uint8Array) {
8484
/* It is *possible* that a runtime could optimize this constant time function.
85-
* Adding `eval` should prevent the optimization, but this is no grantee.
86-
* If you copy this function for your own use, make sure to educate yourself.
87-
* Side channel attacks are pernicious and subtle.
88-
*/
89-
eval('') // eslint-disable-line no-eval
90-
/* Check for early return (Postcondition) UNTESTED: Size is well-know information.
91-
* and does not leak information about contents.
92-
*/
85+
* Adding `eval` could prevent the optimization, but this is no guarantee.
86+
* The eval below is commented out
87+
* because if a browser is using a Content Security Policy with `'unsafe-eval'`
88+
* it would fail on this eval.
89+
* The value in attempting to ensure that this function is not optimized
90+
* is not worth the cost of making customers allow `'unsafe-eval'`.
91+
* If you want to copy this function for your own use,
92+
* please review the timing-attack link above.
93+
* Side channel attacks are pernicious and subtle.
94+
*/
95+
// eval('') // eslint-disable-line no-eval
96+
/* Check for early return (Postcondition) UNTESTED: Size is well-know information
97+
* and does not leak information about contents.
98+
*/
9399
if (a.byteLength !== b.byteLength) return false
94100

95101
let diff = 0

0 commit comments

Comments
 (0)