feat(upload): define mini-app manifest schema and validator
This commit is contained in:
1 parent
0d8e390c00
commit
22ca9fd656
4 files changed
+401
No files matched your search
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"task": "task-53-the-commons-hollow-unknown-perm",
|
||||
"scenario": "failure",
|
||||
"description": "Submit a manifest declaring an unknown permission string and assert it is rejected rather than silently ignored",
|
||||
"module": "src/lib/miniappManifest.ts",
|
||||
"function": "validateManifest",
|
||||
"ranAt": "2026-11-18T02:15:48.686Z",
|
||||
"input": {
|
||||
"name": "rogue mini-app",
|
||||
"icon": "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
"entryPoint": "index.html",
|
||||
"permissions": [
|
||||
"ledger.delete-everything"
|
||||
]
|
||||
},
|
||||
"result": {
|
||||
"valid": false,
|
||||
"errors": [
|
||||
"manifest declares unknown permission \"ledger.delete-everything\"; must be one of: ledger.read, ledger.write, eventlog.read, eventlog.write, identity.read"
|
||||
]
|
||||
},
|
||||
"assertion": "The unknown permission string \"ledger.delete-everything\" is NOT silently dropped/ignored - the whole manifest is rejected (valid:false) with an error naming the offending permission and listing the full allowed set.",
|
||||
"verifiedBy": "npx vitest run src/lib/miniappManifest.test.ts (\"rejects a manifest declaring an unknown permission string instead of silently ignoring it\" + assertValidManifest throwing variant, both passed) + direct script execution captured above"
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
{
|
||||
"task": "task-53-the-commons-hollow",
|
||||
"scenario": "happy",
|
||||
"description": "Validate 5 well-formed mini-app manifests, one per declared-permission combination (MINIAPP_PERMISSIONS)",
|
||||
"module": "src/lib/miniappManifest.ts",
|
||||
"function": "validateManifest",
|
||||
"ranAt": "2026-11-18T02:15:48.686Z",
|
||||
"variants": [
|
||||
{
|
||||
"permission": "ledger.read",
|
||||
"input": {
|
||||
"name": "ledger.read mini-app",
|
||||
"icon": "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
"entryPoint": "index.html",
|
||||
"permissions": [
|
||||
"ledger.read"
|
||||
]
|
||||
},
|
||||
"result": {
|
||||
"valid": true,
|
||||
"errors": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"permission": "ledger.write",
|
||||
"input": {
|
||||
"name": "ledger.write mini-app",
|
||||
"icon": "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
"entryPoint": "index.html",
|
||||
"permissions": [
|
||||
"ledger.write"
|
||||
]
|
||||
},
|
||||
"result": {
|
||||
"valid": true,
|
||||
"errors": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"permission": "eventlog.read",
|
||||
"input": {
|
||||
"name": "eventlog.read mini-app",
|
||||
"icon": "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
"entryPoint": "index.html",
|
||||
"permissions": [
|
||||
"eventlog.read"
|
||||
]
|
||||
},
|
||||
"result": {
|
||||
"valid": true,
|
||||
"errors": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"permission": "eventlog.write",
|
||||
"input": {
|
||||
"name": "eventlog.write mini-app",
|
||||
"icon": "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
"entryPoint": "index.html",
|
||||
"permissions": [
|
||||
"eventlog.write"
|
||||
]
|
||||
},
|
||||
"result": {
|
||||
"valid": true,
|
||||
"errors": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"permission": "identity.read",
|
||||
"input": {
|
||||
"name": "identity.read mini-app",
|
||||
"icon": "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
"entryPoint": "index.html",
|
||||
"permissions": [
|
||||
"identity.read"
|
||||
]
|
||||
},
|
||||
"result": {
|
||||
"valid": true,
|
||||
"errors": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"assertion": "Every one of the 5 manifests (one per permission in MINIAPP_PERMISSIONS: ledger.read, ledger.write, eventlog.read, eventlog.write, identity.read) returns {valid: true, errors: []}",
|
||||
"verifiedBy": "npx vitest run src/lib/miniappManifest.test.ts (it.each(MINIAPP_PERMISSIONS) suite, 5/5 passed) + direct script execution captured above"
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
assertValidManifest,
|
||||
MINIAPP_PERMISSIONS,
|
||||
ManifestValidationError,
|
||||
validateManifest,
|
||||
type MiniappManifest,
|
||||
type MiniappPermission,
|
||||
} from "./miniappManifest";
|
||||
|
||||
function manifest(permissions: MiniappPermission[]): MiniappManifest {
|
||||
return {
|
||||
name: "Mentor Schedule",
|
||||
icon: "data:image/svg+xml;base64,PHN2Zy8+",
|
||||
entryPoint: "index.html",
|
||||
permissions,
|
||||
};
|
||||
}
|
||||
|
||||
describe("miniappManifest.validateManifest", () => {
|
||||
it("accepts a well-formed manifest", () => {
|
||||
const result = validateManifest(manifest(["ledger.read"]));
|
||||
expect(result).toEqual({ valid: true, errors: [] });
|
||||
});
|
||||
|
||||
it.each(MINIAPP_PERMISSIONS)(
|
||||
"accepts a well-formed manifest declaring only %s",
|
||||
(permission) => {
|
||||
const result = validateManifest(manifest([permission]));
|
||||
expect(result.valid).toBe(true);
|
||||
expect(result.errors).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it("accepts a well-formed manifest declaring no permissions at all", () => {
|
||||
const result = validateManifest(manifest([]));
|
||||
expect(result.valid).toBe(true);
|
||||
});
|
||||
|
||||
it.each(["name", "icon", "entryPoint"] as const)(
|
||||
"rejects a manifest missing required field %s, naming it in the error",
|
||||
(field) => {
|
||||
const input: Record<string, unknown> = { ...manifest(["ledger.read"]) };
|
||||
delete input[field];
|
||||
|
||||
const result = validateManifest(input);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors).toHaveLength(1);
|
||||
expect(result.errors[0]).toContain(field);
|
||||
expect(result.errors[0]).toContain("missing required field");
|
||||
},
|
||||
);
|
||||
|
||||
it('rejects a manifest missing the required "permissions" field', () => {
|
||||
const input: Record<string, unknown> = { ...manifest(["ledger.read"]) };
|
||||
delete input.permissions;
|
||||
|
||||
const result = validateManifest(input);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors[0]).toContain("permissions");
|
||||
expect(result.errors[0]).toContain("missing required field");
|
||||
});
|
||||
|
||||
it("rejects a manifest whose permissions field is not an array", () => {
|
||||
const input = { ...manifest([]), permissions: "ledger.read" };
|
||||
|
||||
const result = validateManifest(input);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors[0]).toContain("permissions");
|
||||
expect(result.errors[0]).toContain("must be an array");
|
||||
});
|
||||
|
||||
it("rejects a manifest declaring an unknown permission string instead of silently ignoring it", () => {
|
||||
const input = manifest(["ledger.read", "ledger.delete-everything"] as MiniappPermission[]);
|
||||
|
||||
const result = validateManifest(input);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors.some((e) => e.includes("ledger.delete-everything"))).toBe(true);
|
||||
expect(result.errors.some((e) => e.includes("unknown permission"))).toBe(true);
|
||||
});
|
||||
|
||||
it("reports every missing field at once for a manifest missing several", () => {
|
||||
const result = validateManifest({ permissions: ["identity.read"] });
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors).toHaveLength(3);
|
||||
expect(result.errors.some((e) => e.includes("name"))).toBe(true);
|
||||
expect(result.errors.some((e) => e.includes("icon"))).toBe(true);
|
||||
expect(result.errors.some((e) => e.includes("entryPoint"))).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects a non-object manifest", () => {
|
||||
const result = validateManifest("not a manifest");
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors[0]).toContain("must be a JSON object");
|
||||
});
|
||||
});
|
||||
|
||||
describe("miniappManifest.assertValidManifest", () => {
|
||||
it("returns the manifest unchanged for a well-formed input", () => {
|
||||
const input = manifest(["eventlog.write"]);
|
||||
expect(assertValidManifest(input)).toEqual(input);
|
||||
});
|
||||
|
||||
it("throws ManifestValidationError naming the missing field", () => {
|
||||
const input: Record<string, unknown> = { ...manifest(["ledger.read"]) };
|
||||
delete input.name;
|
||||
|
||||
expect(() => assertValidManifest(input)).toThrow(ManifestValidationError);
|
||||
try {
|
||||
assertValidManifest(input);
|
||||
throw new Error("expected assertValidManifest to throw");
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(ManifestValidationError);
|
||||
expect((err as ManifestValidationError).field).toBe("name");
|
||||
expect((err as ManifestValidationError).message).toContain("name");
|
||||
}
|
||||
});
|
||||
|
||||
it("throws for an unknown permission string rather than silently ignoring it", () => {
|
||||
const input = manifest(["not-a-real-permission" as MiniappPermission]);
|
||||
|
||||
expect(() => assertValidManifest(input)).toThrow(ManifestValidationError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* Mini-app manifest schema + validator (plan todo 53, Wave 6 upload).
|
||||
*
|
||||
* A user-uploaded mini-app package (draft D10: HTML + manifest, no
|
||||
* sandboxing/postMessage isolation in the MVP) ships a manifest JSON file
|
||||
* declaring its display name, icon, entry-point marker (the file inside the
|
||||
* package to load), and the shared-SDK permissions it wants against the
|
||||
* ledger, event log, and identity modules. This module defines that shape
|
||||
* and validates an uploaded manifest against it before the package is ever
|
||||
* stored.
|
||||
*
|
||||
* Scope limit (draft D10, explicit): validation here checks shape and
|
||||
* completeness ONLY. It is not, and must never become, a
|
||||
* permission-enforcement layer - D10 explicitly cuts runtime enforcement
|
||||
* for the MVP (mini-apps get direct shared-SDK access once installed). A
|
||||
* manifest that passes `validateManifest` has merely been checked for a
|
||||
* well-formed declaration; this module never blocks a declared permission
|
||||
* from being honored at runtime.
|
||||
*/
|
||||
|
||||
/** The only permission strings a mini-app manifest may declare. */
|
||||
export const MINIAPP_PERMISSIONS = [
|
||||
"ledger.read",
|
||||
"ledger.write",
|
||||
"eventlog.read",
|
||||
"eventlog.write",
|
||||
"identity.read",
|
||||
] as const;
|
||||
|
||||
export type MiniappPermission = (typeof MINIAPP_PERMISSIONS)[number];
|
||||
|
||||
const PERMISSION_SET: ReadonlySet<string> = new Set(MINIAPP_PERMISSIONS);
|
||||
|
||||
/** The validated shape of an uploaded mini-app manifest. */
|
||||
export interface MiniappManifest {
|
||||
/** Display name shown in the launcher home grid. */
|
||||
name: string;
|
||||
/** Icon reference (emoji, data URI, or relative path within the package). */
|
||||
icon: string;
|
||||
/** Entry-point marker: the file inside the package to load on launch. */
|
||||
entryPoint: string;
|
||||
/** Declared shared-SDK permissions, each one of `MINIAPP_PERMISSIONS`. */
|
||||
permissions: MiniappPermission[];
|
||||
}
|
||||
|
||||
const REQUIRED_STRING_FIELDS = ["name", "icon", "entryPoint"] as const;
|
||||
|
||||
/** One validation failure: which field, and an actionable message. */
|
||||
export interface ManifestFieldError {
|
||||
field: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface ManifestValidationResult {
|
||||
valid: boolean;
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown by `assertValidManifest` for a manifest that fails validation.
|
||||
* Names the first offending field so the caller can report precisely what
|
||||
* is wrong, rather than a generic "invalid manifest".
|
||||
*/
|
||||
export class ManifestValidationError extends Error {
|
||||
readonly field: string;
|
||||
|
||||
constructor(fieldError: ManifestFieldError) {
|
||||
super(fieldError.message);
|
||||
this.name = "ManifestValidationError";
|
||||
this.field = fieldError.field;
|
||||
}
|
||||
}
|
||||
|
||||
function describeType(value: unknown): string {
|
||||
if (value === null) return "null";
|
||||
if (Array.isArray(value)) return "an array";
|
||||
return typeof value;
|
||||
}
|
||||
|
||||
function collectFieldErrors(input: unknown): ManifestFieldError[] {
|
||||
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
||||
return [
|
||||
{
|
||||
field: "(root)",
|
||||
message: `manifest must be a JSON object, got ${describeType(input)}`,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
const errors: ManifestFieldError[] = [];
|
||||
const m = input as Record<string, unknown>;
|
||||
|
||||
for (const field of REQUIRED_STRING_FIELDS) {
|
||||
const value = m[field];
|
||||
if (typeof value !== "string" || value.trim().length === 0) {
|
||||
errors.push({
|
||||
field,
|
||||
message: `manifest is missing required field "${field}" (expected a non-empty string)`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!("permissions" in m)) {
|
||||
errors.push({
|
||||
field: "permissions",
|
||||
message:
|
||||
'manifest is missing required field "permissions" (expected an array of declared permissions)',
|
||||
});
|
||||
} else if (!Array.isArray(m.permissions)) {
|
||||
errors.push({
|
||||
field: "permissions",
|
||||
message: `manifest field "permissions" must be an array, got ${describeType(m.permissions)}`,
|
||||
});
|
||||
} else {
|
||||
for (const perm of m.permissions) {
|
||||
if (typeof perm !== "string" || !PERMISSION_SET.has(perm)) {
|
||||
errors.push({
|
||||
field: "permissions",
|
||||
message: `manifest declares unknown permission ${JSON.stringify(perm)}; must be one of: ${MINIAPP_PERMISSIONS.join(", ")}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an uploaded manifest's shape and completeness against the
|
||||
* mini-app manifest schema. Returns `{valid: true, errors: []}` for a
|
||||
* well-formed manifest, or `{valid: false, errors}` with one specific,
|
||||
* actionable message per missing required field or unknown permission
|
||||
* string found. Callers (the upload storage path) must refuse to store a
|
||||
* manifest this rejects.
|
||||
*
|
||||
* This function checks shape/completeness only - it performs no
|
||||
* permission-enforcement of any kind (see module doc / draft D10).
|
||||
*/
|
||||
export function validateManifest(input: unknown): ManifestValidationResult {
|
||||
const fieldErrors = collectFieldErrors(input);
|
||||
return {
|
||||
valid: fieldErrors.length === 0,
|
||||
errors: fieldErrors.map((e) => e.message),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Same check as `validateManifest`, but throws `ManifestValidationError`
|
||||
* naming the first offending field instead of returning a result, and
|
||||
* narrows the return type to `MiniappManifest` on success - for call sites
|
||||
* (e.g. the upload storage path) that want to fail fast rather than branch
|
||||
* on a result object.
|
||||
*/
|
||||
export function assertValidManifest(input: unknown): MiniappManifest {
|
||||
const fieldErrors = collectFieldErrors(input);
|
||||
if (fieldErrors.length > 0) {
|
||||
throw new ManifestValidationError(fieldErrors[0]);
|
||||
}
|
||||
return input as MiniappManifest;
|
||||
}
|
||||
Reference in new issue
Block a user