mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 20:29:02 +01:00
Inviting a collaborator required them to already have a Loki account. The members route answered "No Loki account uses that email. Ask them to register first." — a dead end with no way to ask. The only other invite path creates a LOCAL-PASSWORD account and adds it to the inviter's whole org: broader than one project, and a second identity path beside OrangeCat, which fleet STACK.md says our own products must not grow. Now any email works: - Has an account: added at once, as before. - No account: a one-time invite is created, emailed when mail is configured, and ALWAYS returned as a link to copy — an email that was sent is not an email that was received. They open it, sign in with OrangeCat (creating that account on the way), and join exactly this project with exactly the role chosen. No Loki password anywhere. The link is bound to the invited email. A token is a bearer secret that gets forwarded; on its own it would let whoever holds the link in. The wrong account is told which address it was for, masked (ge•••@gmail.com) because the link may be in the wrong hands. Only a sha-256 of the token is stored, so a leaked backup cannot be replayed as working links. Acceptance spends the invite and grants the membership in one transaction guarded on accepted_at/revoked_at IS NULL, so a double click or two tabs grants once. Re-inviting the same email revokes the old link rather than leaving two live ones. "emailed" is only reported when mail is configured AND the send succeeded: sendEmail returns quietly when mail is off, so trusting the call alone would have claimed an email nobody sent. The panel moved from a collapsed "Editors" disclosure inside the Feedback tab to the top of the project's Settings tab — where every other product keeps "people with access", and where George looked for it. It now offers Editor or Viewer, lists pending invites, and lets the owner withdraw one. Editors do not see pending invitees' emails. The accept API lives under /api/invitations/project/ so it inherits the existing public-route exclusion instead of widening the proxy allow-list. Migration 0080 is hand-written, like 0073-0079: drizzle/meta's snapshot stops at 0072, so `drizzle-kit generate` diffs against a stale picture — it believes beacon_sessions still exists and calendar_busy does not, and would have emitted a DROP and a duplicate CREATE alongside this table. Tests: 14 checks on the acceptance rule, proved by mutation — removing the email binding turns the suite red. tsc clean, lint 0 errors, check:design ok, format ok, test:unit 260/260, test:home 130/130. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
128 lines
4.3 KiB
TypeScript
128 lines
4.3 KiB
TypeScript
/**
|
|
* Invitations into one project: the rules that decide who gets into someone
|
|
* else's project. Each check is a way the grant could go to the wrong person.
|
|
*/
|
|
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
evaluateInviteAcceptance,
|
|
hashInviteToken,
|
|
inviteExpiry,
|
|
maskEmail,
|
|
newInviteToken,
|
|
normalizeInviteEmail,
|
|
PROJECT_INVITE_TTL_DAYS,
|
|
projectInvitePath,
|
|
type InviteState,
|
|
} from "@/lib/project-invites";
|
|
|
|
let passed = 0;
|
|
const check = (label: string, fn: () => void) => {
|
|
fn();
|
|
passed += 1;
|
|
console.log(` ✓ ${label}`);
|
|
};
|
|
|
|
const NOW = new Date("2026-09-24T12:00:00Z");
|
|
const live = (over: Partial<InviteState> = {}): InviteState => ({
|
|
email: "georgbotsmann@gmail.com",
|
|
expiresAt: new Date(NOW.getTime() + 60_000),
|
|
acceptedAt: null,
|
|
revokedAt: null,
|
|
...over,
|
|
});
|
|
|
|
check("the invited account may accept", () => {
|
|
assert.deepEqual(evaluateInviteAcceptance(live(), "georgbotsmann@gmail.com", NOW), { ok: true });
|
|
});
|
|
|
|
check("email matching ignores case and surrounding space", () => {
|
|
const v = evaluateInviteAcceptance(live(), " GeorgBotsmann@Gmail.com ", NOW);
|
|
assert.equal(v.ok, true);
|
|
});
|
|
|
|
// The token is a bearer secret that gets forwarded. It must not be enough.
|
|
check("a forwarded link opens nothing for a different account", () => {
|
|
const v = evaluateInviteAcceptance(live(), "someone.else@example.com", NOW);
|
|
assert.equal(v.ok, false);
|
|
assert.equal(!v.ok && v.reason, "wrong-account");
|
|
});
|
|
|
|
check("the wrong-account message never shows the full invited address", () => {
|
|
const v = evaluateInviteAcceptance(live(), "someone.else@example.com", NOW);
|
|
assert.ok(
|
|
!v.ok && !v.message.includes("georgbotsmann@"),
|
|
"full address leaked to the link holder",
|
|
);
|
|
assert.ok(!v.ok && v.message.includes("ge•••@gmail.com"));
|
|
});
|
|
|
|
check("an account with no email cannot be matched, so cannot accept", () => {
|
|
for (const e of [null, undefined, "", " "]) {
|
|
const v = evaluateInviteAcceptance(live(), e, NOW);
|
|
assert.equal(!v.ok && v.reason, "no-email", `accepted with email=${JSON.stringify(e)}`);
|
|
}
|
|
});
|
|
|
|
check("a withdrawn invite grants nothing, even to the right person", () => {
|
|
const v = evaluateInviteAcceptance(live({ revokedAt: NOW }), "georgbotsmann@gmail.com", NOW);
|
|
assert.equal(!v.ok && v.reason, "revoked");
|
|
});
|
|
|
|
check("an invite grants once", () => {
|
|
const v = evaluateInviteAcceptance(live({ acceptedAt: NOW }), "georgbotsmann@gmail.com", NOW);
|
|
assert.equal(!v.ok && v.reason, "accepted");
|
|
});
|
|
|
|
check("an expired invite grants nothing — expiry is the boundary, not after it", () => {
|
|
const atExpiry = evaluateInviteAcceptance(
|
|
live({ expiresAt: NOW }),
|
|
"georgbotsmann@gmail.com",
|
|
NOW,
|
|
);
|
|
assert.equal(!atExpiry.ok && atExpiry.reason, "expired");
|
|
});
|
|
|
|
// Why an invite fails is the state of the invite first, identity second: a
|
|
// revoked invite should not tell a stranger to "sign in with that account".
|
|
check("a dead invite reports its state before judging who you are", () => {
|
|
const v = evaluateInviteAcceptance(live({ revokedAt: NOW }), "someone.else@example.com", NOW);
|
|
assert.equal(!v.ok && v.reason, "revoked");
|
|
});
|
|
|
|
check("the database keeps a hash, never the token", () => {
|
|
const token = newInviteToken();
|
|
const hash = hashInviteToken(token);
|
|
assert.notEqual(hash, token);
|
|
assert.ok(!hash.includes(token));
|
|
assert.equal(hash, hashInviteToken(token), "hashing must be deterministic to look invites up");
|
|
assert.match(hash, /^[0-9a-f]{64}$/);
|
|
});
|
|
|
|
check("tokens are long, URL-safe and not repeated", () => {
|
|
const seen = new Set<string>();
|
|
for (let i = 0; i < 200; i++) {
|
|
const t = newInviteToken();
|
|
assert.ok(t.length >= 43, "32 random bytes");
|
|
assert.match(t, /^[A-Za-z0-9_-]+$/, "URL-safe");
|
|
assert.ok(!seen.has(t));
|
|
seen.add(t);
|
|
}
|
|
});
|
|
|
|
check("the link carries the raw token under /invite/project/", () => {
|
|
assert.equal(projectInvitePath("abc"), "/invite/project/abc");
|
|
});
|
|
|
|
check("an invite lasts the stated number of days", () => {
|
|
const days = (inviteExpiry(NOW).getTime() - NOW.getTime()) / 86_400_000;
|
|
assert.equal(days, PROJECT_INVITE_TTL_DAYS);
|
|
});
|
|
|
|
check("stored emails are normalised the same way they are compared", () => {
|
|
assert.equal(normalizeInviteEmail(" A@B.CH "), "a@b.ch");
|
|
assert.equal(maskEmail("GeorgBotsmann@Gmail.com"), "ge•••@gmail.com");
|
|
});
|
|
|
|
console.log(`\nproject-invites: ${passed} checks passed`);
|