webhook.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import http from 'node:http';
  4. import { once } from 'node:events';
  5. import { createHmac } from 'node:crypto';
  6. import { mkdtempSync, rmSync, rmdirSync } from 'node:fs';
  7. import { tmpdir } from 'node:os';
  8. import { join } from 'node:path';
  9. import { createStore } from '../src/store.js';
  10. import { normalizeMessages } from '../src/teams-network-records.js';
  11. import { refreshChatNames } from '../src/chat-names.js';
  12. import { createWebhookService, sendWebhook, isAllowedWebhookAddress, validateWebhookUrl } from '../src/webhook-service.js';
  13. function fixture(send=async()=>({status:204})) {
  14. const store=createStore(':memory:'); let time=Date.parse('2026-09-08T08:00:00Z');
  15. const service=createWebhookService({store,send,now:()=>time,classify:record=>record.raw});
  16. const add=(id,raw={},options={})=>store.upsertRecord({source:'web-api',accountId:'test-account',kind:'message',externalId:id,parentId:'test-chat',title:'Fictional',text:'Fictional body',occurredAt:new Date(time+100).toISOString(),
  17. raw:{senderId:'other-user',isFromSelf:false,mentionsSelf:false,isDirect:false,...raw},...options.record},{ingestion:options.ingestion || 'live'});
  18. const subscription=(overrides={})=>service.configure({url:'https://receiver.example/secret-path?key=private',enabled:true,mode:'all',...overrides});
  19. return {store,service,add,subscription,advance:ms=>{time+=ms;},now:()=>time,close:()=>store.close()};
  20. }
  21. test('local participant-name enrichment does not grant private-chat notification evidence or replay imported metadata', async () => {
  22. const store=createStore(':memory:'), time=Date.parse('2026-09-08T08:00:00Z');
  23. const self='22222222-2222-2222-2222-222222222222',peer='33333333-3333-3333-3333-333333333333';
  24. const accountId=`web-api:11111111-1111-1111-1111-111111111111:${self}`,chatId=`19:${self}_${peer}@unq.gbl.spaces`;
  25. let sends=0;
  26. const service=createWebhookService({store,now:()=>time,send:async()=>{sends++;return{status:204};}});
  27. try {
  28. service.configure({url:'https://receiver.example/hook',enabled:true,mode:'direct'});
  29. store.upsertRecord({source:'web-api',accountId,kind:'message',externalId:'peer',parentId:chatId,title:'聊天消息',text:'🔥',occurredAt:new Date(time+100).toISOString(),
  30. raw:{senderId:peer,selfUserId:self,author:'Fictional colleague',mentions:[]}},{ingestion:'live'});
  31. assert.equal(refreshChatNames(store,{accountId,chatId}),1);
  32. const chat=store.getRecordByIdentity({source:'web-api',accountId,kind:'chat',externalId:chatId});
  33. assert.equal(chat.title,'Fictional colleague'); assert.equal(chat.raw.isOneOnOne,undefined); assert.equal(chat.raw.chatType,undefined);
  34. await service.dispatchOnce();
  35. assert.equal(sends,0); assert.equal(service.listDeliveries().items.length,0);
  36. } finally {store.close();}
  37. });
  38. test('notification modes distinguish mention of self, direct chat, self messages and unknown metadata',async()=>{
  39. for(const [mode,expected] of [['all',3],['mention',1],['direct',1],['mention_or_direct',2]]) {
  40. let calls=0;const f=fixture(async()=>{calls++;return{status:204};});
  41. try {
  42. f.subscription({mode}); f.add('group'); f.add('mention',{mentionsSelf:true}); f.add('direct',{isDirect:true});
  43. f.add('mine',{isFromSelf:true,mentionsSelf:true,isDirect:true}); f.add('unknown',{isFromSelf:null,mentionsSelf:true});
  44. await f.service.dispatchOnce();assert.equal(calls,expected,mode);
  45. }finally{f.close();}
  46. }
  47. });
  48. test('captured API metadata flows through the real classifier, database and signed notification queue',async()=>{
  49. const store=createStore(':memory:'), sent=[];
  50. const time=Date.parse('2026-09-08T08:00:00Z');
  51. const self='11111111-1111-4111-8111-111111111111', other='22222222-2222-4222-8222-222222222222';
  52. const accountId=`web-api:33333333-3333-4333-8333-333333333333:${self}`;
  53. const service=createWebhookService({store,now:()=>time,send:async request=>{sent.push(JSON.parse(request.body));return{status:204};}});
  54. try {
  55. service.configure({url:'https://receiver.example/fictional',enabled:true,mode:'mention_or_direct'});
  56. store.upsertRecord({source:'web-api',accountId,kind:'chat',externalId:'fictional-direct',raw:{isOneOnOne:true}});
  57. for(const [id,chatId,sender,mentions] of [
  58. ['direct','fictional-direct',other,[]],['mention','fictional-group',other,[self]],
  59. ['other-mention','fictional-group',other,[other]],['self','fictional-direct',self,[self]],
  60. ]) {
  61. const {records}=normalizeMessages({messages:[{id,type:'Message',messagetype:'Text',content:'Fictional message',
  62. conversationid:chatId,conversationLink:`https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/conversations/${encodeURIComponent(chatId)}`,
  63. originalarrivaltime:new Date(time+100).toISOString(),
  64. from:`https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/contacts/${encodeURIComponent(`8:orgid:${sender}`)}`,
  65. properties:{mentions:JSON.stringify(mentions.map(id=>({mentionType:'person',mri:`8:orgid:${id}`})))},
  66. }]},{accountId,chatId});
  67. for(const record of records)store.upsertRecord(record,{ingestion:'live'});
  68. }
  69. await service.dispatchOnce();
  70. assert.equal(sent.length,2);
  71. assert.equal(sent.filter(p=>p.message.isDirect===true).length,1);
  72. assert.equal(sent.filter(p=>p.message.mentionsSelf===true).length,1);
  73. } finally {store.close();}
  74. });
  75. test('new subscriptions, history/import/old discovery, edits and duplicate reads never flood reminders',async()=>{
  76. let calls=0;const f=fixture(async()=>{calls++;return{status:204};});
  77. try {
  78. f.add('before'); f.subscription(); f.add('fresh'); f.add('history',{}, {ingestion:'history'});f.add('import',{}, {ingestion:'import'});
  79. f.add('old',{}, {record:{occurredAt:'2025-01-01T00:00:00Z'}}); f.add('unknown-time',{}, {record:{occurredAt:null}});
  80. f.add('fresh'); f.add('fresh',{}, {record:{text:'Fictional edit'}});
  81. await f.service.dispatchOnce();await f.service.dispatchOnce();assert.equal(calls,1);
  82. assert.equal(f.service.listDeliveries().items.length,1);
  83. }finally{f.close();}
  84. });
  85. test('record, version and notification change sequence commit atomically',()=>{
  86. const f=fixture();
  87. try {
  88. assert.throws(()=>f.store.transaction(()=>{f.add('rolled-back');throw new Error('rollback');}));
  89. assert.equal(f.store.stats().total,0);assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,0);
  90. assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,0);
  91. f.add('saved');f.add('saved');assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,1);
  92. }finally{f.close();}
  93. });
  94. test('a fresh message first captured by history is notified once after identical live confirmation',async()=>{
  95. const sent=[];const f=fixture(async request=>{sent.push(JSON.parse(request.body));return{status:204};});
  96. try {
  97. f.subscription({includeContent:true});
  98. f.add('history-first',{}, {ingestion:'history'});
  99. await f.service.dispatchOnce();assert.equal(sent.length,0);
  100. const unchanged=f.add('history-first');assert.equal(unchanged.changed,false);
  101. assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,1);
  102. await f.service.dispatchOnce();assert.equal(sent.length,1);
  103. assert.equal(sent[0].message.text,'Fictional body');
  104. f.add('history-first');f.add('history-first',{}, {record:{text:'Fictional later edit'}});
  105. await f.service.dispatchOnce();assert.equal(sent.length,1);
  106. assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,1);
  107. assert.equal(f.store.readOne('SELECT ingestion FROM change_log ORDER BY seq LIMIT 1').ingestion,'history');
  108. }finally{f.close();}
  109. });
  110. test('the first live snapshot is retained when a history record changes before live confirmation',async()=>{
  111. let sent;const f=fixture(async request=>{sent=JSON.parse(request.body);return{status:204};});
  112. try {
  113. f.subscription({includeContent:true});f.add('history-first',{}, {ingestion:'history',record:{text:'Fictional history snapshot'}});
  114. f.add('history-first',{}, {record:{text:'Fictional first live snapshot'}});
  115. f.add('history-first',{}, {record:{text:'Fictional later edit'}});
  116. await f.service.dispatchOnce();assert.equal(sent.message.text,'Fictional first live snapshot');
  117. const created=f.store.readOne("SELECT seq FROM change_log WHERE action='created'");
  118. assert.equal(sent.sequence,created.seq);
  119. assert.equal(f.service.listDeliveries().items[0].seq,created.seq);
  120. }finally{f.close();}
  121. });
  122. test('imported, unknown, old history and pre-change-log records never become new-message reminders',async()=>{
  123. let sent=0;const f=fixture(async()=>{sent++;return{status:204};});
  124. try {
  125. f.subscription();
  126. for(const ingestion of ['import','unknown']) {
  127. f.add(ingestion,{}, {ingestion});f.add(ingestion);f.add(ingestion,{}, {record:{text:'Fictional edited import'}});
  128. }
  129. f.add('legacy',{}, {ingestion:'history'});
  130. // The previous release stored versions but had no creation change events.
  131. f.store.execute("DELETE FROM change_log WHERE recordId=(SELECT id FROM records WHERE externalId='legacy')");
  132. f.add('legacy');f.add('legacy',{}, {record:{text:'Fictional legacy edit'}});
  133. const oldRecord={occurredAt:'2025-01-01T00:00:00Z'};
  134. f.add('old-history',{}, {ingestion:'history',record:oldRecord});f.add('old-history',{}, {record:oldRecord});
  135. await f.service.dispatchOnce();assert.equal(sent,0);
  136. assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,1);
  137. }finally{f.close();}
  138. });
  139. test('history-to-live promotion and its observation roll back together',()=>{
  140. const f=fixture();
  141. try {
  142. f.add('history-first',{}, {ingestion:'history'});
  143. assert.throws(()=>f.store.transaction(()=>{f.add('history-first');throw new Error('rollback');}));
  144. assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,0);
  145. assert.equal(f.store.readOne('SELECT COUNT(*) n FROM change_log').n,1);
  146. f.add('history-first');assert.equal(f.store.readOne('SELECT COUNT(*) n FROM message_live_observations').n,1);
  147. }finally{f.close();}
  148. });
  149. test('pending deliveries survive restart and abandoned leases retry once with the original event ID',async()=>{
  150. const directory=mkdtempSync(join(tmpdir(),'teams-webhook-restart-')), filename=join(directory,'fictional.sqlite');
  151. let store=createStore(filename), time=Date.parse('2026-09-08T08:00:00Z'), sent=[];
  152. const options=()=>({store,now:()=>time,classify:record=>record.raw,send:async request=>{sent.push(JSON.parse(request.body));return{status:204};}});
  153. try {
  154. let service=createWebhookService(options());
  155. service.configure({url:'https://receiver.example/fictional',enabled:true,mode:'all'});
  156. const record={source:'web-api',accountId:'fictional-account',kind:'message',externalId:'fictional-message',title:'Fictional',text:'Fictional',
  157. occurredAt:new Date(time+100).toISOString(),raw:{isFromSelf:false}};
  158. store.upsertRecord(record,{ingestion:'history'});store.upsertRecord(record,{ingestion:'live'});
  159. await service.dispatchOnce({signal:AbortSignal.abort()});
  160. const delivery=service.listDeliveries().items[0];assert.equal(delivery.status,'pending');
  161. store.execute("UPDATE webhook_deliveries SET status='inflight',attempts=1,leaseToken='fictional-abandoned-lease',leaseUntil=? WHERE id=?",
  162. [new Date(time+60_000).toISOString(),delivery.id]);
  163. store.close();store=createStore(filename);service=createWebhookService(options());
  164. await service.dispatchOnce();assert.equal(sent.length,0);
  165. time+=60_000;await service.dispatchOnce();assert.equal(sent.length,1);assert.equal(sent[0].id,delivery.id);
  166. assert.equal(service.listDeliveries().items[0].attempts,2);
  167. store.upsertRecord(record,{ingestion:'live'});await service.dispatchOnce();assert.equal(sent.length,1);
  168. }finally{
  169. store.close();for(const suffix of ['','-wal','-shm'])rmSync(filename+suffix,{force:true});rmdirSync(directory);
  170. }
  171. });
  172. test('migration converts the previous change-log cursor to a live-observation baseline without replay or gaps',async()=>{
  173. const directory=mkdtempSync(join(tmpdir(),'teams-webhook-migration-')), filename=join(directory,'fictional.sqlite');
  174. let store=createStore(filename), sent=[];
  175. const time=Date.parse('2026-09-08T08:00:00Z');
  176. const options=()=>({store,now:()=>time,classify:record=>record.raw,send:async request=>{sent.push(JSON.parse(request.body));return{status:204};}});
  177. const record={source:'web-api',accountId:'fictional-account',kind:'message',externalId:'fictional-before-upgrade',title:'Fictional',text:'Fictional',
  178. occurredAt:new Date(time+100).toISOString(),raw:{isFromSelf:false}};
  179. try {
  180. store.upsertRecord(record,{ingestion:'live'});
  181. for(let i=0;i<5;i++)store.upsertRecord({...record,kind:'activity',externalId:`fictional-activity-${i}`});
  182. let service=createWebhookService(options());
  183. const id=service.configure({url:'https://receiver.example/fictional',enabled:true,mode:'all'}).subscription.id;
  184. const pendingId=service.configure({url:'https://receiver.example/fictional-pending',enabled:true,mode:'all'}).subscription.id;
  185. // Recreate the older schema's cursor domain with several non-message changes.
  186. store.execute('UPDATE webhook_subscriptions SET cursor=(SELECT MAX(seq) FROM change_log) WHERE id=?',[id]);
  187. store.execute('UPDATE webhook_subscriptions SET cursor=0 WHERE id=?',[pendingId]);
  188. store.execute('DELETE FROM message_live_observations');
  189. store.execute("DELETE FROM state WHERE key='migration:message-live-observations:v1'");
  190. store.close();store=createStore(filename);service=createWebhookService(options());
  191. assert.equal(service.list().items.find(item=>item.id===id).cursor,store.readOne('SELECT MAX(seq) n FROM message_live_observations').n);
  192. assert.equal(service.list().items.find(item=>item.id===pendingId).cursor,0);
  193. await service.dispatchOnce();assert.equal(sent.length,1);
  194. assert.equal(service.listDeliveries({subscriptionId:id}).items.length,0);
  195. assert.equal(service.listDeliveries({subscriptionId:pendingId}).items.length,1);
  196. store.upsertRecord({...record,externalId:'fictional-after-upgrade'},{ingestion:'live'});
  197. await service.dispatchOnce();assert.equal(sent.length,3);
  198. assert.equal(sent.slice(1).every(item=>item.message.id===store.readOne("SELECT id FROM records WHERE externalId='fictional-after-upgrade'").id),true);
  199. }finally{
  200. store.close();for(const suffix of ['','-wal','-shm'])rmSync(filename+suffix,{force:true});rmdirSync(directory);
  201. }
  202. });
  203. test('payload is signed, defaults to metadata, and public status does not reveal secrets or endpoint path',async()=>{
  204. let sent;const f=fixture(async request=>{sent=request;return{status:200};});
  205. try {
  206. const {signingSecret}=f.subscription();f.add('signed');await f.service.dispatchOnce();
  207. const payload=JSON.parse(sent.body);assert.equal(payload.message.text,undefined);assert.equal(payload.message.title,undefined);
  208. const expected=createHmac('sha256',signingSecret).update(sent.headers['X-Teams-Timestamp']+'.'+sent.body).digest('hex');
  209. assert.equal(sent.headers['X-Teams-Signature'],'sha256='+expected);assert.equal(sent.headers['X-Teams-Event-Id'],payload.id);
  210. assert.doesNotMatch(JSON.stringify([f.service.list(),f.service.listDeliveries(),f.service.status()]),new RegExp(signingSecret+'|secret-path|key=private|Fictional body'));
  211. }finally{f.close();}
  212. });
  213. test('explicit content and account/chat scope are honored',async()=>{
  214. const sent=[];const f=fixture(async request=>{sent.push(JSON.parse(request.body));return{status:200};});
  215. try {
  216. f.subscription({accountIds:['test-account'],chatIds:['test-chat'],includeContent:true});
  217. f.add('one');f.add('foreign',{}, {record:{accountId:'other-account'}});f.add('other-chat',{}, {record:{parentId:'elsewhere'}});
  218. await f.service.dispatchOnce();assert.equal(sent.length,1);assert.equal(sent[0].message.text,'Fictional body');
  219. }finally{f.close();}
  220. });
  221. test('Retry-After persists a stable event and retries after delay; permanent failures can be explicitly retried',async()=>{
  222. const events=[];let code=429;const f=fixture(async request=>{events.push(request.headers['X-Teams-Event-Id']);return{status:code,retryAfter:'120'};});
  223. try {
  224. f.subscription();f.add('retry');await f.service.dispatchOnce();await f.service.dispatchOnce();assert.equal(events.length,1);
  225. f.advance(119_000);await f.service.dispatchOnce();assert.equal(events.length,1);
  226. f.advance(1000);code=400;await f.service.dispatchOnce();assert.equal(events.length,2);assert.equal(events[0],events[1]);
  227. const delivery=f.service.listDeliveries().items[0];assert.equal(delivery.status,'dead');
  228. code=200;f.service.retry({deliveryId:delivery.id});await f.service.dispatchOnce();assert.equal(events.length,3);assert.equal(events[0],events[2]);
  229. }finally{f.close();}
  230. });
  231. test('excessive Retry-After becomes a manual failure instead of retrying earlier than requested',async()=>{
  232. const f=fixture(async()=>({status:429,retryAfter:'99999999999999999999'}));
  233. try {f.subscription();f.add('long-delay');await f.service.dispatchOnce();assert.equal(f.service.listDeliveries().items[0].status,'dead');}
  234. finally {f.close();}
  235. });
  236. test('configuration changes cancel old queued destinations, disabled subscriptions do not replay old messages',async()=>{
  237. let calls=0;const f=fixture(async()=>{calls++;return{status:503};});
  238. try {
  239. const id=f.subscription().subscription.id;f.add('queued');await f.service.dispatchOnce();
  240. f.service.configure({id,enabled:false});f.advance(60_000);await f.service.dispatchOnce();assert.equal(calls,1);
  241. f.add('while-disabled');f.service.configure({id,url:'https://new.example/another-secret',enabled:true});
  242. await f.service.dispatchOnce();assert.equal(calls,1);assert.equal(f.service.listDeliveries().items[0].status,'cancelled');
  243. }finally{f.close();}
  244. });
  245. test('concurrent dispatchers claim one lease and a late response cannot revive a removed subscription',async()=>{
  246. let release,calls=0;const f=fixture(async()=>{calls++;return new Promise(resolve=>{release=resolve;});});
  247. try {
  248. const id=f.subscription().subscription.id;f.add('one');const first=f.service.dispatchOnce();
  249. const other=createWebhookService({store:f.store,now:f.now,send:async()=>{calls++;return{status:200};},classify:r=>r.raw});
  250. await other.dispatchOnce();assert.equal(calls,1);f.service.remove({id});release({status:200});await first;
  251. assert.equal(f.service.listDeliveries().items.length,0);
  252. }finally{f.close();}
  253. });
  254. test('URL and DNS checks reject private, metadata, credentials and redirects without exposing response content',async()=>{
  255. for(const address of ['127.0.0.1','10.0.0.1','169.254.169.254','172.16.0.1','192.168.1.1','::1','::ffff:127.0.0.1','fc00::1','2001:db8::1']) assert.equal(isAllowedWebhookAddress(address),false,address);
  256. assert.equal(isAllowedWebhookAddress('8.8.8.8'),true);assert.equal(isAllowedWebhookAddress('2606:4700:4700::1111'),true);
  257. assert.equal(isAllowedWebhookAddress('127.0.0.1',true),true);assert.equal(isAllowedWebhookAddress('10.0.0.1',true),false);
  258. for(const url of ['http://public.example/hook','https://user:secret@public.example/','https://169.254.169.254/','file:///test','https://public.example/#secret']) assert.throws(()=>validateWebhookUrl(url));
  259. await assert.rejects(sendWebhook({url:'https://rebind.example/hook',body:'{}',headers:{}},{resolveHost:async()=>[{address:'127.0.0.1',family:4}]}));
  260. });
  261. test('real loopback receiver obtains one signed JSON POST; redirects are returned without a second request',async()=>{
  262. const received=[];const server=http.createServer(async(req,res)=>{let body='';for await(const chunk of req)body+=chunk;received.push({method:req.method,body});res.writeHead(302,{Location:'http://127.0.0.1:1/should-not-follow'});res.end('private receiver body');});
  263. server.listen(0,'127.0.0.1');await once(server,'listening');
  264. try {
  265. const response=await sendWebhook({url:`http://127.0.0.1:${server.address().port}/test`,allowLocal:true,body:'{"fictional":true}',headers:{'X-Test':'safe'}});
  266. assert.equal(response.status,302);assert.deepEqual(received,[{method:'POST',body:'{"fictional":true}'}]);assert.doesNotMatch(JSON.stringify(response),/private receiver body/);
  267. }finally{await new Promise(resolve=>server.close(resolve));}
  268. });