Skip to content
Merged
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
5 changes: 3 additions & 2 deletions apps/pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.1",
"private": true,
"type": "module",
"description": "LogicSRC credentials — Express app on Postgres (@profullstack/libsql-pg): auth + end-to-end-encrypted team credential sharing.",
"description": "LogicSRC credentials \u2014 Express app on Postgres (@profullstack/libsql-pg): auth + end-to-end-encrypted team credential sharing.",
"scripts": {
"start": "bun src/server.mjs",
"dev": "bun --watch src/server.mjs",
Expand All @@ -13,10 +13,11 @@
"dependencies": {
"@logicsrc/cli": "workspace:*",
"@profullstack/coinpay-onboard": "0.3.1",
"@profullstack/key-rotation": "0.1.0",
"@profullstack/footer": "latest",
"@profullstack/key-rotation": "0.1.0",
"@profullstack/libsql-pg": "^0.1.4",
"@profullstack/profile": "0.1.8",
"@profullstack/sealed-secrets": "0.1.1",
"@simplewebauthn/browser": "^14.0.0",
"@simplewebauthn/server": "^14.0.3",
"cookie-parser": "^1.4.7",
Expand Down
110 changes: 110 additions & 0 deletions apps/pwa/public/app-grant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// App grant consent (/grant) and the /apps revoke buttons.
//
// The value is decrypted HERE with the member's identity key (same libsodium
// calls as vault.js), sealed with @profullstack/sealed-secrets to the app's
// per-user inbox key, and only the sealed text is posted. The identity key and
// the plaintext never leave this page.
import { createSealer } from "/vendor/sealed-secrets/index.js";

const STORE = "logicsrc.identityKey";
const $ = (s) => document.querySelector(s);

for (const b of document.querySelectorAll("[data-revoke]")) {
b.addEventListener("click", async () => {
if (!confirm("Revoke this key from the app? Rotate it too if it may have been used.")) return;
await fetch(`/api/v1/app-grants/${encodeURIComponent(b.dataset.revoke)}`, { method: "DELETE", credentials: "same-origin" });
location.reload();
});
}

const main = $("#grant");
if (main) {
const status = $("#grant-status");
const say = (m) => (status.textContent = m || "");
const requestId = main.dataset.requestId;
const registered = main.dataset.publicKey;
const recipient = JSON.parse(main.dataset.recipient);
const returnUrl = main.dataset.returnUrl;

const saved = () => {
try {
return sessionStorage.getItem(STORE) || localStorage.getItem(STORE) || "";
} catch {
return "";
}
};
const parseKey = (raw) => {
let t = String(raw || "").trim();
if (t.startsWith("{")) {
const j = JSON.parse(t);
t = (j.keys && j.keys.secretKey) || j.secretKey || "";
}
return t.replace(/^["']|["']$/g, "").replace(/\s+/g, "");
};
const getJSON = async (url) => {
const res = await fetch(url, { credentials: "same-origin", headers: { accept: "application/json" } });
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(body.error || `HTTP ${res.status}`);
return body;
};
const postJSON = async (url, data) => {
const res = await fetch(url, { method: "POST", credentials: "same-origin", headers: { "content-type": "application/json", accept: "application/json" }, body: JSON.stringify(data) });
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(body.error || `HTTP ${res.status}`);
return body;
};

async function openVault(vaultId, secretKeyB64) {
if (!window.sodium) throw new Error("The crypto library did not load. Reload the page.");
const na = await window.sodium.ready.then(() => window.sodium);
const V = na.base64_variants.ORIGINAL;
const sk = na.from_base64(secretKeyB64, V);
const pk = na.crypto_scalarmult_base(sk);
if (na.to_base64(pk, V) !== registered) throw new Error("That key does not match the identity registered for your account.");
const g = await getJSON(`/api/credshare/vaults/${encodeURIComponent(vaultId)}/grant`);
const dek = na.crypto_box_seal_open(na.from_base64(g.wrappedDek, V), pk, sk);
const body = await getJSON(`/api/credshare/vaults/${encodeURIComponent(vaultId)}/secrets`);
const out = {};
for (const s of body.secrets || []) {
out[s.name] = { fingerprint: s.fingerprint, open: () => na.to_string(na.crypto_secretbox_open_easy(na.from_base64(s.ciphertext, V), na.from_base64(s.nonce, V), dek)) };
}
return out;
}

$("#grant-approve").addEventListener("click", async () => {
const btn = $("#grant-approve");
btn.disabled = true;
try {
const vaultId = $("#grant-vault").value;
if (!vaultId) throw new Error("Pick a vault.");
const key = parseKey($("#grant-key").value || saved());
if (!key) throw new Error("Paste your identity key.");
say("Opening the vault in this browser…");
const secrets = await openVault(vaultId, key);
const sealer = createSealer({ app: recipient.app, scope: recipient.scope });
const deliveries = [];
for (const row of document.querySelectorAll("tr[data-key]")) {
const keyName = row.dataset.key;
const source = row.querySelector("input[name=src]").value.trim();
const s = secrets[source];
if (!s) throw new Error(`No secret named ${source} in that vault.`);
// Same shape the app's own secrets page stores: sealed:v2:<inbox entry>, field = the app's key name.
const entry = await sealer.sealToInbox(recipient.inbox_public_key, { [keyName]: s.open() }, recipient.user_id);
deliveries.push({ key_name: keyName, vault_id: vaultId, source_name: source, fingerprint: s.fingerprint, sealed: `sealed:v2:${JSON.stringify(entry)}` });
}
say("Sealed. Sending…");
const r = await postJSON(`/api/v1/app-grants/requests/${encodeURIComponent(requestId)}/approve`, { deliveries });
const failed = r.delivered.filter((d) => !d.delivered);
say(failed.length ? `Granted, but the app did not take ${failed.map((d) => d.key_name).join(", ")} yet (${failed[0].error}). It retries on the next rotation; you can also revoke on /apps.` : "Done. Returning to the app…");
if (!failed.length) setTimeout(() => (location.href = r.return_url || returnUrl), 800);
} catch (e) {
say(e.message);
btn.disabled = false;
}
});

$("#grant-deny").addEventListener("click", async () => {
const r = await postJSON(`/api/v1/app-grants/requests/${encodeURIComponent(requestId)}/deny`, {}).catch(() => ({}));
location.href = r.return_url || returnUrl;
});
}
186 changes: 186 additions & 0 deletions apps/pwa/src/lib/app-grants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// App grants: hand one named secret to an external app, sealed in the person's
// browser to that app's per-user inbox key. See migrations/013_app_grants.sql.
//
// The app proves who it is by serving the grant manifest from its own origin
// over TLS; logicsrc fetches it, never trusts a body the app POSTed.
//
// GET <origin>/.well-known/logicsrc-grant/<nonce>
// { "v": 1, "app_name": "MeshHook",
// "keys": [{ "name": "COINPAY_API_KEY", "description": "..." }],
// "recipient": { "format": "sealed-secrets/inbox-v2", "app": "meshhook",
// "scope": "workflow_secrets", "user_id": "<app user id>",
// "inbox_public_key": "<base64>" },
// "webhook_url": "<same origin, https>", "webhook_secret": "<32+ chars>",
// "return_url": "<same origin>", "expires_at": <ms> }

import { createHash, createHmac, randomBytes } from "node:crypto";
import { lookup } from "node:dns/promises";
import { isIP } from "node:net";

export const MANIFEST_PATH = "/.well-known/logicsrc-grant/";
export const RECIPIENT_FORMAT = "sealed-secrets/inbox-v2";
export const SEALED_PREFIX = "sealed:v2:";
export const MAX_KEYS = 20;
const KEY_NAME = /^[A-Za-z_][A-Za-z0-9_.-]{0,127}$/;

export class GrantError extends Error {
constructor(message, status = 400, code = "bad_request") {
super(message);
this.status = status;
this.code = code;
}
}

/** Only https origins with a real host name (http://localhost only when allowed, for tests and dev). */
export function parseOrigin(raw, { allowLocal = false } = {}) {
let u;
try {
u = new URL(String(raw ?? ""));
} catch {
throw new GrantError("origin must be a URL such as https://meshhook.com");
}
const local = allowLocal && u.protocol === "http:" && ["localhost", "127.0.0.1"].includes(u.hostname);
if (u.protocol !== "https:" && !local) throw new GrantError("origin must be https");
if (u.username || u.password || (u.pathname !== "/" && u.pathname !== "") || u.search || u.hash) {
throw new GrantError("origin must be a bare origin, like https://meshhook.com");
}
if (!local && (isIP(u.hostname) || !u.hostname.includes("."))) throw new GrantError("origin must use a public host name");
return u.origin;
}

/** v4/v6 ranges a manifest fetch must never reach (loopback, private, link-local, CGNAT, metadata). */
export function isPrivateAddress(ip) {
if (isIP(ip) === 4) {
const [a, b] = ip.split(".").map(Number);
return a === 10 || a === 127 || a === 0 || (a === 169 && b === 254) || (a === 172 && b >= 16 && b <= 31) ||
(a === 192 && b === 168) || (a === 100 && b >= 64 && b <= 127) || a >= 224;
}
const v = ip.toLowerCase();
if (v.startsWith("::ffff:")) return isPrivateAddress(v.slice(7));
return v === "::1" || v === "::" || v.startsWith("fc") || v.startsWith("fd") || v.startsWith("fe8") || v.startsWith("fe9") ||
v.startsWith("fea") || v.startsWith("feb") || v.startsWith("ff");
}

async function assertPublicHost(hostname) {
const addrs = await lookup(hostname, { all: true }).catch(() => []);
if (!addrs.length) throw new GrantError(`${hostname} does not resolve`, 502, "origin_unreachable");
if (addrs.some((a) => isPrivateAddress(a.address))) throw new GrantError(`${hostname} resolves to a private address`, 400, "origin_private");
}

const sameOrigin = (url, origin) => {
try {
return new URL(url).origin === origin;
} catch {
return false;
}
};

/** Validate a manifest against the origin it was fetched from. Returns a normalised copy. */
export function validateManifest(m, origin, { now = Date.now() } = {}) {
if (!m || typeof m !== "object" || m.v !== 1) throw new GrantError("manifest must be an object with v: 1", 502, "bad_manifest");
const appName = String(m.app_name ?? "").trim().slice(0, 80);
if (!appName) throw new GrantError("manifest needs app_name", 502, "bad_manifest");
if (!Array.isArray(m.keys) || !m.keys.length || m.keys.length > MAX_KEYS) throw new GrantError(`manifest needs 1 to ${MAX_KEYS} keys`, 502, "bad_manifest");
const keys = m.keys.map((k) => {
const name = String(k?.name ?? "");
if (!KEY_NAME.test(name)) throw new GrantError(`"${name}" is not a valid key name`, 502, "bad_manifest");
return { name, description: String(k?.description ?? "").slice(0, 200) };
});
const r = m.recipient ?? {};
if (r.format !== RECIPIENT_FORMAT) throw new GrantError(`recipient.format must be ${RECIPIENT_FORMAT}`, 502, "bad_manifest");
for (const f of ["app", "scope", "user_id", "inbox_public_key"]) {
if (typeof r[f] !== "string" || !r[f]) throw new GrantError(`recipient.${f} is required`, 502, "bad_manifest");
}
if (!sameOrigin(m.webhook_url, origin)) throw new GrantError("webhook_url must be on the app's own origin", 502, "bad_manifest");
if (typeof m.webhook_secret !== "string" || m.webhook_secret.length < 32) throw new GrantError("webhook_secret must be 32+ characters", 502, "bad_manifest");
const returnUrl = m.return_url && sameOrigin(m.return_url, origin) ? m.return_url : origin;
const expiresAt = Number(m.expires_at) || now + 15 * 60_000;
if (expiresAt <= now) throw new GrantError("this grant request has expired; start again from the app", 410, "expired");
return {
v: 1,
app_name: appName,
keys,
recipient: { format: r.format, app: r.app, scope: r.scope, user_id: r.user_id, inbox_public_key: r.inbox_public_key },
webhook_url: m.webhook_url,
webhook_secret: m.webhook_secret,
return_url: returnUrl,
expires_at: Math.min(expiresAt, now + 60 * 60_000),
};
}

/** Fetch the manifest from the app's origin: TLS is what proves which app is asking. */
export async function fetchManifest(origin, nonce, { fetchImpl = fetch, allowLocal = false, now = Date.now() } = {}) {
if (!/^[A-Za-z0-9_-]{16,128}$/.test(String(nonce ?? ""))) throw new GrantError("nonce must be 16 to 128 URL-safe characters");
const o = parseOrigin(origin, { allowLocal });
if (!allowLocal) await assertPublicHost(new URL(o).hostname);
let res;
try {
res = await fetchImpl(`${o}${MANIFEST_PATH}${encodeURIComponent(nonce)}`, {
headers: { accept: "application/json", "user-agent": "logicsrc-grants/1" },
redirect: "error",
signal: AbortSignal.timeout(8000),
});
} catch (e) {
throw new GrantError(`could not reach ${o}: ${e.message}`, 502, "origin_unreachable");
}
if (res.status === 404) throw new GrantError("the app does not know this request (expired or already used)", 404, "unknown_request");
if (!res.ok) throw new GrantError(`${o} answered ${res.status}`, 502, "origin_error");
const text = await res.text();
if (text.length > 64_000) throw new GrantError("manifest too large", 502, "bad_manifest");
let body;
try {
body = JSON.parse(text);
} catch {
throw new GrantError("manifest is not JSON", 502, "bad_manifest");
}
return validateManifest(body, o, { now });
}

/** A short, stable fingerprint of a recipient (what changes if the app's inbox key changes). */
export const recipientFingerprint = (r) =>
createHash("sha256").update(`${r.app}\n${r.scope}\n${r.user_id}\n${r.inbox_public_key}`).digest("hex").slice(0, 16);

/**
* Standard Webhooks signature: v1,<base64 HMAC-SHA256(secret, "<id>.<ts>.<body>")>.
* The secret is the app's webhook_secret as UTF-8 (a whsec_ prefix is base64 per the spec).
*/
export function signWebhook(secret, id, ts, body) {
const key = secret.startsWith("whsec_") ? Buffer.from(secret.slice(6), "base64") : Buffer.from(secret, "utf8");
return `v1,${createHmac("sha256", key).update(`${id}.${ts}.${body}`).digest("base64")}`;
}

/** POST one event to the app. Returns { ok, status?, error? }; never throws. */
export async function sendWebhook(url, secret, payload, { fetchImpl = fetch, allowLocal = false } = {}) {
// Re-checked on every delivery: a host that resolved publicly at consent time
// may point inside our network by the time a rotation fires.
if (!allowLocal) {
try {
await assertPublicHost(new URL(url).hostname);
} catch (e) {
return { ok: false, error: e.message };
}
}
const body = JSON.stringify(payload);
const id = `msg_${randomBytes(12).toString("hex")}`;
const ts = Math.floor(Date.now() / 1000);
try {
const res = await fetchImpl(url, {
method: "POST",
headers: {
"content-type": "application/json",
"webhook-id": id,
"webhook-timestamp": String(ts),
"webhook-signature": signWebhook(secret, id, ts, body),
"user-agent": "logicsrc-grants/1",
},
body,
redirect: "error",
signal: AbortSignal.timeout(10_000),
});
return res.ok ? { ok: true, status: res.status } : { ok: false, status: res.status, error: `HTTP ${res.status}` };
} catch (e) {
return { ok: false, error: e.message };
}
}

export const isSealed = (v) => typeof v === "string" && v.startsWith(SEALED_PREFIX) && v.length < 32_000;
39 changes: 39 additions & 0 deletions apps/pwa/src/migrations-pg/013_app_grants.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- Postgres copy of migrations/013_app_grants.sql (INTEGER -> bigint). Additive only.
create table if not exists app_grant_requests (
id TEXT PRIMARY KEY,
origin TEXT NOT NULL,
nonce TEXT NOT NULL,
app_name TEXT NOT NULL,
manifest TEXT NOT NULL,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
status TEXT NOT NULL,
created_at bigint NOT NULL,
expires_at bigint NOT NULL,
decided_at bigint,
UNIQUE (origin, nonce)
);

create table if not exists app_grants (
id TEXT PRIMARY KEY,
request_id TEXT NOT NULL REFERENCES app_grant_requests(id) ON DELETE CASCADE,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
origin TEXT NOT NULL,
app_name TEXT NOT NULL,
key_name TEXT NOT NULL,
vault_id TEXT NOT NULL,
source_name TEXT NOT NULL,
fingerprint TEXT NOT NULL,
recipient TEXT NOT NULL,
recipient_fingerprint TEXT NOT NULL,
webhook_url TEXT NOT NULL,
webhook_secret TEXT NOT NULL,
status TEXT NOT NULL,
created_at bigint NOT NULL,
revoked_at bigint,
last_delivered_at bigint,
last_error TEXT
);

CREATE INDEX IF NOT EXISTS idx_app_grants_user ON app_grants(user_id);
CREATE INDEX IF NOT EXISTS idx_app_grants_fingerprint ON app_grants(fingerprint);
CREATE INDEX IF NOT EXISTS idx_app_grants_vault ON app_grants(vault_id);
Loading
Loading