apitally 0.15.3 → 0.16.1

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 (65) 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 +126 -50
  18. package/dist/express/index.cjs.map +1 -1
  19. package/dist/express/index.js +126 -50
  20. package/dist/express/index.js.map +1 -1
  21. package/dist/express/middleware.cjs +126 -50
  22. package/dist/express/middleware.cjs.map +1 -1
  23. package/dist/express/middleware.js +126 -50
  24. package/dist/express/middleware.js.map +1 -1
  25. package/dist/express/utils.cjs +8 -4
  26. package/dist/express/utils.cjs.map +1 -1
  27. package/dist/express/utils.js +8 -4
  28. package/dist/express/utils.js.map +1 -1
  29. package/dist/fastify/index.cjs +118 -46
  30. package/dist/fastify/index.cjs.map +1 -1
  31. package/dist/fastify/index.js +118 -46
  32. package/dist/fastify/index.js.map +1 -1
  33. package/dist/fastify/plugin.cjs +118 -46
  34. package/dist/fastify/plugin.cjs.map +1 -1
  35. package/dist/fastify/plugin.js +118 -46
  36. package/dist/fastify/plugin.js.map +1 -1
  37. package/dist/h3/index.cjs +118 -46
  38. package/dist/h3/index.cjs.map +1 -1
  39. package/dist/h3/index.js +118 -46
  40. package/dist/h3/index.js.map +1 -1
  41. package/dist/h3/plugin.cjs +118 -46
  42. package/dist/h3/plugin.cjs.map +1 -1
  43. package/dist/h3/plugin.js +118 -46
  44. package/dist/h3/plugin.js.map +1 -1
  45. package/dist/hono/index.cjs +118 -46
  46. package/dist/hono/index.cjs.map +1 -1
  47. package/dist/hono/index.js +118 -46
  48. package/dist/hono/index.js.map +1 -1
  49. package/dist/hono/middleware.cjs +118 -46
  50. package/dist/hono/middleware.cjs.map +1 -1
  51. package/dist/hono/middleware.js +118 -46
  52. package/dist/hono/middleware.js.map +1 -1
  53. package/dist/koa/index.cjs +118 -46
  54. package/dist/koa/index.cjs.map +1 -1
  55. package/dist/koa/index.js +118 -46
  56. package/dist/koa/index.js.map +1 -1
  57. package/dist/koa/middleware.cjs +118 -46
  58. package/dist/koa/middleware.cjs.map +1 -1
  59. package/dist/koa/middleware.js +118 -46
  60. package/dist/koa/middleware.js.map +1 -1
  61. package/dist/nestjs/index.cjs +126 -50
  62. package/dist/nestjs/index.cjs.map +1 -1
  63. package/dist/nestjs/index.js +126 -50
  64. package/dist/nestjs/index.js.map +1 -1
  65. package/package.json +1 -1
@@ -436,6 +436,16 @@ var MASK_HEADER_PATTERNS = [
436
436
  /token/i,
437
437
  /cookie/i
438
438
  ];
439
+ var MASK_BODY_FIELD_PATTERNS = [
440
+ /password/i,
441
+ /pwd/i,
442
+ /token/i,
443
+ /secret/i,
444
+ /auth/i,
445
+ /card[-_ ]?number/i,
446
+ /ccv/i,
447
+ /ssn/i
448
+ ];
439
449
  var DEFAULT_CONFIG = {
440
450
  enabled: false,
441
451
  logQueryParams: true,
@@ -446,6 +456,7 @@ var DEFAULT_CONFIG = {
446
456
  logException: true,
447
457
  maskQueryParams: [],
448
458
  maskHeaders: [],
459
+ maskBodyFields: [],
449
460
  excludePaths: []
450
461
  };
451
462
  var _RequestLogger = class _RequestLogger {
@@ -496,11 +507,23 @@ var _RequestLogger = class _RequestLogger {
496
507
  ];
497
508
  return matchPatterns(name, patterns);
498
509
  }
510
+ shouldMaskBodyField(name) {
511
+ const patterns = [
512
+ ...this.config.maskBodyFields,
513
+ ...MASK_BODY_FIELD_PATTERNS
514
+ ];
515
+ return matchPatterns(name, patterns);
516
+ }
499
517
  hasSupportedContentType(headers) {
500
518
  var _a2;
501
519
  const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
502
520
  return this.isSupportedContentType(contentType);
503
521
  }
522
+ hasJsonContentType(headers) {
523
+ var _a2;
524
+ const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
525
+ return contentType ? /\bjson\b/i.test(contentType) : null;
526
+ }
504
527
  isSupportedContentType(contentType) {
505
528
  return typeof contentType === "string" && ALLOWED_CONTENT_TYPES.some((t) => contentType.startsWith(t));
506
529
  }
@@ -519,6 +542,72 @@ var _RequestLogger = class _RequestLogger {
519
542
  this.shouldMaskHeader(k) ? MASKED : v
520
543
  ]);
