network-probe.test.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { probeCapturedRequest } from '../src/network-probe.js';
  4. const payload = { messages: [{ id: 'm1', content: '<p>Test message</p>', messagetype: 'RichText/Html', conversationid: 'chat-test' }] };
  5. function har() {
  6. return { log: { entries: [{ request: { method: 'GET', url: 'https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/conversations/chat-test/messages?pageSize=50',
  7. headers: [{ name: 'Authorization', value: 'Bearer test-secret' }] },
  8. response: { status: 200, content: { mimeType: 'application/json', text: JSON.stringify(payload) } } }] } };
  9. }
  10. test('captured request probe uses exact recorded GET and reports counts without exposing credentials or messages', async () => {
  11. const result = await probeCapturedRequest(har(), 0, { fetch: async (url, options) => {
  12. assert.equal(url, har().log.entries[0].request.url); assert.equal(options.method, 'GET');
  13. assert.equal(options.redirect, 'manual'); assert.equal(options.credentials, 'omit');
  14. assert.equal(new Headers(options.headers).get('authorization'), 'Bearer test-secret');
  15. return Response.json(payload);
  16. } });
  17. assert.equal(result.ok, true); assert.equal(result.itemCount, 1); assert.equal(result.kind, 'messages');
  18. assert.doesNotMatch(JSON.stringify(result), /test-secret|Test message/);
  19. });
  20. test('redirects, authentication failures and fetch errors never leak request secrets or follow other hosts', async () => {
  21. for (const status of [302, 401, 403, 429, 500]) {
  22. let calls = 0;
  23. const result = await probeCapturedRequest(har(), 0, { fetch: async () => {
  24. calls++; return new Response('test-secret', { status, headers: { Location: 'https://example.com/token=test-secret' } });
  25. } });
  26. assert.equal(calls, 1); assert.equal(result.ok, false); assert.equal(result.status, status);
  27. assert.doesNotMatch(JSON.stringify(result), /test-secret|example\.com/);
  28. }
  29. const result = await probeCapturedRequest(har(), 0, { fetch: async () => { throw new Error('test-secret'); } });
  30. assert.equal(result.ok, false); assert.doesNotMatch(JSON.stringify(result), /test-secret/);
  31. });
  32. test('HTTP 200 must still match the expected business response shape and fit the response limit', async () => {
  33. for (const response of [new Response('<html>login</html>', { headers: { 'content-type': 'text/html' } }), Response.json({ status: 'success' }),
  34. new Response('x'.repeat(2 * 1024 * 1024 + 1), { headers: { 'content-type': 'application/json' } })]) {
  35. const result = await probeCapturedRequest(har(), 0, { fetch: async () => response });
  36. assert.equal(result.ok, false);
  37. }
  38. });
  39. test('a sanitized HAR cannot be silently replayed without the captured authentication', async () => {
  40. const input = har(); input.log.entries[0].request.headers = [];
  41. let calls = 0;
  42. await assert.rejects(probeCapturedRequest(input, 0, { fetch: async () => { calls++; return Response.json(payload); } }));
  43. assert.equal(calls, 0);
  44. });
  45. test('rate limits and temporary failures expose numeric Retry-After metadata without raw headers', async () => {
  46. const now = () => Date.parse('2026-09-08T00:00:00Z');
  47. for (const [status, retryAfter] of [[429, '90'], [503, 'Tue, 08 Sep 2026 00:01:30 GMT']]) {
  48. const result = await probeCapturedRequest(har(), 0, { now,
  49. fetch: async () => new Response('private receiver body', { status, headers: { 'Retry-After': retryAfter, 'X-Private-Header': 'private-token' } }) });
  50. assert.equal(result.retryAfterMs, 90_000); assert.equal(result.ok, false);
  51. assert.doesNotMatch(JSON.stringify(result), /private receiver|private-token|X-Private-Header|Retry-After/);
  52. }
  53. });