allowlist.test.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import os from 'node:os';
  4. import path from 'node:path';
  5. import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
  6. import {
  7. expandWhatsAppIdentifiers,
  8. matchesAllowedSender,
  9. matchesInboundWhatsAppGroup,
  10. matchesAllowedUser,
  11. normalizeWhatsAppIdentifier,
  12. parseAllowedUsers,
  13. } from './allowlist.js';
  14. test('normalizeWhatsAppIdentifier strips jid syntax and plus prefix', () => {
  15. assert.equal(normalizeWhatsAppIdentifier('+19175395595@s.whatsapp.net'), '19175395595');
  16. assert.equal(normalizeWhatsAppIdentifier('267383306489914@lid'), '267383306489914');
  17. assert.equal(normalizeWhatsAppIdentifier('19175395595:12@s.whatsapp.net'), '19175395595');
  18. });
  19. test('expandWhatsAppIdentifiers resolves phone and lid aliases from session files', () => {
  20. const sessionDir = mkdtempSync(path.join(os.tmpdir(), 'hermes-wa-allowlist-'));
  21. try {
  22. writeFileSync(path.join(sessionDir, 'lid-mapping-19175395595.json'), JSON.stringify('267383306489914'));
  23. writeFileSync(path.join(sessionDir, 'lid-mapping-267383306489914_reverse.json'), JSON.stringify('19175395595'));
  24. const aliases = expandWhatsAppIdentifiers('267383306489914@lid', sessionDir);
  25. assert.deepEqual([...aliases].sort(), ['19175395595', '267383306489914']);
  26. } finally {
  27. rmSync(sessionDir, { recursive: true, force: true });
  28. }
  29. });
  30. test('matchesAllowedUser accepts mapped lid sender when allowlist only contains phone number', () => {
  31. const sessionDir = mkdtempSync(path.join(os.tmpdir(), 'hermes-wa-allowlist-'));
  32. try {
  33. writeFileSync(path.join(sessionDir, 'lid-mapping-19175395595.json'), JSON.stringify('267383306489914'));
  34. writeFileSync(path.join(sessionDir, 'lid-mapping-267383306489914_reverse.json'), JSON.stringify('19175395595'));
  35. const allowedUsers = parseAllowedUsers('+19175395595');
  36. assert.equal(matchesAllowedUser('267383306489914@lid', allowedUsers, sessionDir), true);
  37. assert.equal(matchesAllowedUser('188012763865257@lid', allowedUsers, sessionDir), false);
  38. } finally {
  39. rmSync(sessionDir, { recursive: true, force: true });
  40. }
  41. });
  42. test('matchesAllowedSender accepts the Baileys alt phone JID when a first-contact LID has no mapping yet (#63415, #72529)', () => {
  43. const sessionDir = mkdtempSync(path.join(os.tmpdir(), 'hermes-wa-allowlist-'));
  44. try {
  45. const allowedUsers = parseAllowedUsers('+19175395595');
  46. // On a first message from a WhatsApp Multi-Device contact, Baileys can
  47. // emit a LID as senderId before it has written LID mapping files, while
  48. // key.remoteJidAlt / key.participantAlt carries the phone JID.
  49. assert.equal(
  50. matchesAllowedSender('267383306489914@lid', '19175395595@s.whatsapp.net', allowedUsers, sessionDir),
  51. true,
  52. );
  53. assert.equal(
  54. matchesAllowedSender('267383306489914@lid', '18881234567@s.whatsapp.net', allowedUsers, sessionDir),
  55. false,
  56. );
  57. } finally {
  58. rmSync(sessionDir, { recursive: true, force: true });
  59. }
  60. });
  61. test('matchesAllowedUser treats * as allow-all wildcard', () => {
  62. const sessionDir = mkdtempSync(path.join(os.tmpdir(), 'hermes-wa-allowlist-'));
  63. try {
  64. const allowedUsers = parseAllowedUsers('*');
  65. assert.equal(matchesAllowedUser('19175395595@s.whatsapp.net', allowedUsers, sessionDir), true);
  66. assert.equal(matchesAllowedUser('267383306489914@lid', allowedUsers, sessionDir), true);
  67. } finally {
  68. rmSync(sessionDir, { recursive: true, force: true });
  69. }
  70. });
  71. test('group intake follows group policy and group-JID allowlist independently of DM users', () => {
  72. const sessionDir = mkdtempSync(path.join(os.tmpdir(), 'hermes-wa-allowlist-'));
  73. try {
  74. const groupAllowedUsers = parseAllowedUsers('120363001234567890@g.us');
  75. const base = {
  76. chatId: '120363001234567890@g.us',
  77. groupAllowedUsers,
  78. sessionDir,
  79. };
  80. assert.equal(matchesInboundWhatsAppGroup({ ...base, groupPolicy: 'allowlist' }), true);
  81. assert.equal(matchesInboundWhatsAppGroup({ ...base, chatId: 'other@g.us', groupPolicy: 'allowlist' }), false);
  82. assert.equal(matchesInboundWhatsAppGroup({ ...base, chatId: 'other@g.us', groupPolicy: 'open' }), true);
  83. assert.equal(matchesInboundWhatsAppGroup({ ...base, groupPolicy: 'disabled' }), false);
  84. assert.equal(matchesInboundWhatsAppGroup({ ...base, groupPolicy: 'pairing' }), false);
  85. } finally {
  86. rmSync(sessionDir, { recursive: true, force: true });
  87. }
  88. });
  89. test('matchesAllowedUser rejects everyone when allowlist is empty (#8389)', () => {
  90. // Regression guard: empty allowlist used to return true (allow-everyone),
  91. // which let any stranger DM the bridge and trigger a Python-side
  92. // pairing-code reply. Secure default is now "reject unless explicitly
  93. // configured"; operators who want an open bot must set `*`.
  94. const sessionDir = mkdtempSync(path.join(os.tmpdir(), 'hermes-wa-allowlist-'));
  95. try {
  96. const empty = parseAllowedUsers('');
  97. assert.equal(empty.size, 0);
  98. assert.equal(matchesAllowedUser('19175395595@s.whatsapp.net', empty, sessionDir), false);
  99. assert.equal(matchesAllowedUser('267383306489914@lid', empty, sessionDir), false);
  100. // Null/undefined allowlist (defensive) also rejects.
  101. assert.equal(matchesAllowedUser('19175395595@s.whatsapp.net', null, sessionDir), false);
  102. assert.equal(matchesAllowedUser('19175395595@s.whatsapp.net', undefined, sessionDir), false);
  103. } finally {
  104. rmSync(sessionDir, { recursive: true, force: true });
  105. }
  106. });