Podcast Commission Stack: kit under Promo tools > Network sites (read from its own /api/promo-kit feed, placed after Video App Income, added quietly: no notice, no release note) and the email-check code mail for its network gate. siteinvite.js: each site signs with its own secret (SITE_INVITE_SECRET_PCS for the new site, SITE_INVITE_SECRET unchanged for Video App Income), verify() names the signing site and a body that names any other site is refused; the new site has the code call only, no invite emails; the code limits stay one count for all sites (5 an hour per address, 2,000 a day). Chatbot answer + prompt, KNOWLEDGE_DATE 2026-11-25; qa/site-invite.test.mjs 52 -> 80 checks, routing rows in qa/chatbot-parse.mjs

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-25 09:32:18 -06:00
parent 563407e45b
commit c5b9624eda
7 changed files with 117 additions and 22 deletions
+30 -13
View File
@@ -13,6 +13,10 @@
// the email-check code for the site's network gate: the SITE makes and checks the 6-digit code, this app only mails it
// (a requested code is transactional, like a sign-in code). Fixed wording, no links; 5 an hour per address, 2,000 a day in all.
// Signature: X-Invite-Ts (ms) + X-Invite-Sig = hex HMAC-SHA256(SITE_INVITE_SECRET, 'invite.v1.' + ts + '\n' + raw body), 5 minutes.
// A second site (2026-11-25, Podcast Commission Stack): each site signs with ITS OWN secret (SITES[id].secretEnv, else SITE_INVITE_SECRET).
// verify() says which site's secret signed the call and handle() refuses a body that names any other site, so one site's secret can
// never act for another. SITES[id].ops lists what a site may ask for: Podcast Commission Stack has the code mail only, no invite
// emails (their wording is about Video App Income and was approved for it alone). The code limits stay one count for all sites.
//
// The rules:
// - DIRECTS ONLY: each recipient is checked with accounts.isDownlineOf(sender, recipient, 1) at send time (the directs-only email
@@ -31,7 +35,8 @@ const path = require('path');
const crypto = require('crypto');
const db = require('./db');
const SITES = { videoappincome: { name: 'Video App Income' } };
const SITES = { videoappincome: { name: 'Video App Income' }, // no ops list = every call; its secret is SITE_INVITE_SECRET, as before
podcastcommissionstack: { name: 'Podcast Commission Stack', ops: ['code'], secretEnv: 'SITE_INVITE_SECRET_PCS' } };
const THROWAWAY = new Set(['mailinator.com', 'guerrillamail.com', 'guerrillamail.net', 'sharklasers.com', 'grr.la', '10minutemail.com', 'tempmail.com', 'temp-mail.org',
'yopmail.com', 'trashmail.com', 'getnada.com', 'maildrop.cc', 'dispostable.com', 'throwawaymail.com', 'fakeinbox.com', 'mintemail.com', 'mohmal.com', 'emailondeck.com',
'tempail.com', 'tempr.email', 'discard.email', 'spamgourmet.com', 'mailnesia.com', 'mytemp.email', 'tempmailo.com', 'burnermail.io', 'example.com', 'test.com']);
@@ -43,6 +48,17 @@ let R = null; // { dataDir, accounts, mailer, siteConfig, prohibited, hooks: { e
const clock = () => (R && R.clock ? R.clock() : Date.now());
const SECRET = () => String((R && R.secret) || process.env.SITE_INVITE_SECRET || '').trim();
const enabled = () => SECRET().length >= 32;
// the secret a site signs with. A site with its own secretEnv never falls back to SITE_INVITE_SECRET, and a secret that is too short
// or the same as another site's is treated as not set: two sites must not share one.
const secretOf = id => { const s = SITES[id]; if (!s) return ''; if (!s.secretEnv) return SECRET(); const v = String((R && R.secrets && R.secrets[id]) || process.env[s.secretEnv] || '').trim(); return v.length >= 32 && v !== SECRET() ? v : ''; };
const keys = () => Object.keys(SITES).map(id => ({ id, secret: secretOf(id) })).filter(k => k.secret.length >= 32);
// the site a call is for, or the refusal. signer = the site whose secret signed it (from verify); a call made inside this app has none.
function siteOf(b, op, signer) {
const id = lc(b.site), site = SITES[id]; if (!site) return { error: 'unknown site', code: 'site' };
if (signer && signer !== id) return { error: 'That site is not the one this key belongs to.', code: 'site' };
if (site.ops && !site.ops.includes(op)) return { error: 'That call is not open to this site.', code: 'op' };
return { id, site };
}
const hosts = () => String((R && R.linkHosts) || process.env.SITE_INVITE_LINK_HOSTS || 'clickvidpro.com').split(',').map(lc).filter(Boolean);
function cfg() { const sc = (R && R.siteConfig && R.siteConfig()) || {}; const n = (k, lo, hi) => { const v = Math.round(Number(sc[k] == null ? DEFAULTS[k] : sc[k])); return v >= lo && v <= hi ? v : DEFAULTS[k]; };
return { paused: String(sc.siteInvitesPaused == null ? DEFAULTS.siteInvitesPaused : sc.siteInvitesPaused) === '1', perSender: n('siteInvitePerSender', 1, 500), perDay: n('siteInvitePerDay', 1, 5000) }; }
@@ -85,12 +101,12 @@ async function init(opts) { R = opts; J.st = null; await ensureTables(); }
// ---- signature ----
function sign(ts, raw, secret) { return crypto.createHmac('sha256', secret || SECRET()).update('invite.v1.' + ts + '\n' + raw).digest('hex'); }
function verify(headers, raw) {
if (!enabled()) return { status: 503, error: 'site invites not configured' };
const ks = keys(); if (!ks.length) return { status: 503, error: 'site invites not configured' };
const ts = String(headers['x-invite-ts'] || ''), sig = String(headers['x-invite-sig'] || '');
if (!/^\d{13}$/.test(ts) || Math.abs(Date.now() - Number(ts)) > 5 * 60000) return { status: 401, error: 'stale' };
const want = sign(ts, raw);
if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return { status: 401, error: 'bad signature' };
try { const b = JSON.parse(raw || '{}'); return b && typeof b === 'object' ? { body: b } : { status: 400, error: 'bad json' }; } catch (e) { return { status: 400, error: 'bad json' }; }
let signer = ''; for (const k of ks) { const want = sign(ts, raw, k.secret); if (!signer && want.length === sig.length && crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) signer = k.id; }
if (!signer) return { status: 401, error: 'bad signature' };
try { const b = JSON.parse(raw || '{}'); return b && typeof b === 'object' ? { body: b, site: signer } : { status: 400, error: 'bad json' }; } catch (e) { return { status: 400, error: 'bad json' }; }
}
// ---- who ----
@@ -149,8 +165,8 @@ function cleanNote(n) {
const linkOk = l => { try { const u = new URL(String(l)); return u.protocol === 'https:' && hosts().includes(u.hostname.toLowerCase()) && /^\/p\/[A-Za-z0-9]{4,20}(\/[A-Za-z0-9]{4,20})?$/.test(u.pathname) && !u.search && !u.hash; } catch (e) { return false; } };
// ---- the two calls ----
async function eligible(b) {
const site = SITES[lc(b.site)]; if (!site) return { error: 'unknown site', code: 'site' };
async function eligible(b, signer) {
const so = siteOf(b, 'eligible', signer); if (so.error) return so;
const sender = await senderOf(b.sender); if (!sender) return { error: 'The sender has no InstantAdPay account with a username.', code: 'sender' };
const lv = await R.accounts.downline(sender.email, 1), kids = (lv[0] && lv[0].members) || [];
const sent = await S().sentTo(lc(b.site), kids.map(k => lc(k.email))), directs = [];
@@ -158,8 +174,8 @@ async function eligible(b) {
return { ok: true, sender: sender.username, directs, left: await left(sender), paused: cfg().paused };
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function send(b) {
const siteId = lc(b.site), site = SITES[siteId]; if (!site) return { error: 'unknown site', code: 'site' };
async function send(b, signer) {
const so = siteOf(b, 'send', signer); if (so.error) return so; const siteId = so.id, site = so.site;
const rid = String(b.rid || ''); if (!/^[A-Za-z0-9_.:-]{8,80}$/.test(rid)) return { error: 'rid needed', code: 'rid' };
const before = await S().byRid(rid); if (before.length) return { ok: true, already: true, results: before.map(r => ({ username: r.username, status: r.status })), sent: before.filter(r => r.status === 'sent').length };
if (cfg().paused) return { error: 'Invites are paused right now.', code: 'paused' };
@@ -191,8 +207,8 @@ async function send(b) {
}
// ---- the email-check code a site asks for when a new person gives their email at its network gate ----
const codeLog = new Map(); let codeDay = { start: 0, n: 0 };
async function code(b) {
const site = SITES[lc(b.site)]; if (!site) return { error: 'unknown site', code: 'site' };
async function code(b, signer) {
const so = siteOf(b, 'code', signer); if (so.error) return so; const site = so.site;
const em = lc(b.email), c = String(b.code || '');
if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(em) || em.length > 190 || !/^\d{6}$/.test(c)) return { error: 'That does not look like an email address.', code: 'email' };
const d = em.split('@')[1] || '';
@@ -210,9 +226,10 @@ async function code(b) {
try { await R.mailer.send(em, c + ' is your ' + site.name + ' code', text, null, { transactional: true, fromName: site.name }); } catch (e) { console.error('site code mail', e.message); return { error: 'The email could not be sent. Check the address and try again.', code: 'mail' }; }
return { ok: true };
}
async function handle(op, b) { if (op === 'eligible') return eligible(b || {}); if (op === 'send') return send(b || {}); if (op === 'code') return code(b || {}); return { error: 'unknown op' }; }
// signer: the site verify() named (the HTTP route always passes it), so a body can only name the site whose secret signed it
async function handle(op, b, signer) { if (op === 'eligible') return eligible(b || {}, signer); if (op === 'send') return send(b || {}, signer); if (op === 'code') return code(b || {}, signer); return { error: 'unknown op' }; }
async function adminView() { const since = dayStart(clock()), c = cfg(); return { on: enabled(), paused: c.paused, perSender: c.perSender, perDay: c.perDay, today: await S().count(since), recent: (await S().recent(200)).map(r => ({ site: r.site, to: r.username, from: r.senderName, ts: r.ts, status: r.status, link: r.link })) }; }
// one sample to one address, for the owner to read before the feature opens. Not counted, not recorded.
async function sample(to, siteId) { const site = SITES[lc(siteId) || 'videoappincome']; if (!site) return { error: 'unknown site' }; const m = compose(site, 'yoursponsor', 'yourname', 'https://' + hosts()[0] + '/p/faceless', ''); await R.mailer.send(to, '[SAMPLE] ' + m.subject, m.text, m.html); return { ok: true, to, subject: m.subject, text: m.text }; }
async function sample(to, siteId) { const site = SITES[lc(siteId) || 'videoappincome']; if (!site || (site.ops && !site.ops.includes('send'))) return { error: 'unknown site' }; const m = compose(site, 'yoursponsor', 'yourname', 'https://' + hosts()[0] + '/p/faceless', ''); await R.mailer.send(to, '[SAMPLE] ' + m.subject, m.text, m.html); return { ok: true, to, subject: m.subject, text: m.text }; }
module.exports = { init, enabled, verify, sign, handle, adminView, sample, compose, cleanNote, linkOk, DEFAULTS, SITES };