network-capture.test.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { analyzeHar, getReplayRequest } from '../src/network-capture.js';
  4. const jwt = `header.${Buffer.from(JSON.stringify({ exp: 1900000000, name: 'Private Person' })).toString('base64url')}.signature`;
  5. const message = { id: 'private-message-id', body: { content: 'Private conversation text', contentType: 'html' } };
  6. const entry = (json = { value: [message] }, overrides = {}) => ({ request: { method: 'GET', url: 'https://teams.cloud.microsoft/api/v1/users/private-user/conversations/private-chat/messages?syncToken=super-secret&$top=50', headers: [{ name: 'Authorization', value: `Bearer ${jwt}` }, { name: 'Cookie', value: 'Session=private-cookie' }] }, response: { status: 200, content: { mimeType: 'application/json; charset=utf-8', text: JSON.stringify(json) } }, ...overrides });
  7. const har = entries => ({ log: { entries } });
  8. test('recognizes Graph and Skype shapes without exposing credentials, IDs, query values or message samples', () => {
  9. const report = analyzeHar(har([
  10. entry(),
  11. entry({ value: [{ id: 'chat-id', chatType: 'oneOnOne' }] }),
  12. entry({ value: [{ subject: 'Private meeting', start: { dateTime: '2026-09-08T10:00:00' }, end: { dateTime: '2026-09-08T11:00:00' } }] }),
  13. entry({ messages: [{ id: 123, content: 'Private Skype message', messagetype: 'RichText', conversationid: 'private-chat' }] })
  14. ]));
  15. assert.equal(report.entryCount, 4);
  16. assert.deepEqual(report.candidates.map(item => item.kind), ['messages', 'chats', 'calendar', 'messages']);
  17. assert.ok(report.candidates.every(item => item.replayable && item.itemCount === 1));
  18. assert.deepEqual(report.candidates[0].auth.headerNames, ['Authorization', 'Cookie']);
  19. assert.equal(report.candidates[0].auth.expiresAt, '2030-03-17T17:46:40.000Z');
  20. assert.equal(report.candidates[0].endpoint, 'https://teams.cloud.microsoft/api/v1/users/{id}/conversations/{id}/messages');
  21. assert.ok(report.candidates[0].shape.includes('body'));
  22. assert.doesNotMatch(JSON.stringify(report), /Private|private-user|private-chat|private-message|private-cookie|super-secret|syncToken|signature|Bearer|header\./);
  23. });
  24. test('replay preserves actual URL and approved authentication values while restricting other headers', () => {
  25. const captured = entry();
  26. captured.request.headers.push(
  27. { name: 'X-SkypeToken', value: 'opaque-private-token' },
  28. { name: 'Authentication', value: 'session-private-token' },
  29. { name: 'Accept', value: 'application/json' },
  30. { name: 'Content-Type', value: 'application/json' },
  31. { name: 'Origin', value: 'https://teams.microsoft.com/?secret=origin-secret' },
  32. { name: 'Referer', value: 'https://teams.cloud.microsoft/v2/?token=referer-secret' },
  33. { name: 'Host', value: 'evil.example' },
  34. { name: 'X-Unneeded', value: 'secret' },
  35. { name: 'Proxy-Authorization', value: 'private' },
  36. { name: 'X-Refresh-Token', value: 'must-not-export' }
  37. );
  38. const replay = getReplayRequest(har([captured]), 0);
  39. assert.equal(replay.method, 'GET'); assert.equal(replay.url, captured.request.url);
  40. assert.equal(replay.headers.Authorization, `Bearer ${jwt}`);
  41. assert.equal(replay.headers.Cookie, 'Session=private-cookie');
  42. assert.equal(replay.headers['X-SkypeToken'], 'opaque-private-token');
  43. assert.equal(replay.headers.Authentication, 'session-private-token');
  44. assert.equal(replay.headers.Origin, 'https://teams.microsoft.com');
  45. assert.equal(replay.headers.Referer, 'https://teams.cloud.microsoft/v2/');
  46. assert.equal(replay.headers.Host, undefined); assert.equal(replay.headers['X-Unneeded'], undefined);
  47. assert.doesNotMatch(JSON.stringify(replay), /must-not-export|Proxy-Authorization|origin-secret|referer-secret/);
  48. });
  49. test('base64 JSON is decoded, while HTML, malformed JSON and absent bodies remain nonreplayable metadata', () => {
  50. const base64 = entry(); base64.response.content.text = Buffer.from(base64.response.content.text).toString('base64'); base64.response.content.encoding = 'base64';
  51. const html = entry(); html.response.content = { mimeType: 'text/html', text: '<html>Private login content</html>' };
  52. const invalid = entry(); invalid.response.content.text = '{invalid Private body';
  53. const missing = entry(); delete missing.response.content.text;
  54. const corrupt = entry(); corrupt.response.content.encoding = 'base64'; corrupt.response.content.text = 'invalid%';
  55. const report = analyzeHar(har([base64, html, invalid, missing, corrupt]));
  56. assert.equal(report.candidates[0].replayable, true);
  57. assert.ok(report.candidates.slice(1).every(item => item.kind === 'unknown' && !item.replayable));
  58. assert.doesNotMatch(JSON.stringify(report), /Private|invalid%|<html>/);
  59. assert.throws(() => getReplayRequest(har([html]), 0), /JSON/);
  60. });
  61. test('observed Teams read requests retain only exact approved context headers with bounded lengths', () => {
  62. const context = ['x-ms-client-type', 'x-ms-client-version', 'x-ms-region', 'x-ms-partition', 'x-ms-user-type', 'x-ms-migration', 'x-ms-request-id', 'x-ms-session-id', 'x-ms-object-id', 'x-ms-synctoken', 'x-ringoverride', 'clientinfo', 'behavioroverride'];
  63. const captured = entry();
  64. captured.request.headers.push(...context.map(name => ({ name, value: `captured-${name}` })), { name: 'x-ms-unapproved-secret', value: 'private' });
  65. const replay = getReplayRequest(har([captured]), 0);
  66. for (const name of context) assert.equal(replay.headers[name], `captured-${name}`);
  67. assert.equal(replay.headers['x-ms-unapproved-secret'], undefined);
  68. assert.doesNotMatch(JSON.stringify(analyzeHar(har([captured]))), /captured-x-ms|captured-clientinfo/);
  69. captured.request.headers.push({ name: 'clientinfo', value: 'x'.repeat(8193) });
  70. assert.throws(() => getReplayRequest(har([captured]), 0), /长度上限/);
  71. });
  72. test('unknown, empty and mixed shapes cannot claim a working collection adapter', () => {
  73. const report = analyzeHar(har([entry({ value: [] }), entry({ items: [{ id: 'id', text: 'secret' }] }), entry({ value: [message, { unexpected: true }] }), entry({ 'private-account-key': 'secret' })]));
  74. assert.ok(report.candidates.every(item => item.kind === 'unknown' && !item.replayable));
  75. assert.deepEqual(report.candidates.at(-1).shape, ['{other}']);
  76. assert.doesNotMatch(JSON.stringify(report), /private-account-key|secret/);
  77. assert.throws(() => getReplayRequest(har([entry({ value: [] })]), 0), /结构尚未识别/);
  78. });
  79. test('observed Teams updates envelope identifies an empty chat delta without claiming full chat inventory', () => {
  80. const delta = entry({ conversationFolders: {}, teams: [], chats: [], engageCommunities: [], users: [], channels: [], privateFeeds: [], metadata: { syncToken: 'private-sync-token', forwardSyncToken: 'private-forward-token', isPartialData: false, hasMoreChats: false } });
  81. delta.request.url = 'https://teams.cloud.microsoft/api/csa/emea/api/v3/teams/users/me/updates?isPrefetch=true';
  82. const report = analyzeHar(har([delta])).candidates[0];
  83. assert.equal(report.kind, 'chats'); assert.equal(report.itemCount, 0); assert.equal(report.replayable, true);
  84. assert.match(report.reason, /为空.*增量/);
  85. assert.doesNotMatch(JSON.stringify(report), /private-sync-token|private-forward-token/);
  86. assert.equal(getReplayRequest(har([delta]), 0).url, delta.request.url);
  87. const populated = structuredClone(delta);
  88. populated.response.content.text = JSON.stringify({ ...JSON.parse(delta.response.content.text), chats: [{ id: 'private-chat-id', title: 'Private chat title', threadType: 'chat', chatType: 'group', version: 1, members: [], lastMessage: { content: 'Private message' } }] });
  89. const known = analyzeHar(har([populated])).candidates[0];
  90. assert.equal(known.kind, 'chats'); assert.equal(known.itemCount, 1); assert.equal(known.replayable, true);
  91. assert.ok(known.shape.includes('threadType'));
  92. assert.doesNotMatch(JSON.stringify(known), /private-chat-id|Private chat title|Private message/);
  93. const unknownRows = structuredClone(delta);
  94. unknownRows.response.content.text = JSON.stringify({ ...JSON.parse(delta.response.content.text), chats: [{ unexpected: 'unknown row' }] });
  95. assert.equal(analyzeHar(har([unknownRows])).candidates[0].kind, 'unknown');
  96. });
  97. test('observed Teams delta accepts a null title for untitled private chats', () => {
  98. const delta = entry({ conversationFolders: {}, teams: [], users: [], channels: [],
  99. metadata: { isPartialData: false, hasMoreChats: false },
  100. chats: [{ id: 'private-chat-id', title: null, threadType: 'chat', isOneOnOne: true, members: [{ friendlyName: 'Private participant' }] },
  101. { id: 'private-group-id', title: 'Private group title', threadType: 'chat', isOneOnOne: false }] });
  102. delta.request.url = 'https://teams.cloud.microsoft/api/csa/apac/api/v3/teams/users/me/updates';
  103. const report = analyzeHar(har([delta])).candidates[0];
  104. assert.equal(report.kind, 'chats'); assert.equal(report.itemCount, 2); assert.equal(report.replayable, true);
  105. assert.equal(getReplayRequest(har([delta]), 0).url, delta.request.url);
  106. assert.doesNotMatch(JSON.stringify(report), /private-chat-id|private-group-id|Private participant|Private group title/);
  107. });
  108. test('Teams chat title compatibility still rejects malformed title types and missing structural fields', () => {
  109. const envelope = { conversationFolders: {}, teams: [], users: [], channels: [], metadata: { isPartialData: false, hasMoreChats: false } };
  110. const row = { id: 'private-chat-id', title: null, threadType: 'chat' };
  111. const variants = [undefined, 0, false, {}, []].map(title => ({ ...row, title }));
  112. variants.push({ ...row, id: '' }, { ...row, id: 42 }, { ...row, threadType: undefined });
  113. for (const invalidRow of variants) {
  114. const capture = entry({ ...envelope, chats: [invalidRow] });
  115. const report = analyzeHar(har([capture])).candidates[0];
  116. assert.equal(report.kind, 'unknown'); assert.equal(report.replayable, false);
  117. assert.throws(() => getReplayRequest(har([capture]), 0), /结构尚未识别/);
  118. }
  119. });
  120. test('empty message sync response is recognized only with the observed tenant and complete typed metadata', () => {
  121. const json = { messages: [], tenantId: 'private-tenant', _metadata: { syncState: 'https://teams.cloud.microsoft/private-sync-url?secret=value', lastCompleteSegmentStartTime: 0, lastCompleteSegmentEndTime: 1700000000000 } };
  122. const captured = entry(json);
  123. const report = analyzeHar(har([captured])).candidates[0];
  124. assert.equal(report.kind, 'messages'); assert.equal(report.itemCount, 0); assert.equal(report.replayable, true);
  125. assert.doesNotMatch(JSON.stringify(report), /private-tenant|private-sync-url|secret=value/);
  126. const variants = [{ messages: [] }, { ...json, tenantId: null }, { ...json, _metadata: {} }, { ...json, _metadata: { ...json._metadata, lastCompleteSegmentStartTime: '0' } }, { ...json, _metadata: { ...json._metadata, syncState: '' } }];
  127. for (const body of variants) {
  128. const unknown = analyzeHar(har([entry(body)])).candidates[0];
  129. assert.equal(unknown.kind, 'unknown'); assert.equal(unknown.replayable, false);
  130. }
  131. });
  132. test('observed Graph calendar GET paths support full and selected events without confusing body with chat messages', () => {
  133. const date = { dateTime: '2026-09-08T10:00:00', timeZone: 'UTC' };
  134. for (const row of [{ id: 'private-event', subject: 'Private event', start: date, end: date, body: { content: 'Private description' } }, { id: 'private-event', start: date, end: date }]) {
  135. const captured = entry({ value: [row] }); captured.request.url = 'https://graph.microsoft.com/v1.0/me/events/private-event/instances?startDateTime=private-date';
  136. const result = analyzeHar(har([captured])).candidates[0];
  137. assert.equal(result.kind, 'calendar'); assert.equal(result.itemCount, 1); assert.equal(result.replayable, true);
  138. assert.doesNotMatch(JSON.stringify(result), /Private|private-event|private-date/);
  139. }
  140. const empty = entry({ '@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#users/private-user/events', value: [] }); empty.request.url = 'https://graph.microsoft.com/v1.0/me/events';
  141. assert.equal(analyzeHar(har([empty])).candidates[0].kind, 'calendar');
  142. const blocked = ['https://graph.microsoft.com/beta/me/events', 'https://graph.microsoft.com/v1.0/me/messages', 'https://graph.microsoft.com/v1.0/me/chats', 'https://graph.microsoft.com/v1.0/me/events/id/accept'];
  143. for (const url of blocked) { const item = structuredClone(empty); item.request.url = url; assert.throws(() => getReplayRequest(har([item]), 0), /白名单/); }
  144. });
  145. function calendarRpc(action = 'GetCalendarView') {
  146. const calendarEvent = { __type: 'CalendarItem:#Exchange', ItemId: { Id: 'private-item', ChangeKey: 'private-change' }, Subject: 'Private calendar subject', Start: '2026-09-08T10:00:00Z', End: '2026-09-08T11:00:00Z' };
  147. const items = { Items: [calendarEvent], ResponseCode: 'NoError', ResponseClass: 'Success' };
  148. const captured = entry({ Header: {}, Body: action === 'GetCalendarView' ? items : { ResponseMessages: { Items: [items] } } });
  149. captured.request.method = 'POST'; captured.request.url = `https://outlook.office.com/owa/service.svc?action=${action}`;
  150. captured.request.headers.push({ name: 'action', value: action }, { name: 'content-type', value: 'application/json' }, { name: 'Origin', value: 'https://outlook.office.com/?secret=private-origin' }, { name: 'Referer', value: 'https://outlook.office.com/calendar/?secret=private-referrer' }, { name: 'x-owa-sessionid', value: 'private-session' }, { name: 'prefer', value: 'outlook.timezone="UTC"' });
  151. const Body = action === 'GetCalendarView' ? { CalendarId: { BaseFolderId: { Id: 'private-folder' } }, RangeStart: '2026-09-08', RangeEnd: '2026-09-09' } : { EventIds: [{ Id: 'private-item' }], ItemShape: {} };
  152. const body = JSON.stringify({ __type: `${action}JsonRequest:#Exchange`, Header: { __type: 'JsonRequestHeaders:#Exchange' }, Body: { __type: `${action}Request:#Exchange`, ...Body } });
  153. if (action === 'GetCalendarView') captured.request.headers.push({ name: 'x-owa-urlpostdata', value: encodeURIComponent(body) });
  154. else captured.request.postData = { mimeType: 'application/json', text: body };
  155. return captured;
  156. }
  157. test('observed OWA calendar read RPCs retain only captured header/body payloads and classify response envelopes', () => {
  158. for (const action of ['GetCalendarView', 'GetCalendarEvent']) {
  159. const captured = calendarRpc(action), report = analyzeHar(har([captured])).candidates[0], request = getReplayRequest(har([captured]), 0);
  160. assert.equal(report.method, 'POST'); assert.equal(report.kind, 'calendar'); assert.equal(report.itemCount, 1); assert.equal(report.replayable, true);
  161. assert.doesNotMatch(JSON.stringify(report), /Private|private-item|private-folder|private-session|private-change|signature/);
  162. assert.equal(request.method, 'POST'); assert.equal(request.headers.action, action);
  163. assert.equal(request.headers.Origin, 'https://outlook.office.com'); assert.equal(request.headers.Referer, 'https://outlook.office.com/calendar/');
  164. assert.equal(request.headers['x-owa-sessionid'], 'private-session'); assert.equal(request.headers.prefer, 'outlook.timezone="UTC"');
  165. if (action === 'GetCalendarView') { assert.equal(request.body, undefined); assert.equal(request.headers['x-owa-urlpostdata'], captured.request.headers.find(header => header.name === 'x-owa-urlpostdata').value); }
  166. else assert.equal(request.body, captured.request.postData.text);
  167. }
  168. });
  169. test('OWA RPC authorization is limited to exact action, route, method, payload type and successful calendar structure', () => {
  170. const variants = [];
  171. const modify = fn => { const value = calendarRpc(); fn(value); variants.push(value); };
  172. modify(value => { value.request.url = 'https://outlook.office.com/owa/service.svc?action=DeleteItem'; value.request.headers.find(header => header.name === 'action').value = 'DeleteItem'; });
  173. modify(value => { value.request.headers.find(header => header.name === 'action').value = 'GetCalendarEvent'; });
  174. modify(value => { value.request.headers.push({ name: 'Action', value: 'GetCalendarView' }); });
  175. modify(value => { value.request.url += '&action=GetCalendarEvent'; });
  176. modify(value => { value.request.url = value.request.url.replace('?action=', '?Action='); });
  177. modify(value => { value.request.url = value.request.url.replace('outlook.office.com', 'outlook.office365.com'); });
  178. modify(value => { value.request.url = value.request.url.replace('service.svc', 'other.svc'); });
  179. modify(value => { value.request.method = 'PUT'; });
  180. for (const value of variants) { assert.equal(analyzeHar(har([value])).candidates.length, 0); assert.throws(() => getReplayRequest(har([value]), 0), /白名单/); }
  181. const missing = calendarRpc(); missing.request.headers = missing.request.headers.filter(header => header.name !== 'x-owa-urlpostdata');
  182. assert.throws(() => getReplayRequest(har([missing]), 0), /原始负载/);
  183. const wrongType = calendarRpc(); const header = wrongType.request.headers.find(header => header.name === 'x-owa-urlpostdata'); const body = JSON.parse(decodeURIComponent(header.value)); body.__type = 'DeleteItemJsonRequest:#Exchange'; header.value = encodeURIComponent(JSON.stringify(body));
  184. assert.throws(() => getReplayRequest(har([wrongType]), 0), /动作类型不一致/);
  185. const failed = calendarRpc(); const response = JSON.parse(failed.response.content.text); response.Body.ResponseCode = 'ErrorAccessDenied'; failed.response.content.text = JSON.stringify(response);
  186. assert.equal(analyzeHar(har([failed])).candidates[0].kind, 'unknown');
  187. const notCalendar = calendarRpc(); notCalendar.response.content.text = JSON.stringify({ Body: { Items: [{ Subject: 'unknown' }], ResponseCode: 'NoError', ResponseClass: 'Success' } });
  188. assert.equal(analyzeHar(har([notCalendar])).candidates[0].kind, 'unknown');
  189. const long = calendarRpc(); long.request.headers.find(header => header.name === 'x-owa-urlpostdata').value = 'x'.repeat(65537);
  190. assert.throws(() => getReplayRequest(har([long]), 0), /长度上限/);
  191. });
  192. test('host, protocol, userinfo, port, fragment, OAuth and non-GET boundaries are enforced', () => {
  193. const urls = ['https://evil.example/messages', 'https://teams.microsoft.com.evil.example/messages', 'http://teams.microsoft.com/messages', 'https://user:secret@teams.microsoft.com/messages', 'https://teams.microsoft.com:8443/messages', 'https://teams.microsoft.com/messages#secret', 'https://login.microsoftonline.com/token', 'https://teams.cloud.microsoft/oauth2/token', 'https://teams.cloud.microsoft/api/%74oken', 'wss://region.msg.skype.com/messages', 'https://outlook.office.com.evil.example/messages', 'https://graph.microsoft.com/v1.0/chats'];
  194. const captured = urls.map(url => { const item = entry(); item.request.url = url; return item; });
  195. const post = entry(); post.request.method = 'POST'; captured.push(post);
  196. const socket = entry(); socket._resourceType = 'websocket'; captured.push(socket);
  197. assert.equal(analyzeHar(har(captured)).candidates.length, 0);
  198. captured.forEach((_, index) => assert.throws(() => getReplayRequest(har(captured), index), /白名单/));
  199. for (const host of ['teams.microsoft.com', 'api.teams.microsoft.com', 'teams.cloud.microsoft', 'api.teams.cloud.microsoft', 'emea.ng.msg.skype.com', 'outlook.office.com', 'outlook.office365.com']) {
  200. const item = entry(); item.request.url = `https://${host}/api/messages`;
  201. assert.equal(analyzeHar(har([item])).candidates[0].replayable, true, host);
  202. }
  203. });
  204. test('write-like GET actions, unsuccessful responses, request bodies and missing auth cannot replay', () => {
  205. for (const suffix of ['/markRead', '/deleteMessage', '/send', '/messages%2FmarkRead', '/messages?action=sendMessage', '/messages?mark_read=true']) {
  206. const item = entry(); item.request.url = `https://teams.cloud.microsoft${suffix}`;
  207. assert.equal(analyzeHar(har([item])).candidates[0].replayable, false);
  208. assert.throws(() => getReplayRequest(har([item]), 0), /改变远端数据/);
  209. }
  210. const failed = entry(); failed.response.status = 302;
  211. const unauthenticated = entry(); unauthenticated.request.headers = [{ name: 'Accept', value: 'application/json' }];
  212. const body = entry(); body.request.postData = { text: 'write-request' };
  213. assert.throws(() => getReplayRequest(har([failed]), 0), /2xx/);
  214. assert.throws(() => getReplayRequest(har([unauthenticated]), 0), /HAR 可能已脱敏/);
  215. assert.throws(() => getReplayRequest(har([body]), 0), /请求体/);
  216. });
  217. test('large raw and base64 bodies are bounded and unsafe header values are rejected', () => {
  218. const huge = entry({ value: [{ ...message, body: { content: 'x'.repeat(2 * 1024 * 1024) } }] });
  219. const encoded = structuredClone(huge); encoded.response.content.text = Buffer.from(encoded.response.content.text).toString('base64'); encoded.response.content.encoding = 'base64';
  220. assert.ok(analyzeHar(har([huge, encoded])).candidates.every(item => !item.replayable && /2 MiB/.test(item.reason)));
  221. const bad = entry(); bad.request.headers[0].value = 'Bearer secret\r\nInjected: true';
  222. assert.throws(() => getReplayRequest(har([bad]), 0), /控制字符/);
  223. const external = entry(); external.request.headers.push({ name: 'Referer', value: 'https://evil.example/private' }, { name: 'Origin', value: 'https://outlook.office.com/' });
  224. const replay = getReplayRequest(har([external]), 0);
  225. assert.equal(replay.headers.Origin, undefined); assert.equal(replay.headers.Referer, undefined);
  226. });
  227. test('invalid HARs and selectors fail with fixed errors that do not contain request data', () => {
  228. assert.throws(() => analyzeHar({}), /HAR 格式无效/);
  229. assert.throws(() => getReplayRequest(har([entry()]), '0'), /请求编号无效/);
  230. assert.throws(() => getReplayRequest(har([entry()]), 1), /请求编号无效/);
  231. });