Compare commits

...
Author SHA1 Message Date
cayce 398d01fd4c Merge: regla permanente de secretos (anti-filtración)
ci / lint-test-leaks (push) Canceled after 0s
2026-10-31 18:00:41 +00:00
cayce 6ddf76983b regla permanente de secretos: test anti-patrones + hook de journald
ci / lint-test-leaks (pull_request) Canceled after 0s
Nueva regla del operador (2026-10-31): los ficheros de secretos solo se
inspeccionan con herramientas que no muestran valores (jq keys, grep -c,
stat). Prohibido cat/grep -r sobre .env y volcar errores con DSN.

- tests/test_no_credential_patterns.py: 3 tests — DSN con credencial
  literal fuera de placeholders, nsec bech32 versionado, tokens hex de
  64 chars fuera de tests/.
- scripts/check-credential-leaks.sh: hook que falla si journald
  reciente contiene DSN con contraseña o nsec (redactando el valor en
  la salida). Sin timer propio (no autorizado); ejecutar manual o desde
  cron del operador.

Contexto: la rotación del rol hardcap y el DSN filtrado a journald por
el test del gate (corregido; la credencial filtrada ya está rotada).
2026-10-31 18:00:26 +00:00
cayce 88b2ad2980 hardcap: auth middleware en /admin — token obligatorio, comparación en tiempo constante
ci / lint-test-leaks (push) Canceled after 0s
ci / lint-test-leaks (pull_request) Canceled after 0s
- adminAuth: exige Authorization: Bearer <HARDCAP_ADMIN_TOKEN> (o
  X-Admin-Token). Comparación timingSafeEqual sobre sha256 de ambos
  valores (sin leak de longitud ni timing). Fail-closed: sin token
  configurado, TODAS las rutas admin responden 503.
- 401s ahora se loguean con método, ruta e IP (primera trazabilidad de
  acceso que tiene el admin API desde que existe).
- token generado (32 bytes hex) y almacenado SOLO en
  /srv/hardcap-backend/.env — nunca impreso ni versionado.
- test-admin-auth.mjs: 6 tests (sin token, incorrecto, vacío, correcto,
  header alternativo, misma-longitud-distinto) — todos pasan en la VPS.
- desplegado en src + dist; servicio reiniciado y activo.

Nota de contexto: el admin API carecía de auth desde su creación
(index.ts: \x27TODO: add auth middleware before production\x27) y estuvo
alcanzable sin credenciales en :3001 hasta la contención de hoy.
2026-10-31 17:39:38 +00:00
cayce 34ee25b32f Merge: gate de publicación hardcap + outbox auto-suficiente
ci / lint-test-leaks (push) Canceled after 0s
2026-10-31 17:12:08 +00:00
cayce 87a2829b8a containment: hardcap admin API publish gate + outbox self-sufficient pool
ci / lint-test-leaks (pull_request) Canceled after 0s
- hardcap PATCH /admin/articles/:id → PUBLISHED now stages into
  outbound_drafts (channel hardcap) instead of publishing, when
  HARDCAP_PUBLISH_MODE=draft (default). Fail-closed. Verified E2E:
  PATCH on a QUEUED article returned {staged:true, draft_id:7} and the
  article stayed QUEUED.
- bitcoinhotels.org: audited — frontend only (Next.js), no publishing
  path; nothing to contain.
- outbox.mjs: self-sufficient pool (createRequire from
  /srv/cayce-core/node_modules/pg — hosts may lack pg; found by the
  promo simulation which now works end-to-end).
- runtime/hardcap/admin.ts: versioned copy of the patched route
  (hardcap-backend itself is not a git repo).
- deploy.sh: hardcap env wiring (HARDCAP_PUBLISH_MODE, IDENTITY_PATH,
  CAYCE_PGURL appended server-side).

