@opensearch-project/opensearch 3.9.0 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/Transport.js +48 -3
  2. package/package.json +1 -1
package/lib/Transport.js CHANGED
@@ -318,11 +318,35 @@ class Transport {
318
318
  // if the response is compressed, we must handle it
319
319
  // as buffer for allowing decompression later
320
320
  let payload = isCompressed ? [] : '';
321
+ // Bound the body as it arrives; content-length is absent on chunked responses.
322
+ let received = 0;
323
+ const decodedLimit = maxDecodedLength();
321
324
  const onData = isCompressed
322
325
  ? (chunk) => {
326
+ received += chunk.length;
327
+ /* istanbul ignore next: needs a >2 GiB compressed body */
328
+ if (received > MAX_BUFFER_LENGTH) {
329
+ response.destroy();
330
+ return onEnd(
331
+ new RequestAbortedError(
332
+ `The response body size is bigger than the maximum allowed buffer (${MAX_BUFFER_LENGTH})`,
333
+ result
334
+ )
335
+ );
336
+ }
323
337
  payload.push(chunk);
324
338
  }
325
339
  : (chunk) => {
340
+ received += chunk.length;
341
+ if (received > decodedLimit) {
342
+ response.destroy();
343
+ return onEnd(
344
+ new RequestAbortedError(
345
+ `The response body size is bigger than the maximum allowed size (${decodedLimit})`,
346
+ result
347
+ )
348
+ );
349
+ }
326
350
  payload += chunk;
327
351
  };
328
352
  const onEnd = (err) => {
@@ -332,11 +356,15 @@ class Transport {
332
356
  response.removeListener('aborted', onAbort);
333
357
 
334
358
  if (err) {
335
- return onConnectionError(new ConnectionError(err.message));
359
+ // A size-limit abort must not be retried; a connection error may be.
360
+ return onConnectionError(
361
+ err instanceof RequestAbortedError ? err : new ConnectionError(err.message)
362
+ );
336
363
  }
337
364
 
338
365
  if (isCompressed) {
339
- unzip(Buffer.concat(payload), onBody);
366
+ // maxOutputLength caps the decoded size; overflow returns an error, not a throw.
367
+ unzip(Buffer.concat(payload), { maxOutputLength: decodedLimit }, onBody);
340
368
  } else {
341
369
  onBody(null, payload);
342
370
  }
@@ -365,13 +393,30 @@ class Transport {
365
393
  return contentLength + heapUsed > HEAP_SIZE_LIMIT * maxPercentage;
366
394
  };
367
395
 
396
+ const maxDecodedLength = () => {
397
+ let limit = MAX_STRING_LENGTH;
398
+ if (this.memoryCircuitBreaker && this.memoryCircuitBreaker.enabled) {
399
+ const maxPercentage = validateMemoryPercentage(this.memoryCircuitBreaker.maxPercentage);
400
+ const heapUsed = process.memoryUsage().heapUsed;
401
+ const heapBudget = Math.floor(HEAP_SIZE_LIMIT * maxPercentage - heapUsed);
402
+ limit = Math.min(limit, Math.max(1, heapBudget));
403
+ }
404
+ return limit;
405
+ };
406
+
368
407
  const onBody = (err, payload) => {
369
408
  if (err) {
370
409
  this.emit('response', err, result);
371
410
  return callback(err, result);
372
411
  }
373
412
  if (Buffer.isBuffer(payload)) {
374
- payload = payload.toString();
413
+ /* istanbul ignore next: unreachable once the output is capped below the max string length */
414
+ try {
415
+ payload = payload.toString();
416
+ } catch (err) {
417
+ this.emit('response', err, result);
418
+ return callback(err, result);
419
+ }
375
420
  }
376
421
  const isHead = params.method === 'HEAD';
377
422
  // we should attempt the payload deserialization only if:
package/package.json CHANGED
@@ -36,7 +36,7 @@
36
36
  "LICENSE.txt"
37
37
  ],
38
38
  "homepage": "https://www.opensearch.org/",
39
- "version": "3.9.0",
39
+ "version": "3.10.0",
40
40
  "versionCanary": "7.10.0-canary.6",
41
41
  "keywords": [
42
42
  "opensearch",