pi-lean-host 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -62
- package/core/api-guide-types.ts +52 -1
- package/core/api-toggle.ts +20 -1
- package/core/auth.ts +186 -0
- package/core/guide-store.ts +0 -26
- package/core/helpers.ts +173 -57
- package/core/local-helpers.ts +2 -1
- package/core/parse-api-guide.ts +316 -55
- package/core/path-template.ts +30 -1
- package/core/secrets-command.ts +338 -0
- package/core/secrets-store.ts +180 -0
- package/core/ssrf-guard.ts +5 -0
- package/core/transport.ts +114 -26
- package/package.json +14 -1
- package/tools/api-fetch.ts +113 -26
- package/tools/api-guide.ts +13 -15
- package/tools/api-learn.ts +16 -19
- package/tools/api-probe.ts +528 -53
- package/tools/utils.ts +21 -0
package/core/parse-api-guide.ts
CHANGED
|
@@ -139,13 +139,9 @@ function requireHttpUrl(
|
|
|
139
139
|
try {
|
|
140
140
|
const u = new URL(value);
|
|
141
141
|
if (u.protocol !== "http:" && u.protocol !== "https:") {
|
|
142
|
-
return fail(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
"an http or https URL",
|
|
146
|
-
`protocol "${u.protocol}"`,
|
|
147
|
-
{ ...(opts?.protocolFix ? { fix: opts.protocolFix } : {}) },
|
|
148
|
-
);
|
|
142
|
+
return fail(file, key, "an http or https URL", `protocol "${u.protocol}"`, {
|
|
143
|
+
...(opts?.protocolFix ? { fix: opts.protocolFix } : {}),
|
|
144
|
+
});
|
|
149
145
|
}
|
|
150
146
|
} catch {
|
|
151
147
|
return fail(file, key, "a valid http/https URL", `"${value}"`, {
|
|
@@ -254,6 +250,18 @@ function validatePagination(
|
|
|
254
250
|
}
|
|
255
251
|
cfg.pageSize = ps;
|
|
256
252
|
}
|
|
253
|
+
const base = p["base"];
|
|
254
|
+
if (base !== undefined) {
|
|
255
|
+
if (typeof base !== "number" || !Number.isInteger(base)) {
|
|
256
|
+
return fail(
|
|
257
|
+
file,
|
|
258
|
+
`${fieldPrefix}.base`,
|
|
259
|
+
"a finite integer (the seed value for the page param)",
|
|
260
|
+
describeFound(base),
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
cfg.base = base;
|
|
264
|
+
}
|
|
257
265
|
} else if (style === "nextLink") {
|
|
258
266
|
const nlp = p["nextLinkPath"];
|
|
259
267
|
if (typeof nlp !== "string" || nlp === "") {
|
|
@@ -386,6 +394,43 @@ function validateResponseShape(
|
|
|
386
394
|
// Auth
|
|
387
395
|
// ═══════════════════════════════════════════════════════════════════
|
|
388
396
|
|
|
397
|
+
/**
|
|
398
|
+
* Validate a `Record<string, string>` auth sub-field (headers, secretRefs,
|
|
399
|
+
* secretQueryRefs, headerPrefixes). Returns the parsed record, or a ParseError
|
|
400
|
+
* when `raw` is absent/null/non-object/an array, or a value fails `valueOk`
|
|
401
|
+
* (default: any string; `headerPrefixes` passes a non-empty check).
|
|
402
|
+
*/
|
|
403
|
+
function parseStringRecord(
|
|
404
|
+
raw: unknown,
|
|
405
|
+
file: string | undefined,
|
|
406
|
+
fm: string,
|
|
407
|
+
field: string,
|
|
408
|
+
expect: string,
|
|
409
|
+
valueOk: (v: unknown) => boolean = (v) => typeof v === "string",
|
|
410
|
+
): Record<string, string> | ParseApiGuideResult {
|
|
411
|
+
if (
|
|
412
|
+
raw === null ||
|
|
413
|
+
typeof raw !== "object" ||
|
|
414
|
+
Array.isArray(raw) ||
|
|
415
|
+
Object.values(raw).some((v) => !valueOk(v))
|
|
416
|
+
) {
|
|
417
|
+
return fail(file, field, expect, describeFound(raw), {
|
|
418
|
+
snippet: snippetFor(fm, "auth"),
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
return raw as Record<string, string>;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** `true` when a `parseStringRecord` / validator result is a ParseError. */
|
|
425
|
+
function isParseErr<T>(v: T | ParseApiGuideResult): v is ParseApiGuideResult {
|
|
426
|
+
return (
|
|
427
|
+
typeof v === "object" &&
|
|
428
|
+
v !== null &&
|
|
429
|
+
"ok" in v &&
|
|
430
|
+
(v as { ok: unknown }).ok === false
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
389
434
|
function validateAuth(
|
|
390
435
|
raw: unknown,
|
|
391
436
|
file: string | undefined,
|
|
@@ -396,15 +441,9 @@ function validateAuth(
|
|
|
396
441
|
return { kind: "none" };
|
|
397
442
|
}
|
|
398
443
|
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
399
|
-
return fail(
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
"a YAML mapping (kind: ...)",
|
|
403
|
-
describeFound(raw),
|
|
404
|
-
{
|
|
405
|
-
snippet: snippetFor(fm, "auth"),
|
|
406
|
-
},
|
|
407
|
-
);
|
|
444
|
+
return fail(file, "auth", "a YAML mapping (kind: ...)", describeFound(raw), {
|
|
445
|
+
snippet: snippetFor(fm, "auth"),
|
|
446
|
+
});
|
|
408
447
|
}
|
|
409
448
|
const a = raw as Record<string, unknown>;
|
|
410
449
|
const kindRaw = a["kind"];
|
|
@@ -422,31 +461,224 @@ function validateAuth(
|
|
|
422
461
|
"auth.kind",
|
|
423
462
|
"one of: none | static-key | oauth2",
|
|
424
463
|
kindRaw,
|
|
425
|
-
{ fix: "Use `kind: none` (
|
|
464
|
+
{ fix: "Use `kind: none` (public) or `kind: static-key` (keyed header)" },
|
|
426
465
|
);
|
|
427
466
|
}
|
|
467
|
+
// oauth2 is a declared seam, not realized — reject at parse so a recipe
|
|
468
|
+
// can't load an auth mode the transport can't honor.
|
|
469
|
+
if (kindRaw === "oauth2") {
|
|
470
|
+
return fail(file, "auth.kind", "one of: none | static-key", "oauth2", {
|
|
471
|
+
fix: "OAuth2 is not yet implemented. Use kind: none (public) or kind: static-key (keyed header) for now.",
|
|
472
|
+
});
|
|
473
|
+
}
|
|
428
474
|
// Parse optional headers (per-kind extra headers, e.g. X-Api-Key for DEMO_KEY).
|
|
429
475
|
let headers: Record<string, string> | undefined;
|
|
430
|
-
|
|
431
|
-
|
|
476
|
+
if (a["headers"] !== undefined) {
|
|
477
|
+
const hr = parseStringRecord(
|
|
478
|
+
a["headers"],
|
|
479
|
+
file,
|
|
480
|
+
fm,
|
|
481
|
+
"auth.headers",
|
|
482
|
+
"a YAML mapping of string → string",
|
|
483
|
+
);
|
|
484
|
+
if (isParseErr(hr)) return hr;
|
|
485
|
+
headers = hr;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// static-key reference fields (secretRefs/requires/optional).
|
|
489
|
+
let secretRefs: Record<string, string> | undefined;
|
|
490
|
+
if (a["secretRefs"] !== undefined) {
|
|
491
|
+
const sr = parseStringRecord(
|
|
492
|
+
a["secretRefs"],
|
|
493
|
+
file,
|
|
494
|
+
fm,
|
|
495
|
+
"auth.secretRefs",
|
|
496
|
+
"a YAML mapping of header name → secret name",
|
|
497
|
+
);
|
|
498
|
+
if (isParseErr(sr)) return sr;
|
|
499
|
+
secretRefs = sr;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Query-param secrets: maps query param name → secret store name.
|
|
503
|
+
// Same shape + consistency rules as secretRefs; the collision-with-op-`params`
|
|
504
|
+
// rule is enforced in parseApiGuide (auth is parsed before operations).
|
|
505
|
+
let secretQueryRefs: Record<string, string> | undefined;
|
|
506
|
+
if (a["secretQueryRefs"] !== undefined) {
|
|
507
|
+
const qr = parseStringRecord(
|
|
508
|
+
a["secretQueryRefs"],
|
|
509
|
+
file,
|
|
510
|
+
fm,
|
|
511
|
+
"auth.secretQueryRefs",
|
|
512
|
+
"a YAML mapping of query param name → secret name",
|
|
513
|
+
);
|
|
514
|
+
if (isParseErr(qr)) return qr;
|
|
515
|
+
secretQueryRefs = qr;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
let requires: string[] | undefined;
|
|
519
|
+
const reqRaw = a["requires"];
|
|
520
|
+
if (reqRaw !== undefined) {
|
|
432
521
|
if (
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
Object.values(headersRaw).some((v) => typeof v !== "string")
|
|
522
|
+
!Array.isArray(reqRaw) ||
|
|
523
|
+
reqRaw.length === 0 ||
|
|
524
|
+
reqRaw.some((v) => typeof v !== "string")
|
|
437
525
|
) {
|
|
438
526
|
return fail(
|
|
439
527
|
file,
|
|
440
|
-
"auth.
|
|
441
|
-
"a
|
|
442
|
-
describeFound(
|
|
528
|
+
"auth.requires",
|
|
529
|
+
"a non-empty list of secret names",
|
|
530
|
+
describeFound(reqRaw),
|
|
531
|
+
{ snippet: snippetFor(fm, "auth") },
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
requires = reqRaw as string[];
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
let optional: string[] | undefined;
|
|
538
|
+
const optRaw = a["optional"];
|
|
539
|
+
if (optRaw !== undefined) {
|
|
540
|
+
if (
|
|
541
|
+
!Array.isArray(optRaw) ||
|
|
542
|
+
optRaw.length === 0 ||
|
|
543
|
+
optRaw.some((v) => typeof v !== "string")
|
|
544
|
+
) {
|
|
545
|
+
return fail(
|
|
546
|
+
file,
|
|
547
|
+
"auth.optional",
|
|
548
|
+
"a non-empty list of secret names",
|
|
549
|
+
describeFound(optRaw),
|
|
550
|
+
{ snippet: snippetFor(fm, "auth") },
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
optional = optRaw as string[];
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// static-key header prefixes: header name → non-empty prefix string
|
|
557
|
+
// prepended to the resolved secret value. Same mapping shape as secretRefs;
|
|
558
|
+
// every key must also be a secretRefs header (a prefix for a non-secret
|
|
559
|
+
// header is a mistake).
|
|
560
|
+
let headerPrefixes: Record<string, string> | undefined;
|
|
561
|
+
if (a["headerPrefixes"] !== undefined) {
|
|
562
|
+
const hp = parseStringRecord(
|
|
563
|
+
a["headerPrefixes"],
|
|
564
|
+
file,
|
|
565
|
+
fm,
|
|
566
|
+
"auth.headerPrefixes",
|
|
567
|
+
"a YAML mapping of header name → non-empty prefix string",
|
|
568
|
+
(v) => typeof v === "string" && v.length > 0,
|
|
569
|
+
);
|
|
570
|
+
if (isParseErr(hp)) return hp;
|
|
571
|
+
headerPrefixes = hp;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// Fail-closed consistency rules.
|
|
575
|
+
if (
|
|
576
|
+
kindRaw === "none" &&
|
|
577
|
+
(secretRefs || secretQueryRefs || requires || optional || headerPrefixes)
|
|
578
|
+
) {
|
|
579
|
+
return fail(
|
|
580
|
+
file,
|
|
581
|
+
"auth.secretRefs",
|
|
582
|
+
"absent when auth.kind is none",
|
|
583
|
+
"secretRefs/secretQueryRefs/requires/optional with kind: none",
|
|
584
|
+
{
|
|
585
|
+
fix: "Use auth.kind: static-key to reference stored secrets, or remove the auth fields for a public API.",
|
|
586
|
+
},
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
if (requires || optional || secretRefs || secretQueryRefs) {
|
|
590
|
+
const declared = new Set([...(requires ?? []), ...(optional ?? [])]);
|
|
591
|
+
const refNames = new Set([
|
|
592
|
+
...Object.values(secretRefs ?? {}),
|
|
593
|
+
...Object.values(secretQueryRefs ?? {}),
|
|
594
|
+
]);
|
|
595
|
+
const checkRefs = (
|
|
596
|
+
refs: Record<string, string>,
|
|
597
|
+
field: string,
|
|
598
|
+
): ParseApiGuideResult | null => {
|
|
599
|
+
for (const outName of Object.keys(refs)) {
|
|
600
|
+
const secretName = refs[outName]!;
|
|
601
|
+
if (!declared.has(secretName)) {
|
|
602
|
+
return fail(
|
|
603
|
+
file,
|
|
604
|
+
`${field}.${outName}`,
|
|
605
|
+
"a secret name declared in auth.requires or auth.optional",
|
|
606
|
+
`"${secretName}" is not in requires/optional`,
|
|
607
|
+
{
|
|
608
|
+
fix: `Add "${secretName}" to auth.requires or auth.optional (or fix the reference).`,
|
|
609
|
+
},
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return null;
|
|
614
|
+
};
|
|
615
|
+
const r1 = checkRefs(secretRefs ?? {}, "auth.secretRefs");
|
|
616
|
+
if (r1) return r1;
|
|
617
|
+
const r2 = checkRefs(secretQueryRefs ?? {}, "auth.secretQueryRefs");
|
|
618
|
+
if (r2) return r2;
|
|
619
|
+
const both = (requires ?? []).filter((n) => (optional ?? []).includes(n));
|
|
620
|
+
if (both.length > 0) {
|
|
621
|
+
return fail(
|
|
622
|
+
file,
|
|
623
|
+
"auth.requires",
|
|
624
|
+
"secret names not duplicated across requires and optional",
|
|
625
|
+
`in both: ${both.join(", ")}`,
|
|
626
|
+
{
|
|
627
|
+
fix: `Move "${both[0]}" to either requires or optional, not both.`,
|
|
628
|
+
},
|
|
443
629
|
);
|
|
444
630
|
}
|
|
445
|
-
|
|
631
|
+
const checkDeclared = (
|
|
632
|
+
names: string[],
|
|
633
|
+
field: string,
|
|
634
|
+
): ParseApiGuideResult | null => {
|
|
635
|
+
for (const name of names) {
|
|
636
|
+
if (!refNames.has(name)) {
|
|
637
|
+
return fail(
|
|
638
|
+
file,
|
|
639
|
+
field,
|
|
640
|
+
"a secret name referenced by auth.secretRefs or auth.secretQueryRefs",
|
|
641
|
+
`"${name}" is declared here but not referenced by any header/query ref`,
|
|
642
|
+
{
|
|
643
|
+
fix: `Reference "${name}" from auth.secretRefs or auth.secretQueryRefs, or remove it from ${field}.`,
|
|
644
|
+
},
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return null;
|
|
649
|
+
};
|
|
650
|
+
const r3 = checkDeclared(requires ?? [], "auth.requires");
|
|
651
|
+
if (r3) return r3;
|
|
652
|
+
const r4 = checkDeclared(optional ?? [], "auth.optional");
|
|
653
|
+
if (r4) return r4;
|
|
654
|
+
}
|
|
655
|
+
// Every headerPrefixes key must target a secret-injected header; a prefix
|
|
656
|
+
// for a header that isn't in secretRefs is a mistake. Checked outside the
|
|
657
|
+
// secretRefs/requires/optional block so a headerPrefixes-only guide fails
|
|
658
|
+
// too (the prefix would otherwise be dead at fetch time silently).
|
|
659
|
+
if (headerPrefixes) {
|
|
660
|
+
for (const headerName of Object.keys(headerPrefixes)) {
|
|
661
|
+
if (!secretRefs || !(headerName in secretRefs)) {
|
|
662
|
+
return fail(
|
|
663
|
+
file,
|
|
664
|
+
`auth.headerPrefixes.${headerName}`,
|
|
665
|
+
"a header name present in auth.secretRefs",
|
|
666
|
+
`"${headerName}" is not a secret-injected header`,
|
|
667
|
+
{
|
|
668
|
+
fix: `Add "${headerName}" to auth.secretRefs (or remove the prefix).`,
|
|
669
|
+
},
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
446
673
|
}
|
|
447
674
|
|
|
448
675
|
const result: AuthConfig = { kind: kindRaw as AuthKind };
|
|
449
676
|
if (headers !== undefined) result.headers = headers;
|
|
677
|
+
if (secretRefs !== undefined) result.secretRefs = secretRefs;
|
|
678
|
+
if (headerPrefixes !== undefined) result.headerPrefixes = headerPrefixes;
|
|
679
|
+
if (secretQueryRefs !== undefined) result.secretQueryRefs = secretQueryRefs;
|
|
680
|
+
if (requires !== undefined) result.requires = requires;
|
|
681
|
+
if (optional !== undefined) result.optional = optional;
|
|
450
682
|
return result;
|
|
451
683
|
}
|
|
452
684
|
|
|
@@ -570,8 +802,7 @@ function validateOperation(
|
|
|
570
802
|
);
|
|
571
803
|
}
|
|
572
804
|
const s = spec as Record<string, unknown>;
|
|
573
|
-
const onlyDescription =
|
|
574
|
-
Object.keys(s).length === 1 && "description" in s;
|
|
805
|
+
const onlyDescription = Object.keys(s).length === 1 && "description" in s;
|
|
575
806
|
if (!onlyDescription) {
|
|
576
807
|
return fail(
|
|
577
808
|
file,
|
|
@@ -723,11 +954,7 @@ function validateOperation(
|
|
|
723
954
|
const opPaginationRaw = o["pagination"];
|
|
724
955
|
let opPagination: PaginationConfig | undefined;
|
|
725
956
|
if (opPaginationRaw !== undefined) {
|
|
726
|
-
const pr = validatePagination(
|
|
727
|
-
opPaginationRaw,
|
|
728
|
-
fieldPath("pagination"),
|
|
729
|
-
file,
|
|
730
|
-
);
|
|
957
|
+
const pr = validatePagination(opPaginationRaw, fieldPath("pagination"), file);
|
|
731
958
|
if (!("style" in pr && typeof pr.style === "string")) {
|
|
732
959
|
return pr as ParseApiGuideResult;
|
|
733
960
|
}
|
|
@@ -775,13 +1002,13 @@ function validateOperation(
|
|
|
775
1002
|
params,
|
|
776
1003
|
pathParams,
|
|
777
1004
|
...(Object.keys(pathParamDocs).length > 0 ? { pathParamDocs } : {}),
|
|
778
|
-
...(helper
|
|
779
|
-
...(transform
|
|
780
|
-
...(passthrough
|
|
781
|
-
...(dateParams
|
|
1005
|
+
...(helper === undefined ? {} : { helper }),
|
|
1006
|
+
...(transform === undefined ? {} : { transform }),
|
|
1007
|
+
...(passthrough === undefined ? {} : { passthrough }),
|
|
1008
|
+
...(dateParams === undefined ? {} : { dateParams }),
|
|
782
1009
|
...(parseOverride ? { parse: parseOverride } : {}),
|
|
783
1010
|
...(opPagination ? { pagination: opPagination } : {}),
|
|
784
|
-
...(opGatherAllMax
|
|
1011
|
+
...(opGatherAllMax === undefined ? {} : { gatherAllMax: opGatherAllMax }),
|
|
785
1012
|
};
|
|
786
1013
|
return operation;
|
|
787
1014
|
}
|
|
@@ -942,6 +1169,24 @@ export function parseApiGuide(
|
|
|
942
1169
|
description = descriptionRaw;
|
|
943
1170
|
}
|
|
944
1171
|
|
|
1172
|
+
// schemaVersion — metadata-only attribution (design: "schemaVersion is
|
|
1173
|
+
// attribution, not enforcement"). Read from frontmatter, surfaced on the
|
|
1174
|
+
// parsed guide when present, left `undefined` when absent. Never gates,
|
|
1175
|
+
// warns, or alters parse. A malformed (non-integer/negative) value is
|
|
1176
|
+
// tolerated and ignored — it must never reject a guide.
|
|
1177
|
+
let schemaVersion: number | undefined;
|
|
1178
|
+
const schemaVersionRaw = m["schemaVersion"];
|
|
1179
|
+
if (schemaVersionRaw !== undefined) {
|
|
1180
|
+
if (
|
|
1181
|
+
typeof schemaVersionRaw === "number" &&
|
|
1182
|
+
Number.isInteger(schemaVersionRaw) &&
|
|
1183
|
+
schemaVersionRaw >= 0
|
|
1184
|
+
) {
|
|
1185
|
+
schemaVersion = schemaVersionRaw;
|
|
1186
|
+
}
|
|
1187
|
+
// else: tolerated + ignored — metadata stays silent on malformed input.
|
|
1188
|
+
}
|
|
1189
|
+
|
|
945
1190
|
let gatherAllMax = GATHER_ALL_MAX_FALLBACK;
|
|
946
1191
|
const gatherAllMaxRaw = m["gatherAllMax"];
|
|
947
1192
|
if (gatherAllMaxRaw !== undefined) {
|
|
@@ -1008,6 +1253,30 @@ export function parseApiGuide(
|
|
|
1008
1253
|
operations.push(opRes);
|
|
1009
1254
|
}
|
|
1010
1255
|
|
|
1256
|
+
// Cross-field: a secretQueryRefs param name must not appear in any
|
|
1257
|
+
// operation's `params` map — the agent must never be able to supply a
|
|
1258
|
+
// secretly-injected param. Checked here because auth is parsed before
|
|
1259
|
+
// operations. `passthrough` ops are NOT rejected (the runtime skips
|
|
1260
|
+
// secretQueryRefs keys in buildQueryParams' passthrough branch); only a
|
|
1261
|
+
// declared-op colliding with the injected param is a parse error.
|
|
1262
|
+
if (auth.kind === "static-key" && auth.secretQueryRefs) {
|
|
1263
|
+
for (const paramName of Object.keys(auth.secretQueryRefs)) {
|
|
1264
|
+
for (const dOp of operations) {
|
|
1265
|
+
if (paramName in dOp.params) {
|
|
1266
|
+
return fail(
|
|
1267
|
+
file,
|
|
1268
|
+
`auth.secretQueryRefs.${paramName}`,
|
|
1269
|
+
"a query param name not declared in any operation's params",
|
|
1270
|
+
`also a param of operation "${dOp.name}"`,
|
|
1271
|
+
{
|
|
1272
|
+
fix: `Remove "${paramName}" from operation "${dOp.name}"'s params map — it is code-injected from the secrets store and the agent must not be able to set it.`,
|
|
1273
|
+
},
|
|
1274
|
+
);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1011
1280
|
const guide: ApiGuide = {
|
|
1012
1281
|
content,
|
|
1013
1282
|
updated,
|
|
@@ -1022,6 +1291,7 @@ export function parseApiGuide(
|
|
|
1022
1291
|
...(docs ? { docs } : {}),
|
|
1023
1292
|
...(organization ? { organization } : {}),
|
|
1024
1293
|
...(description ? { description } : {}),
|
|
1294
|
+
...(schemaVersion === undefined ? {} : { schemaVersion }),
|
|
1025
1295
|
gatherAllMax,
|
|
1026
1296
|
auth,
|
|
1027
1297
|
responseShape,
|
|
@@ -1132,9 +1402,7 @@ function formatOpSummary(ops: { name: string }[]): string {
|
|
|
1132
1402
|
* shortName + op names only (the status-quo shape), so un-backfilled
|
|
1133
1403
|
* guides render consistently with those that have a one-line summary.
|
|
1134
1404
|
*/
|
|
1135
|
-
export function formatGuideListings(
|
|
1136
|
-
entries: { guide: ApiGuide; dirName: string }[],
|
|
1137
|
-
): string {
|
|
1405
|
+
export function formatGuideListings(entries: { guide: ApiGuide }[]): string {
|
|
1138
1406
|
const lines: string[] = [];
|
|
1139
1407
|
for (const { guide } of entries) {
|
|
1140
1408
|
lines.push(
|
|
@@ -1168,11 +1436,11 @@ export function formatApiGuideCatalog(loaded: LoadedApiGuides): string {
|
|
|
1168
1436
|
for (const [name, guide] of Object.entries(loaded.guides)) {
|
|
1169
1437
|
if (guide.organization) {
|
|
1170
1438
|
const idx = orgIndex.get(guide.organization);
|
|
1171
|
-
if (idx
|
|
1172
|
-
orgRows[idx]!.guides.push(guide);
|
|
1173
|
-
} else {
|
|
1439
|
+
if (idx === undefined) {
|
|
1174
1440
|
orgIndex.set(guide.organization, orgRows.length);
|
|
1175
1441
|
orgRows.push({ org: guide.organization, guides: [guide] });
|
|
1442
|
+
} else {
|
|
1443
|
+
orgRows[idx]!.guides.push(guide);
|
|
1176
1444
|
}
|
|
1177
1445
|
} else {
|
|
1178
1446
|
orgless.push({ name, guide });
|
|
@@ -1190,9 +1458,7 @@ export function formatApiGuideCatalog(loaded: LoadedApiGuides): string {
|
|
|
1190
1458
|
}
|
|
1191
1459
|
for (const { name, guide } of orgless) {
|
|
1192
1460
|
const domains =
|
|
1193
|
-
guide.domains && guide.domains.length > 0
|
|
1194
|
-
? guide.domains.join(", ")
|
|
1195
|
-
: name;
|
|
1461
|
+
guide.domains && guide.domains.length > 0 ? guide.domains.join(", ") : name;
|
|
1196
1462
|
lines.push(
|
|
1197
1463
|
` ${guide.icon} ${guide.shortName} — ${domains} (verified ${guide.verified}, ${guide.operations.length} ops)`,
|
|
1198
1464
|
);
|
|
@@ -1202,13 +1468,8 @@ export function formatApiGuideCatalog(loaded: LoadedApiGuides): string {
|
|
|
1202
1468
|
` ⚠ malformed — ${mal.filename}: ${mal.error.field} — expected ${mal.error.expected}; found ${mal.error.found}`,
|
|
1203
1469
|
);
|
|
1204
1470
|
}
|
|
1205
|
-
if (
|
|
1206
|
-
|
|
1207
|
-
loaded.malformed.length === 0
|
|
1208
|
-
) {
|
|
1209
|
-
lines.push(
|
|
1210
|
-
" (no guides — call api-learn({domain, recipe}) to author one)",
|
|
1211
|
-
);
|
|
1471
|
+
if (Object.keys(loaded.guides).length === 0 && loaded.malformed.length === 0) {
|
|
1472
|
+
lines.push(" (no guides — call api-learn({domain, recipe}) to author one)");
|
|
1212
1473
|
}
|
|
1213
1474
|
lines.push("");
|
|
1214
1475
|
lines.push(
|
package/core/path-template.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* - `extractPathTokens` — pull `{token}` names out of a templated path.
|
|
9
9
|
* - `fillPathTemplate` — substitute `{token}` placeholders from params.
|
|
10
10
|
* - `joinUrl` — join a base host + path + query string into a full URL.
|
|
11
|
+
* - `assertSafeDomain` — reject a domain that could escape the guides dir.
|
|
11
12
|
*/
|
|
12
13
|
|
|
13
14
|
/** Extract `{token}` names from a templated path, in order, deduplicated. */
|
|
@@ -46,5 +47,33 @@ export function joinUrl(baseHost: string, path: string, query: string): string {
|
|
|
46
47
|
const base = baseHost.endsWith("/") ? baseHost : `${baseHost}/`;
|
|
47
48
|
const rel = path.startsWith("/") ? path.slice(1) : path;
|
|
48
49
|
const url = new URL(rel, base).toString();
|
|
49
|
-
|
|
50
|
+
if (!query) return url;
|
|
51
|
+
const sep = url.includes("?") ? "&" : "?";
|
|
52
|
+
return `${url}${sep}${query}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Reject a `domain` that could escape the guides dir via path traversal.
|
|
57
|
+
*
|
|
58
|
+
* Domains are used in `join(guidesDir, domain, ...)` for helper lookups
|
|
59
|
+
* and guide writes. A user-typed `/api helpers ../../foo` or an agent
|
|
60
|
+
* `api-learn({domain: "../x"})` must not read or write outside the
|
|
61
|
+
* guides dir. Returns the domain if safe, throws otherwise.
|
|
62
|
+
*/
|
|
63
|
+
export function assertSafeDomain(domain: string): string {
|
|
64
|
+
// A safe domain is a single path segment: no separators, no NUL,
|
|
65
|
+
// and not "."/".." (self/parent). Anything else is a literal dir name.
|
|
66
|
+
if (
|
|
67
|
+
domain.length === 0 ||
|
|
68
|
+
domain.includes("/") ||
|
|
69
|
+
domain.includes("\\") ||
|
|
70
|
+
domain.includes("\0") ||
|
|
71
|
+
domain === "." ||
|
|
72
|
+
domain === ".."
|
|
73
|
+
) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Invalid domain '${domain}': must be a single path segment with no '/', '\\', or '..'.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return domain;
|
|
50
79
|
}
|