From d9fb610c44ed5a4d587bf3eb1035b427847f7d26 Mon Sep 17 00:00:00 2001 From: Samet Date: Tue, 24 Nov 2026 22:10:09 +0300 Subject: [PATCH] fix(appraisal): reject unknown request fields --- services/appraisal-api/README.md | 9 ++++ .../src/appraisal-fixtures.test.ts | 26 ++++++++++++ services/appraisal-api/src/appraisal.test.ts | 28 +++++++++++++ services/appraisal-api/src/appraisal.ts | 42 ++++++++++++++++++- services/appraisal-api/src/fixtures/index.ts | 2 + .../src/fixtures/unknown-fields.json | 12 ++++++ services/appraisal-api/src/index.ts | 2 + 7 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 services/appraisal-api/src/fixtures/unknown-fields.json diff --git a/services/appraisal-api/README.md b/services/appraisal-api/README.md index 105d7910..f5338b8e 100644 --- a/services/appraisal-api/README.md +++ b/services/appraisal-api/README.md @@ -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. 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 @@ -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. 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. diff --git a/services/appraisal-api/src/appraisal-fixtures.test.ts b/services/appraisal-api/src/appraisal-fixtures.test.ts index 0318bd71..e38c1968 100644 --- a/services/appraisal-api/src/appraisal-fixtures.test.ts +++ b/services/appraisal-api/src/appraisal-fixtures.test.ts @@ -22,6 +22,7 @@ import { invalidScoreValues, missingFields, oversizedText, + unknownFields, validRequest, validResponse, wrongTypes, @@ -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`); @@ -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); + }); +}); diff --git a/services/appraisal-api/src/appraisal.test.ts b/services/appraisal-api/src/appraisal.test.ts index 55af7d34..af7a15b4 100644 --- a/services/appraisal-api/src/appraisal.test.ts +++ b/services/appraisal-api/src/appraisal.test.ts @@ -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), + ); +}); diff --git a/services/appraisal-api/src/appraisal.ts b/services/appraisal-api/src/appraisal.ts index 021300f7..85c50380 100644 --- a/services/appraisal-api/src/appraisal.ts +++ b/services/appraisal-api/src/appraisal.ts @@ -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 = new Set(APPRAISAL_REQUEST_KEYS); +const ALLOWED_ATTRIBUTE_KEYS: ReadonlySet = 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, + allowed: ReadonlySet, +): 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; @@ -200,6 +235,8 @@ export function parseAppraisalRequest(raw: unknown): AppraisalRequest { // before any payment is offered or settled. assertNoCredentialFieldsDeep(raw); const o = raw as Record; + // 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"); } @@ -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; - 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)) { diff --git a/services/appraisal-api/src/fixtures/index.ts b/services/appraisal-api/src/fixtures/index.ts index 2ec36b72..9471d3a7 100644 --- a/services/appraisal-api/src/fixtures/index.ts +++ b/services/appraisal-api/src/fixtures/index.ts @@ -36,6 +36,7 @@ export const oversizedText = loadFixture>("oversized-tex export const invalidScoreValues = loadFixture>( "invalid-score-values.json", ); +export const unknownFields = loadFixture>("unknown-fields.json"); export const allFixtures = [ validRequest, @@ -44,4 +45,5 @@ export const allFixtures = [ wrongTypes, oversizedText, invalidScoreValues, + unknownFields, ] as const; diff --git a/services/appraisal-api/src/fixtures/unknown-fields.json b/services/appraisal-api/src/fixtures/unknown-fields.json new file mode 100644 index 00000000..79c24a6f --- /dev/null +++ b/services/appraisal-api/src/fixtures/unknown-fields.json @@ -0,0 +1,12 @@ +{ + "itemRef": "sub-rosa://grant/42", + "basePrice": 100, + "category": "grant", + "amount": "1000000", + "attributes": { + "quality": 80, + "demand": 60, + "scarcity": 40, + "risk": 20 + } +} diff --git a/services/appraisal-api/src/index.ts b/services/appraisal-api/src/index.ts index eeebc330..83e31f0d 100644 --- a/services/appraisal-api/src/index.ts +++ b/services/appraisal-api/src/index.ts @@ -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,