15
15
16
16
import {
17
17
serializeFactory , aadFactory ,
18
- MessageHeader // eslint-disable-line no-unused-vars
18
+ MessageHeader , // eslint-disable-line no-unused-vars
19
+ Maximum
19
20
} from '@aws-crypto/serialize'
20
21
// @ts -ignore
21
22
import { Transform as PortableTransform } from 'readable-stream'
@@ -49,11 +50,17 @@ const ioTick = () => new Promise(resolve => setImmediate(resolve))
49
50
const noop = ( ) => { }
50
51
type ErrBack = ( err ?: Error ) => void
51
52
52
- export function getFramedEncryptStream ( getCipher : GetCipher , messageHeader : MessageHeader , dispose : Function ) {
53
+ export function getFramedEncryptStream ( getCipher : GetCipher , messageHeader : MessageHeader , dispose : Function , plaintextLength ?: number ) {
53
54
let accumulatingFrame : AccumulatingFrame = { contentLength : 0 , content : [ ] , sequenceNumber : 1 }
54
55
let pathologicalDrain : Function = noop
55
56
const { frameLength } = messageHeader
56
57
58
+ /* Precondition: plaintextLength must be within bounds.
59
+ * The Maximum.BYTES_PER_MESSAGE is set to be within Number.MAX_SAFE_INTEGER
60
+ * See serialize/identifiers.ts enum Maximum for more details.
61
+ */
62
+ needs ( ! plaintextLength || ( plaintextLength >= 0 && Maximum . BYTES_PER_MESSAGE >= plaintextLength ) , 'plaintextLength out of bounds.' )
63
+
57
64
/* Keeping the messageHeader, accumulatingFrame and pathologicalDrain private is the intention here.
58
65
* It is already unlikely that these values could be touched in the current composition of streams,
59
66
* but a different composition may change this.
@@ -63,6 +70,11 @@ export function getFramedEncryptStream (getCipher: GetCipher, messageHeader: Mes
63
70
_transform ( chunk : Buffer , encoding : string , callback : ErrBack ) {
64
71
const contentLeft = frameLength - accumulatingFrame . contentLength
65
72
73
+ /* Precondition: Must not process more than plaintextLength.
74
+ * The plaintextLength is the MAXIMUM value that can be encrypted.
75
+ */
76
+ needs ( ! plaintextLength || ( plaintextLength -= chunk . length ) >= 0 , 'Encrypted data exceeded plaintextLength.' )
77
+
66
78
/* Check for early return (Postcondition): Have not accumulated a frame. */
67
79
if ( contentLeft > chunk . length ) {
68
80
// eat more
0 commit comments