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