🔴 UNRELATED CRITICAL FINDING (needs operator decision, not patched
without approval): hardcap admin API has NO authentication and binds
*:3001 — reachable from the public internet (HTTP 200 from outside,
including DELETE /admin/articles/all). Recommend: auth middleware +
bind 127.0.0.1 + firewall, plus rotation of credentials that appeared
in journald (hardcap DB role DSN leaked via a psql error line during
testing — fixed in the same patch, but journald retains it).
2026-10-31 17:11:57 +00:00
cayce f618574043 Merge PR #19: contención 2/2 — 10050, resumen de borradores, BTCPay prioridad 1
ci / lint-test-leaks (push) Canceled after 0s
2026-10-31 16:49:22 +00:00
cayce 058126b80d containment 2/2: kind 10050 (inbox relays) + drafts summary in supervisor DM + grants suspended, BTCPay priority-1
ci / lint-test-leaks (pull_request) Canceled after 0s
- publish-10050.mjs: publishes Cayce kind 10050 (NIP-65 message-relays/
  inbox) listing the 6 relays where the listener actually reads DMs.
  EXECUTED once with operator authorization: event id
  188ef397824e8f29b5e1bea661ae65b1f8532ee97617747b9fbde57c6e8dfce9,
  accepted 6/6 relays.
- initiative.mjs: the cycle DM to the supervisor now includes a summary
  of outbound_drafts from the last 24h (id, type, first 15 words) so the
  operator reviews what the agent WANTED to publish.
