Files
puter/extensions/useremailrx/index.ts
Daniel Salazar 6b8af52077 feat(auth): sign-in pipeline, per-account wrong-password budget and idle session expiry (#4692)
Sign-in
- One sign-in path (SignInService) for password, 2FA, recovery code, signup
  and OIDC. It emits puter.signin.attempt (before the account lookup on
  POST /login), puter.signin.validate (before the session is created) and
  puter.signin. Listeners can refuse or require phone or card verification; a
  listener that throws is ignored.
- POST /login keeps a wrong-password budget per account across all networks,
  20 an hour and 50 a day. A browser that has signed in to the account before
  is not refused, and the budget belongs to the current password.
- An account unused for dormantSignInDays (default 180) confirms an
  unconfirmed email at sign-in, where the deployment can send mail.
- New account emails: sign-in from a new browser, sign-in attempts blocked,
  password reset completed.
- pwnedPasswords (off by default): a k-anonymity range lookup after a
  successful password sign-in, with a notice to the owner.
- requestSignalHeaders and requestSignalValue turn configured request headers
  into req.requestSignals and a puter.request.flagged event.

Sessions
- web and app sessions end after 180 days without activity; accounts with no
  password and no email keep 365 days. A missing expiry on web, app and asset
  rows no longer means no expiry, and ended rows are purged.
- The cookie a private hosted site sets from ?auth_token= is tied to the
  token's session row and carries a 7-day expiry.

Email confirmation codes
- A 6-digit code is valid for 60 minutes and has a wrong-code budget.

Mailbox ingress
- email.ingress.validate runs before a message is stored; a listener can refuse
  it or record a sender check. X-Puter-Sender-Auth is written by the server
  only. Header blocks over 256 KiB are refused.

GUI
- Login sends the device fingerprint, can show the captcha widget
  (gui_params.turnstileLogin), offers a password reset when sign-in is locked,
  and runs pending verification windows after a popup sign-in or signup.
2026-11-18 23:02:16 -07:00

249 lines
8.9 KiB
TypeScript

import { extension } from '@heyputer/backend/src/extensions';
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
import type { Request } from 'express';
import type { Readable } from 'node:stream';
import {
MAX_MESSAGE_BYTES,
type SenderAuthOutcome,
isPuterEmailAddress,
isTempUser,
puterEmailUsername,
stampSenderAuth,
storeInboxMessage,
} from '@heyputer/backend/src/services/email/mailbox.js';
import {
MAX_HEADER_BYTES,
headBlockOf,
readHeadBlock,
replaceHead,
} from '@heyputer/backend/src/services/email/rawMessage.js';
// copied form peer secrets checker
const COMPARE_KEY = randomBytes(32);
const secretsEqual = (a: string, b: string): boolean =>
timingSafeEqual(
createHmac('sha256', COMPARE_KEY).update(a).digest(),
createHmac('sha256', COMPARE_KEY).update(b).digest(),
);
const USER_EMAIL_CONFIG = (extension.config as Record<string, unknown>)
.userEmail as { secret?: string; feedbackAddresses?: unknown } | undefined;
const INGRESS_SECRET = USER_EMAIL_CONFIG?.secret;
/**
* Local parts that take complaint feedback rather than naming a mailbox. The
* same names are reserved at signup, so no account can be handed them.
*/
const DEFAULT_FEEDBACK_ADDRESSES = ['fbl', 'abuse', 'postmaster'];
const FEEDBACK_ADDRESSES = new Set(
(Array.isArray(USER_EMAIL_CONFIG?.feedbackAddresses)
? USER_EMAIL_CONFIG.feedbackAddresses
: DEFAULT_FEEDBACK_ADDRESSES
).map((local) => String(local).toLowerCase()),
);
/**
* Feedback is handed over whole, so it is read into memory; a complaint report
* quotes one message and has no business near the mailbox ceiling.
*/
const MAX_FEEDBACK_BYTES = 5 * 1024 * 1024;
const isFeedbackAddress = (to: string): boolean =>
isPuterEmailAddress(to) &&
FEEDBACK_ADDRESSES.has(puterEmailUsername(to).toLowerCase());
/**
* A listener's refusal reason as one short line of printable ASCII: the relay
* quotes it in an SMTP reply.
*/
const refusalText = (reason: string | null): string =>
(reason ?? '')
.replace(/[^\x20-\x7e]+/g, ' ')
.trim()
.slice(0, 200) || 'Message refused';
const readBody = async (req: Request): Promise<Buffer> => {
const chunks: Buffer[] = [];
for await (const chunk of req) chunks.push(chunk as Buffer);
return Buffer.concat(chunks);
};
if (!INGRESS_SECRET) {
console.warn(
'[useremailrx] config.userEmail.secret unset - email ingress disabled',
);
} else {
extension.post(
'/email/ingress',
{
subdomain: 'api',
},
async (req, res) => {
// Answer first, then deal with the body nobody read. Touching the
// request before the response has flushed takes the response down
// with the connection and the caller sees a reset with no status,
// so this hangs off `finish` — and it has to be the event rather
// than `res.end(cb)`, because the compression middleware replaces
// `res.end` with an `(chunk, encoding)` signature that has no
// callback parameter, so a callback lands in `chunk` and dies in
// `Buffer.byteLength`.
//
// `drain` discards the rest of a body we were entitled to read and
// leaves the connection reusable. `close` is for bodies we do not
// want to read at all: an unauthenticated caller's, or one whose
// declared length is missing or over the cap.
const refuse = (
status: number,
how: 'drain' | 'close',
body?: string,
) => {
res.once('finish', () =>
how === 'drain' ? req.resume() : req.destroy(),
);
res.status(status).end(body);
};
const presented = req.query.SECRET;
if (
typeof presented !== 'string' ||
!secretsEqual(presented, INGRESS_SECRET)
) {
// Nothing has authenticated this caller, so refuse to spend
// bandwidth reading whatever they were uploading.
refuse(403, 'close');
return;
}
const size = Number(req.headers['content-length']);
if (!Number.isInteger(size) || size <= 0) {
refuse(411, 'close');
return;
}
if (size > MAX_MESSAGE_BYTES) {
refuse(413, 'close');
return;
}
const stores = extension.import('store');
const services = extension.import('service');
const to = String(req.query.to ?? '');
let content: Buffer | Request = req;
if (isFeedbackAddress(to)) {
if (size > MAX_FEEDBACK_BYTES) {
refuse(413, 'close');
return;
}
const raw = await readBody(req);
const event = {
to,
from:
typeof req.query.from === 'string'
? req.query.from
: null,
raw,
handled: false,
};
await extension
.import('client')
.event.emitAndWait('email.ingress.feedback', event, {});
if (event.handled) {
res.end();
return;
}
// Nobody took it: route it like any other message.
content = raw;
}
const user = await stores.user.getByUsername(
puterEmailUsername(to),
);
if (!user) {
refuse(404, 'drain');
return;
}
if (isTempUser(user)) {
if (content === req) req.resume();
res.end();
return;
}
let head: Buffer;
if (Buffer.isBuffer(content)) {
head = headBlockOf(content);
if (head.length > MAX_HEADER_BYTES) {
refuse(413, 'drain');
return;
}
} else {
const block = await readHeadBlock(req, MAX_HEADER_BYTES);
if (!block) {
refuse(413, 'close');
return;
}
head = block.head;
// A message that is all header block was read whole.
if (block.spent) content = head;
}
let reading: Promise<Buffer> | null = Buffer.isBuffer(content)
? Promise.resolve(content)
: null;
const check = {
to,
envelopeFrom:
typeof req.query.mailfrom === 'string' && req.query.mailfrom
? req.query.mailfrom
: null,
head,
size: Buffer.isBuffer(content) ? content.length : size,
readMessage: () => (reading ??= readBody(req)),
allow: true,
message: null as string | null,
senderAuth: null as SenderAuthOutcome | null,
};
const meta: { listener_failed?: boolean } = {};
// A listener that throws is no opinion: what it set is undone and
// the message is filed.
await extension
.import('client')
.event.emitAndWait('email.ingress.validate', check, meta, {
revertOnFailure: ['allow', 'message', 'senderAuth'],
});
if (meta.listener_failed) {
console.warn(
'[useremailrx] an email.ingress.validate listener failed; filing the message without its check',
);
}
if (check.allow === false) {
refuse(422, 'drain', refusalText(check.message));
return;
}
// The sender check's header is ours alone: any copy the sender
// wrote goes, whether or not a check ran.
const newHead = stampSenderAuth(head, check.senderAuth);
const whole = reading ? await reading : null;
let stored: Buffer | Request | Readable = whole ?? req;
let storedSize = check.size;
if (newHead !== head) {
stored = whole
? Buffer.concat([newHead, whole.subarray(head.length)])
: replaceHead(req, head.length, newHead);
storedSize = check.size - head.length + newHead.length;
}
await storeInboxMessage(services.fs, user, {
subject: req.query.subject,
content: stored,
size: storedSize,
});
res.end();
},
);
}