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
@@ -439,6 +439,16 @@ var MASK_HEADER_PATTERNS = [
439
439
  /token/i,
440
440
  /cookie/i
441
441
  ];
442
+ var MASK_BODY_FIELD_PATTERNS = [
443
+ /password/i,
444
+ /pwd/i,
445
+ /token/i,
446
+ /secret/i,
447
+ /auth/i,
448
+ /card[-_ ]?number/i,
449
+ /ccv/i,
450
+ /ssn/i
451
+ ];
442
452
  var DEFAULT_CONFIG = {
443
453
  enabled: false,
444
454
  logQueryParams: true,
@@ -449,6 +459,7 @@ var DEFAULT_CONFIG = {
449
459
  logException: true,
450
460
  maskQueryParams: [],
451
461
  maskHeaders: [],
462
+ maskBodyFields: [],
452
463
  excludePaths: []
453
464
  };
454
465
  var _RequestLogger = class _RequestLogger {
@@ -499,11 +510,23 @@ var _RequestLogger = class _RequestLogger {
499
510
  ];
500
511
  return matchPatterns(name, patterns);
501
512
  }
513
+ shouldMaskBodyField(name) {
514
+ const patterns = [
515
+ ...this.config.maskBodyFields,
516
+ ...MASK_BODY_FIELD_PATTERNS
517
+ ];
518
+ return matchPatterns(name, patterns);
519
+ }
502
520
  hasSupportedContentType(headers) {
503
521
  var _a3;
504
522
  const contentType = (_a3 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a3[1];
505
523
  return this.isSupportedContentType(contentType);
506
524
  }
525
+ hasJsonContentType(headers) {
526
+ var _a3;
527
+ const contentType = (_a3 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a3[1];
528
+ return contentType ? /\bjson\b/i.test(contentType) : null;
529
+ }
507
530
  isSupportedContentType(contentType) {
508
531
  return typeof contentType === "string" && ALLOWED_CONTENT_TYPES.some((t) => contentType.startsWith(t));
509
532
  }
@@ -522,6 +545,72 @@ var _RequestLogger = class _RequestLogger {
522
545
  this.shouldMaskHeader(k) ? MASKED : v
523
546
  ]);
524
547
  }
548
+ maskBody(data) {
549
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
550
+ const result = {};
551
+ for (const [key, value] of Object.entries(data)) {
552
+ if (typeof value === "string" && this.shouldMaskBodyField(key)) {
553
+ result[key] = MASKED;
554
+ } else {
555
+ result[key] = this.maskBody(value);
556
+ }
557
+ }
558
+ return result;
559
+ }
560
+ if (Array.isArray(data)) {
561
+ return data.map((item) => this.maskBody(item));
562
+ }
563
+ return data;
564
+ }
565
+ applyMasking(item) {
566
+ if (this.config.maskRequestBodyCallback && item.request.body && item.request.body !== BODY_TOO_LARGE) {
567
+ try {
568
+ const maskedBody = this.config.maskRequestBodyCallback(item.request);
569
+ item.request.body = maskedBody ?? BODY_MASKED;
570
+ } catch {
571
+ item.request.body = void 0;
572
+ }
573
+ }
574
+ if (this.config.maskResponseBodyCallback && item.response.body && item.response.body !== BODY_TOO_LARGE) {
575
+ try {
576
+ const maskedBody = this.config.maskResponseBodyCallback(item.request, item.response);
577
+ item.response.body = maskedBody ?? BODY_MASKED;
578
+ } catch {
579
+ item.response.body = void 0;
580
+ }
581
+ }
582
+ if (item.request.body && item.request.body.length > MAX_BODY_SIZE) {
583
+ item.request.body = BODY_TOO_LARGE;
584
+ }
585
+ if (item.response.body && item.response.body.length > MAX_BODY_SIZE) {
586
+ item.response.body = BODY_TOO_LARGE;
587
+ }
588
+ for (const key of [
589
+ "request",
590
+ "response"
591
+ ]) {
592
+ const bodyData = item[key].body;
593
+ if (!bodyData || bodyData === BODY_TOO_LARGE || bodyData === BODY_MASKED) {
594
+ continue;
595
+ }
596
+ const headers = item[key].headers;
597
+ const hasJsonContent = this.hasJsonContentType(headers);
598
+ if (hasJsonContent === null || hasJsonContent) {
599
+ try {
600
+ const parsedBody = JSON.parse(bodyData.toString());
601
+ const maskedBody = this.maskBody(parsedBody);
602
+ item[key].body = import_buffer2.Buffer.from(JSON.stringify(maskedBody));
603
+ } catch {
604
+ }
605
+ }
606
+ }
607
+ item.request.headers = this.config.logRequestHeaders ? this.maskHeaders(item.request.headers) : [];
608
+ item.response.headers = this.config.logResponseHeaders ? this.maskHeaders(item.response.headers) : [];
609
+ const url = new URL(item.request.url);
610
+ url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
611
+ item.request.url = url.toString();
612
+ return item;
613
+ }
525
614
  logRequest(request, response, error) {
526
615
  var _a3, _b, _c;
527
616
  if (!this.enabled || this.suspendUntil !== null) return;
@@ -531,64 +620,30 @@ var _RequestLogger = class _RequestLogger {
531
620
  if (this.shouldExcludePath(path) || this.shouldExcludeUserAgent(userAgent) || (((_c = (_b = this.config).excludeCallback) == null ? void 0 : _c.call(_b, request, response)) ?? false)) {
532
621
  return;
533
622
  }
534
- url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
535
- request.url = url.toString();
536
623
  if (!this.config.logRequestBody || !this.hasSupportedContentType(request.headers)) {
537
624
  request.body = void 0;
538
- } else if (request.body) {
539
- if (request.body.length > MAX_BODY_SIZE) {
540
- request.body = BODY_TOO_LARGE;
541
- } else if (this.config.maskRequestBodyCallback) {
542
- try {
543
- request.body = this.config.maskRequestBodyCallback(request) ?? BODY_MASKED;
544
- if (request.body.length > MAX_BODY_SIZE) {
545
- request.body = BODY_TOO_LARGE;
546
- }
547
- } catch {
548
- request.body = void 0;
549
- }
550
- }
551
625
  }
552
626
  if (!this.config.logResponseBody || !this.hasSupportedContentType(response.headers)) {
553
627
  response.body = void 0;
554
- } else if (response.body) {
555
- if (response.body.length > MAX_BODY_SIZE) {
556
- response.body = BODY_TOO_LARGE;
557
- } else if (this.config.maskResponseBodyCallback) {
558
- try {
559
- response.body = this.config.maskResponseBodyCallback(request, response) ?? BODY_MASKED;
560
- if (response.body.length > MAX_BODY_SIZE) {
561
- response.body = BODY_TOO_LARGE;
562
- }
563
- } catch {
564
- response.body = void 0;
565
- }
566
- }
567
628
  }
568
- request.headers = this.config.logRequestHeaders ? this.maskHeaders(request.headers) : [];
569
- response.headers = this.config.logResponseHeaders ? this.maskHeaders(response.headers) : [];
629
+ if (request.size !== void 0 && request.size < 0) {
630
+ request.size = void 0;
631
+ }
632
+ if (response.size !== void 0 && response.size < 0) {
633
+ response.size = void 0;
634
+ }
570
635
  const item = {
571
636
  uuid: (0, import_crypto3.randomUUID)(),
572
- request: skipEmptyValues(request),
573
- response: skipEmptyValues(response),
637
+ request,
638
+ response,
574
639
  exception: error && this.config.logException ? {
575
640
  type: error.name,
576
641
  message: truncateExceptionMessage(error.message),
577
642
  stacktrace: truncateExceptionStackTrace(error.stack || ""),
578
643
  sentryEventId: getSentryEventId()
579
- } : null
644
+ } : void 0
580
645
  };
581
- [
582
- item.request.body,
583
- item.response.body
584
- ].forEach((body) => {
585
- if (body) {
586
- body.toJSON = function() {
587
- return this.toString("base64");
588
- };
589
- }
590
- });
591
- this.pendingWrites.push(JSON.stringify(item));
646
+ this.pendingWrites.push(item);
592
647
  if (this.pendingWrites.length > MAX_PENDING_WRITES) {
593
648
  this.pendingWrites.shift();
594
649
  }
@@ -602,9 +657,26 @@ var _RequestLogger = class _RequestLogger {
602
657
  this.currentFile = new TempGzipFile();
603
658
  }
604
659
  while (this.pendingWrites.length > 0) {
605
- const item = this.pendingWrites.shift();
660
+ let item = this.pendingWrites.shift();
606
661
  if (item) {
607
- await this.currentFile.writeLine(import_buffer2.Buffer.from(item));
662
+ item = this.applyMasking(item);
663
+ const finalItem = {
664
+ uuid: item.uuid,
665
+ request: skipEmptyValues(item.request),
666
+ response: skipEmptyValues(item.response),
667
+ exception: item.exception
668
+ };
669
+ [
670
+ finalItem.request.body,
671
+ finalItem.response.body
672
+ ].forEach((body) => {
673
+ if (body) {
674
+ body.toJSON = function() {
675
+ return this.toString("base64");
676
+ };
677
+ }
678
+ });
679
+ await this.currentFile.writeLine(import_buffer2.Buffer.from(JSON.stringify(finalItem)));
608
680
  }
609
681
  }
610
682
  });