apitally 0.15.3 → 0.16.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 (61) hide show
  1. package/dist/adonisjs/middleware.cjs.map +1 -1
  2. package/dist/adonisjs/middleware.js.map +1 -1
  3. package/dist/adonisjs/provider.cjs +118 -46
  4. package/dist/adonisjs/provider.cjs.map +1 -1
  5. package/dist/adonisjs/provider.js +118 -46
  6. package/dist/adonisjs/provider.js.map +1 -1
  7. package/dist/common/client.cjs +118 -46
  8. package/dist/common/client.cjs.map +1 -1
  9. package/dist/common/client.js +118 -46
  10. package/dist/common/client.js.map +1 -1
  11. package/dist/common/requestLogger.cjs +118 -46
  12. package/dist/common/requestLogger.cjs.map +1 -1
  13. package/dist/common/requestLogger.d.cts +5 -0
  14. package/dist/common/requestLogger.d.ts +5 -0
  15. package/dist/common/requestLogger.js +118 -46
  16. package/dist/common/requestLogger.js.map +1 -1
  17. package/dist/express/index.cjs +118 -46
  18. package/dist/express/index.cjs.map +1 -1
  19. package/dist/express/index.js +118 -46
  20. package/dist/express/index.js.map +1 -1
  21. package/dist/express/middleware.cjs +118 -46
  22. package/dist/express/middleware.cjs.map +1 -1
  23. package/dist/express/middleware.js +118 -46
  24. package/dist/express/middleware.js.map +1 -1
  25. package/dist/fastify/index.cjs +118 -46
  26. package/dist/fastify/index.cjs.map +1 -1
  27. package/dist/fastify/index.js +118 -46
  28. package/dist/fastify/index.js.map +1 -1
  29. package/dist/fastify/plugin.cjs +118 -46
  30. package/dist/fastify/plugin.cjs.map +1 -1
  31. package/dist/fastify/plugin.js +118 -46
  32. package/dist/fastify/plugin.js.map +1 -1
  33. package/dist/h3/index.cjs +118 -46
  34. package/dist/h3/index.cjs.map +1 -1
  35. package/dist/h3/index.js +118 -46
  36. package/dist/h3/index.js.map +1 -1
  37. package/dist/h3/plugin.cjs +118 -46
  38. package/dist/h3/plugin.cjs.map +1 -1
  39. package/dist/h3/plugin.js +118 -46
  40. package/dist/h3/plugin.js.map +1 -1
  41. package/dist/hono/index.cjs +118 -46
  42. package/dist/hono/index.cjs.map +1 -1
  43. package/dist/hono/index.js +118 -46
  44. package/dist/hono/index.js.map +1 -1
  45. package/dist/hono/middleware.cjs +118 -46
  46. package/dist/hono/middleware.cjs.map +1 -1
  47. package/dist/hono/middleware.js +118 -46
  48. package/dist/hono/middleware.js.map +1 -1
  49. package/dist/koa/index.cjs +118 -46
  50. package/dist/koa/index.cjs.map +1 -1
  51. package/dist/koa/index.js +118 -46
  52. package/dist/koa/index.js.map +1 -1
  53. package/dist/koa/middleware.cjs +118 -46
  54. package/dist/koa/middleware.cjs.map +1 -1
  55. package/dist/koa/middleware.js +118 -46
  56. package/dist/koa/middleware.js.map +1 -1
  57. package/dist/nestjs/index.cjs +118 -46
  58. package/dist/nestjs/index.cjs.map +1 -1
  59. package/dist/nestjs/index.js +118 -46
  60. package/dist/nestjs/index.js.map +1 -1
  61. package/package.json +1 -1
@@ -409,6 +409,16 @@ var MASK_HEADER_PATTERNS = [
409
409
  /token/i,
410
410
  /cookie/i
411
411
  ];
