@nest-omni/core 4.1.3-3 → 4.1.3-31
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.
- package/audit/audit.module.d.ts +1 -0
- package/audit/audit.module.js +51 -2
- package/audit/controllers/audit.controller.d.ts +57 -1
- package/audit/controllers/audit.controller.js +43 -0
- package/audit/decorators/audit-action.decorator.d.ts +74 -0
- package/audit/decorators/audit-action.decorator.js +42 -0
- package/audit/decorators/audit-controller.decorator.d.ts +1 -1
- package/audit/decorators/audit-controller.decorator.js +2 -2
- package/audit/decorators/audit-operation.decorator.d.ts +0 -7
- package/audit/decorators/audit-operation.decorator.js +0 -7
- package/audit/decorators/entity-audit.decorator.d.ts +78 -2
- package/audit/decorators/entity-audit.decorator.js +145 -4
- package/audit/decorators/index.d.ts +2 -0
- package/audit/decorators/index.js +2 -0
- package/audit/dto/audit-action-query.dto.d.ts +13 -0
- package/audit/dto/audit-action-query.dto.js +77 -0
- package/audit/dto/index.d.ts +1 -0
- package/audit/dto/index.js +1 -0
- package/audit/entities/audit-action-summary.entity.d.ts +23 -0
- package/audit/entities/audit-action-summary.entity.js +101 -0
- package/audit/entities/entity-audit-log.entity.d.ts +7 -2
- package/audit/entities/entity-audit-log.entity.js +45 -9
- package/audit/entities/entity-transaction.entity.d.ts +8 -2
- package/audit/entities/entity-transaction.entity.js +39 -3
- package/audit/entities/index.d.ts +3 -0
- package/audit/entities/index.js +3 -0
- package/audit/entities/manual-operation-log.entity.d.ts +0 -2
- package/audit/entities/manual-operation-log.entity.js +8 -9
- package/audit/enums/audit.enums.d.ts +14 -6
- package/audit/enums/audit.enums.js +18 -9
- package/audit/examples/decorator-value-mapping.example.d.ts +70 -0
- package/audit/examples/decorator-value-mapping.example.js +414 -0
- package/audit/index.d.ts +5 -1
- package/audit/index.js +29 -2
- package/audit/interceptors/audit-action.interceptor.d.ts +39 -0
- package/audit/interceptors/audit-action.interceptor.js +217 -0
- package/audit/interceptors/audit.interceptor.d.ts +1 -0
- package/audit/interceptors/audit.interceptor.js +19 -11
- package/audit/interceptors/index.d.ts +1 -0
- package/audit/interceptors/index.js +1 -0
- package/audit/interfaces/audit.interfaces.d.ts +132 -4
- package/audit/services/audit-action.service.d.ts +142 -0
- package/audit/services/audit-action.service.js +246 -0
- package/audit/services/audit-context.service.d.ts +91 -0
- package/audit/services/audit-context.service.js +170 -0
- package/audit/services/entity-audit.service.d.ts +220 -9
- package/audit/services/entity-audit.service.js +761 -72
- package/audit/services/index.d.ts +3 -0
- package/audit/services/index.js +3 -0
- package/audit/services/manual-audit-log.service.d.ts +23 -23
- package/audit/services/manual-audit-log.service.js +34 -57
- package/audit/services/multi-database.service.d.ts +0 -5
- package/audit/services/multi-database.service.js +0 -24
- package/audit/services/operation-description.service.d.ts +14 -3
- package/audit/services/operation-description.service.js +165 -26
- package/audit/services/transaction-audit.service.d.ts +1 -0
- package/audit/services/transaction-audit.service.js +12 -9
- package/audit/subscribers/entity-audit.subscriber.d.ts +5 -0
- package/audit/subscribers/entity-audit.subscriber.js +69 -5
- package/cache/cache.module.d.ts +7 -8
- package/cache/cache.module.js +15 -13
- package/cache/cache.service.d.ts +6 -4
- package/cache/cache.service.js +24 -12
- package/cache/decorators/cache-put.decorator.js +5 -4
- package/cache/dependencies/callback.dependency.js +9 -0
- package/cache/dependencies/db.dependency.d.ts +43 -12
- package/cache/dependencies/db.dependency.js +46 -18
- package/cache/dependencies/tag.dependency.d.ts +31 -4
- package/cache/dependencies/tag.dependency.js +100 -11
- package/cache/entities/index.d.ts +1 -0
- package/cache/entities/index.js +17 -0
- package/cache/entities/typeorm-cache.entity.d.ts +71 -0
- package/cache/entities/typeorm-cache.entity.js +110 -0
- package/cache/index.d.ts +2 -1
- package/cache/index.js +19 -2
- package/cache/interfaces/cache-options.interface.d.ts +8 -0
- package/cache/providers/index.d.ts +2 -1
- package/cache/providers/index.js +2 -1
- package/cache/providers/lrucache.provider.d.ts +77 -0
- package/cache/providers/lrucache.provider.js +228 -0
- package/cache/providers/redis-cache.provider.d.ts +1 -0
- package/cache/providers/redis-cache.provider.js +8 -6
- package/cache/providers/typeorm-cache.provider.d.ts +211 -0
- package/cache/providers/typeorm-cache.provider.js +483 -0
- package/common/boilerplate.polyfill.d.ts +1 -0
- package/common/boilerplate.polyfill.js +18 -1
- package/common/helpers/validation-metadata-helper.d.ts +112 -0
- package/common/helpers/validation-metadata-helper.js +164 -0
- package/common/index.d.ts +1 -0
- package/common/index.js +4 -0
- package/decorators/examples/field-i18n.example.d.ts +294 -0
- package/decorators/examples/field-i18n.example.js +478 -0
- package/decorators/field.decorators.d.ts +95 -3
- package/decorators/field.decorators.js +152 -18
- package/decorators/transform.decorators.d.ts +0 -2
- package/decorators/transform.decorators.js +0 -23
- package/decorators/translate.decorator.d.ts +26 -0
- package/decorators/translate.decorator.js +26 -1
- package/email-log/email-log.constants.d.ts +8 -0
- package/email-log/email-log.constants.js +11 -0
- package/email-log/email-log.module.d.ts +47 -0
- package/email-log/email-log.module.js +140 -0
- package/email-log/index.d.ts +11 -0
- package/email-log/index.js +48 -0
- package/email-log/interfaces/email-log-options.interface.d.ts +61 -0
- package/email-log/interfaces/email-log-options.interface.js +134 -0
- package/email-log/interfaces/email-log-transport.interface.d.ts +20 -0
- package/email-log/interfaces/email-log-transport.interface.js +2 -0
- package/email-log/interfaces/index.d.ts +2 -0
- package/email-log/interfaces/index.js +18 -0
- package/email-log/providers/email-provider.d.ts +42 -0
- package/email-log/providers/email-provider.js +127 -0
- package/email-log/providers/index.d.ts +1 -0
- package/email-log/providers/index.js +17 -0
- package/email-log/services/email-log-alert.service.d.ts +46 -0
- package/email-log/services/email-log-alert.service.js +162 -0
- package/email-log/services/email-log-formatter.service.d.ts +78 -0
- package/email-log/services/email-log-formatter.service.js +442 -0
- package/email-log/services/email-log-logger.service.d.ts +85 -0
- package/email-log/services/email-log-logger.service.js +168 -0
- package/email-log/services/email-log-rate-limiter.service.d.ts +42 -0
- package/email-log/services/email-log-rate-limiter.service.js +110 -0
- package/email-log/services/email-log-transport.service.d.ts +80 -0
- package/email-log/services/email-log-transport.service.js +271 -0
- package/email-log/services/index.d.ts +5 -0
- package/email-log/services/index.js +21 -0
- package/email-log/transports/index.d.ts +1 -0
- package/email-log/transports/index.js +17 -0
- package/email-log/transports/pino-email.transport.d.ts +56 -0
- package/email-log/transports/pino-email.transport.js +188 -0
- package/email-log/utils/index.d.ts +2 -0
- package/email-log/utils/index.js +18 -0
- package/email-log/utils/log-level.helper.d.ts +46 -0
- package/email-log/utils/log-level.helper.js +74 -0
- package/email-log/utils/pino-transport.utils.d.ts +135 -0
- package/email-log/utils/pino-transport.utils.js +238 -0
- package/file-upload/controllers/file-access.controller.d.ts +23 -0
- package/file-upload/controllers/file-access.controller.js +128 -0
- package/file-upload/decorators/column.decorator.d.ts +151 -0
- package/file-upload/decorators/column.decorator.js +273 -0
- package/file-upload/decorators/csv-data.decorator.d.ts +30 -0
- package/file-upload/decorators/csv-data.decorator.js +85 -0
- package/file-upload/decorators/csv-import.decorator.d.ts +34 -0
- package/file-upload/decorators/csv-import.decorator.js +24 -0
- package/file-upload/decorators/examples/column-mapping.example.d.ts +76 -0
- package/file-upload/decorators/examples/column-mapping.example.js +122 -0
- package/file-upload/decorators/excel-data.decorator.d.ts +30 -0
- package/file-upload/decorators/excel-data.decorator.js +85 -0
- package/file-upload/decorators/file-upload.decorator.d.ts +83 -0
- package/file-upload/decorators/file-upload.decorator.js +172 -0
- package/file-upload/decorators/index.d.ts +5 -0
- package/file-upload/decorators/index.js +38 -0
- package/file-upload/decorators/process.decorator.d.ts +40 -0
- package/file-upload/decorators/process.decorator.js +52 -0
- package/file-upload/decorators/validate-data.decorator.d.ts +91 -0
- package/file-upload/decorators/validate-data.decorator.js +39 -0
- package/file-upload/dto/create-file.dto.d.ts +24 -0
- package/file-upload/dto/create-file.dto.js +112 -0
- package/file-upload/dto/find-files.dto.d.ts +15 -0
- package/file-upload/dto/find-files.dto.js +76 -0
- package/file-upload/dto/index.d.ts +4 -0
- package/file-upload/dto/index.js +20 -0
- package/file-upload/dto/pagination.dto.d.ts +7 -0
- package/file-upload/dto/pagination.dto.js +39 -0
- package/file-upload/dto/update-file.dto.d.ts +15 -0
- package/file-upload/dto/update-file.dto.js +67 -0
- package/file-upload/entities/file-metadata.entity.d.ts +25 -0
- package/file-upload/entities/file-metadata.entity.js +76 -0
- package/file-upload/entities/file.entity.d.ts +114 -0
- package/file-upload/entities/file.entity.js +350 -0
- package/file-upload/entities/index.d.ts +2 -0
- package/file-upload/entities/index.js +18 -0
- package/file-upload/enums/file-type.enum.d.ts +72 -0
- package/file-upload/enums/file-type.enum.js +212 -0
- package/file-upload/exceptions/file-upload.exception.d.ts +57 -0
- package/file-upload/exceptions/file-upload.exception.js +120 -0
- package/file-upload/exceptions/index.d.ts +1 -0
- package/file-upload/exceptions/index.js +17 -0
- package/file-upload/file-upload.module.d.ts +89 -0
- package/file-upload/file-upload.module.js +292 -0
- package/file-upload/index.d.ts +37 -0
- package/file-upload/index.js +77 -0
- package/file-upload/interceptors/file-upload.interceptor.d.ts +101 -0
- package/file-upload/interceptors/file-upload.interceptor.js +594 -0
- package/file-upload/interceptors/index.d.ts +1 -0
- package/file-upload/interceptors/index.js +17 -0
- package/file-upload/interfaces/custom-file-type.interface.d.ts +72 -0
- package/file-upload/interfaces/custom-file-type.interface.js +2 -0
- package/file-upload/interfaces/file-buffer.interface.d.ts +72 -0
- package/file-upload/interfaces/file-buffer.interface.js +2 -0
- package/file-upload/interfaces/file-entity.interface.d.ts +142 -0
- package/file-upload/interfaces/file-entity.interface.js +28 -0
- package/file-upload/interfaces/file-metadata.interface.d.ts +21 -0
- package/file-upload/interfaces/file-metadata.interface.js +2 -0
- package/file-upload/interfaces/file-processor.interface.d.ts +93 -0
- package/file-upload/interfaces/file-processor.interface.js +2 -0
- package/file-upload/interfaces/file-upload-options.interface.d.ts +74 -0
- package/file-upload/interfaces/file-upload-options.interface.js +5 -0
- package/file-upload/interfaces/index.d.ts +7 -0
- package/file-upload/interfaces/index.js +24 -0
- package/file-upload/interfaces/processor-options.interface.d.ts +102 -0
- package/file-upload/interfaces/processor-options.interface.js +2 -0
- package/file-upload/interfaces/storage-provider.interface.d.ts +239 -0
- package/file-upload/interfaces/storage-provider.interface.js +2 -0
- package/file-upload/interfaces/upload-options.interface.d.ts +19 -0
- package/file-upload/interfaces/upload-options.interface.js +2 -0
- package/file-upload/processors/csv.processor.d.ts +98 -0
- package/file-upload/processors/csv.processor.js +391 -0
- package/file-upload/processors/excel.processor.d.ts +130 -0
- package/file-upload/processors/excel.processor.js +547 -0
- package/file-upload/processors/image.processor.d.ts +199 -0
- package/file-upload/processors/image.processor.js +377 -0
- package/file-upload/providers/index.d.ts +2 -0
- package/file-upload/providers/index.js +18 -0
- package/file-upload/providers/local-storage.provider.d.ts +98 -0
- package/file-upload/providers/local-storage.provider.js +484 -0
- package/file-upload/providers/s3-storage.provider.d.ts +87 -0
- package/file-upload/providers/s3-storage.provider.js +455 -0
- package/file-upload/services/file-signature-validator.service.d.ts +118 -0
- package/file-upload/services/file-signature-validator.service.js +376 -0
- package/file-upload/services/file.service.d.ts +193 -0
- package/file-upload/services/file.service.js +638 -0
- package/file-upload/services/index.d.ts +4 -0
- package/file-upload/services/index.js +20 -0
- package/file-upload/services/malicious-file-detector.service.d.ts +300 -0
- package/file-upload/services/malicious-file-detector.service.js +1234 -0
- package/file-upload/services/mime-registry.service.d.ts +47 -0
- package/file-upload/services/mime-registry.service.js +167 -0
- package/file-upload/utils/checksum.util.d.ts +28 -0
- package/file-upload/utils/checksum.util.js +65 -0
- package/file-upload/utils/dynamic-import.util.d.ts +54 -0
- package/file-upload/utils/dynamic-import.util.js +156 -0
- package/file-upload/utils/filename.util.d.ts +59 -0
- package/file-upload/utils/filename.util.js +184 -0
- package/file-upload/utils/filepath.util.d.ts +70 -0
- package/file-upload/utils/filepath.util.js +152 -0
- package/file-upload/utils/index.d.ts +4 -0
- package/file-upload/utils/index.js +20 -0
- package/filters/bad-request.filter.d.ts +9 -0
- package/filters/bad-request.filter.js +57 -16
- package/http-client/config/http-client.config.d.ts +5 -0
- package/http-client/config/http-client.config.js +27 -14
- package/http-client/decorators/http-client.decorators.d.ts +7 -28
- package/http-client/decorators/http-client.decorators.js +124 -99
- package/http-client/entities/http-log.entity.d.ts +0 -20
- package/http-client/entities/http-log.entity.js +1 -21
- package/http-client/examples/advanced-usage.example.d.ts +4 -5
- package/http-client/examples/advanced-usage.example.js +7 -59
- package/http-client/examples/axios-config-extended.example.d.ts +17 -0
- package/http-client/examples/axios-config-extended.example.js +311 -0
- package/http-client/examples/flexible-response-example.d.ts +28 -0
- package/http-client/examples/flexible-response-example.js +120 -0
- package/http-client/examples/index.d.ts +2 -0
- package/http-client/examples/index.js +2 -0
- package/http-client/examples/proxy-from-environment.example.d.ts +133 -0
- package/http-client/examples/proxy-from-environment.example.js +409 -0
- package/http-client/examples/ssl-certificate.example.d.ts +47 -0
- package/http-client/examples/ssl-certificate.example.js +432 -0
- package/http-client/http-client.module.d.ts +43 -2
- package/http-client/http-client.module.js +150 -90
- package/http-client/index.d.ts +1 -1
- package/http-client/interfaces/api-client-config.interface.d.ts +24 -103
- package/http-client/interfaces/http-client-config.interface.d.ts +137 -62
- package/http-client/services/api-client-registry.service.d.ts +8 -21
- package/http-client/services/api-client-registry.service.js +31 -282
- package/http-client/services/circuit-breaker.service.d.ts +69 -2
- package/http-client/services/circuit-breaker.service.js +185 -7
- package/http-client/services/http-client.service.d.ts +85 -23
- package/http-client/services/http-client.service.js +512 -168
- package/http-client/services/http-log-query.service.js +0 -13
- package/http-client/services/index.d.ts +0 -1
- package/http-client/services/index.js +0 -1
- package/http-client/services/logging.service.d.ts +69 -16
- package/http-client/services/logging.service.js +290 -170
- package/http-client/utils/call-stack-extractor.util.d.ts +26 -0
- package/http-client/utils/call-stack-extractor.util.js +35 -0
- package/http-client/utils/context-extractor.util.d.ts +2 -0
- package/http-client/utils/context-extractor.util.js +17 -3
- package/http-client/utils/curl-generator.util.js +2 -5
- package/http-client/utils/index.d.ts +2 -0
- package/http-client/utils/index.js +2 -0
- package/http-client/utils/proxy-environment.util.d.ts +42 -0
- package/http-client/utils/proxy-environment.util.js +154 -0
- package/http-client/utils/retry-recorder.util.d.ts +0 -4
- package/http-client/utils/retry-recorder.util.js +2 -27
- package/http-client/utils/sanitize.util.d.ts +58 -0
- package/http-client/utils/sanitize.util.js +188 -0
- package/http-client/utils/security-validator.util.d.ts +118 -0
- package/http-client/utils/security-validator.util.js +354 -0
- package/index.d.ts +4 -1
- package/index.js +6 -1
- package/interceptors/translation-interceptor.service.d.ts +7 -0
- package/interceptors/translation-interceptor.service.js +40 -8
- package/ip-filter/constants.d.ts +21 -0
- package/ip-filter/constants.js +24 -0
- package/ip-filter/decorators/index.d.ts +1 -0
- package/ip-filter/decorators/index.js +17 -0
- package/ip-filter/decorators/ip-filter.decorator.d.ts +58 -0
- package/ip-filter/decorators/ip-filter.decorator.js +79 -0
- package/ip-filter/guards/index.d.ts +1 -0
- package/ip-filter/guards/index.js +17 -0
- package/ip-filter/guards/ip-filter.guard.d.ts +62 -0
- package/ip-filter/guards/ip-filter.guard.js +174 -0
- package/ip-filter/index.d.ts +7 -0
- package/ip-filter/index.js +23 -0
- package/ip-filter/interfaces/index.d.ts +4 -0
- package/ip-filter/interfaces/index.js +20 -0
- package/ip-filter/interfaces/ip-filter-async-options.interface.d.ts +15 -0
- package/ip-filter/interfaces/ip-filter-async-options.interface.js +2 -0
- package/ip-filter/interfaces/ip-filter-metadata.interface.d.ts +26 -0
- package/ip-filter/interfaces/ip-filter-metadata.interface.js +2 -0
- package/ip-filter/interfaces/ip-filter-options.interface.d.ts +34 -0
- package/ip-filter/interfaces/ip-filter-options.interface.js +2 -0
- package/ip-filter/interfaces/ip-rule.interface.d.ts +36 -0
- package/ip-filter/interfaces/ip-rule.interface.js +2 -0
- package/ip-filter/ip-filter.module.d.ts +55 -0
- package/ip-filter/ip-filter.module.js +105 -0
- package/ip-filter/services/index.d.ts +1 -0
- package/ip-filter/services/index.js +17 -0
- package/ip-filter/services/ip-filter.service.d.ts +92 -0
- package/ip-filter/services/ip-filter.service.js +238 -0
- package/ip-filter/utils/index.d.ts +1 -0
- package/ip-filter/utils/index.js +17 -0
- package/ip-filter/utils/ip-utils.d.ts +61 -0
- package/ip-filter/utils/ip-utils.js +162 -0
- package/package.json +34 -29
- package/providers/context.provider.d.ts +9 -0
- package/providers/context.provider.js +13 -0
- package/redis-lock/comprehensive-lock-cleanup.service.d.ts +94 -0
- package/redis-lock/comprehensive-lock-cleanup.service.js +253 -0
- package/redis-lock/index.d.ts +2 -0
- package/redis-lock/index.js +5 -1
- package/redis-lock/lock-heartbeat.service.d.ts +80 -0
- package/redis-lock/lock-heartbeat.service.js +232 -0
- package/redis-lock/redis-lock.module.d.ts +6 -0
- package/redis-lock/redis-lock.module.js +136 -77
- package/redis-lock/redis-lock.service.d.ts +31 -0
- package/redis-lock/redis-lock.service.js +124 -17
- package/setup/bootstrap.setup.d.ts +2 -1
- package/setup/bootstrap.setup.js +3 -2
- package/setup/index.d.ts +1 -0
- package/setup/index.js +1 -0
- package/setup/run-in-mode.decorator.d.ts +56 -0
- package/setup/run-in-mode.decorator.js +92 -0
- package/setup/schedule.decorator.d.ts +1 -0
- package/setup/schedule.decorator.js +28 -13
- package/setup/worker.decorator.js +10 -1
- package/shared/index.d.ts +1 -1
- package/shared/index.js +1 -1
- package/shared/{serviceRegistryModule.js → service-registry.module.js} +28 -17
- package/shared/services/api-config.service.d.ts +41 -0
- package/shared/services/api-config.service.js +175 -8
- package/shared/services/index.d.ts +0 -1
- package/shared/services/index.js +0 -1
- package/validators/custom-validate.validator.d.ts +1 -0
- package/validators/custom-validate.validator.js +1 -0
- package/validators/file-mimetype.validator.d.ts +0 -2
- package/validators/file-mimetype.validator.js +4 -6
- package/validators/is-exists.validator.d.ts +15 -6
- package/validators/is-exists.validator.js +8 -7
- package/validators/is-unique.validator.d.ts +22 -7
- package/validators/is-unique.validator.js +41 -17
- package/vault/vault-config.service.js +1 -1
- package/cache/providers/memory-cache.provider.d.ts +0 -49
- package/cache/providers/memory-cache.provider.js +0 -197
- package/http-client/services/cache.service.d.ts +0 -76
- package/http-client/services/cache.service.js +0 -333
- package/shared/services/validator.service.d.ts +0 -3
- package/shared/services/validator.service.js +0 -20
- /package/shared/{serviceRegistryModule.d.ts → service-registry.module.d.ts} +0 -0
|
@@ -13,8 +13,17 @@ class ContextExtractor {
|
|
|
13
13
|
static getHttpContext() {
|
|
14
14
|
try {
|
|
15
15
|
const requestId = providers_1.ContextProvider.getRequestId();
|
|
16
|
-
|
|
16
|
+
// 使用安全的方式获取 authUser,避免抛出异常
|
|
17
|
+
let authUser = undefined;
|
|
18
|
+
try {
|
|
19
|
+
authUser = providers_1.ContextProvider.getAuthUser();
|
|
20
|
+
}
|
|
21
|
+
catch (_a) {
|
|
22
|
+
// 在测试/CLI环境中可能没有用户上下文
|
|
23
|
+
authUser = undefined;
|
|
24
|
+
}
|
|
17
25
|
const router = providers_1.ContextProvider.getRouter();
|
|
26
|
+
const source = providers_1.ContextProvider.getSource();
|
|
18
27
|
return {
|
|
19
28
|
requestId,
|
|
20
29
|
userId: authUser === null || authUser === void 0 ? void 0 : authUser.uid,
|
|
@@ -23,11 +32,12 @@ class ContextExtractor {
|
|
|
23
32
|
metadata: {
|
|
24
33
|
authUser,
|
|
25
34
|
router,
|
|
35
|
+
source, // 添加 source 字段
|
|
26
36
|
},
|
|
27
37
|
tags: [],
|
|
28
38
|
};
|
|
29
39
|
}
|
|
30
|
-
catch (
|
|
40
|
+
catch (_b) {
|
|
31
41
|
// If no context is available, return empty context
|
|
32
42
|
return {
|
|
33
43
|
tags: [],
|
|
@@ -71,11 +81,15 @@ class ContextExtractor {
|
|
|
71
81
|
}
|
|
72
82
|
/**
|
|
73
83
|
* 获取用户代理
|
|
84
|
+
* 注意:当前 ContextProvider 不存储 userAgent 信息
|
|
85
|
+
* 如需使用此功能,需要扩展 ContextProvider 或从请求头中获取
|
|
74
86
|
*/
|
|
75
87
|
static getUserAgent() {
|
|
76
88
|
try {
|
|
77
89
|
const router = providers_1.ContextProvider.getRouter();
|
|
78
|
-
|
|
90
|
+
// TODO: userAgent 需要从请求头中获取,当前 router 对象不包含此字段
|
|
91
|
+
// 可以考虑扩展 ContextProvider 来存储完整的请求信息
|
|
92
|
+
return router === null || router === void 0 ? void 0 : router.userAgent;
|
|
79
93
|
}
|
|
80
94
|
catch (_a) {
|
|
81
95
|
return undefined;
|
|
@@ -137,13 +137,10 @@ class CurlGenerator {
|
|
|
137
137
|
config.url = urlMatch[1];
|
|
138
138
|
}
|
|
139
139
|
// 提取头信息
|
|
140
|
-
const headerMatches = curlCommand.
|
|
140
|
+
const headerMatches = Array.from(curlCommand.matchAll(/-H\s+'([^:]+):\s*([^']+)'/g));
|
|
141
141
|
config.headers = {};
|
|
142
142
|
headerMatches.forEach((header) => {
|
|
143
|
-
|
|
144
|
-
if (headerMatch) {
|
|
145
|
-
config.headers[headerMatch[1]] = headerMatch[2];
|
|
146
|
-
}
|
|
143
|
+
config.headers[header[1]] = header[2];
|
|
147
144
|
});
|
|
148
145
|
// 提取数据
|
|
149
146
|
const dataMatch = curlCommand.match(/-d\s+'([^']+)'/);
|
|
@@ -18,3 +18,5 @@ __exportStar(require("./curl-generator.util"), exports);
|
|
|
18
18
|
__exportStar(require("./retry-recorder.util"), exports);
|
|
19
19
|
__exportStar(require("./context-extractor.util"), exports);
|
|
20
20
|
__exportStar(require("./call-stack-extractor.util"), exports);
|
|
21
|
+
__exportStar(require("./proxy-environment.util"), exports);
|
|
22
|
+
__exportStar(require("./sanitize.util"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ProxyConfig } from '../interfaces/http-client-config.interface';
|
|
2
|
+
/**
|
|
3
|
+
* 代理环境变量解析工具
|
|
4
|
+
* 支持标准的代理环境变量:HTTP_PROXY, HTTPS_PROXY, NO_PROXY
|
|
5
|
+
*/
|
|
6
|
+
export declare class ProxyEnvironmentParser {
|
|
7
|
+
private static readonly logger;
|
|
8
|
+
/**
|
|
9
|
+
* 从环境变量解析代理配置
|
|
10
|
+
* @param protocol 请求协议 (http 或 https)
|
|
11
|
+
* @param targetUrl 目标 URL(用于检查 NO_PROXY)
|
|
12
|
+
* @returns 解析后的代理配置,如果不应使用代理则返回 false
|
|
13
|
+
*/
|
|
14
|
+
static parseFromEnvironment(protocol?: 'http' | 'https', targetUrl?: string): false | Required<Omit<ProxyConfig, 'enabled' | 'fromEnvironment'>>;
|
|
15
|
+
/**
|
|
16
|
+
* 获取所有代理相关的环境变量
|
|
17
|
+
*/
|
|
18
|
+
static getProxyEnvironmentVariables(): {
|
|
19
|
+
HTTP_PROXY?: string;
|
|
20
|
+
HTTPS_PROXY?: string;
|
|
21
|
+
NO_PROXY?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* 检查是否设置了代理环境变量
|
|
25
|
+
*/
|
|
26
|
+
static hasProxyEnvironment(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 解析代理 URL
|
|
29
|
+
* 支持格式: http://proxy:port, http://user:pass@proxy:port
|
|
30
|
+
*/
|
|
31
|
+
private static parseProxyUrl;
|
|
32
|
+
/**
|
|
33
|
+
* 检查是否应该绕过代理
|
|
34
|
+
* 基于 NO_PROXY 环境变量
|
|
35
|
+
*/
|
|
36
|
+
private static shouldBypassProxy;
|
|
37
|
+
/**
|
|
38
|
+
* 简单的 IP 地址匹配
|
|
39
|
+
* 支持精确匹配和 CIDR 表示法
|
|
40
|
+
*/
|
|
41
|
+
private static isIpMatch;
|
|
42
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProxyEnvironmentParser = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
/**
|
|
6
|
+
* 代理环境变量解析工具
|
|
7
|
+
* 支持标准的代理环境变量:HTTP_PROXY, HTTPS_PROXY, NO_PROXY
|
|
8
|
+
*/
|
|
9
|
+
class ProxyEnvironmentParser {
|
|
10
|
+
/**
|
|
11
|
+
* 从环境变量解析代理配置
|
|
12
|
+
* @param protocol 请求协议 (http 或 https)
|
|
13
|
+
* @param targetUrl 目标 URL(用于检查 NO_PROXY)
|
|
14
|
+
* @returns 解析后的代理配置,如果不应使用代理则返回 false
|
|
15
|
+
*/
|
|
16
|
+
static parseFromEnvironment(protocol = 'http', targetUrl) {
|
|
17
|
+
// 获取代理环境变量 (大写和小写都支持)
|
|
18
|
+
const proxyEnv = protocol === 'https'
|
|
19
|
+
? process.env.HTTPS_PROXY ||
|
|
20
|
+
process.env.https_proxy ||
|
|
21
|
+
process.env.HTTP_PROXY ||
|
|
22
|
+
process.env.http_proxy
|
|
23
|
+
: process.env.HTTP_PROXY || process.env.http_proxy;
|
|
24
|
+
// 检查是否应该跳过代理
|
|
25
|
+
if (targetUrl && this.shouldBypassProxy(targetUrl)) {
|
|
26
|
+
this.logger.debug(`Bypassing proxy for ${targetUrl} due to NO_PROXY`);
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
if (!proxyEnv) {
|
|
30
|
+
this.logger.debug('No proxy environment variable found');
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const proxyConfig = this.parseProxyUrl(proxyEnv);
|
|
35
|
+
this.logger.debug(`Parsed proxy configuration from environment: ${JSON.stringify(proxyConfig)}`);
|
|
36
|
+
return proxyConfig;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
this.logger.warn(`Failed to parse proxy URL from environment: ${error.message}`);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 获取所有代理相关的环境变量
|
|
45
|
+
*/
|
|
46
|
+
static getProxyEnvironmentVariables() {
|
|
47
|
+
return {
|
|
48
|
+
HTTP_PROXY: process.env.HTTP_PROXY || process.env.http_proxy,
|
|
49
|
+
HTTPS_PROXY: process.env.HTTPS_PROXY || process.env.https_proxy,
|
|
50
|
+
NO_PROXY: process.env.NO_PROXY || process.env.no_proxy,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 检查是否设置了代理环境变量
|
|
55
|
+
*/
|
|
56
|
+
static hasProxyEnvironment() {
|
|
57
|
+
const vars = this.getProxyEnvironmentVariables();
|
|
58
|
+
return !!(vars.HTTP_PROXY || vars.HTTPS_PROXY);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 解析代理 URL
|
|
62
|
+
* 支持格式: http://proxy:port, http://user:pass@proxy:port
|
|
63
|
+
*/
|
|
64
|
+
static parseProxyUrl(proxyUrl) {
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(proxyUrl);
|
|
67
|
+
const config = {
|
|
68
|
+
protocol: url.protocol.replace(':', '') || 'http',
|
|
69
|
+
host: url.hostname,
|
|
70
|
+
port: url.port
|
|
71
|
+
? parseInt(url.port, 10)
|
|
72
|
+
: url.protocol === 'https:'
|
|
73
|
+
? 443
|
|
74
|
+
: 80,
|
|
75
|
+
auth: undefined,
|
|
76
|
+
};
|
|
77
|
+
// 解析认证信息
|
|
78
|
+
if (url.username || url.password) {
|
|
79
|
+
config.auth = {
|
|
80
|
+
username: decodeURIComponent(url.username),
|
|
81
|
+
password: decodeURIComponent(url.password),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return config;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new Error(`Invalid proxy URL format: ${proxyUrl}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 检查是否应该绕过代理
|
|
92
|
+
* 基于 NO_PROXY 环境变量
|
|
93
|
+
*/
|
|
94
|
+
static shouldBypassProxy(targetUrl) {
|
|
95
|
+
const noProxy = process.env.NO_PROXY || process.env.no_proxy;
|
|
96
|
+
if (!noProxy) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const url = new URL(targetUrl);
|
|
101
|
+
const hostname = url.hostname;
|
|
102
|
+
// NO_PROXY 可以是逗号分隔的列表
|
|
103
|
+
const bypassList = noProxy
|
|
104
|
+
.split(',')
|
|
105
|
+
.map((item) => item.trim().toLowerCase());
|
|
106
|
+
for (const bypass of bypassList) {
|
|
107
|
+
if (!bypass)
|
|
108
|
+
continue;
|
|
109
|
+
// 精确匹配
|
|
110
|
+
if (hostname.toLowerCase() === bypass) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// 通配符匹配 (例如 .example.com)
|
|
114
|
+
if (bypass.startsWith('.') && hostname.toLowerCase().endsWith(bypass)) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
// 后缀匹配 (例如 example.com 匹配 api.example.com)
|
|
118
|
+
if (hostname.toLowerCase().endsWith('.' + bypass)) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
// 特殊值 * 表示所有域名都绕过
|
|
122
|
+
if (bypass === '*') {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
// IP 地址范围匹配 (简单实现)
|
|
126
|
+
if (this.isIpMatch(hostname, bypass)) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
this.logger.warn(`Failed to parse target URL for NO_PROXY check: ${error.message}`);
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 简单的 IP 地址匹配
|
|
138
|
+
* 支持精确匹配和 CIDR 表示法
|
|
139
|
+
*/
|
|
140
|
+
static isIpMatch(hostname, pattern) {
|
|
141
|
+
// 精确 IP 匹配
|
|
142
|
+
if (hostname === pattern) {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
// CIDR 匹配 (简化版本,不做完整的 CIDR 计算)
|
|
146
|
+
if (pattern.includes('/')) {
|
|
147
|
+
const [baseIp] = pattern.split('/');
|
|
148
|
+
return hostname.startsWith(baseIp.substring(0, baseIp.lastIndexOf('.')));
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.ProxyEnvironmentParser = ProxyEnvironmentParser;
|
|
154
|
+
ProxyEnvironmentParser.logger = new common_1.Logger(ProxyEnvironmentParser.name);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RetryRecorder = void 0;
|
|
4
|
+
const sanitize_util_1 = require("./sanitize.util");
|
|
4
5
|
/**
|
|
5
6
|
* 请求重试记录器
|
|
6
7
|
*/
|
|
@@ -22,7 +23,7 @@ class RetryRecorder {
|
|
|
22
23
|
requestConfig: {
|
|
23
24
|
method: ((_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'UNKNOWN',
|
|
24
25
|
url: config.url || '',
|
|
25
|
-
headers:
|
|
26
|
+
headers: sanitize_util_1.SanitizeUtil.sanitizeHeaders(config.headers || {}),
|
|
26
27
|
},
|
|
27
28
|
};
|
|
28
29
|
}
|
|
@@ -83,32 +84,6 @@ class RetryRecorder {
|
|
|
83
84
|
}
|
|
84
85
|
return `HTTP error: ${status}`;
|
|
85
86
|
}
|
|
86
|
-
/**
|
|
87
|
-
* 过滤敏感头信息
|
|
88
|
-
*/
|
|
89
|
-
static sanitizeHeaders(headers) {
|
|
90
|
-
const sanitized = {};
|
|
91
|
-
const sensitiveKeys = [
|
|
92
|
-
'authorization',
|
|
93
|
-
'apikey',
|
|
94
|
-
'password',
|
|
95
|
-
'secret',
|
|
96
|
-
'token',
|
|
97
|
-
'x-api-key',
|
|
98
|
-
'x-auth-token',
|
|
99
|
-
'cookie',
|
|
100
|
-
'set-cookie',
|
|
101
|
-
];
|
|
102
|
-
Object.entries(headers).forEach(([key, value]) => {
|
|
103
|
-
if (sensitiveKeys.some((sensitive) => key.toLowerCase().includes(sensitive))) {
|
|
104
|
-
sanitized[key] = '[FILTERED]';
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
sanitized[key] = value;
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
return sanitized;
|
|
111
|
-
}
|
|
112
87
|
/**
|
|
113
88
|
* 添加重试记录
|
|
114
89
|
*/
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP 请求数据脱敏工具
|
|
3
|
+
* 统一处理 headers、body、query string 的敏感信息过滤
|
|
4
|
+
*/
|
|
5
|
+
export declare class SanitizeUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 默认敏感字段列表
|
|
8
|
+
*/
|
|
9
|
+
private static readonly DEFAULT_SENSITIVE_FIELDS;
|
|
10
|
+
/**
|
|
11
|
+
* 脱敏 headers
|
|
12
|
+
* @param headers - 原始 headers 对象
|
|
13
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
14
|
+
* @returns 脱敏后的 headers
|
|
15
|
+
*/
|
|
16
|
+
static sanitizeHeaders(headers: any, sensitiveFields?: string[]): Record<string, string>;
|
|
17
|
+
/**
|
|
18
|
+
* 脱敏 body
|
|
19
|
+
* @param body - 原始 body 数据
|
|
20
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
21
|
+
* @returns 脱敏后的 body
|
|
22
|
+
*/
|
|
23
|
+
static sanitizeBody(body: any, sensitiveFields?: string[]): any;
|
|
24
|
+
/**
|
|
25
|
+
* 脱敏 URL 中的 query string
|
|
26
|
+
* @param url - 原始 URL
|
|
27
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
28
|
+
* @returns 脱敏后的 URL
|
|
29
|
+
*/
|
|
30
|
+
static sanitizeQueryString(url: string, sensitiveFields?: string[]): string;
|
|
31
|
+
/**
|
|
32
|
+
* 脱敏 params 对象
|
|
33
|
+
* @param params - 原始 params 对象
|
|
34
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
35
|
+
* @returns 脱敏后的 params
|
|
36
|
+
*/
|
|
37
|
+
static sanitizeParams(params: Record<string, any>, sensitiveFields?: string[]): Record<string, any>;
|
|
38
|
+
/**
|
|
39
|
+
* 将 body 转换为字符串并脱敏
|
|
40
|
+
* @param data - 原始数据
|
|
41
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
42
|
+
* @returns 脱敏后的字符串
|
|
43
|
+
*/
|
|
44
|
+
static sanitizeBodyAsString(data: any, sensitiveFields?: string[]): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* 判断字段是否为敏感字段
|
|
47
|
+
* @param key - 字段名
|
|
48
|
+
* @param sensitiveFields - 敏感字段列表
|
|
49
|
+
* @returns 是否为敏感字段
|
|
50
|
+
*/
|
|
51
|
+
private static isSensitiveField;
|
|
52
|
+
/**
|
|
53
|
+
* 递归脱敏对象中的敏感字段
|
|
54
|
+
* @param obj - 目标对象
|
|
55
|
+
* @param sensitiveFields - 敏感字段列表
|
|
56
|
+
*/
|
|
57
|
+
private static sanitizeObject;
|
|
58
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SanitizeUtil = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* HTTP 请求数据脱敏工具
|
|
6
|
+
* 统一处理 headers、body、query string 的敏感信息过滤
|
|
7
|
+
*/
|
|
8
|
+
class SanitizeUtil {
|
|
9
|
+
/**
|
|
10
|
+
* 脱敏 headers
|
|
11
|
+
* @param headers - 原始 headers 对象
|
|
12
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
13
|
+
* @returns 脱敏后的 headers
|
|
14
|
+
*/
|
|
15
|
+
static sanitizeHeaders(headers, sensitiveFields = []) {
|
|
16
|
+
if (!headers)
|
|
17
|
+
return {};
|
|
18
|
+
const sanitized = {};
|
|
19
|
+
const fieldsToSanitize = [
|
|
20
|
+
...this.DEFAULT_SENSITIVE_FIELDS,
|
|
21
|
+
...sensitiveFields,
|
|
22
|
+
];
|
|
23
|
+
Object.keys(headers).forEach((key) => {
|
|
24
|
+
if (this.isSensitiveField(key, fieldsToSanitize)) {
|
|
25
|
+
sanitized[key] = '[FILTERED]';
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
sanitized[key] = String(headers[key]);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return sanitized;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 脱敏 body
|
|
35
|
+
* @param body - 原始 body 数据
|
|
36
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
37
|
+
* @returns 脱敏后的 body
|
|
38
|
+
*/
|
|
39
|
+
static sanitizeBody(body, sensitiveFields = []) {
|
|
40
|
+
if (!body)
|
|
41
|
+
return undefined;
|
|
42
|
+
if (typeof body === 'string') {
|
|
43
|
+
try {
|
|
44
|
+
body = JSON.parse(body);
|
|
45
|
+
}
|
|
46
|
+
catch (_a) {
|
|
47
|
+
// 非 JSON 字符串,不做处理
|
|
48
|
+
return body.length > 1000 ? body.substring(0, 1000) + '...' : body;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (typeof body === 'object') {
|
|
52
|
+
const sanitized = Object.assign({}, body);
|
|
53
|
+
const fieldsToSanitize = [
|
|
54
|
+
...this.DEFAULT_SENSITIVE_FIELDS,
|
|
55
|
+
...sensitiveFields,
|
|
56
|
+
];
|
|
57
|
+
this.sanitizeObject(sanitized, fieldsToSanitize);
|
|
58
|
+
return sanitized;
|
|
59
|
+
}
|
|
60
|
+
return body;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 脱敏 URL 中的 query string
|
|
64
|
+
* @param url - 原始 URL
|
|
65
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
66
|
+
* @returns 脱敏后的 URL
|
|
67
|
+
*/
|
|
68
|
+
static sanitizeQueryString(url, sensitiveFields = []) {
|
|
69
|
+
if (!url)
|
|
70
|
+
return url;
|
|
71
|
+
const queryIndex = url.indexOf('?');
|
|
72
|
+
if (queryIndex === -1) {
|
|
73
|
+
return url; // 没有 query string
|
|
74
|
+
}
|
|
75
|
+
const baseUrl = url.substring(0, queryIndex);
|
|
76
|
+
const queryString = url.substring(queryIndex + 1);
|
|
77
|
+
const params = new URLSearchParams(queryString);
|
|
78
|
+
const fieldsToSanitize = [
|
|
79
|
+
...this.DEFAULT_SENSITIVE_FIELDS,
|
|
80
|
+
...sensitiveFields,
|
|
81
|
+
];
|
|
82
|
+
params.forEach((value, key) => {
|
|
83
|
+
if (this.isSensitiveField(key, fieldsToSanitize)) {
|
|
84
|
+
params.set(key, '[FILTERED]');
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const sanitizedQuery = params.toString();
|
|
88
|
+
return sanitizedQuery ? `${baseUrl}?${sanitizedQuery}` : baseUrl;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 脱敏 params 对象
|
|
92
|
+
* @param params - 原始 params 对象
|
|
93
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
94
|
+
* @returns 脱敏后的 params
|
|
95
|
+
*/
|
|
96
|
+
static sanitizeParams(params, sensitiveFields = []) {
|
|
97
|
+
if (!params)
|
|
98
|
+
return {};
|
|
99
|
+
const sanitized = {};
|
|
100
|
+
const fieldsToSanitize = [
|
|
101
|
+
...this.DEFAULT_SENSITIVE_FIELDS,
|
|
102
|
+
...sensitiveFields,
|
|
103
|
+
];
|
|
104
|
+
Object.keys(params).forEach((key) => {
|
|
105
|
+
if (this.isSensitiveField(key, fieldsToSanitize)) {
|
|
106
|
+
sanitized[key] = '[FILTERED]';
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
sanitized[key] = params[key];
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return sanitized;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 将 body 转换为字符串并脱敏
|
|
116
|
+
* @param data - 原始数据
|
|
117
|
+
* @param sensitiveFields - 自定义敏感字段列表
|
|
118
|
+
* @returns 脱敏后的字符串
|
|
119
|
+
*/
|
|
120
|
+
static sanitizeBodyAsString(data, sensitiveFields = []) {
|
|
121
|
+
const sanitized = this.sanitizeBody(data, sensitiveFields);
|
|
122
|
+
if (!sanitized)
|
|
123
|
+
return undefined;
|
|
124
|
+
if (typeof sanitized === 'string') {
|
|
125
|
+
return sanitized.length > 5000
|
|
126
|
+
? sanitized.substring(0, 5000) + '...'
|
|
127
|
+
: sanitized;
|
|
128
|
+
}
|
|
129
|
+
const jsonString = JSON.stringify(sanitized);
|
|
130
|
+
return jsonString.length > 5000
|
|
131
|
+
? jsonString.substring(0, 5000) + '...'
|
|
132
|
+
: jsonString;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 判断字段是否为敏感字段
|
|
136
|
+
* @param key - 字段名
|
|
137
|
+
* @param sensitiveFields - 敏感字段列表
|
|
138
|
+
* @returns 是否为敏感字段
|
|
139
|
+
*/
|
|
140
|
+
static isSensitiveField(key, sensitiveFields) {
|
|
141
|
+
const lowerKey = key.toLowerCase();
|
|
142
|
+
return sensitiveFields.some((field) => lowerKey.includes(field.toLowerCase()));
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* 递归脱敏对象中的敏感字段
|
|
146
|
+
* @param obj - 目标对象
|
|
147
|
+
* @param sensitiveFields - 敏感字段列表
|
|
148
|
+
*/
|
|
149
|
+
static sanitizeObject(obj, sensitiveFields) {
|
|
150
|
+
if (typeof obj !== 'object' || obj === null)
|
|
151
|
+
return;
|
|
152
|
+
for (const key in obj) {
|
|
153
|
+
if (this.isSensitiveField(key, sensitiveFields)) {
|
|
154
|
+
obj[key] = '[FILTERED]';
|
|
155
|
+
}
|
|
156
|
+
else if (typeof obj[key] === 'object') {
|
|
157
|
+
this.sanitizeObject(obj[key], sensitiveFields);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
exports.SanitizeUtil = SanitizeUtil;
|
|
163
|
+
/**
|
|
164
|
+
* 默认敏感字段列表
|
|
165
|
+
*/
|
|
166
|
+
SanitizeUtil.DEFAULT_SENSITIVE_FIELDS = [
|
|
167
|
+
// Headers 相关
|
|
168
|
+
'authorization',
|
|
169
|
+
'apikey',
|
|
170
|
+
'x-api-key',
|
|
171
|
+
'x-auth-token',
|
|
172
|
+
'cookie',
|
|
173
|
+
'set-cookie',
|
|
174
|
+
// Body/Query 通用字段
|
|
175
|
+
'password',
|
|
176
|
+
'secret',
|
|
177
|
+
'token',
|
|
178
|
+
'key',
|
|
179
|
+
'credential',
|
|
180
|
+
'private',
|
|
181
|
+
'confidential',
|
|
182
|
+
'ssn',
|
|
183
|
+
'creditCard',
|
|
184
|
+
'accessToken',
|
|
185
|
+
'refreshToken',
|
|
186
|
+
'apiKey',
|
|
187
|
+
'apiSecret',
|
|
188
|
+
];
|