feat(mcp): derive an API surface view from retained web network captures (#1891)
* feat(mcp): derive an API surface view from retained web network captures * fix(mcp): fail closed on sensitive parents and budget api-surface path expansion * fix(mcp): keep recorded api-surface paths and budget only templated segments * fix(mcp): read api-surface paths only from canonical URLs and charge new values once * fix(mcp): reject authority-less api-surface URLs and key groups by rule positions * fix(mcp): label api-surface confidence from all groups and cap transient path memory * fix(mcp): budget api-surface dedup sets and narrow detail outputs Charge retained deduplication sets before allocation, advertise only each analysis operation's summary from its detail output, and document that any templated group makes every API-surface page inferred. * fix(mcp): bound api-surface path text and record limitations Charge retained literal path copies before allocation, publish only valid media-type essences, and deduplicate examined record limitations onto the view. * fix(mcp): keep api-surface refusals and path splits inside the reply budget Sensitive-parent refusals now use the token sensitive_values_unretained so declared literals are not echoed. A later path split is charged against bytes already retained, and the published example includes the HAR record limitation with its matching digest. * fix(mcp): budget api-surface method text before group keys Charge method and origin text against the reply budget before the group key is serialized. Document that samples merge only when method, origin, and exact segment rules match. * fix(mcp): bound api-surface copies and control-character authorities Reject authorities containing a tab or newline so WHATWG cannot move userinfo into the recorded path. Charge four path copies, two method and origin copies, and each distinct content type against bytes already retained. * fix(mcp): preflight combined api-surface group charges Total path, method, origin, and segment charges before retaining a new group. Track content types in a set so distinct MIME essences are not compared quadratically. * fix(mcp): charge api-surface content-type sets on first use Create the per-group content-type set when the first essence is retained, and charge it before allocation. Groups with no content type no longer keep an empty set. * refactor(analysis): centralize API-surface ownership and admission Own grouping, deduplication, and combined retained/temporary resource admission in one accumulator. Charge fixed group storage before allocation and materialize only the requested page. Share recorded URL parsing between capture redaction and projection, preserving source paths and excluding ambiguous credential fields. Cover resource admission invariants and the compiled HAR-to-view workflow. * fix(analysis): admit capture schema clone before parsing * fix(javascript): share Electron mutation proof with wrapped calls * test(mcp): align graph and catalog checks with current contracts * fix(analysis): admit and validate portable capture views --------- Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
Matt Van Horn
Cursor Agent
morluto
parent
64b71b180a
commit
880a603fe8
@@ -0,0 +1,460 @@
|
||||
import { expect, it } from "vitest";
|
||||
import {
|
||||
analysisViewCaptureFixture,
|
||||
analysisViewCaptureEvidence,
|
||||
} from "../../../tests/fixtures/analysisView.js";
|
||||
import { projectAnalysisView } from "./analysisView.js";
|
||||
|
||||
const project = (
|
||||
capture = analysisViewCaptureFixture(),
|
||||
offset = 0,
|
||||
limit = 50,
|
||||
parameters: unknown = { sensitive_value_count: 0 },
|
||||
) => {
|
||||
const parent = analysisViewCaptureEvidence(capture);
|
||||
return projectAnalysisView(
|
||||
{
|
||||
evidenceId: parent.evidence_id,
|
||||
operation: parent.operation,
|
||||
normalizedResult: parent.normalized_result,
|
||||
limitations: [],
|
||||
parameters,
|
||||
},
|
||||
{ kind: "api-surface", offset, limit },
|
||||
);
|
||||
};
|
||||
it("groups only exact formats, preserves literal routes, excludes URLs and produces deterministic digests", () => {
|
||||
const result = project();
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(project()).toEqual(result);
|
||||
const surface = result.value;
|
||||
expect(surface.groups.map((group) => group.path_template)).toEqual([
|
||||
"/api/CreateDocument",
|
||||
"/api/ListDocuments",
|
||||
"/api/Report2",
|
||||
"/api/Report3",
|
||||
"/api/v1/users",
|
||||
"/api/v2/users",
|
||||
"/dates/2023-02-29",
|
||||
"/dates/{iso-date}",
|
||||
"/ids/abc123",
|
||||
"/ids/{hex}",
|
||||
"/ids/{uuid}",
|
||||
"/users/{numeric}",
|
||||
]);
|
||||
expect(surface.groups.at(-1)).toMatchObject({
|
||||
record_ordinals: [0, 1],
|
||||
statuses: [200, 404],
|
||||
content_types: ["application/json", "text/plain"],
|
||||
flags: ["mixed-statuses", "mixed-content-types"],
|
||||
templated_segments: [
|
||||
{
|
||||
segment_index: 1,
|
||||
rule: "numeric",
|
||||
basis: "inferred",
|
||||
observed_values: ["8812", "9031"],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(surface.excluded_records).toEqual([
|
||||
{ ordinal: 13, reason: "url-redacted" },
|
||||
{ ordinal: 14, reason: "url-unparseable" },
|
||||
]);
|
||||
expect(JSON.stringify(surface)).not.toContain("not-in-view");
|
||||
expect(JSON.stringify(surface)).not.toContain("雪");
|
||||
});
|
||||
it("pages groups independently of record completeness and preserves null digests", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.artifact.sha256 = null;
|
||||
capture.total_records = 30;
|
||||
const result = project(capture, 10, 2);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value).toMatchObject({
|
||||
artifact: { sha256: null },
|
||||
coverage: { examined: 2, total: 12, exhausted: true, next_offset: null },
|
||||
records: { examined: 15, total: 30, excluded: 2 },
|
||||
});
|
||||
expect(result.value.limitations.join(" ")).toContain("ordinal subset");
|
||||
expect(project(capture, 50, 2)).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
coverage: { status: "empty", exhausted: true, next_offset: null },
|
||||
},
|
||||
});
|
||||
});
|
||||
it("rejects native captures and malformed parents with typed errors", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.format = "mitmproxy";
|
||||
expect(project(capture)).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisUnsupportedTargetError" },
|
||||
});
|
||||
expect(
|
||||
projectAnalysisView(
|
||||
{
|
||||
evidenceId: `ev_${"a".repeat(64)}`,
|
||||
operation: "inspect_web_network_capture",
|
||||
normalizedResult: {},
|
||||
limitations: [],
|
||||
},
|
||||
{ kind: "api-surface", offset: 0, limit: 1 },
|
||||
),
|
||||
).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } });
|
||||
});
|
||||
|
||||
it("preserves partial response facts and explains unavailable requests", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 5);
|
||||
const values = [
|
||||
{
|
||||
request: {
|
||||
method: "GET",
|
||||
url: "https://user:password@api.example.test/1/2?private=value",
|
||||
},
|
||||
response: { status: 200, content: null },
|
||||
},
|
||||
{ request: { method: null, url: "https://api.example.test/" } },
|
||||
null,
|
||||
{ request: { method: "GET", url: "data:text/plain,hello" } },
|
||||
{
|
||||
request: { method: "GET", url: "https://api.example.test/1/2" },
|
||||
response: { status: null, content: { mimeType: "application/json" } },
|
||||
},
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
record.reported = values[index] ?? null;
|
||||
});
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups).toMatchObject([
|
||||
{
|
||||
origin: "https://api.example.test",
|
||||
path_template: "/{numeric}/{numeric}",
|
||||
statuses: [200],
|
||||
content_types: ["application/json"],
|
||||
templated_segments: [
|
||||
{ segment_index: 0, observed_values: ["1"] },
|
||||
{ segment_index: 1, observed_values: ["2"] },
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(result.value.excluded_records).toEqual([
|
||||
{ ordinal: 1, reason: "method-unavailable" },
|
||||
{ ordinal: 2, reason: "request-unavailable" },
|
||||
{ ordinal: 3, reason: "url-unparseable" },
|
||||
]);
|
||||
expect(result.value.unknowns).not.toEqual([]);
|
||||
expect(JSON.stringify(result.value)).not.toContain("password");
|
||||
expect(JSON.stringify(result.value)).not.toContain("private");
|
||||
});
|
||||
|
||||
it("keeps encoded segments literal and distinguishes origin and method", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 4);
|
||||
const urls = [
|
||||
"https://a.test/%31",
|
||||
"https://a.test/1",
|
||||
"https://b.test/1",
|
||||
"https://a.test/1",
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
record.reported = {
|
||||
request: { method: index === 3 ? "POST" : "GET", url: urls[index]! },
|
||||
response: {
|
||||
status: 200,
|
||||
content: { mimeType: "application/json; charset=utf-8" },
|
||||
},
|
||||
};
|
||||
});
|
||||
const result = project(capture, 0, 2);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.coverage).toMatchObject({
|
||||
total: 4,
|
||||
next_offset: 2,
|
||||
exhausted: false,
|
||||
});
|
||||
expect(result.value.groups[0]?.path_template).toBe("/%31");
|
||||
});
|
||||
|
||||
it("refuses parents whose sensitive declarations it cannot check", () => {
|
||||
for (const parameters of [{ sensitive_value_count: 1 }, {}, null]) {
|
||||
const result = project(analysisViewCaptureFixture(), 0, 50, parameters);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisUnsupportedTargetError" },
|
||||
});
|
||||
if (result.ok) continue;
|
||||
expect(result.error).toMatchObject({
|
||||
reason: "sensitive_values_unretained",
|
||||
});
|
||||
expect(result.error.message).not.toContain("path templates");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects cyclic captures and accessor members without invoking them", () => {
|
||||
const cyclic: { self?: unknown } = {};
|
||||
cyclic.self = cyclic;
|
||||
let accessed = false;
|
||||
const accessor = {
|
||||
get records() {
|
||||
accessed = true;
|
||||
return [];
|
||||
},
|
||||
};
|
||||
const hiddenAccessor = Object.defineProperty({}, "records", {
|
||||
get() {
|
||||
accessed = true;
|
||||
return [];
|
||||
},
|
||||
});
|
||||
const inherited = Object.create({ records: [] }) as unknown;
|
||||
for (const normalizedResult of [cyclic, accessor, hiddenAccessor, inherited])
|
||||
expect(
|
||||
projectAnalysisView(
|
||||
{
|
||||
evidenceId: `ev_${"a".repeat(64)}`,
|
||||
operation: "inspect_web_network_capture",
|
||||
normalizedResult,
|
||||
limitations: [],
|
||||
},
|
||||
{ kind: "api-surface", offset: 0, limit: 1 },
|
||||
),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisInputError" },
|
||||
});
|
||||
expect(accessed).toBe(false);
|
||||
});
|
||||
|
||||
it("treats HAR missing-response sentinels as unknown", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 1);
|
||||
capture.records[0]!.reported = {
|
||||
request: { method: "GET", url: "https://api.example.test/a" },
|
||||
response: { status: 0, content: { mimeType: " ; charset=utf-8" } },
|
||||
};
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups[0]).toMatchObject({
|
||||
statuses: [],
|
||||
content_types: [],
|
||||
});
|
||||
expect(result.value.unknowns).not.toEqual([]);
|
||||
});
|
||||
|
||||
it("refuses a path whose templated representation exceeds projection capacity", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 1);
|
||||
capture.records[0]!.reported = {
|
||||
request: {
|
||||
method: "GET",
|
||||
url: `https://api.example.test${"/1".repeat(800_000)}`,
|
||||
},
|
||||
};
|
||||
expect(project(capture)).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisResourceConstraintError" },
|
||||
});
|
||||
});
|
||||
|
||||
it("scans highly segmented literal paths and keeps recorded dot segments", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 3);
|
||||
const urls = [
|
||||
`https://api.example.test${"/a".repeat(90_000)}`,
|
||||
"https://api.example.test/a/%2e%2e/admin",
|
||||
"https://api.example.test/admin",
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
record.reported = { request: { method: "GET", url: urls[index] ?? null } };
|
||||
});
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups.map((group) => group.path_template)).toEqual(
|
||||
expect.arrayContaining(["/a/%2e%2e/admin", "/admin"]),
|
||||
);
|
||||
expect(result.value.groups).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("excludes slashless URL spellings instead of reading their paths", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 4);
|
||||
const urls = [
|
||||
"https:user:pass@example.test/path",
|
||||
"https:123/456",
|
||||
"https:///user:pass@example.test/path",
|
||||
"https://\\user:pass@example.test/path",
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
record.reported = { request: { method: "GET", url: urls[index] ?? null } };
|
||||
});
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups).toEqual([]);
|
||||
expect(result.value.excluded_records).toEqual([
|
||||
{ ordinal: 0, reason: "url-unparseable" },
|
||||
{ ordinal: 1, reason: "url-unparseable" },
|
||||
{ ordinal: 2, reason: "url-unparseable" },
|
||||
{ ordinal: 3, reason: "url-unparseable" },
|
||||
]);
|
||||
expect(JSON.stringify(result.value)).not.toContain("pass");
|
||||
});
|
||||
|
||||
it("excludes control-only authorities instead of publishing their userinfo", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 3);
|
||||
const urls = [
|
||||
"https://\t/user:pass@example.test/path",
|
||||
"https://\n/user:pass@example.test/path",
|
||||
"https://\r/user:pass@example.test/path",
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
record.reported = { request: { method: "GET", url: urls[index] ?? null } };
|
||||
});
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups).toEqual([]);
|
||||
expect(result.value.excluded_records).toEqual([
|
||||
{ ordinal: 0, reason: "url-unparseable" },
|
||||
{ ordinal: 1, reason: "url-unparseable" },
|
||||
{ ordinal: 2, reason: "url-unparseable" },
|
||||
]);
|
||||
expect(JSON.stringify(result.value)).not.toContain("pass");
|
||||
});
|
||||
|
||||
it("keeps distinct content types without repeating a retained essence", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
const record = capture.records[0]!;
|
||||
const types = ["text/plain", "application/json", "text/plain"];
|
||||
capture.records = types.map((mimeType, ordinal) => ({
|
||||
...record,
|
||||
ordinal,
|
||||
reported: {
|
||||
request: { method: "GET", url: "https://api.example.test/a" },
|
||||
response: { status: 200, content: { mimeType } },
|
||||
},
|
||||
}));
|
||||
capture.total_records = capture.records.length;
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups[0]?.content_types).toEqual([
|
||||
"application/json",
|
||||
"text/plain",
|
||||
]);
|
||||
});
|
||||
|
||||
it("charges repeated observed values once when merging", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
const record = capture.records[0]!;
|
||||
const path = "/1".repeat(800);
|
||||
capture.records = Array.from({ length: 1_000 }, (_, ordinal) => ({
|
||||
...record,
|
||||
ordinal,
|
||||
reported: {
|
||||
request: { method: "GET", url: `https://api.example.test${path}` },
|
||||
},
|
||||
}));
|
||||
capture.total_records = capture.records.length;
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups).toHaveLength(1);
|
||||
expect(result.value.groups[0]?.templated_segments).toHaveLength(800);
|
||||
});
|
||||
|
||||
it("keeps a recorded literal placeholder apart from a templated segment", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 2);
|
||||
const urls = [
|
||||
"https://api.example.test/users/{numeric}",
|
||||
"https://api.example.test/users/123",
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
record.reported = { request: { method: "GET", url: urls[index] ?? null } };
|
||||
});
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups).toHaveLength(2);
|
||||
expect(
|
||||
result.value.groups.map((group) => group.templated_segments.length).sort(),
|
||||
).toEqual([0, 1]);
|
||||
});
|
||||
|
||||
it("treats malformed media types as unknown and keeps source limitations", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
const caveat =
|
||||
"HAR text is producer-decoded Unicode. Original body bytes remain unknown.";
|
||||
const captureCaveat = "Producer omitted exchanges before capture startup.";
|
||||
capture.limitations = [captureCaveat, caveat];
|
||||
const malformed = [
|
||||
"text /plain",
|
||||
"unknown",
|
||||
"\u00a0text/plain\u00a0",
|
||||
"\u000btext/plain\u000b",
|
||||
"\u000ctext/plain\u000c",
|
||||
"text/K",
|
||||
];
|
||||
capture.records = malformed.map((_, ordinal) => ({
|
||||
...capture.records[0]!,
|
||||
ordinal,
|
||||
}));
|
||||
capture.total_records = capture.records.length;
|
||||
capture.records.forEach((record, index) => {
|
||||
record.limitations = [caveat];
|
||||
record.reported = {
|
||||
request: { method: "GET", url: "https://api.example.test/a" },
|
||||
response: {
|
||||
status: 200,
|
||||
content: { mimeType: malformed[index]! },
|
||||
},
|
||||
};
|
||||
});
|
||||
const result = project(capture);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups[0]).toMatchObject({
|
||||
content_types: [],
|
||||
flags: [],
|
||||
});
|
||||
expect(result.value.unknowns).not.toEqual([]);
|
||||
expect(result.value.limitations).toContain(caveat);
|
||||
expect(result.value.limitations).toContain(captureCaveat);
|
||||
expect(result.value.limitations.filter((item) => item === caveat)).toEqual([
|
||||
caveat,
|
||||
]);
|
||||
});
|
||||
|
||||
it("compares media types case-insensitively and counts templated groups across pages", () => {
|
||||
const capture = analysisViewCaptureFixture();
|
||||
capture.records = capture.records.slice(0, 3);
|
||||
const reported = [
|
||||
["https://api.example.test/a", "Application/JSON"],
|
||||
["https://api.example.test/a", " \tapplication/json\t ; charset=utf-8"],
|
||||
["https://api.example.test/users/7", "application/json"],
|
||||
];
|
||||
capture.records.forEach((record, index) => {
|
||||
const [url, mimeType] = reported[index] ?? [];
|
||||
record.reported = {
|
||||
request: { method: "GET", url: url ?? null },
|
||||
response: { status: 200, content: { mimeType: mimeType ?? null } },
|
||||
};
|
||||
});
|
||||
const result = project(capture, 0, 1);
|
||||
if (!result.ok || result.value.kind !== "api-surface")
|
||||
throw new Error("Expected surface");
|
||||
expect(result.value.groups[0]).toMatchObject({
|
||||
path_template: "/a",
|
||||
content_types: ["application/json"],
|
||||
flags: [],
|
||||
});
|
||||
expect(result.value.templated_groups).toBe(1);
|
||||
});
|
||||
Reference in New Issue
Block a user