521
544
  }
545
+ maskBody(data) {
546
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
547
+ const result = {};
548
+ for (const [key, value] of Object.entries(data)) {
549
+ if (typeof value === "string" && this.shouldMaskBodyField(key)) {
550
+ result[key] = MASKED;
551
+ } else {
552
+ result[key] = this.maskBody(value);
553
+ }
554
+ }
555
+ return result;
556
+ }
557
+ if (Array.isArray(data)) {
558
+ return data.map((item) => this.maskBody(item));
559
+ }
560
+ return data;
561
+ }
562
+ applyMasking(item) {
563
+ if (this.config.maskRequestBodyCallback && item.request.body && item.request.body !== BODY_TOO_LARGE) {
564
+ try {
565
+ const maskedBody = this.config.maskRequestBodyCallback(item.request);
566
+ item.request.body = maskedBody ?? BODY_MASKED;
567
+ } catch {
568
+ item.request.body = void 0;
569
+ }
570
+ }
571
+ if (this.config.maskResponseBodyCallback && item.response.body && item.response.body !== BODY_TOO_LARGE) {
572
+ try {
573
+ const maskedBody = this.config.maskResponseBodyCallback(item.request, item.response);
574
+ item.response.body = maskedBody ?? BODY_MASKED;
575
+ } catch {
576
+ item.response.body = void 0;
577
+ }
578
+ }
579
+ if (item.request.body && item.request.body.length > MAX_BODY_SIZE) {
580
+ item.request.body = BODY_TOO_LARGE;
581
+ }
582
+ if (item.response.body && item.response.body.length > MAX_BODY_SIZE) {
583
+ item.response.body = BODY_TOO_LARGE;
584
+ }
585
+ for (const key of [
586
+ "request",
587
+ "response"
588
+ ]) {
589
+ const bodyData = item[key].body;
590
+ if (!bodyData || bodyData === BODY_TOO_LARGE || bodyData === BODY_MASKED) {
591
+ continue;
592
+ }
593
+ const headers = item[key].headers;
594
+ const hasJsonContent = this.hasJsonContentType(headers);
595
+ if (hasJsonContent === null || hasJsonContent) {
596
+ try {
597
+ const parsedBody = JSON.parse(bodyData.toString());
598
+ const maskedBody = this.maskBody(parsedBody);
599
+ item[key].body = import_buffer2.Buffer.from(JSON.stringify(maskedBody));
600
+ } catch {
601
+ }
602
+ }
603
+ }
604
+ item.request.headers = this.config.logRequestHeaders ? this.maskHeaders(item.request.headers) : [];
605
+ item.response.headers = this.config.logResponseHeaders ? this.maskHeaders(item.response.headers) : [];
606
+ const url = new URL(item.request.url);
607
+ url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
608
+ item.request.url = url.toString();
609
+ return item;
610
+ }
522
611
  logRequest(request, response, error) {
523
612
  var _a2, _b, _c;
524
613
  if (!this.enabled || this.suspendUntil !== null) return;
@@ -528,64 +617,30 @@ var _RequestLogger = class _RequestLogger {
528
617
  if (this.shouldExcludePath(path) || this.shouldExcludeUserAgent(userAgent) || (((_c = (_b = this.config).excludeCallback) == null ? void 0 : _c.call(_b, request, response)) ?? false)) {
529
618
  return;
530
619
  }
531
- url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
532
- request.url = url.toString();
533
620
  if (!this.config.logRequestBody || !this.hasSupportedContentType(request.headers)) {
534
621
  request.body = void 0;
535
- } else if (request.body) {
536
- if (request.body.length > MAX_BODY_SIZE) {
537
- request.body = BODY_TOO_LARGE;
538
- } else if (this.config.maskRequestBodyCallback) {
539
- try {
540
- request.body = this.config.maskRequestBodyCallback(request) ?? BODY_MASKED;
541
- if (request.body.length > MAX_BODY_SIZE) {
542
- request.body = BODY_TOO_LARGE;
543
- }
544
- } catch {
545
- request.body = void 0;
546
- }
547
- }
548
622
  }
549
623
  if (!this.config.logResponseBody || !this.hasSupportedContentType(response.headers)) {
550
624
  response.body = void 0;
551
- } else if (response.body) {
552
- if (response.body.length > MAX_BODY_SIZE) {
553
- response.body = BODY_TOO_LARGE;
554
- } else if (this.config.maskResponseBodyCallback) {
555
- try {
556
- response.body = this.config.maskResponseBodyCallback(request, response) ?? BODY_MASKED;
557
- if (response.body.length > MAX_BODY_SIZE) {
558
- response.body = BODY_TOO_LARGE;
559
- }
560
- } catch {
561
- response.body = void 0;
562
- }
563
- }
564
625
  }
565
- request.headers = this.config.logRequestHeaders ? this.maskHeaders(request.headers) : [];
566
- response.headers = this.config.logResponseHeaders ? this.maskHeaders(response.headers) : [];
626
+ if (request.size !== void 0 && request.size < 0) {
627
+ request.size = void 0;
628
+ }
629
+ if (response.size !== void 0 && response.size < 0) {
630
+ response.size = void 0;
631
+ }
567
632
  const item = {
568
633
  uuid: (0, import_crypto3.randomUUID)(),
569
- request: skipEmptyValues(request),
570
- response: skipEmptyValues(response),
634
+ request,
635
+ response,
571
636
  exception: error && this.config.logException ? {
572
637
  type: error.name,
573
638
  message: truncateExceptionMessage(error.message),
574
639
  stacktrace: truncateExceptionStackTrace(error.stack || ""),
575
640
  sentryEventId: getSentryEventId()
576
- } : null
641
+ } : void 0
577
642
  };
578
- [
579
- item.request.body,
580
- item.response.body
581
- ].forEach((body) => {
582
- if (body) {
583
- body.toJSON = function() {
584
- return this.toString("base64");
585
- };
586
- }
587
- });
588
- this.pendingWrites.push(JSON.stringify(item));
643
+ this.pendingWrites.push(item);
589
644
  if (this.pendingWrites.length > MAX_PENDING_WRITES) {
590
645
  this.pendingWrites.shift();
591
646
  }
@@ -599,9 +654,26 @@ var _RequestLogger = class _RequestLogger {
599
654
  this.currentFile = new TempGzipFile();
600
655
  }
601
656
  while (this.pendingWrites.length > 0) {
602
- const item = this.pendingWrites.shift();
657
+ let item = this.pendingWrites.shift();
603
658
  if (item) {
604
- await this.currentFile.writeLine(import_buffer2.Buffer.from(item));
659
+ item = this.applyMasking(item);
660
+ const finalItem = {
661
+ uuid: item.uuid,
662
+ request: skipEmptyValues(item.request),
663
+ response: skipEmptyValues(item.response),
664
+ exception: item.exception
665
+ };
666
+ [
667
+ finalItem.request.body,
668
+ finalItem.response.body
669
+ ].forEach((body) => {
670
+ if (body) {
671
+ body.toJSON = function() {
672
+ return this.toString("base64");
673
+ };
674
+ }
675
+ });
676
+ await this.currentFile.writeLine(import_buffer2.Buffer.from(JSON.stringify(finalItem)));
605
677
  }
606
678
  }
607
679
  });