allowlist.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import path from 'path';
  2. import { existsSync, readFileSync } from 'fs';
  3. export function normalizeWhatsAppIdentifier(value) {
  4. return String(value || '')
  5. .trim()
  6. .replace(/:.*@/, '@')
  7. .replace(/@.*/, '')
  8. .replace(/^\+/, '');
  9. }
  10. export function parseAllowedUsers(rawValue) {
  11. return new Set(
  12. String(rawValue || '')
  13. .split(',')
  14. .map((value) => normalizeWhatsAppIdentifier(value))
  15. .filter(Boolean)
  16. );
  17. }
  18. function readMappingFile(sessionDir, identifier, suffix = '') {
  19. const filePath = path.join(sessionDir, `lid-mapping-${identifier}${suffix}.json`);
  20. if (!existsSync(filePath)) {
  21. return null;
  22. }
  23. try {
  24. const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
  25. const normalized = normalizeWhatsAppIdentifier(parsed);
  26. return normalized || null;
  27. } catch {
  28. return null;
  29. }
  30. }
  31. export function expandWhatsAppIdentifiers(identifier, sessionDir) {
  32. const normalized = normalizeWhatsAppIdentifier(identifier);
  33. if (!normalized) {
  34. return new Set();
  35. }
  36. // Walk both phone->LID and LID->phone mapping files so allowlists can use
  37. // either form transparently in bot mode.
  38. const resolved = new Set();
  39. const queue = [normalized];
  40. while (queue.length > 0) {
  41. const current = queue.shift();
  42. if (!current || resolved.has(current)) {
  43. continue;
  44. }
  45. resolved.add(current);
  46. for (const suffix of ['', '_reverse']) {
  47. const mapped = readMappingFile(sessionDir, current, suffix);
  48. if (mapped && !resolved.has(mapped)) {
  49. queue.push(mapped);
  50. }
  51. }
  52. }
  53. return resolved;
  54. }
  55. export function matchesAllowedSender(senderId, senderAltId, allowedUsers, sessionDir) {
  56. // WhatsApp Multi-Device can expose a first-contact sender as an opaque LID
  57. // before it persists LID mapping files. Baileys supplies the same sender's
  58. // phone JID separately (key.remoteJidAlt for DMs, key.participantAlt in
  59. // groups), so consult it as an additional alias without changing the
  60. // allowlist itself (#63415, #72529).
  61. return matchesAllowedUser(senderId, allowedUsers, sessionDir)
  62. || matchesAllowedUser(senderAltId, allowedUsers, sessionDir);
  63. }
  64. export function matchesInboundWhatsAppGroup({
  65. chatId,
  66. groupPolicy,
  67. groupAllowedUsers,
  68. sessionDir,
  69. }) {
  70. if (groupPolicy === 'disabled' || groupPolicy === 'pairing') {
  71. return false;
  72. }
  73. if (groupPolicy === 'allowlist') {
  74. return matchesAllowedUser(chatId, groupAllowedUsers, sessionDir);
  75. }
  76. return groupPolicy === 'open';
  77. }
  78. export function matchesAllowedUser(senderId, allowedUsers, sessionDir) {
  79. // Empty allowlist = NO ONE allowed (secure default, #8389). Operators
  80. // who want an open bot must set ``WHATSAPP_ALLOWED_USERS=*`` explicitly.
  81. // Previous behaviour (empty → return true) let any stranger DM the
  82. // bridge and trigger a Python-side pairing-code reply.
  83. if (!allowedUsers || allowedUsers.size === 0) {
  84. return false;
  85. }
  86. // "*" means allow everyone (consistent with SIGNAL_GROUP_ALLOWED_USERS)
  87. if (allowedUsers.has('*')) {
  88. return true;
  89. }
  90. const aliases = expandWhatsAppIdentifiers(senderId, sessionDir);
  91. for (const alias of aliases) {
  92. if (allowedUsers.has(alias)) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }