Skip to content

Commit 2237d2d

Browse files
cliffhallclaude
andcommitted
perf(network): address #1387 fifth-pass — memoize masking, mask DCR mgmt token (#1386)
Fifth-pass review (LGTM): - Memoized `maskSecretsInBody` in `BodyPreview` (recommended item #1): a Reveal/Hide click no longer re-parses + re-walks the body; cost is once per mount, and the `key` remount re-runs it on body/content-type change. Guarded so a too-large body is never parsed (the hook runs unconditionally, with the size check inside the memo). - Added `registration_access_token` (RFC 7592 DCR management credential, same bearer class) to the masked key set, and a confidential-client `/register` response fixture test (#5) asserting client_secret + registration_access_token masked, client_id/metadata visible. - Tightened the `isMaskableValue` doc to state the exact contract: non-null, non-empty-string values are masked wholesale (#3). Items #2 (transport parse cost — capped by the memo) and #4 (test-only MASK_PLACEHOLDER export) left as-is per the review. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent af7ad0d commit 2237d2d

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

‎clients/web/src/components/groups/NetworkEntry/NetworkEntry.tsx‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useMemo, useState } from "react";
22
import {
33
Badge,
44
Button,
@@ -147,6 +147,27 @@ function BodyPreview({
147147
const [revealed, setRevealed] = useState(false);
148148

149149
const tooLarge = body.length > MAX_INLINE_BODY_CHARS;
150+
151+
// OAuth responses (token exchange, DCR) and the token request carry
152+
// bearer-grade secrets. Mask them by default and gate the raw values behind
153+
// an explicit reveal so they aren't exposed at a glance during a
154+
// screen-share. The entry's content-type scopes which parser runs (so a
155+
// plaintext/HTML error body is never guessed at). Bodies without secrets
156+
// render as-is with no toggle.
157+
//
158+
// Memoized so a Reveal/Hide click (a re-render) doesn't re-parse and re-walk
159+
// the body; the cost is paid once per mount, and the `key={…}` remount on
160+
// body/content-type change re-runs it. Skipped for too-large bodies so we
161+
// never parse something we won't display (the hook must run unconditionally,
162+
// hence the in-memo guard rather than an early return above it).
163+
const { masked, hasSecrets } = useMemo(
164+
() =>
165+
tooLarge
166+
? { masked: body, hasSecrets: false }
167+
: maskSecretsInBody(body, contentType),
168+
[tooLarge, body, contentType],
169+
);
170+
150171
if (tooLarge) {
151172
return (
152173
<Text size="xs" c="dimmed">
@@ -155,13 +176,6 @@ function BodyPreview({
155176
);
156177
}
157178

158-
// OAuth responses (token exchange, DCR) and the token request carry
159-
// bearer-grade secrets. Mask them by default and gate the raw values behind
160-
// an explicit reveal so they aren't exposed at a glance during a
161-
// screen-share. The entry's content-type scopes which parser runs (so a
162-
// plaintext/HTML error body is never guessed at). Bodies without secrets
163-
// render as-is with no toggle.
164-
const { masked, hasSecrets } = maskSecretsInBody(body, contentType);
165179
if (!hasSecrets) {
166180
return <ContentViewer block={{ type: "text", text: body }} copyable />;
167181
}

‎clients/web/src/utils/maskSecrets.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ describe("maskSecretsInBody", () => {
6363
expect(JSON.parse(masked).access_token).toBe(MASK_PLACEHOLDER);
6464
});
6565

66+
it("masks a confidential-client DCR (/register) response", () => {
67+
// RFC 7591/7592 happy path: client_secret and the registration management
68+
// token are bearer-grade and masked; client_id and metadata stay visible.
69+
const body = JSON.stringify({
70+
client_id: "dyn-123",
71+
client_secret: "REG-SECRET",
72+
registration_access_token: "REG-RAT",
73+
registration_client_uri: "http://localhost:3001/register/dyn-123",
74+
client_id_issued_at: 1735689600,
75+
});
76+
const { masked, hasSecrets } = maskSecretsInBody(body);
77+
expect(hasSecrets).toBe(true);
78+
const parsed = JSON.parse(masked);
79+
expect(parsed.client_secret).toBe(MASK_PLACEHOLDER);
80+
expect(parsed.registration_access_token).toBe(MASK_PLACEHOLDER);
81+
expect(masked).not.toContain("REG-SECRET");
82+
expect(masked).not.toContain("REG-RAT");
83+
// Non-secret fields untouched.
84+
expect(parsed.client_id).toBe("dyn-123");
85+
expect(parsed.registration_client_uri).toBe(
86+
"http://localhost:3001/register/dyn-123",
87+
);
88+
});
89+
6690
it("reports no secrets (and leaves content intact) for discovery metadata", () => {
6791
const body = JSON.stringify({
6892
issuer: "http://localhost:3001/",

‎clients/web/src/utils/maskSecrets.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
// Keys masked in JSON bodies — bearer-grade secrets only. `code` is
1717
// deliberately NOT here: a JSON body's `code` is usually something else (e.g.
1818
// a JSON-RPC error `code`), and we don't want to mask those.
19+
// `registration_access_token` is the DCR management credential (RFC 7592),
20+
// same bearer class as `access_token`.
1921
const JSON_SENSITIVE_KEYS = new Set([
2022
"access_token",
2123
"refresh_token",
2224
"id_token",
2325
"client_secret",
26+
"registration_access_token",
2427
]);
2528

2629
// Keys masked in form-encoded bodies — the JSON set plus the single-use OAuth
@@ -41,10 +44,11 @@ function isSensitiveKey(set: ReadonlySet<string>, key: string): boolean {
4144
return set.has(key.toLowerCase());
4245
}
4346

44-
// Whether a value under a sensitive key should be masked. Strings are masked
45-
// when non-empty (an empty `access_token` carries nothing); any non-string,
46-
// non-null value (a non-standard object/array/number wrapper) is masked
47-
// wholesale so it can't leak through the recursion under a sensitive key.
47+
// Whether a value under a sensitive key should be masked. The contract is
48+
// "any non-null, non-empty-string value": strings are masked when non-empty
49+
// (an empty `access_token` carries nothing), and any non-string value
50+
// (object/array/number/boolean wrapper — pathological for OAuth, but a safe
51+
// default) is masked wholesale so it can't leak through the recursion.
4852
function isMaskableValue(value: unknown): boolean {
4953
if (value === null || value === undefined) return false;
5054
if (typeof value === "string") return value.length > 0;

0 commit comments

Comments
 (0)