- initiative.mjs: supervisor directive (CAYCE_DIRECTIVE_BTCPAY=1,
  reversible) injected into the cycle prompt: grants/opportunity search
  SUSPENDED, single priority = consolidate the BTCPay Server proposal
  (opportunity #106 / draft #33) ready for HUMAN sending. MANDATE text
  untouched (only the supervisor can change it).
- initiative.mjs: add_opportunity now rejects duplicates (the agent had
  re-registered OpenSats ×10, HRF ×6).
- opportunity #106 status -> priority-1.
- check-relay-silence.mjs: queries the relays for Cayce-signed events
  since a timestamp; allows only kind 10050 and supervisor DMs; exits 1
  on any violation. Verified: 0 events, 0 violations since deployment.
- deploy.sh: sudo for the daemon restart, optional files tolerated.

Gate: make check green (96 tests).
2026-10-31 16:48:54 +00:00
cayce e06cb85ce8 Merge PR #18: contención de salidas a terceros + identidad versionada
ci / lint-test-leaks (push) Canceled after 0s
2026-10-31 16:45:06 +00:00
9 changed files with 417 additions and 5 deletions
+45
View File
@@ -0,0 +1,45 @@
// check-relay-silence — verifica que Cayce NO publique a relays desde <T>.
// Uso: node check-relay-silence.mjs <epoch-segundos-desde> [horas-ventana]
// Eventos permitidos: kind 10050 (inbox relays, autorizado), kind 4/1059
// (DMs AL SUPERVISOR — canal interno autorizado). Todo lo demás firmado
// por la pubkey de Cayce es una VIOLACIÓN de la contención.
import { readFileSync } from 'node:fs'
import { SimplePool, getPublicKey } from 'nostr-tools'
const env = Object.fromEntries(
readFileSync('/srv/cayce-nostr/.env', 'utf8').split('\n').filter(l => l.includes('=')).map(l => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1)])
)
// decodificar nsec (bech32 o hex) — mismo approach verificado en publish-10050
let sk
{
const { nsecDeserialize } = await import('nostr-tools')
sk = env.NSEC_HEX.startsWith('nsec1') ? nsecDeserialize(env.NSEC_HEX) : Buffer.from(env.NSEC_HEX, 'hex')
}
const RELAYS = (env.RELAYS || 'wss://relay.damus.io,wss://nos.lol,wss://relay.primal.net,wss://relay.snort.social,wss://nostr-pub.wellorder.net').split(',').map(s => s.trim())
const pk = getPublicKey(sk)
const sinceArg = Number(process.argv[2])
const hours = Number(process.argv[3] || 24)
if (!sinceArg) { console.error('uso: node check-relay-silence.mjs <epoch_s> [horas]'); process.exit(2) }
const T = sinceArg
console.log(`[silence] ventanas: eventos de Cayce desde ${new Date(T * 1000).toISOString()} (últimas ${hours}h)`)
console.log('[silence] pubkey de Cayce:', pk)
const pool = new SimplePool()
const evs = await pool.querySync(RELAYS, { authors: [pk], since: T }, 8000).catch(() => [])
pool.close(RELAYS)
const supervisor = env.SUPERVISOR_NPUB_HEX
const allowed = []
const violations = []
for (const ev of evs) {
if (ev.kind === 10050) { allowed.push(ev); continue }
if ((ev.kind === 4 || ev.kind === 1059) && supervisor && ev.tags.some(t => t[0] === 'p' && t[1] === supervisor)) { allowed.push(ev); continue }
violations.push(ev)
}
console.log(`[silence] eventos vistos: ${evs.length} · permitidos: ${allowed.length} (10050/DM-supervisor) · VIOLACIONES: ${violations.length}`)
for (const ev of violations.slice(0, 10)) {
console.log(` ✗ kind ${ev.kind} ${new Date(ev.created_at * 1000).toISOString()} ${ev.id.slice(0, 12)} — ${ev.content.slice(0, 60)}`)
}
process.exit(violations.length ? 1 : 0)
+7 -3
View File
@@ -11,8 +11,9 @@ install -m 644 "$SRC/editorials.mjs" /srv/cayce-core/editorials.mjs
install -m 644 "$SRC/initiative.mjs" /srv/cayce-core/initiative.mjs
install -m 644 "$SRC/outbox.mjs" /srv/cayce-nostr/outbox.mjs
install -m 644 "$SRC/daemon.mjs" /srv/cayce-nostr/daemon.mjs
install -m 755 "$SRC/publish-10050.mjs" /srv/cayce-nostr/publish-10050.mjs
install -m 644 "$SRC/check-relay-silence.mjs" /srv/cayce-nostr/check-relay-silence.mjs
# opcionales (llegan con el PR de verificación/10050)
install -m 755 "$SRC/publish-10050.mjs" /srv/cayce-nostr/publish-10050.mjs 2>/dev/null || true
install -m 644 "$SRC/check-relay-silence.mjs" /srv/cayce-nostr/check-relay-silence.mjs 2>/dev/null || true
install -m 644 "$SRC/outbox.mjs" /home/ubuntu/cayce-social/outbox.mjs
install -m 644 "$SRC/promo.mjs" /home/ubuntu/cayce-social/promo.mjs
install -m 644 "$SRC/outbox.mjs" /home/ubuntu/cayce-mail/outbox.mjs
@@ -24,5 +25,8 @@ for ENVF in /srv/cayce-core/.env /srv/cayce-nostr/.env /home/ubuntu/cayce-social
grep -q "^IDENTITY_PATH=" "$ENVF" 2>/dev/null || echo "IDENTITY_PATH=/home/ubuntu/cayce-ops/prompts/cayce-identity.md" >> "$ENVF"
done
systemctl restart cayce-nostr
sudo install -m 644 '$SRC/hardcap/admin.ts' /srv/hardcap-backend/src/routes/admin.ts 2>/dev/null || true
install -m 755 '$SRC/hardcap/test-admin-auth.mjs' /srv/hardcap-backend/runtime-tests/test-admin-auth.mjs 2>/dev/null || true
sudo systemctl restart hardcap-backend
sudo systemctl restart cayce-nostr
echo "deploy OK — mode=$(grep -h "^CAYCE_PUBLISH_MODE=" /srv/cayce-core/.env | head -1)"
+176
View File
@@ -0,0 +1,176 @@
import { Router } from 'express'
import { prisma } from '../lib/prisma'
import { ingestAllSources } from '../services/ingestion'
const router = Router()
// ── auth middleware (2026-10-31): token obligatorio, comparación en tiempo constante.
// Fail-closed: sin HARDCAP_ADMIN_TOKEN configurado, TODAS las rutas admin responden 503.
import { createHash, timingSafeEqual } from 'crypto'
const ADMIN_TOKEN = process.env.HARDCAP_ADMIN_TOKEN || ''
function _safeEqual(a: string, b: string): boolean {
// hash de ambos a longitud fija: sin leak de longitud ni de timing por contenido
return timingSafeEqual(createHash('sha256').update(String(a)).digest(), createHash('sha256').update(String(b)).digest())
}
function adminAuth(req: any, res: any, next: any) {
if (!ADMIN_TOKEN) {
console.error('[admin-auth] HARDCAP_ADMIN_TOKEN not set — admin request REFUSED (fail-closed)')
return res.status(503).json({ error: 'admin disabled: token not configured' })
}
const provided = String(req.headers['authorization'] || '').replace(/^Bearer\s+/i, '') || String(req.headers['x-admin-token'] || '')
if (provided && _safeEqual(provided, ADMIN_TOKEN)) return next()
console.error(`[admin-auth] 401 ${req.method} ${req.originalUrl} from ${req.socket.remoteAddress}`)
return res.status(401).json({ error: 'unauthorized' })
}
router.use(adminAuth)
// regla permanente de secretos: jamás loguear el token ni volcar headers.
// ── Articles ──────────────────────────────────────────────
// GET /admin/articles?status=QUEUED&topic=lightning
router.get('/articles', async (req, res) => {
const { status, topic } = req.query
const articles = await prisma.article.findMany({
where: {
...(status ? { status: status as any } : {}),
...(topic ? { topics: { has: topic as string } } : {}),
},
include: { source: { select: { name: true } } },
orderBy: { publishedAt: 'desc' },
take: 50,
})
res.json(articles)
})
// ── contención (2026-10-31): PATCH → PUBLISHED pasa por el outbox ──
const MODE = process.env.HARDCAP_PUBLISH_MODE || 'draft';
let pgCache = null;
function caycePool() {
if (!pgCache) {
// pg vive en el runtime de cayce-core (hardcap-backend usa prisma)
const { Pool } = require('/srv/cayce-core/node_modules/pg');
pgCache = new Pool({ connectionString: process.env.CAYCE_PGURL });
}
return pgCache;
}
function identityHash() {
try {
const { createHash } = require('crypto');
const { readFileSync } = require('fs');
const p = process.env.IDENTITY_PATH || '/home/ubuntu/cayce-ops/prompts/cayce-identity.md';
return createHash('sha256').update(readFileSync(p)).digest('hex');
} catch (e) { return null; }
}
// PATCH /admin/articles/:id — update status (CONTENCIÓN: draft no publica)
router.patch('/articles/:id', async (req, res) => {
const { id } = req.params
const { status } = req.body
if (status === 'PUBLISHED' && MODE !== 'live') {
try {
const art = await prisma.article.findUnique({ where: { id } })
const content = `${art?.title || id} | ${art?.summary || ''}`.slice(0, 400)
const ih = identityHash()
const r = await caycePool().query(
"INSERT INTO outbound_drafts (channel, type, content, identity_path, identity_hash, meta) VALUES ($1,$2,$3,$4,$5,$6) RETURNING id",
['hardcap', 'article_publish', content,
process.env.IDENTITY_PATH || '/home/ubuntu/cayce-ops/prompts/cayce-identity.md',
ih, JSON.stringify({ article_id: id })]
)
console.log(`[outbox] STAGED draft #${r.rows[0].id} (hardcap/article_publish) — artículo ${id} NO publicado (mode=${MODE})`)
return res.json({ staged: true, draft_id: String(r.rows[0].id), article_id: id })
} catch (e) {
// fail-closed: sin borrador, NO se publica. Sin volcar el comando ni el DSN al log.
console.error('[outbox] staging failed — PATCH descartado (fail-closed):', String((e && e.message) || e).slice(0, 120))
return res.status(500).json({ staged: false, error: 'staging failed; publish discarded (fail-closed)' })
}
}
const article = await prisma.article.update({
where: { id },
data: { status },
})
res.json(article)
})
// ── Sources ──────────────────────────────────────────────
// GET /admin/sources
router.get('/sources', async (_req, res) => {
const sources = await prisma.source.findMany({ orderBy: { name: 'asc' } })
res.json(sources)
})
// POST /admin/sources
router.post('/sources', async (req, res) => {
const { name, rssUrl, tier, topicBias } = req.body
const source = await prisma.source.create({
data: { name, rssUrl, tier: tier ?? 2, topicBias: topicBias ?? [] },
})
res.json(source)
})
// PATCH /admin/sources/:id — toggle active, update tier
router.patch('/sources/:id', async (req, res) => {
const { id } = req.params
const { active, tier, topicBias } = req.body
const source = await prisma.source.update({
where: { id },
data: {
...(active !== undefined ? { active } : {}),
...(tier !== undefined ? { tier } : {}),
...(topicBias !== undefined ? { topicBias } : {}),
},
})
res.json(source)
})
// ── Topic Filters ─────────────────────────────────────────
// GET /admin/topics
router.get('/topics', async (_req, res) => {
const topics = await prisma.topicFilter.findMany({ orderBy: { label: 'asc' } })
res.json(topics)
})
// PATCH /admin/topics/:id — toggle active, adjust weight
router.patch('/topics/:id', async (req, res) => {
const { id } = req.params
const { active, weight, keywordsInclude, keywordsExclude } = req.body
const topic = await prisma.topicFilter.update({
where: { id },
data: {
...(active !== undefined ? { active } : {}),
...(weight !== undefined ? { weight } : {}),
...(keywordsInclude !== undefined ? { keywordsInclude } : {}),
...(keywordsExclude !== undefined ? { keywordsExclude } : {}),
},
})
res.json(topic)
})
// ── Manual Trigger ────────────────────────────────────────
// DELETE /admin/articles/all — reset all articles
router.delete('/articles/all', async (_req, res) => {
const result = await prisma.article.deleteMany({})
res.json({ deleted: result.count })
})
// POST /admin/ingest — trigger ingestion manually
router.post('/ingest', async (_req, res) => {
res.json({ message: 'Ingestion started' })
// Fire and forget
ingestAllSources().catch(console.error)
})
export default router
+31
View File
@@ -0,0 +1,31 @@
// test-admin-auth — verifica el middleware de /admin (hardcap-backend).
// Ejecutar en la VPS: node /srv/hardcap-backend/runtime-tests/test-admin-auth.mjs
// (o node runtime/hardcap/test-admin-auth.mjs desde el repo)
// Requiere HARDCAP_ADMIN_TOKEN en el .env del servicio y el servicio activo en 127.0.0.1:3001.
import { readFileSync } from 'node:fs'
const BASE = 'http://127.0.0.1:3001'
const env = Object.fromEntries(
readFileSync('/srv/hardcap-backend/.env', 'utf8').split('\n').filter(l => l.includes('=')).map(l => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1)])
)
const TOKEN = env.HARDCAP_ADMIN_TOKEN
if (!TOKEN) { console.error('HARDCAP_ADMIN_TOKEN no configurado — el test no puede pasar'); process.exit(2) }
let failed = 0
async function check(name, path, opts, expected) {
const r = await fetch(BASE + path, { ...opts, signal: AbortSignal.timeout(8000) })
const ok = r.status === expected
if (!ok) failed++
console.log(`${ok ? '✓' : '✗'} ${name}: esperado ${expected}, obtenido ${r.status}`)
}
await check('sin token → 401', '/admin/articles?status=QUEUED', {}, 401)
await check('token incorrecto → 401', '/admin/articles?status=QUEUED', { headers: { Authorization: 'Bearer ' + 'x'.repeat(64) } }, 401)
await check('token vacío en header → 401', '/admin/sources', { headers: { Authorization: 'Bearer ' } }, 401)
await check('token correcto → 200', '/admin/articles?status=QUEUED', { headers: { Authorization: `Bearer ${TOKEN}` } }, 200)
await check('header alternativo X-Admin-Token → 200', '/admin/sources', { headers: { 'x-admin-token': TOKEN } }, 200)
// la comparación debe ser en tiempo constante: un token de la misma longitud pero distinto NO pasa
await check('token misma longitud, distinto → 401', '/admin/articles?status=QUEUED', { headers: { Authorization: 'Bearer ' + 'y'.repeat(TOKEN.length) } }, 401)
console.log(failed ? `\n${failed} TESTS FALLARON` : '\nTODOS LOS TESTS PASARON')
process.exit(failed ? 1 : 0)
+24 -1
View File
@@ -52,6 +52,13 @@ async function toolWebFetch({ url }) {
}
async function toolAddOpportunity(a) {
// directiva supervisor 2026-10-31: sin dedup el agente re-registraba la
// misma oportunidad (OpenSats ×10, HRF ×6). Ahora: si existe, se rechaza.
const dup = await pgc.query('SELECT id FROM opportunities WHERE lower(source_name) = lower($1) LIMIT 1', [a.source_name])
if (dup.rowCount) {
await log('yellow', 'opportunity_duplicate_rejected', { source_name: a.source_name, existing_id: dup.rows[0].id })
return { duplicate: true, existing_id: dup.rows[0].id, note: 'ya registrada — no se duplica; continuá con el resto del ciclo' }
}
await pgc.query(
`INSERT INTO opportunities (source_name, url, mechanic, fit, effort, expected, first_action)
VALUES ($1,$2,$3,$4,$5,$6,$7)`,
@@ -165,10 +172,21 @@ Una acción por respuesta. El sistema ejecuta y te devuelve el resultado. Si tu
// ── el loop
async function runCycle() {
const state = await stateSummary()
// directiva del supervisor (2026-10-31): grants suspendidos, BTCPay prioridad 1.
// No modifica el MANDATE (eso es 🔴); es una orden operativa reversible con
// CAYCE_DIRECTIVE_BTCPAY=0 en el .env.
const DIRECTIVE = process.env.CAYCE_DIRECTIVE_BTCPAY === '1' ? `
=== DIRECTIVA DEL SUPERVISOR (2026-10-31 — prevalece sobre §7b hasta nuevo aviso) ===
1. SUSPENDIDA la búsqueda y registro de grants y nuevas oportunidades. NO uses add_opportunity salvo para la excepción del punto 2.
2. PRIORIDAD 1 ÚNICA: la propuesta de BTCPay Server — oportunidad #106 "BTCPay Server Deployment & Content Service" y draft #33 "BTCPay Server Plugin: Event & Travel Integration". Trabajo esperado: consolidar UNA propuesta final lista para envío humano (alcance, arquitectura, presupuesto, plan de entrega) como draft nuevo marcado claramente. NO la envíes: la envía el operador.
3. La contención está activa: publish_note crea BORRADORES, no publica. Sigue contando como acción productiva.
` : ''
const messages = [
{
role: 'user',
content: `${MANDATE}
${DIRECTIVE}
=== TU ESTADO ACTUAL ===
${JSON.stringify(state, null, 2)}
@@ -261,7 +279,12 @@ Empieza ahora. Cuando hayas cumplido §7b, responde finish con un summary honest
const finalSummary = summary ?? 'max steps'
await log(s7b_ok ? 'green' : 'red', 'initiative_cycle',
{ steps, productive, repairs, trace, summary: finalSummary }, s7b_ok ? 'done' : 'error')
await dm(`INITIATIVE ${new Date().toISOString().slice(0, 10)} — ciclo ${s7b_ok ? 'cumplido' : 'falló'} · ${steps} pasos · ${productive} productivas · ${trace.map(t => t.action).join(' → ') || 'sin acciones'}\n\n${finalSummary}`)
const drafts24 = (await pgc.query(
`SELECT id, type, content FROM outbound_drafts WHERE ts > now() - interval '24 hours' ORDER BY id`
).catch(() => ({ rows: [] }))).rows
const draftLines = drafts24.map(d => `#${d.id} [${d.type}] ${String(d.content).replace(/\s+/g, ' ').split(' ').slice(0, 15).join(' ')}`)
const draftBlock = `\n\nBORRADORES contención (24h: ${draftLines.length}) — revisión y aprobación pendientes del operador:\n${draftLines.join('\n') || 'ninguno'}`
await dm(`INITIATIVE ${new Date().toISOString().slice(0, 10)} — ciclo ${s7b_ok ? 'cumplido' : 'falló'} · ${steps} pasos · ${productive} productivas · ${trace.map(t => t.action).join(' → ') || 'sin acciones'}\n\n${finalSummary}${draftBlock}`)
return { steps, productive, s7b_ok, summary: finalSummary, trace }
}
+4 -1
View File
@@ -13,6 +13,9 @@
import { readFileSync } from 'node:fs'
import { createHash } from 'node:crypto'
import { createRequire } from 'node:module'
const require_ = createRequire(import.meta.url)
const IDENTITY_PATH = process.env.IDENTITY_PATH || '/home/ubuntu/cayce-ops/prompts/cayce-identity.md'
@@ -67,7 +70,7 @@ let ownPool = null
async function poolFor(poolPg) {
if (poolPg) return poolPg
if (!ownPool) {
const pg = (await import('pg')).default
const pg = require_('/srv/cayce-core/node_modules/pg')
const env = Object.fromEntries(
readFileSync('/srv/cayce-core/.env', 'utf8').split('\n').filter(l => l.includes('=')).map(l => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1)])
)
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/node
// publish-10050 — publica el kind 10050 (NIP-65 memento: relays de inbox
// para DMs/gift wraps) de Cayce. Autorizado explícitamente por el operador.
// Se ejecuta UNA vez (script, no daemon).
import { readFileSync } from 'node:fs'
import { SimplePool, finalizeEvent, getPublicKey } from 'nostr-tools'
const env = Object.fromEntries(
readFileSync('/srv/cayce-nostr/.env', 'utf8').split('\n').filter(l => l.includes('=')).map(l => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1)])
)
const skRaw = env.NSEC_HEX
const { nsecDeserialize } = await import('nostr-tools') // soporta hex y bech32 vía fallback
let sk
try { sk = skRaw.startsWith('nsec1') ? nsecDeserialize(skRaw) : Buffer.from(skRaw, 'hex') } catch (e) {
console.error('nsec ilegible:', String(e).slice(0, 80)); process.exit(1)
}
const pk = getPublicKey(sk)
// relays donde Cayce ESCUCHA DMs (los mismos del listener) — son su inbox
const INBOX = [
'wss://relay.damus.io',
'wss://nos.lol',
'wss://relay.primal.net',
'wss://relay.snort.social',
'wss://nostr-pub.wellorder.net',
'wss://public.relaying.io',
]
const ev = finalizeEvent({
kind: 10050,
created_at: Math.floor(Date.now() / 1000),
tags: INBOX.map(r => ['relay', r]),
content: '',
}, sk)
console.log('[10050] pubkey:', pk)
console.log('[10050] relays de inbox declarados:', INBOX.join(', '))
const pool = new SimplePool()
const ok = []
for (const r of INBOX) {
try {
await Promise.race([
pool.publish([r], ev),
new Promise((_, rej) => setTimeout(() => rej(new Error('timeout')), 6000)),
])
ok.push(r)
} catch (e) { console.error(`[10050] ${r}: ${String(e).slice(0, 60)}`) }
}
pool.close(INBOX)
console.log(`[10050] publicado/aceptado en ${ok.length}/${INBOX.length} relays — event id ${ev.id}`)
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# check-credential-leaks — falla si journald reciente contiene patrones de credencial.
# Regla permanente: los secretos jamás en logs. Ejecutar manualmente o desde cron
# (sin timer propio: no fue autorizado).
set -euo pipefail
SINCE="${1:-2 hours ago}"
HITS=$(journalctl --since "$SINCE" --no-pager 2>/dev/null | grep -cE "(postgres|postgresql)://[A-Za-z0-9_]+:[^@[:space:]]{8,}@|nsec1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{50,}" || true)
if [ "$HITS" -gt 0 ]; then
echo "ALERTA: $HITS líneas de journald (desde $SINCE) contienen patrones de credencial" >&2
journalctl --since "$SINCE" --no-pager 2>/dev/null | grep -nE "(postgres|postgresql)://[A-Za-z0-9_]+:[^@[:space:]]{8,}@|nsec1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{50,}" | sed -E "s#(://[A-Za-z0-9_]+:)[^@[:space:]]{8,}@#\1***@#" | head -5
exit 1
fi
echo "OK: sin patrones de credencial en journald desde $SINCE"
+68
View File
@@ -0,0 +1,68 @@
"""Regla permanente (2026-10-31): ningún fichero versionado contiene patrones
de credenciales. Placeholders de .env.example están permitidos.
Fallará si alguien mete un DSN con contraseña real, un token en claro, o
cualquier valor que deba vivir solo en .env / tokens.json.
"""
from __future__ import annotations
import re
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
SCAN_DIRS = ["runtime", "deploy", "docs", "scripts", "prompts"]
SCAN_GLOBS = ["*.py", "*.mjs", "*.js", "*.ts", "*.md", "*.yml", "*.yaml", "*.sh", "*.sql"]
# DSN con credencial literal (user:pass@host) — excluye placeholders conocidos
DSN = re.compile(r"(postgres|postgresql|mysql|mongodb)(\+[a-z]+)?://[A-Za-z0-9_]+:(?!password\b|change-me\b|<[^>]+>)[^@\s/]{8,}@", re.I)
# tokens hex largos fuera de tests (los fixtures de tests están allowlistados por diseño)
HEXTOKEN = re.compile(r"\b[0-9a-f]{64}\b")
# nsec bech32
NSEC = re.compile(r"nsec1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{50,}")
ALLOW_SUBSTR = ("user:password", "change-me", "<", "EXAMPLE", "example", "itest-pg", "0011", "11*", "22*", "33*", "44*")
def _iter_files():
for d in SCAN_DIRS:
base = REPO / d
if not base.exists():
continue
for pat in SCAN_GLOBS:
for f in base.rglob(pat):
if ".git" in f.parts or "node_modules" in f.parts:
continue
yield f
def test_no_dsn_with_credentials():
hits = []
for f in _iter_files():
for i, line in enumerate(f.read_text(errors="ignore").splitlines(), 1):
if DSN.search(line) and not any(a in line for a in ALLOW_SUBSTR):
hits.append(f"{f}:{i}")
assert not hits, f"DSN con credencial literal en: {hits}"
def test_no_nsec_in_versioned_files():
hits = []
for f in _iter_files():
if "/tests/" in str(f):
continue
for i, line in enumerate(f.read_text(errors="ignore").splitlines(), 1):
if NSEC.search(line):
hits.append(f"{f}:{i}")
assert not hits, f"nsec en fichero versionado: {hits}"
def test_no_raw_hex_tokens_outside_tests():
"""Tokens hex de 64 chars fuera de tests/ y del conftest (las claves de
test viven solo en tests/). El outbox no debe contener secretos."""
hits = []
for f in _iter_files():
if "/tests/" in str(f) or f.name == "cayce-identity.md":
continue
for i, line in enumerate(f.read_text(errors="ignore").splitlines(), 1):
if HEXTOKEN.search(line):
hits.append(f"{f}:{i}")
assert not hits, f"posible token hex versionado: {hits}"