| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5437,8 +5437,11 @@ changes: | |||
| 5437 | 5437 | * `b` {ArrayBuffer|Buffer|TypedArray|DataView} | |
| 5438 | 5438 | * Returns: {boolean} | |
| 5439 | 5439 | ||
| 5440 | - This function is based on a constant-time algorithm. | ||
| 5441 | - Returns true if `a` is equal to `b`, without leaking timing information that | ||
| 5440 | + This function compares the underlying bytes that represent the given | ||
| 5441 | + `ArrayBuffer`, `TypedArray`, or `DataView` instances using a constant-time | ||
| 5442 | + algorithm. | ||
| 5443 | + | ||
| 5444 | + This function does not leak timing information that | ||
| 5442 | 5445 | would allow an attacker to guess one of the values. This is suitable for | |
| 5443 | 5446 | comparing HMAC digests or secret values like authentication cookies or | |
| 5444 | 5447 | [capability urls](https://www.w3.org/TR/capability-urls/). | |
@@ -5451,6 +5454,12 @@ If at least one of `a` and `b` is a `TypedArray` with more than one byte per | |||
| 5451 | 5454 | entry, such as `Uint16Array`, the result will be computed using the platform | |
| 5452 | 5455 | byte order. | |
| 5453 | 5456 | ||
| 5457 | + <strong class="critical">When both of the inputs are `Float32Array`s or | ||
| 5458 | + `Float64Array`s, this function might return unexpected results due to IEEE 754 | ||
| 5459 | + encoding of floating-point numbers. In particular, neither `x === y` nor | ||
| 5460 | + `Object.is(x, y)` implies that the byte representations of two floating-point | ||
| 5461 | + numbers `x` and `y` are equal.</strong> | ||
| 5462 | + | ||
| 5454 | 5463 | Use of `crypto.timingSafeEqual` does not guarantee that the _surrounding_ code | |
| 5455 | 5464 | is timing-safe. Care should be taken to ensure that the surrounding code does | |
| 5456 | 5465 | not introduce timing vulnerabilities. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,41 @@ assert.strictEqual( | |||
| 32 | 32 | } | |
| 33 | 33 | } | |
| 34 | 34 | ||
| 35 | + { | ||
| 36 | + // When the inputs are floating-point numbers, timingSafeEqual neither has | ||
| 37 | + // equality nor SameValue semantics. It just compares the underlying bytes, | ||
| 38 | + // ignoring the TypedArray type completely. | ||
| 39 | + | ||
| 40 | + const cmp = (fn) => (a, b) => a.every((x, i) => fn(x, b[i])); | ||
| 41 | + const eq = cmp((a, b) => a === b); | ||
| 42 | + const is = cmp(Object.is); | ||
| 43 | + | ||
| 44 | + function test(a, b, { equal, sameValue, timingSafeEqual }) { | ||
| 45 | + assert.strictEqual(eq(a, b), equal); | ||
| 46 | + assert.strictEqual(is(a, b), sameValue); | ||
| 47 | + assert.strictEqual(crypto.timingSafeEqual(a, b), timingSafeEqual); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + test(new Float32Array([NaN]), new Float32Array([NaN]), { | ||
| 51 | + equal: false, | ||
| 52 | + sameValue: true, | ||
| 53 | + timingSafeEqual: true | ||
| 54 | + }); | ||
| 55 | + | ||
| 56 | + test(new Float64Array([0]), new Float64Array([-0]), { | ||
| 57 | + equal: true, | ||
| 58 | + sameValue: false, | ||
| 59 | + timingSafeEqual: false | ||
| 60 | + }); | ||
| 61 | + | ||
| 62 | + const x = new BigInt64Array([0x7ff0000000000001n, 0xfff0000000000001n]); | ||
| 63 | + test(new Float64Array(x.buffer), new Float64Array([NaN, NaN]), { | ||
| 64 | + equal: false, | ||
| 65 | + sameValue: true, | ||
| 66 | + timingSafeEqual: false | ||
| 67 | + }); | ||
| 68 | + } | ||
| 69 | + | ||
| 35 | 70 | assert.throws( | |
| 36 | 71 | () => crypto.timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2])), | |
| 37 | 72 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments