Skip to content

Commit 922d57f

Browse files
committed
fix(network): bound, drain, and redact the in-app capture precisely
Address review findings on the React Native network capture: - Eviction counts ride the drain result so buffered and dropped notes can never disagree with what a dump returned. - The in-app body cap takes the dump's payload budget; header values are capped at 512 characters across 64 entries at every capture seam. - JSON-quoted secrets with escaped quotes mask whole; the unquoted rule keeps masking 'api_key=value'; truncation markers count against the payload budget. - Drain JSON parsing fails with a typed reason instead of throwing raw. - whatwg-fetch-on-XHR double capture is suppressed by a synchronous URL claim held across the fetch() call. - The 2048 payload budget is one contract constant, shared by the daemon and the RN transport. - The test-app network lab is a registered route with README coverage.
1 parent 869b1d6 commit 922d57f

21 files changed

Lines changed: 265 additions & 57 deletions

File tree

‎examples/test-app/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ It is intentionally small, but each surface is dense with durable accessibility
1919
- `Automation lab`: long-press, alert-result, app-event, app-state, appearance, orientation, permission-recovery, log canaries, a flattened (`accessible={true}`) text input, and an Apple Pay sheet hosted in `com.apple.PassbookUIService`
2020
- `WebView accessibility`: a deterministic semantic fixture plus live websites with varied HTML for native accessibility snapshot verification
2121
- `Perf lab`: seeded JavaScript render problems for `react-devtools profile`, one per scenario (see [Perf lab](#perf-lab))
22+
- `Network lab`: deterministic `fetch` and `XMLHttpRequest` traffic for `network dump` verification (see [Network lab](#network-lab))
2223
- `Heavy feed`: a Bluesky-shaped home feed whose accessibility tree is deeper than 65 levels (see [Heavy feed](#heavy-feed))
2324

2425
Navigation uses Expo Router native bottom tabs, so the tab bar itself is also part of the test surface.
@@ -53,6 +54,7 @@ These are the main case families this app can support without adding more screen
5354
- retryable error banners
5455
- cart counters and quantity changes
5556
- screenshot and recording proof capture
57+
- in-app `network dump` on real `fetch` and `XMLHttpRequest` traffic from the network lab
5658

5759
## Run locally
5860

@@ -262,6 +264,20 @@ agent-device react-devtools profile stop
262264

263265
`src/screens/PerfLabScreen.tsx` imports only React and React Native, so it can be copied into a bare React Native app to check the profiler on other React Native versions.
264266

267+
## Network lab
268+
269+
`agent-device-test-app://network-lab` fires one real request per press: **Run fetch** (`network-fetch`)
270+
GETs `httpbin.org/json`, **Run post** (`network-post`) POSTs a JSON body to `httpbin.org/post`, and
271+
**Run XHR** (`network-xhr`) GETs through `XMLHttpRequest`. `network-lab-status` reports
272+
`fetch:200:<bytes>` once a request settles. Use it to verify an in-app `network dump`:
273+
274+
```bash
275+
agent-device open "agent-device-test-app://network-lab" --platform ios
276+
agent-device network dump # first dump arms the in-app capture
277+
agent-device click 'id="network-fetch"'
278+
agent-device network dump --include all # reports the GET with status, headers, bodies
279+
```
280+
265281
## Heavy feed
266282

267283
**Settings → Open heavy feed** (`open-heavy-feed`) opens a JavaScript-only reproduction of a heavy

‎examples/test-app/app/_layout.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function RootLayoutContent() {
3131
<Stack.Screen name="heavy-feed" options={{ headerShown: true, title: 'Heavy feed' }} />
3232
<Stack.Screen name="ticker" />
3333
<Stack.Screen name="perf-lab" />
34+
<Stack.Screen name="network-lab" options={{ headerShown: true, title: 'Network lab' }} />
3435
<Stack.Screen name="product/[productId]" />
3536
<Stack.Screen name="webview" />
3637
</Stack>

‎examples/test-app/app/network-lab.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ export default function NetworkLabRoute() {
5252

5353
return (
5454
<ScrollView
55-
testID="network-lab"
56-
style={{ flex: 1 }}
5755
contentContainerStyle={[styles.content, { paddingTop: insets.top + 16 }]}
56+
style={{ flex: 1 }}
57+
testID="network-lab"
5858
>
5959
<Text testID="network-lab-title">Network lab</Text>
60-
<Pressable testID="network-fetch" style={styles.button} onPress={() => void runFetch()}>
60+
<Pressable onPress={() => void runFetch()} style={styles.button} testID="network-fetch">
6161
<Text>Run fetch</Text>
6262
</Pressable>
63-
<Pressable testID="network-post" style={styles.button} onPress={() => void runPost()}>
63+
<Pressable onPress={() => void runPost()} style={styles.button} testID="network-post">
6464
<Text>Run post</Text>
6565
</Pressable>
66-
<Pressable testID="network-xhr" style={styles.button} onPress={runXhr}>
66+
<Pressable onPress={runXhr} style={styles.button} testID="network-xhr">
6767
<Text>Run XHR</Text>
6868
</Pressable>
6969
<View>

‎packages/capture-kit/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"types": "./src/index.ts",
1515
"default": "./src/index.ts"
1616
},
17+
"./network-provider-capture": {
18+
"types": "./src/network-provider-capture.ts",
19+
"default": "./src/network-provider-capture.ts"
20+
},
1721
"./android-replacement-surface-occlusion": {
1822
"types": "./src/snapshot/android-replacement-surface-occlusion.ts",
1923
"default": "./src/snapshot/android-replacement-surface-occlusion.ts"

‎packages/capture-kit/src/index.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,3 @@ export {
3131
readRecentNetworkTrafficFromText,
3232
type NetworkScan,
3333
} from './network-traffic.ts';
34-
export {
35-
captureNetworkProviderTraffic,
36-
type NetworkProviderCapture,
37-
} from './network-provider-capture.ts';

‎packages/capture-kit/src/network-provider-capture.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
} from '@agent-device/contracts/network-runtime';
66
import type { PlatformRuntimeHost } from '@agent-device/contracts/platform-runtime-operations';
77
import type { DeviceInfo } from '@agent-device/kernel/device';
8+
import { redactDiagnosticData } from '@agent-device/kernel/redaction';
89

910
/**
1011
* A provider source answers a dump with entries, with nothing yet to report, or not at all.
@@ -53,7 +54,13 @@ export async function captureNetworkProviderTraffic(
5354
const bounded = AbortSignal.any([signal, AbortSignal.timeout(PROVIDER_CAPTURE_BUDGET_MS)]);
5455
let result;
5556
try {
56-
result = await dump({ maxEntries: input.maxEntries, include: input.include }, bounded);
57+
// Racing the budget signal bounds the whole provider read: the transport's own steps can
58+
// each carry longer internal timeouts, and an unbounded provider would push the dump past
59+
// the command envelope while its work keeps running after capture-kit moved on.
60+
result = await Promise.race([
61+
dump({ maxEntries: input.maxEntries, include: input.include }, bounded),
62+
abortRejection(bounded, input.sessionId),
63+
]);
5764
} catch (error) {
5865
if (signal.aborted) throw error;
5966
return { status: 'unavailable', notes: [providerFailureNote(error)] };
@@ -64,7 +71,28 @@ export async function captureNetworkProviderTraffic(
6471
return { status: 'captured', result };
6572
}
6673

74+
function abortRejection(signal: AbortSignal, sessionId: string): Promise<never> {
75+
return new Promise<never>((_resolve, reject) => {
76+
if (signal.aborted) {
77+
reject(new Error(`The in-app network capture exceeded its budget for session ${sessionId}.`));
78+
return;
79+
}
80+
signal.addEventListener(
81+
'abort',
82+
() =>
83+
reject(
84+
new Error(`The in-app network capture exceeded its budget for session ${sessionId}.`),
85+
),
86+
{ once: true },
87+
);
88+
});
89+
}
90+
6791
function providerFailureNote(error: unknown): string {
68-
const message = error instanceof Error ? error.message : String(error);
92+
// The message can carry text from a local dev server or the app runtime, so it passes through
93+
// the diagnostic redaction owner before it reaches a response note.
94+
const { message } = redactDiagnosticData({
95+
message: error instanceof Error ? error.message : String(error),
96+
});
6997
return `In-app network capture could not answer this dump: ${message} This dump reads the session app log instead.`;
7098
}

‎packages/contracts/src/network-runtime.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export type NetworkDumpInput = Readonly<{
2323
maxScanLines: number;
2424
}>;
2525

26+
/** Payload budget one `network dump` applies; the same bound pre-binds what a transport reads. */
27+
export const NETWORK_MAX_PAYLOAD_CHARS = 2048;
28+
2629
export type NetworkProviderEntry = Readonly<{
2730
timestamp?: string;
2831
method?: string;

‎packages/daemon/src/sessions/session-observability/internal/session-network.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { DaemonPlatformServices } from '../../../core/platform-services.ts';
22
import type { NetworkDumpResult } from '@agent-device/contracts/network-runtime';
3+
import { NETWORK_MAX_PAYLOAD_CHARS } from '@agent-device/contracts/network-runtime';
34
import {
45
networkAdmissionUse,
56
resolveNetworkRuntimePlan,
@@ -19,7 +20,6 @@ import type { DaemonRequest } from '../../../core/daemon-request.ts';
1920
const NETWORK_ACTIONS = ['dump', 'log'] as const;
2021
const NETWORK_ACTIONS_MESSAGE = `network requires ${NETWORK_ACTIONS.join(' or ')}`;
2122
const NETWORK_INCLUDE_MESSAGE = `network include mode must be one of: ${NETWORK_INCLUDE_MODES.join(', ')}`;
22-
const NETWORK_MAX_PAYLOAD_CHARS = 2048;
2323
const NETWORK_MAX_SCAN_LINES = 4000;
2424

2525
export type NetworkHandlerParams = Readonly<{
@@ -170,6 +170,8 @@ function networkDumpResponse(
170170
data: {
171171
entries: result.entries,
172172
source: 'provider',
173+
// Reaching this branch means the app-side hook answered this very dump, so the capture is
174+
// live by construction; the app-log process state says nothing about it.
173175
active: true,
174176
state: 'active',
175177
backend: result.backend,

‎packages/kernel/src/redaction.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ test('other diagnostic strings retain the 400-character limit', () => {
4949

5050
test('a network payload keeps its own budget instead of the diagnostic limit', () => {
5151
const payload = 'p'.repeat(5000);
52-
assert.equal(redactNetworkPayload(payload, 2048).length, 2048 + '...<truncated>'.length);
52+
// The truncation marker counts against the budget, like the diagnostic bound.
53+
assert.equal(redactNetworkPayload(payload, 2048).length, 2048);
5354
assert.ok(redactNetworkPayload(payload, 2048).endsWith('...<truncated>'));
5455
assert.equal(redactNetworkPayload('p'.repeat(100), 2048), 'p'.repeat(100));
5556
});
@@ -65,6 +66,20 @@ test('a network payload masks secrets in each field syntax it can hold', () => {
6566
redactNetworkPayload('{"token":"adc_agent_..."}', 2048),
6667
'{"token":"adc_agent_..."}',
6768
);
69+
assert.equal(
70+
redactNetworkPayload("curl --header 'api_key=sk-live-9999'", 2048),
71+
"curl --header 'api_key=[REDACTED]",
72+
);
73+
assert.equal(
74+
redactNetworkPayload(String.raw`{"apiKey":"ab\"cd-secret"}`, 2048),
75+
'{"apiKey":"[REDACTED]"}',
76+
);
77+
});
78+
79+
test('a network payload truncation stays inside its budget', () => {
80+
const redacted = redactNetworkPayload(`${'x'.repeat(3000)}token=abc123secret`, 2048);
81+
assert.equal(redacted.length, 2048);
82+
assert.ok(redacted.includes('truncated'));
6883
});
6984

7085
test('a header with a sensitive name is replaced and its peers keep their text', () => {

‎packages/kernel/src/redaction.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const SENSITIVE_ASSIGNMENT_RE =
1111
* earlier rule cannot match the same way.
1212
*/
1313
const SENSITIVE_JSON_ASSIGNMENT_RE =
14-
/(["'][a-z0-9_-]*(?:api[_-]?key|token|secret|password|user[_-]?code|device[_-]?code|refresh[_-]?credential)[a-z0-9_-]*["']\s*[=:]\s*)("[^"]*"|'[^']*')/gi;
14+
/(["'][a-z0-9_-]*(?:api[_-]?key|token|secret|password|user[_-]?code|device[_-]?code|refresh[_-]?credential)[a-z0-9_-]*["']\s*[=:]\s*)("(?:[^"\\]|\\.)*"|'[^']*')/gi;
1515
const URL_RE = /https?:\/\/[^\s"'<>]+/gi;
1616
const REDACTED_STRING_MAX_LENGTH = 400;
1717
const REDACTED_STDERR_MAX_LENGTH = 8192;
@@ -29,7 +29,9 @@ export function redactDiagnosticData<T>(input: T): T {
2929
export function redactNetworkPayload(value: string, maxLength: number): string {
3030
const redacted = maskSecrets(value);
3131
if (redacted.length <= maxLength) return redacted;
32-
return `${redacted.slice(0, maxLength)}${TRUNCATION_SUFFIX}`;
32+
// The truncation marker counts against the budget, like the diagnostic bound.
33+
const kept = Math.max(0, maxLength - TRUNCATION_SUFFIX.length);
34+
return `${redacted.slice(0, kept)}${TRUNCATION_SUFFIX}`;
3335
}
3436

3537
/**
@@ -108,8 +110,16 @@ function maskSecrets(value: string, keyHint?: string): string {
108110
output = output.replace(
109111
SENSITIVE_ASSIGNMENT_RE,
110112
(match, key: string, separator: string, rawValue: string, offset: number, input: string) => {
111-
// A quoted key is the JSON form's job; masking it here would drop its quotes.
112-
if (input[offset - 1] === '"' || input[offset - 1] === "'") return match;
113+
// Only the exact JSON form (`"key":` with the key closing its quote before the separator) is
114+
// the JSON rule's job; a quote merely in front of the key (`'api_key=value'`) is a shell-like
115+
// assignment this rule has always masked.
116+
const before = input[offset - 1];
117+
if (
118+
(before === '"' || before === "'") &&
119+
/^["']\s*[=:]/.test(input.slice(offset + key.length))
120+
) {
121+
return match;
122+
}
113123
if (isSafeSetupUrlAssignment({ key, separator, rawValue, offset, input })) return match;
114124
if (isDocumentedTokenPlaceholder(rawValue)) return match;
115125
return `${key}${separator}[REDACTED]`;

0 commit comments

Comments
 (0)