412
+ var MASK_BODY_FIELD_PATTERNS = [
413
+ /password/i,
414
+ /pwd/i,
415
+ /token/i,
416
+ /secret/i,
417
+ /auth/i,
418
+ /card[-_ ]?number/i,
419
+ /ccv/i,
420
+ /ssn/i
421
+ ];
412
422
  var DEFAULT_CONFIG = {
413
423
  enabled: false,
414
424
  logQueryParams: true,
@@ -419,6 +429,7 @@ var DEFAULT_CONFIG = {
419
429
  logException: true,
420
430
  maskQueryParams: [],
421
431
  maskHeaders: [],
432
+ maskBodyFields: [],
422
433
  excludePaths: []
423
434
  };
424
435
  var _RequestLogger = class _RequestLogger {
@@ -469,11 +480,23 @@ var _RequestLogger = class _RequestLogger {
469
480
  ];
470
481
  return matchPatterns(name, patterns);
471
482
  }
483
+ shouldMaskBodyField(name) {
484
+ const patterns = [
485
+ ...this.config.maskBodyFields,
486
+ ...MASK_BODY_FIELD_PATTERNS
487
+ ];
488
+ return matchPatterns(name, patterns);
489
+ }
472
490
  hasSupportedContentType(headers) {
473
491
  var _a2;
474
492
  const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
475
493
  return this.isSupportedContentType(contentType);
476
494
  }
495
+ hasJsonContentType(headers) {
496
+ var _a2;
497
+ const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
498
+ return contentType ? /\bjson\b/i.test(contentType) : null;
499
+ }
477
500
  isSupportedContentType(contentType) {
478
501
  return typeof contentType === "string" && ALLOWED_CONTENT_TYPES.some((t) => contentType.startsWith(t));
479
502
  }
@@ -492,6 +515,72 @@ var _RequestLogger = class _RequestLogger {
492
515
  this.shouldMaskHeader(k) ? MASKED : v
493
516
  ]);
494
517
  }
