logger.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { maskEmail, redact, redactString } from '../src/logger.js';
  4. test('bearer tokens are redacted', () => {
  5. const out = redactString('Authorization: Bearer sk-abc123DEF456ghi', false);
  6. assert.ok(!out.includes('sk-abc123DEF456ghi'));
  7. assert.match(out, /Bearer \[REDACTED\]/);
  8. });
  9. test('WooCommerce consumer keys are redacted', () => {
  10. const out = redactString('ck_1234567890abcdef and cs_abcdef1234567890', false);
  11. assert.ok(!out.includes('ck_1234567890abcdef'));
  12. assert.ok(!out.includes('cs_abcdef1234567890'));
  13. });
  14. test('query-string credentials are redacted', () => {
  15. const out = redactString(
  16. 'https://shop.test/wp-json/wc/v3/orders?consumer_key=ck_secretvalue&consumer_secret=cs_secretvalue',
  17. false,
  18. );
  19. assert.ok(!out.includes('ck_secretvalue'));
  20. assert.ok(!out.includes('cs_secretvalue'));
  21. });
  22. test('e-mail addresses are redacted when PII logging is off', () => {
  23. const out = redactString('contact jan.kowalski@example.com about order', false);
  24. assert.ok(!out.includes('jan.kowalski@example.com'));
  25. });
  26. test('secret-looking object keys are always redacted, even with PII logging on', () => {
  27. const out = redact(
  28. { api_token: 'abc', consumer_secret: 'def', nested: { password: 'ghi' } },
  29. true,
  30. ) as Record<string, unknown>;
  31. assert.equal(out.api_token, '[REDACTED]');
  32. assert.equal(out.consumer_secret, '[REDACTED]');
  33. assert.equal((out.nested as Record<string, unknown>).password, '[REDACTED]');
  34. });
  35. test('PII keys are masked when PII logging is off', () => {
  36. const out = redact({ email: 'jan@example.com', content: 'treść wiadomości' }, false) as Record<
  37. string,
  38. unknown
  39. >;
  40. assert.equal(out.email, 'j***@example.com');
  41. assert.equal(out.content, '[REDACTED_PII]');
  42. });
  43. test('maskEmail keeps only the first character and the domain', () => {
  44. assert.equal(maskEmail('jan.kowalski@example.com'), 'j***@example.com');
  45. assert.equal(maskEmail('not-an-email'), '[REDACTED]');
  46. });
  47. test('redact terminates on deeply nested structures', () => {
  48. type Nested = { next?: Nested };
  49. let deep: Nested = {};
  50. for (let i = 0; i < 30; i++) deep = { next: deep };
  51. assert.doesNotThrow(() => redact(deep, false));
  52. });