Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions services/appraisal-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ JSON next to the source so integrators can copy-paste them.
| `src/fixtures/wrong-types.json` | Arrays / strings where scalars expected | **400** — `itemRef must be a non-empty string` (first field checked) |
| `src/fixtures/oversized-text.json` | `itemRef` / `category` past the documented length caps | **400** — `itemRef must be at most 256 characters` (and `category` cap of 64) |
| `src/fixtures/invalid-score-values.json` | Attribute scores of wrong JSON type | **400** — `attributes.<field> must be a number` |
| `src/fixtures/unknown-fields.json` | Extra top-level key (e.g. mistyped payment field) | **400** — `request contains unknown fields` |

These are the **exact** payloads exercised by `src/appraisal-fixtures.test.ts`.
The fixture names are exported from `src/fixtures/index.ts` for use in other
Expand All @@ -109,8 +110,16 @@ matching test.
| `category` longer than 64 characters | `category must be at most 64 characters` |
| `attributes` present but not an object | `attributes must be an object` |
| Any score not a finite number (string, boolean, array, object, null, NaN, Infinity) | `attributes.<field> must be a number` |
| Unknown top-level or attribute key | `request contains unknown fields` |
| Body is not a JSON object | `body must be a JSON object` |

The request schema is **closed**: only the documented top-level keys
(`itemRef`, `basePrice`, `category`, `attributes`) and attribute keys
(`quality`, `demand`, `scarcity`, `risk`) are accepted. Unknown keys produce
the stable `request contains unknown fields` message and never echo submitted
values. Over HTTP, `AppraisalInputError` maps to **400**
`{ "error": "Invalid appraisal request" }`.

Numeric scores outside `0..100` are **clamped** rather than rejected: integrators
may submit raw 0–100 survey data without coercing it first. NaN/Infinity *are*
rejected because they would otherwise propagate invalidly into the math.
Expand Down
26 changes: 26 additions & 0 deletions services/appraisal-api/src/appraisal-fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
invalidScoreValues,
missingFields,
oversizedText,
unknownFields,
validRequest,
validResponse,
wrongTypes,
Expand Down Expand Up @@ -51,6 +52,7 @@ describe("appraisal schema fixtures load", () => {
wrongTypes,
oversizedText,
invalidScoreValues,
unknownFields,
]) {
assert.equal(typeof f.value, "object");
assert.ok(f.value !== null, `fixture ${f.description} must be an object`);
Expand Down Expand Up @@ -243,3 +245,27 @@ describe("invalid score values fixture — full cascade", () => {
assert.equal(req.attributes?.demand, 100);
});
});

describe("unknown fields fixture", () => {
test("rejects with a stable message that does not echo submitted values", () => {
const amount = (unknownFields.value as { amount: string }).amount;
const err = captureError(() => parseAppraisalRequest(unknownFields.value));
assert.ok(err instanceof AppraisalInputError);
assert.equal(err.message, "request contains unknown fields");
assert.equal(err.message.includes(amount), false);
});

test("unknown attribute keys use the same stable message", () => {
const leaked = "SHOULD-NOT-APPEAR";
const err = captureError(() =>
parseAppraisalRequest({
itemRef: "ok",
basePrice: 100,
attributes: { quality: 50, payTo: leaked },
}),
);
assert.ok(err instanceof AppraisalInputError);
assert.equal(err.message, "request contains unknown fields");
assert.equal(err.message.includes(leaked), false);
});
});
28 changes: 28 additions & 0 deletions services/appraisal-api/src/appraisal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,31 @@ test("parseAppraisalRequest validates and clamps", () => {
assert.equal(parsed.attributes?.quality, 100);
assert.equal(parsed.attributes?.risk, 0);
});

test("parseAppraisalRequest rejects unknown fields without echoing values", () => {
const leaked = "PAY-SECRET-SHOULD-NOT-ECHO";
assert.throws(
() =>
parseAppraisalRequest({
itemRef: "x",
basePrice: 1,
amount: leaked,
}),
(e: unknown) =>
e instanceof AppraisalInputError &&
e.message === "request contains unknown fields" &&
!e.message.includes(leaked),
);
assert.throws(
() =>
parseAppraisalRequest({
itemRef: "x",
basePrice: 1,
attributes: { quality: 50, rarity: leaked },
}),
(e: unknown) =>
e instanceof AppraisalInputError &&
e.message === "request contains unknown fields" &&
!e.message.includes(leaked),
);
});
42 changes: 40 additions & 2 deletions services/appraisal-api/src/appraisal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,41 @@ export function assertNoCredentialFields(value: unknown): void {
assertNoCredentialFieldsDeep(value);
}