518
+ maskBody(data) {
519
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
520
+ const result = {};
521
+ for (const [key, value] of Object.entries(data)) {
522
+ if (typeof value === "string" && this.shouldMaskBodyField(key)) {
523
+ result[key] = MASKED;
524
+ } else {
525
+ result[key] = this.maskBody(value);
526
+ }
527
+ }
528
+ return result;
529
+ }
530
+ if (Array.isArray(data)) {
531
+ return data.map((item) => this.maskBody(item));
532
+ }
533
+ return data;
534
+ }
535
+ applyMasking(item) {
536
+ if (this.config.maskRequestBodyCallback && item.request.body && item.request.body !== BODY_TOO_LARGE) {
537
+ try {
538
+ const maskedBody = this.config.maskRequestBodyCallback(item.request);
539
+ item.request.body = maskedBody ?? BODY_MASKED;
540
+ } catch {
541
+ item.request.body = void 0;
542
+ }
543
+ }
544
+ if (this.config.maskResponseBodyCallback && item.response.body && item.response.body !== BODY_TOO_LARGE) {
545
+ try {
546
+ const maskedBody = this.config.maskResponseBodyCallback(item.request, item.response);
547
+ item.response.body = maskedBody ?? BODY_MASKED;
548
+ } catch {
549
+ item.response.body = void 0;
550
+ }
551
+ }
552
+ if (item.request.body && item.request.body.length > MAX_BODY_SIZE) {
553
+ item.request.body = BODY_TOO_LARGE;
554
+ }
555
+ if (item.response.body && item.response.body.length > MAX_BODY_SIZE) {
556
+ item.response.body = BODY_TOO_LARGE;
557
+ }
558
+ for (const key of [
559
+ "request",
560
+ "response"
561
+ ]) {
562
+ const bodyData = item[key].body;
563
+ if (!bodyData || bodyData === BODY_TOO_LARGE || bodyData === BODY_MASKED) {
564
+ continue;
565
+ }
566
+ const headers = item[key].headers;
567
+ const hasJsonContent = this.hasJsonContentType(headers);
568
+ if (hasJsonContent === null || hasJsonContent) {
569
+ try {
570
+ const parsedBody = JSON.parse(bodyData.toString());
571
+ const maskedBody = this.maskBody(parsedBody);
572
+ item[key].body = Buffer3.from(JSON.stringify(maskedBody));
573
+ } catch {
574
+ }
575
+ }
576
+ }
577
+ item.request.headers = this.config.logRequestHeaders ? this.maskHeaders(item.request.headers) : [];
578
+ item.response.headers = this.config.logResponseHeaders ? this.maskHeaders(item.response.headers) : [];
579
+ const url = new URL(item.request.url);
580
+ url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
581
+ item.request.url = url.toString();
582
+ return item;
583
+ }
495
584
  logRequest(request, response, error) {
496
585
  var _a2, _b, _c;
497
586
  if (!this.enabled || this.suspendUntil !== null) return;
@@ -501,64 +590,30 @@ var _RequestLogger = class _RequestLogger {
501
590
  if (this.shouldExcludePath(path) || this.shouldExcludeUserAgent(userAgent) || (((_c = (_b = this.config).excludeCallback) == null ? void 0 : _c.call(_b, request, response)) ?? false)) {
502
591
  return;
503
592
  }
504
- url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
505
- request.url = url.toString();
506
593
  if (!this.config.logRequestBody || !this.hasSupportedContentType(request.headers)) {
507
594
  request.body = void 0;
508
- } else if (request.body) {
509
- if (request.body.length > MAX_BODY_SIZE) {
510
- request.body = BODY_TOO_LARGE;
511
- } else if (this.config.maskRequestBodyCallback) {
512
- try {
513
- request.body = this.config.maskRequestBodyCallback(request) ?? BODY_MASKED;
514
- if (request.body.length > MAX_BODY_SIZE) {
515
- request.body = BODY_TOO_LARGE;
516
- }
517
- } catch {
518
- request.body = void 0;
519
- }
520
- }
521
595
  }
522
596
  if (!this.config.logResponseBody || !this.hasSupportedContentType(response.headers)) {
523
597
  response.body = void 0;
524
- } else if (response.body) {
525
- if (response.body.length > MAX_BODY_SIZE) {
526
- response.body = BODY_TOO_LARGE;
527
- } else if (this.config.maskResponseBodyCallback) {
528
- try {
529
- response.body = this.config.maskResponseBodyCallback(request, response) ?? BODY_MASKED;
530
- if (response.body.length > MAX_BODY_SIZE) {
531
- response.body = BODY_TOO_LARGE;
532
- }
533
- } catch {
534
- response.body = void 0;
535
- }
536
- }
537
598
  }
538
- request.headers = this.config.logRequestHeaders ? this.maskHeaders(request.headers) : [];
539
- response.headers = this.config.logResponseHeaders ? this.maskHeaders(response.headers) : [];
599
+ if (request.size !== void 0 && request.size < 0) {
600
+ request.size = void 0;
601
+ }
602
+ if (response.size !== void 0 && response.size < 0) {
603
+ response.size = void 0;
604
+ }
540
605
  const item = {
541
606
  uuid: randomUUID2(),
542
- request: skipEmptyValues(request),
543
- response: skipEmptyValues(response),
607
+ request,
608
+ response,
544
609
  exception: error && this.config.logException ? {
545
610
  type: error.name,
546
611
  message: truncateExceptionMessage(error.message),
547
612
  stacktrace: truncateExceptionStackTrace(error.stack || ""),
548
613
  sentryEventId: getSentryEventId()
549
- } : null
614
+ } : void 0
550
615
  };
551
- [
552
- item.request.body,
553
- item.response.body
554
- ].forEach((body) => {
555
- if (body) {
556
- body.toJSON = function() {
557
- return this.toString("base64");
558
- };
559
- }
560
- });
561
- this.pendingWrites.push(JSON.stringify(item));
616
+ this.pendingWrites.push(item);
562
617
  if (this.pendingWrites.length > MAX_PENDING_WRITES) {
563
618
  this.pendingWrites.shift();
564
619
  }
@@ -572,9 +627,26 @@ var _RequestLogger = class _RequestLogger {
572
627
  this.currentFile = new TempGzipFile();
573
628
  }
574
629
  while (this.pendingWrites.length > 0) {
575
- const item = this.pendingWrites.shift();
630
+ let item = this.pendingWrites.shift();
576
631
  if (item) {
577
- await this.currentFile.writeLine(Buffer3.from(item));
632
+ item = this.applyMasking(item);
633
+ const finalItem = {
634
+ uuid: item.uuid,
635
+ request: skipEmptyValues(item.request),
636
+ response: skipEmptyValues(item.response),
637
+ exception: item.exception
638
+ };
639
+ [
640
+ finalItem.request.body,
641
+ finalItem.response.body
642
+ ].forEach((body) => {
643
+ if (body) {
644
+ body.toJSON = function() {
645
+ return this.toString("base64");
646
+ };
647
+ }
648
+ });
649
+ await this.currentFile.writeLine(Buffer3.from(JSON.stringify(finalItem)));
578
650
  }
579
651
  }
580
652
  });