diff --git a/src/logging/diagnostic-support-redaction.ts b/src/logging/diagnostic-support-redaction.ts index ee7e41d25b2..28219e83923 100644 --- a/src/logging/diagnostic-support-redaction.ts +++ b/src/logging/diagnostic-support-redaction.ts @@ -55,6 +55,11 @@ type SupportObjectEntry = { value: unknown; }; +type LimitedSupportArray = { + count: number; + items: unknown[]; +}; + function asRecord(value: unknown): Record | undefined { if (!value || typeof value !== "object" || Array.isArray(value)) { return undefined; @@ -95,10 +100,14 @@ function createSupportRecord(): Record { return Object.create(null) as Record; } +function hasOwnRecordKey(record: Record, key: string): boolean { + return Object.prototype.hasOwnProperty.call(record, key); +} + function countOwnObjectEntries(record: Record): number { let count = 0; for (const key in record) { - if (Object.prototype.hasOwnProperty.call(record, key)) { + if (hasOwnRecordKey(record, key)) { count += 1; } } @@ -112,7 +121,7 @@ function limitedSupportObjectEntries(record: Record): { let count = 0; const entries: SupportObjectEntry[] = []; for (const key in record) { - if (!Object.prototype.hasOwnProperty.call(record, key)) { + if (!hasOwnRecordKey(record, key)) { continue; } count += 1; @@ -125,6 +134,13 @@ function limitedSupportObjectEntries(record: Record): { return { count, entries }; } +function limitedSupportArray(value: unknown[]): LimitedSupportArray { + return { + count: value.length, + items: value.slice(0, MAX_SUPPORT_ARRAY_ITEMS), + }; +} + function addTruncationMetadata(sanitized: Record, count: number): void { if (count > MAX_SUPPORT_OBJECT_ENTRIES) { sanitized[TRUNCATED_SUPPORT_FIELD] = { @@ -367,17 +383,13 @@ export function sanitizeSupportSnapshotValue( return ""; } if (Array.isArray(value)) { + const { count, items } = limitedSupportArray(value); if (key === "programArguments") { - return supportArrayResult( - sanitizeCommandArguments(value.slice(0, MAX_SUPPORT_ARRAY_ITEMS), redaction), - value.length, - ); + return supportArrayResult(sanitizeCommandArguments(items, redaction), count); } return supportArrayResult( - value - .slice(0, MAX_SUPPORT_ARRAY_ITEMS) - .map((entry) => sanitizeSupportSnapshotValue(entry, redaction, key, depth + 1)), - value.length, + items.map((entry) => sanitizeSupportSnapshotValue(entry, redaction, key, depth + 1)), + count, ); } const record = asRecord(value); @@ -423,11 +435,10 @@ export function sanitizeSupportConfigValue( count: value.length, }; } + const { count, items } = limitedSupportArray(value); return supportArrayResult( - value - .slice(0, MAX_SUPPORT_ARRAY_ITEMS) - .map((entry) => sanitizeSupportConfigValue(entry, redaction, key, depth + 1)), - value.length, + items.map((entry) => sanitizeSupportConfigValue(entry, redaction, key, depth + 1)), + count, ); } const record = asRecord(value);