|
| 1 | +const { Transform } = require('stream'); |
| 2 | +const { errors } = require('arsenal'); |
| 3 | +const { maximumAllowedPartSize } = require('../../../constants'); |
| 4 | + |
| 5 | +/** |
| 6 | + * This class is designed to handle the chunks sent in a streaming |
| 7 | + * unsigned playload trailer request. In this iteration, we are not checking |
| 8 | + * the checksums, but we are removing them from the stream. |
| 9 | + * S3C-9732 will deal with checksum verification. |
| 10 | + */ |
| 11 | +class TrailingChecksumTransform extends Transform { |
| 12 | + /** |
| 13 | + * @constructor |
| 14 | + * @param {object} log - logger object |
| 15 | + */ |
| 16 | + constructor(log) { |
| 17 | + super({}); |
| 18 | + this.log = log; |
| 19 | + this.chunkSizeBuffer = Buffer.alloc(0); |
| 20 | + this.bytesToDiscard = 0; // when trailing \r\n are present, we discard them but they can be in different chunks |
| 21 | + this.bytesToRead = 0; // when a chunk is advertised, the size is put here and we forward all bytes |
| 22 | + this.streamClosed = false; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * This function is executed when there is no more data to be read but before the stream is closed |
| 27 | + * We will verify that the trailing checksum structure was upheld |
| 28 | + * |
| 29 | + * @param {function} callback - Callback(err, data) |
| 30 | + * @return {function} executes callback with err if applicable |
| 31 | + */ |
| 32 | + _flush(callback) { |
| 33 | + if (!this.streamClosed) { |
| 34 | + this.log.error('stream ended without closing chunked encoding'); |
| 35 | + return callback(errors.InvalidArgument); |
| 36 | + } |
| 37 | + return callback(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * This function will remove the trailing checksum from the stream |
| 42 | + * |
| 43 | + * @param {Buffer} chunkInput - chunk from request body |
| 44 | + * @param {string} encoding - Data encoding |
| 45 | + * @param {function} callback - Callback(err, justDataChunk, encoding) |
| 46 | + * @return {function} executes callback with err if applicable |
| 47 | + */ |
| 48 | + _transform(chunkInput, encoding, callback) { |
| 49 | + let chunk = chunkInput; |
| 50 | + while (chunk.byteLength > 0 && !this.streamClosed) { |
| 51 | + if (this.bytesToDiscard > 0) { |
| 52 | + const toDiscard = Math.min(this.bytesToDiscard, chunk.byteLength); |
| 53 | + chunk = chunk.subarray(toDiscard); |
| 54 | + this.bytesToDiscard -= toDiscard; |
| 55 | + continue; |
| 56 | + } |
| 57 | + // forward up to bytesToRead bytes from the chunk, restart processing on leftover |
| 58 | + if (this.bytesToRead > 0) { |
| 59 | + const toRead = Math.min(this.bytesToRead, chunk.byteLength); |
| 60 | + this.push(chunk.subarray(0, toRead)); |
| 61 | + chunk = chunk.subarray(toRead); |
| 62 | + this.bytesToRead -= toRead; |
| 63 | + if (this.bytesToRead === 0) { |
| 64 | + this.bytesToDiscard = 2; |
| 65 | + } |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + // we are now looking for the chunk size field |
| 70 | + // no need to look further than 10 bytes since the field cannot be bigger: the max |
| 71 | + // chunk size is 5GB (see constants.maximumAllowedPartSize) |
| 72 | + const lineBreakIndex = chunk.subarray(0, 10).indexOf('\r'); |
| 73 | + const bytesToKeep = lineBreakIndex === -1 ? chunk.byteLength : lineBreakIndex; |
| 74 | + if (this.chunkSizeBuffer.byteLength + bytesToKeep > 10) { |
| 75 | + this.log.error('chunk size field too big', { |
| 76 | + chunkSizeBuffer: this.chunkSizeBuffer.subarray(0, 11).toString('hex'), |
| 77 | + chunkSizeBufferLength: this.chunkSizeBuffer.length, |
| 78 | + truncatedChunk: chunk.subarray(0, 10).toString('hex'), |
| 79 | + }); |
| 80 | + // if bigger, the chunk would be over 5 GB |
| 81 | + // returning early to avoid a DoS by memory exhaustion |
| 82 | + return callback(errors.InvalidArgument); |
| 83 | + } |
| 84 | + if (lineBreakIndex === -1) { |
| 85 | + // no delimiter, we'll keep the chunk for later |
| 86 | + this.chunkSizeBuffer = Buffer.concat([this.chunkSizeBuffer, chunk]); |
| 87 | + return callback(); |
| 88 | + } |
| 89 | + |
| 90 | + this.chunkSizeBuffer = Buffer.concat([this.chunkSizeBuffer, chunk.subarray(0, lineBreakIndex)]); |
| 91 | + chunk = chunk.subarray(lineBreakIndex); |
| 92 | + |
| 93 | + // chunk-size is sent in hex |
| 94 | + const chunkSizeStr = this.chunkSizeBuffer.toString(); |
| 95 | + const dataSize = parseInt(chunkSizeStr, 16); |
| 96 | + // we check that the parsing is correct (parseInt returns a partial parse when it fails) |
| 97 | + if (isNaN(dataSize) || dataSize.toString(16) !== chunkSizeStr.toLowerCase()) { |
| 98 | + this.log.error('invalid chunk size', { chunkSizeBuffer: chunkSizeStr }); |
| 99 | + return callback(errors.InvalidArgument); |
| 100 | + } |
| 101 | + this.chunkSizeBuffer = Buffer.alloc(0); |
| 102 | + if (dataSize === 0) { |
| 103 | + // TODO: check if the checksum is correct (S3C-9732) |
| 104 | + // last chunk, no more data to read, the stream is closed |
| 105 | + this.streamClosed = true; |
| 106 | + } |
| 107 | + if (dataSize > maximumAllowedPartSize) { |
| 108 | + this.log.error('chunk size too big', { dataSize }); |
| 109 | + return callback(errors.EntityTooLarge); |
| 110 | + } |
| 111 | + this.bytesToRead = dataSize; |
| 112 | + this.bytesToDiscard = 2; |
| 113 | + } |
| 114 | + |
| 115 | + return callback(); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +module.exports = TrailingChecksumTransform; |
0 commit comments