Skip to content

Commit d3118d7

Browse files
authored
fix: BYTES_PER_KEY value (#193)
Maximum number of bytes that are allowed to be encrypted under a single cached data key across messages. The maximum value defined in the AWS Encryption SDK specification is 2 ** 63 - 1. However Javascript can only perform safe operations on values up to Number.MAX_SAFE_INTEGER === 9007199254740991 === 2 ** 53 - 1. e.g Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 => true Number.MAX_SAFE_INTEGER + 1 > Number.MAX_SAFE_INTEGER + 2 => false Number.MAX_SAFE_INTEGER + 1 < Number.MAX_SAFE_INTEGER + 2 => false This means that after 2 ** 53 - 1 the process of accumulating a byte count will never yield an accurate comparison and so, never halt. The choice here to use 2 ** 53 - 1 instead of Number.MAX_SAFE_INTEGER is deliberate. This is because in the future Number.MAX_SAFE_INTEGER could be raised to 2 ** 66 or some value larger 2 ** 63.
1 parent 77ad031 commit d3118d7

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

modules/cache-material/src/caching_cryptographic_materials_decorators.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ export function decorateProperties<S extends SupportedAlgorithmSuites> (
4949
/* Precondition: You *can not* cache something forever. */
5050
needs(maxAge > 0, 'You must configure a maxAge')
5151
/* Precondition: maxBytesEncrypted must be inside bounds. i.e. positive and not more than the maximum. */
52-
needs(!maxBytesEncrypted || (maxBytesEncrypted > 0 && Maximum.BYTES_PER_KEY >= maxBytesEncrypted), 'maxBytesEncrypted is outside of bounds.')
52+
needs(!maxBytesEncrypted || (maxBytesEncrypted > 0 && Maximum.BYTES_PER_CACHED_KEY_LIMIT >= maxBytesEncrypted), 'maxBytesEncrypted is outside of bounds.')
5353
/* Precondition: maxMessagesEncrypted must be inside bounds. i.e. positive and not more than the maximum. */
54-
needs(!maxMessagesEncrypted || (maxMessagesEncrypted > 0 && Maximum.MESSAGES_PER_KEY >= maxMessagesEncrypted), 'maxMessagesEncrypted is outside of bounds.')
54+
needs(!maxMessagesEncrypted || (maxMessagesEncrypted > 0 && Maximum.MESSAGES_PER_CACHED_KEY_LIMIT >= maxMessagesEncrypted), 'maxMessagesEncrypted is outside of bounds.')
5555
/* Precondition: partition must be a string. */
5656
needs(partition && typeof partition === 'string', 'partition must be a string.')
5757

5858
readOnlyProperty(obj, '_cache', cache)
5959
readOnlyProperty(obj, '_backingMaterialsManager', backingMaterialsManager)
6060
readOnlyProperty(obj, '_maxAge', maxAge)
61-
readOnlyProperty(obj, '_maxBytesEncrypted', maxBytesEncrypted || Maximum.BYTES_PER_KEY)
62-
readOnlyProperty(obj, '_maxMessagesEncrypted', maxMessagesEncrypted || Maximum.MESSAGES_PER_KEY)
61+
readOnlyProperty(obj, '_maxBytesEncrypted', maxBytesEncrypted || Maximum.BYTES_PER_CACHED_KEY_LIMIT)
62+
readOnlyProperty(obj, '_maxMessagesEncrypted', maxMessagesEncrypted || Maximum.MESSAGES_PER_CACHED_KEY_LIMIT)
6363
readOnlyProperty(obj, '_partition', partition)
6464
}
6565

modules/serialize/src/identifiers.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,25 @@ Object.freeze(SequenceIdentifier)
6161

6262
export enum Maximum {
6363
// Maximum number of messages which are allowed to be encrypted under a single cached data key
64-
MESSAGES_PER_KEY = 2 ** 32, // eslint-disable-line no-unused-vars
65-
// Maximum number of bytes which are allowed to be encrypted under a single cached data key
66-
BYTES_PER_KEY = 2 ** 63 - 1, // eslint-disable-line no-unused-vars
64+
MESSAGES_PER_CACHED_KEY_LIMIT = 2 ** 32, // eslint-disable-line no-unused-vars
65+
/* Maximum number of bytes that are allowed to be encrypted
66+
* under a single cached data key across messages.
67+
* The maximum value defined in the AWS Encryption SDK specification is 2 ** 63 - 1.
68+
* However Javascript can only perform safe operations on values
69+
* up to Number.MAX_SAFE_INTEGER === 9007199254740991 === 2 ** 53 - 1.
70+
* e.g
71+
* Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 => true
72+
* Number.MAX_SAFE_INTEGER + 1 > Number.MAX_SAFE_INTEGER + 2 => false
73+
* Number.MAX_SAFE_INTEGER + 1 < Number.MAX_SAFE_INTEGER + 2 => false
74+
*
75+
* This means that after 2 ** 53 - 1 the process of accumulating a byte count
76+
* will never yield an accurate comparison and so, never halt.
77+
*
78+
* The choice here to use 2 ** 53 - 1 instead of Number.MAX_SAFE_INTEGER is deliberate.
79+
* This is because in the future Number.MAX_SAFE_INTEGER could be raised to 2 ** 66
80+
* or some value larger 2 ** 63.
81+
*/
82+
BYTES_PER_CACHED_KEY_LIMIT = 2 ** 53 - 1, // eslint-disable-line no-unused-vars
6783
// Maximum number of frames allowed in one message as defined in specification
6884
FRAME_COUNT = 2 ** 32 - 1, // eslint-disable-line no-unused-vars
6985
// Maximum bytes allowed in a single frame as defined in specification

0 commit comments

Comments
 (0)