mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-10-02 12:54:03 +00:00
* Add the LogicSRC OpenCreds specification Leaving a password manager means writing every secret you own to disk in the clear, and losing whatever the spreadsheet had no column for. A CSV is plaintext by construction, lossy by omission, and carries no integrity: nothing in it says which rows were meant to be there, so a truncated import looks exactly like a complete one. The same gap showed up inside LogicSRC. `logicsrc credentials` moves .env secrets and SSH keys through end-to-end-encrypted team vaults, but it can only model a key/value pair. A card, a passport, a login with a TOTP seed, or an OAuth account with a refresh token are all things people already keep in a vault, and none of them are a key/value pair. OpenCreds defines three things: the item, the vault, and the database. - Six item types (login, card, identity, note, key, account) as one record with a type and a named field group, so everything the user typed lives in a single encrypted blob. Codes 1-4 match MarkSyncr's deployed vault and are not renumbered; compatibility is cheaper than elegance. - AES-256-GCM over that record with the item id bound as AAD. Without it, anyone with storage write access could move a low-value login's ciphertext into a high-value row and watch what the user does next. - A key hierarchy where the user key is random, not derived, so a password change re-wraps 32 bytes rather than re-encrypting a vault. The auth hash comes out of a different HKDF label than the wrap key, which is what lets it reach a server at all. - A portable .opencreds file, encrypted by default, whose header is the AAD over the payload -- so the manifest is authenticated by the same tag as the data and a truncated import fails rather than reporting success. The plaintext form exists because people move to products that read nothing else; it is opt-in, confirmed, 0600, and labelled "protected": false in its own header. Namespaces are carried as data, not fixed by the spec: labels are compiled into every ciphertext a vault has written, so editing one does not migrate a vault, it makes it undecryptable. MarkSyncr's deployed vault is conformant by declaring `marksyncr`. Ships: prd/0004, nine spec pages under docs/opencreds/, six JSON Schemas, the @logicsrc/opencreds reference implementation with CSV importers for five products, `logicsrc vault` and the standalone `opencreds` binary, and the spec page at logicsrc.com/opencreds. `vault` rather than `creds` because `creds` is already an alias of `logicsrc credentials`, and the two are different: one moves a pair between providers, the other stores a record. @logicsrc/validators now registers every schema by $id before compiling, so the database schema can $ref the item and manifest schemas rather than restating them. 120 tests, including CLI end-to-end coverage of the masking rules, exit codes, and the manifest-mismatch path. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRQrfuwuYKKV5UB9kLHuX5 * Make the OpenCreds conformance claim executable The conformance page described a fixture suite and an `opencreds conformance` command that did not exist. A specification that documents a conformance surface it cannot run is a specification nobody can hold to, including us. `opencreds conformance` now runs the requirement list as code -- one check per C-number, carrying its own id and level -- and emits the report shape the spec publishes. It exits 2 when a MUST does not pass, so it can gate CI directly. The reference implementation reports 29 passed, 0 failed, 1 skipped; the skip is C19, because key management for the team profile lives in @logicsrc/plugin-credential-sharing rather than in this package, and a skipped MAY does not affect conformance. Fixtures are generated (`--emit-fixtures <dir>`) rather than hand-written. A vector produced by an implementation and then verified by it is worth more than a JSON file someone typed: the typed file drifts silently when the format moves, and the generated one cannot. Fourteen files, including an invalid/ set every conforming reader must reject -- a wrong field group, a weak KDF, an unregistered namespace, a short payload and a tampered manifest. The CLI requirements stay with the end-to-end tests that drive the real binary through a child process; a command cannot meaningfully check its own exit codes, and a masked value that is only masked in the library is not masked. conformance.md and cli.md now describe what ships. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRQrfuwuYKKV5UB9kLHuX5 * Add @logicsrc/opencreds to the lockfile `npm ci` refuses a lockfile that does not match package.json, and the new workspace package plus the CLI's dependency on it were never recorded: the worktree was bootstrapped by hardlinking node_modules rather than installing, so npm was never asked to update the lock. Adds the workspace link and the package entry. No dependency versions move. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRQrfuwuYKKV5UB9kLHuX5 * Register PRD 0004, and stop the fixtures looking like real secrets Two CI failures, both mine. `prd/README.md` is generated by `logicsrc prd index --write` and the scaffold test asserts it is current, so adding a PRD without regenerating it leaves the repo's own conformance check failing. Regenerated. The MCP test asserts the next free PRD id against the live prd/ directory — its comment says it advances with every PRD added — so it moves to 0005. ThreatCrush flagged three of the example strings: a PEM header in the item-model docs and in the conformance fixture, and an `sk_live_` prefixed token. All placeholders, none real, but the finding is the scanner working. A fixture only has to exercise the field, and a real-looking private key header or live-key prefix sitting in the tree trains both the scanner and the people reading its output to shrug at exactly the shape that matters. Replaced with obvious placeholders rather than suppressing the rule. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRQrfuwuYKKV5UB9kLHuX5 --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
286 lines
12 KiB
TypeScript
286 lines
12 KiB
TypeScript
/**
|
|
* Structural validation with JSON pointers.
|
|
*
|
|
* Deliberately not Ajv. This package runs in a browser extension's service
|
|
* worker, where a schema compiler is both weight and a CSP problem, and the CLI
|
|
* contract asks for one diagnostic per failure pointing at the exact location —
|
|
* which is easier to produce well by hand than to extract from a validator's
|
|
* error objects. `@logicsrc/validators` holds the published JSON Schemas for
|
|
* anyone who wants schema-based validation instead.
|
|
*/
|
|
|
|
import { ITEM_TYPE, ITEM_TYPE_NAMES, MAX_HISTORY_ENTRIES, OPENCREDS_VERSION } from "./types.js";
|
|
import { NAMESPACE_PATTERN, REGISTERED_NAMESPACES } from "./types.js";
|
|
import type { Database, Item } from "./types.js";
|
|
|
|
export interface Diagnostic {
|
|
/** JSON pointer into the document. */
|
|
pointer: string;
|
|
message: string;
|
|
severity: "error" | "warning";
|
|
}
|
|
|
|
const UUID = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
const TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
|
|
const URI_MATCHES = ["domain", "host", "startsWith", "exact", "regex", "never"];
|
|
const FIELD_KINDS = ["text", "hidden", "boolean", "linked"];
|
|
const KEY_KINDS = ["ssh", "pgp", "api", "symmetric", "certificate", "env"];
|
|
const GROUPS = ["login", "card", "identity", "key", "account"];
|
|
|
|
function isObject(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
/** Validate one item. `at` is the pointer prefix, e.g. "/items/17". */
|
|
export function validateItem(value: unknown, at = ""): Diagnostic[] {
|
|
const out: Diagnostic[] = [];
|
|
const err = (pointer: string, message: string): void => {
|
|
out.push({ pointer: `${at}${pointer}`, message, severity: "error" });
|
|
};
|
|
|
|
if (!isObject(value)) {
|
|
return [{ pointer: at || "/", message: "an item must be an object", severity: "error" }];
|
|
}
|
|
|
|
if (!Number.isInteger(value.v) || (value.v as number) < 1) {
|
|
err("/v", "missing or invalid item schema version");
|
|
}
|
|
if (typeof value.id !== "string" || !UUID.test(value.id)) {
|
|
err("/id", "id must be a UUID; it is bound into the ciphertext and cannot be reassigned");
|
|
}
|
|
if (typeof value.type !== "string" || !(value.type in ITEM_TYPE)) {
|
|
err("/type", `${JSON.stringify(value.type)} is not one of ${ITEM_TYPE_NAMES.join(", ")}`);
|
|
}
|
|
if (typeof value.name !== "string") err("/name", "name must be a string (it may be empty)");
|
|
for (const field of ["createdAt", "updatedAt"] as const) {
|
|
if (typeof value[field] !== "string" || !TIMESTAMP.test(value[field] as string)) {
|
|
err(`/${field}`, "must be an RFC 3339 timestamp");
|
|
}
|
|
}
|
|
|
|
const type = value.type as string;
|
|
|
|
// A group belonging to another type is either a broken importer or an attempt
|
|
// to smuggle a field past a type-based permission check.
|
|
for (const group of GROUPS) {
|
|
if (group !== type && value[group] !== undefined) {
|
|
err(`/${group}`, `a ${type} item must not carry a ${group} field group`);
|
|
}
|
|
}
|
|
|
|
if (type === "login" && value.login !== undefined) {
|
|
const login = value.login;
|
|
if (!isObject(login)) {
|
|
err("/login", "must be an object");
|
|
} else if (login.uris !== undefined) {
|
|
if (!Array.isArray(login.uris)) {
|
|
err("/login/uris", "must be an array");
|
|
} else {
|
|
login.uris.forEach((uri, i) => {
|
|
if (!isObject(uri)) {
|
|
err(`/login/uris/${i}`, "must be an object");
|
|
return;
|
|
}
|
|
if (typeof uri.uri !== "string") err(`/login/uris/${i}/uri`, "must be a string");
|
|
if (uri.match !== undefined && !URI_MATCHES.includes(uri.match as string)) {
|
|
err(`/login/uris/${i}/match`, `${JSON.stringify(uri.match)} is not a valid match rule`);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
if (type === "key" && isObject(value.key)) {
|
|
const key = value.key;
|
|
if (key.keyType !== undefined && key.keyType !== "" && !KEY_KINDS.includes(key.keyType as string)) {
|
|
err("/key/keyType", `${JSON.stringify(key.keyType)} is not one of ${KEY_KINDS.join(", ")}`);
|
|
}
|
|
if (typeof key.mode === "string" && key.mode !== "" && !/^0?[0-7]{3}$/.test(key.mode)) {
|
|
err("/key/mode", "must be an octal POSIX mode such as \"0600\"");
|
|
}
|
|
}
|
|
|
|
if (type === "account" && isObject(value.account) && value.account.scopes !== undefined) {
|
|
if (!Array.isArray(value.account.scopes)) err("/account/scopes", "must be an array of strings");
|
|
}
|
|
|
|
if (value.history !== undefined) {
|
|
if (!Array.isArray(value.history)) {
|
|
err("/history", "must be an array");
|
|
} else {
|
|
if (type !== "login" && value.history.length > 0) {
|
|
err("/history", "password history is defined only for login items");
|
|
}
|
|
if (value.history.length > MAX_HISTORY_ENTRIES) {
|
|
err("/history", `history is capped at ${MAX_HISTORY_ENTRIES} entries, found ${value.history.length}`);
|
|
}
|
|
value.history.forEach((entry, i) => {
|
|
if (!isObject(entry) || typeof entry.password !== "string") {
|
|
err(`/history/${i}/password`, "must be a string");
|
|
}
|
|
if (!isObject(entry) || typeof entry.changedAt !== "string" || !TIMESTAMP.test(entry.changedAt)) {
|
|
err(`/history/${i}/changedAt`, "must be an RFC 3339 timestamp");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
if (value.fields !== undefined) {
|
|
if (!Array.isArray(value.fields)) {
|
|
err("/fields", "must be an array");
|
|
} else {
|
|
value.fields.forEach((field, i) => {
|
|
if (!isObject(field)) {
|
|
err(`/fields/${i}`, "must be an object");
|
|
return;
|
|
}
|
|
if (typeof field.name !== "string") err(`/fields/${i}/name`, "must be a string");
|
|
if (typeof field.value !== "string") err(`/fields/${i}/value`, "must be a string");
|
|
if (!FIELD_KINDS.includes(field.type as string)) {
|
|
err(`/fields/${i}/type`, `${JSON.stringify(field.type)} is not one of ${FIELD_KINDS.join(", ")}`);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
if (value.folderId !== undefined && value.folderId !== null && typeof value.folderId !== "string") {
|
|
err("/folderId", "must be a folder id or null");
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/** Validate a database document. Does not decrypt; see openDatabase for that. */
|
|
export function validateDatabase(value: unknown): Diagnostic[] {
|
|
const out: Diagnostic[] = [];
|
|
const err = (pointer: string, message: string): void => {
|
|
out.push({ pointer, message, severity: "error" });
|
|
};
|
|
const warn = (pointer: string, message: string): void => {
|
|
out.push({ pointer, message, severity: "warning" });
|
|
};
|
|
|
|
if (!isObject(value)) {
|
|
return [{ pointer: "/", message: "a database must be a JSON object", severity: "error" }];
|
|
}
|
|
if (value.type !== "opencreds.database") {
|
|
err("/type", 'must be "opencreds.database"');
|
|
}
|
|
if (value.opencreds !== OPENCREDS_VERSION) {
|
|
err("/opencreds", `unsupported version ${JSON.stringify(value.opencreds)}; this build reads ${OPENCREDS_VERSION}`);
|
|
}
|
|
if (typeof value.namespace !== "string" || !NAMESPACE_PATTERN.test(value.namespace)) {
|
|
err("/namespace", "must match ^[a-z][a-z0-9-]{1,31}$");
|
|
} else if (!REGISTERED_NAMESPACES.includes(value.namespace)) {
|
|
warn("/namespace", `"${value.namespace}" is not a registered namespace; opening it needs an explicit opt-in`);
|
|
}
|
|
if (typeof value.exportedAt !== "string" || !TIMESTAMP.test(value.exportedAt)) {
|
|
err("/exportedAt", "must be an RFC 3339 timestamp");
|
|
}
|
|
if (typeof value.protected !== "boolean") {
|
|
err("/protected", "must be a boolean");
|
|
}
|
|
|
|
const manifest = value.manifest;
|
|
if (!isObject(manifest)) {
|
|
err("/manifest", "missing; an OpenCreds database states what it contains and that statement is checked");
|
|
} else {
|
|
if (!Number.isInteger(manifest.itemCount)) err("/manifest/itemCount", "must be an integer");
|
|
if (!Number.isInteger(manifest.folderCount)) err("/manifest/folderCount", "must be an integer");
|
|
if (typeof manifest.digest !== "string") err("/manifest/digest", "must be a base64 SHA-256");
|
|
if (manifest.types !== undefined && !isObject(manifest.types)) {
|
|
err("/manifest/types", "must be an object of type name to count");
|
|
}
|
|
}
|
|
|
|
if (value.protected === true) {
|
|
if (typeof value.iv !== "string") err("/iv", "an encrypted database must carry an iv");
|
|
if (typeof value.ciphertext !== "string") err("/ciphertext", "an encrypted database must carry a ciphertext");
|
|
if (value.items !== undefined) err("/items", "an encrypted database must not also state its items in the clear");
|
|
if (value.kdf !== undefined) {
|
|
if (!isObject(value.kdf)) {
|
|
err("/kdf", "must be an object");
|
|
} else {
|
|
if (!Number.isInteger(value.kdf.iterations) || (value.kdf.iterations as number) < 100_000) {
|
|
err("/kdf/iterations", "must be at least 100000");
|
|
}
|
|
if (typeof value.kdf.salt !== "string") err("/kdf/salt", "must be a base64 salt");
|
|
}
|
|
}
|
|
} else if (value.protected === false) {
|
|
if (!Array.isArray(value.items)) {
|
|
err("/items", "a plaintext database must carry its items");
|
|
} else {
|
|
(value.items as unknown[]).forEach((item, i) => {
|
|
out.push(...validateItem(item, `/items/${i}`));
|
|
});
|
|
}
|
|
if (value.iv !== undefined || value.ciphertext !== undefined) {
|
|
err("/ciphertext", "a plaintext database must not carry ciphertext");
|
|
}
|
|
warn("/protected", "this file holds every secret in the vault in the clear");
|
|
}
|
|
|
|
if (value.folders !== undefined) {
|
|
if (!Array.isArray(value.folders)) {
|
|
err("/folders", "must be an array");
|
|
} else {
|
|
(value.folders as unknown[]).forEach((folder, i) => {
|
|
if (!isObject(folder)) {
|
|
err(`/folders/${i}`, "must be an object");
|
|
return;
|
|
}
|
|
if (typeof folder.id !== "string" || !UUID.test(folder.id)) err(`/folders/${i}/id`, "must be a UUID");
|
|
if (typeof folder.name !== "string") err(`/folders/${i}/name`, "must be a string");
|
|
});
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Validate any OpenCreds document, guessing which kind it is.
|
|
*
|
|
* A person running `opencreds validate` on a file has a file, not a schema
|
|
* name; asking them which kind it is would be asking them the question they
|
|
* came here to answer.
|
|
*/
|
|
export function validateDocument(value: unknown): { kind: string; diagnostics: Diagnostic[] } {
|
|
if (isObject(value) && value.type === "opencreds.database") {
|
|
return { kind: "database", diagnostics: validateDatabase(value) };
|
|
}
|
|
if (isObject(value) && Array.isArray(value.items)) {
|
|
const diagnostics: Diagnostic[] = [];
|
|
(value.items as unknown[]).forEach((item, i) => diagnostics.push(...validateItem(item, `/items/${i}`)));
|
|
return { kind: "items", diagnostics };
|
|
}
|
|
if (Array.isArray(value)) {
|
|
const diagnostics: Diagnostic[] = [];
|
|
value.forEach((item, i) => diagnostics.push(...validateItem(item, `/${i}`)));
|
|
return { kind: "items", diagnostics };
|
|
}
|
|
return { kind: "item", diagnostics: validateItem(value) };
|
|
}
|
|
|
|
export function hasErrors(diagnostics: Diagnostic[]): boolean {
|
|
return diagnostics.some((d) => d.severity === "error");
|
|
}
|
|
|
|
/** Render diagnostics the way the CLI contract specifies: pointer, then message. */
|
|
export function formatDiagnostics(diagnostics: Diagnostic[]): string {
|
|
if (diagnostics.length === 0) return "";
|
|
const width = Math.max(...diagnostics.map((d) => d.pointer.length));
|
|
return diagnostics
|
|
.map((d) => `${d.pointer.padEnd(width)} ${d.severity === "warning" ? "warning: " : ""}${d.message}`)
|
|
.join("\n");
|
|
}
|
|
|
|
/** A rough type guard for a parsed database, before the deeper checks run. */
|
|
export function looksLikeDatabase(value: unknown): value is Database {
|
|
return isObject(value) && value.type === "opencreds.database";
|
|
}
|
|
|
|
export function looksLikeItem(value: unknown): value is Item {
|
|
return isObject(value) && typeof value.type === "string" && value.type in ITEM_TYPE;
|
|
}
|