/** Documented top-level request keys — anything else is rejected. */
export const APPRAISAL_REQUEST_KEYS = Object.freeze([
"itemRef",
"basePrice",
"category",
"attributes",
] as const);

/** Documented attribute score keys — anything else is rejected. */
export const APPRAISAL_ATTRIBUTE_KEYS = Object.freeze([
"quality",
"demand",
"scarcity",
"risk",
] as const);

const ALLOWED_REQUEST_KEYS: ReadonlySet<string> = new Set(APPRAISAL_REQUEST_KEYS);
const ALLOWED_ATTRIBUTE_KEYS: ReadonlySet<string> = new Set(APPRAISAL_ATTRIBUTE_KEYS);

/**
* Closed-schema guard: unknown keys fail closed with a stable message that
* never echoes submitted field names or values (callers may have mistyped a
* payment/secret field into the body).
*/
function assertOnlyAllowedKeys(
obj: Record<string, unknown>,
allowed: ReadonlySet<string>,
): void {
for (const key of Object.keys(obj)) {
if (!allowed.has(key)) {
throw new AppraisalInputError("request contains unknown fields");
}
}
}

/** Mint a payment quote for one appraisal call. Pure except for `nowSeconds`. */
export function buildAppraisalQuote(params: {
asset: string;
Expand Down Expand Up @@ -200,6 +235,8 @@ export function parseAppraisalRequest(raw: unknown): AppraisalRequest {
// before any payment is offered or settled.
assertNoCredentialFieldsDeep(raw);
const o = raw as Record<string, unknown>;
// Closed schema: misspelled payment/appraisal fields must not be silently ignored.
assertOnlyAllowedKeys(o, ALLOWED_REQUEST_KEYS);
if (typeof o.itemRef !== "string" || o.itemRef.trim() === "") {
throw new AppraisalInputError("itemRef must be a non-empty string");
}
Expand All @@ -221,11 +258,12 @@ export function parseAppraisalRequest(raw: unknown): AppraisalRequest {
}
const attrs: AppraisalAttributes = {};
if (o.attributes !== undefined) {
if (typeof o.attributes !== "object" || o.attributes === null) {
if (typeof o.attributes !== "object" || o.attributes === null || Array.isArray(o.attributes)) {
throw new AppraisalInputError("attributes must be an object");
}
const a = o.attributes as Record<string, unknown>;
for (const key of ["quality", "demand", "scarcity", "risk"] as const) {
assertOnlyAllowedKeys(a, ALLOWED_ATTRIBUTE_KEYS);
for (const key of APPRAISAL_ATTRIBUTE_KEYS) {
if (a[key] !== undefined) {
const v = a[key];
if (typeof v !== "number" || !Number.isFinite(v)) {
Expand Down
2 changes: 2 additions & 0 deletions services/appraisal-api/src/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const oversizedText = loadFixture<Record<string, unknown>>("oversized-tex
export const invalidScoreValues = loadFixture<Record<string, unknown>>(
"invalid-score-values.json",
);
export const unknownFields = loadFixture<Record<string, unknown>>("unknown-fields.json");

export const allFixtures = [
validRequest,
Expand All @@ -44,4 +45,5 @@ export const allFixtures = [
wrongTypes,
oversizedText,
invalidScoreValues,
unknownFields,
] as const;
12 changes: 12 additions & 0 deletions services/appraisal-api/src/fixtures/unknown-fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"itemRef": "sub-rosa://grant/42",
"basePrice": 100,
"category": "grant",
"amount": "1000000",
"attributes": {
"quality": 80,
"demand": 60,
"scarcity": 40,
"risk": 20
}
}
2 changes: 2 additions & 0 deletions services/appraisal-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2026 Sub Rosa contributors
export {
APPRAISAL_ATTRIBUTE_KEYS,
APPRAISAL_MODEL,
APPRAISAL_QUOTE_TTL_SECONDS,
APPRAISAL_REQUEST_KEYS,
MAX_APPRAISAL_BODY_BYTES,
MAX_CATEGORY_LENGTH,
MAX_ITEMREF_LENGTH,
Expand Down
Loading