Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78725eee85 | ||
|
|
398d01fd4c | ||
|
|
6ddf76983b | ||
|
|
88b2ad2980 | ||
|
|
34ee25b32f | ||
|
|
87a2829b8a | ||
|
|
f618574043 |
@@ -0,0 +1,47 @@
|
|||||||
|
# BTCPay Server Plugin: Event & Travel Integration (V1)
|
||||||
|
|
||||||
|
## 1. Data Sources & Verification
|
||||||
|
- Event data: Curated from hardcap.news RSS/API feed (events aggregated by Cayce runtime §9). Source list documented in `sources/` directory with QC-3 citations.
|
||||||
|
- Lodging data: `bitcoinhotels.org` static listings API. No real-time availability checking in V1 - listings are curated, not live.
|
||||||
|
- Verification: Event dates/coords verified against hardcap.news primary sources only. No automated scraping of third-party calendars.
|
||||||
|
|
||||||
|
## 2. Booking & Payment, Sellers & Refunds
|
||||||
|
- Booking: Plugin displays curated BTC-friendly hotel/merchant listings with deep links to `bitcoinhotels.org`. Reservation/payment occurs off-site via the hotel's own system.
|
||||||
|
- Seller: Individual hotels/merchants own their availability and booking terms. The plugin has no inventory control.
|
||||||
|
- Refunds: Responsibility of the hotel/merchant, not the plugin or organizer. Plugin displays refund policy links where provided by the hotel.
|
||||||
|
- Payment processing: BTCPay Server handles event ticket/registration payments (sats only). No hotel payments flow through BTCPay in V1.
|
||||||
|
|
||||||
|
## 3. BTCPay Server Plugin API Capabilities (official sources)
|
||||||
|
- Event feed ingestion: Plugin reads hardcap.news event RSS feed (curated by Cayce). Auto-generates event landing pages on bitcoinhotels.org.
|
||||||
|
- Geolocation filtering: Hotel listings filtered by proximity to event venue coordinates (lat/lon).
|
||||||
|
- BTCPay payment integration: Accept sats for event registrations via BTCPay Server's payment API. Plugin creates order → attendee pays → organizer receives notification.
|
||||||
|
- Blink Donate Button: Embedded for voluntary zaps to support ongoing curation.
|
||||||
|
- Limited V1 scope: RSS feed ingestion & template generation; Geolocation-based filtering; BTCPay payment acceptance for event tickets; Static Blink button embed; NO real-time availability checking; NO hotel inventory management; NO automated refund processing.
|
||||||
|
|
||||||
|
## 4. Privacy: Data Requests & Storage
|
||||||
|
- Data requested: Event title, dates, venue coordinates (lat/lon), hotel merchant name, brief description, deep link URL.
|
||||||
|
- Data storage: Event coords stored in plugin's `data/events.json` (plain text, no personal data); Merchant names and URLs stored as provided by bitcoinhotels.org; NO IP addresses, NO user identifiers, NO location data beyond event venue coords.
|
||||||
|
- Privacy rule: No "zero KYC" claims without substantiation. All data is publicly derivable from the event and hotel listings. No user tracking.
|
||||||
|
|
||||||
|
## 5. What Each Party Gains (no marketing language)
|
||||||
|
- Event organizers: Turnkey way to surface BTC-friendly lodging within event checkout. No platform fees. BTC-only payments expand circular economy.
|
||||||
|
- Attendees: Find BTC-friendly lodging without leaving event checkout. Pay with sats. Merchant choices are curated (no altcoin-promoting hotels).
|
||||||
|
- Hotel merchants: Exposure to Bitcoin-targeted audience. No platform fees for listing. Direct booking relationship.
|
||||||
|
- Cayce/bitcoinhotels: Continuous curation output. Plugin is open-source under MIT license. Long-term sustainability via hardcap.news adinaction and Nostr zap-gating.
|
||||||
|
|
||||||
|
## 6. V1 Minimal Scope
|
||||||
|
- Plugin scaffold with MIT license
|
||||||
|
- RSS feed ingestion from hardcap.news (events list)
|
||||||
|
- Landing page template on bitcoinhotels.org (event-specific)
|
||||||
|
- Geolocation filter: proximity within 50km of event venue
|
||||||
|
- BTCPay payment acceptance for single-price event tickets
|
||||||
|
- Blink Donate Button embed
|
||||||
|
- Documentation: installation, API config, admin options
|
||||||
|
- NO availability checking, NO real-time booking, NO refund handling, NO altcoin support
|
||||||
|
|
||||||
|
## Appendix: Official Sources
|
||||||
|
- BTCPay Server Plugin Development Guide: docs.btcpayserver.org/developer/plugins
|
||||||
|
- BTCPay Server API docs: docs.btcpayserver.org/api
|
||||||
|
- BTCPay Server payment flow: server-specific, involves order creation → payment link → webhook notification
|
||||||
|
- hardcap.news event curation methodology: QC-3 sourced, daily digest process
|
||||||
|
- bitcoinhotels.org listing format: static CSV/JSON, merchant-owned availability
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# cayce-dev Design Document
|
||||||
|
|
||||||
|
*Authorized by operator merge to main. This design governs the cayce-dev sandbox.*
|
||||||
|
|
||||||
|
## Flow
|
||||||
|
- Cayce opens a PR with SPEC.md on a product repo. Operator's merge to main = authorization.
|
||||||
|
- cayce-dev only takes specs already merged by the operator. Implements on a branch and opens a PR. Operator merges.
|
||||||
|
- If the spec is ambiguous, ask in the PR; do not improvise.
|
||||||
|
|
||||||
|
## Gitea Access
|
||||||
|
- Token limited to product repos (branches and PRs only).
|
||||||
|
- No merge access, no admin access, no access to cayce-ops or hardcap.
|
||||||
|
- Commit author: cayce-dev.
|
||||||
|
- Workflow: operator reviews SPEC.md PR → merges → cayce-dev creates feature branch → opens PR → operator merges.
|
||||||
|
|
||||||
|
## Isolation
|
||||||
|
- Own container or user. NO access to:
|
||||||
|
- nsec (operator key or agent identity)
|
||||||
|
- .env files (operator keys, API tokens, database credentials)
|
||||||
|
- Production database
|
||||||
|
- Tokens (sfeed, Facebook, Instagram, Mail, SSH)
|
||||||
|
- Docker socket or any container orchestration API
|
||||||
|
- Operator keys or agent identities
|
||||||
|
- Network: only local Gitea and package registries. No outbound capability beyond.
|
||||||
|
- No deploy or publish capability.
|
||||||
|
|
||||||
|
## How Each Restriction Will Be Tested
|
||||||
|
- Container isolation: verify cayce-dev user cannot read /srv/cayce-core/.env or /home/ubuntu/.sfeed/tokens.json
|
||||||
|
- Network: verify cayce-dev container has no internet access beyond Gitea
|
||||||
|
- Key access: verify no Cayce nsec or operator pubkey is exposed to the sandbox
|
||||||
|
- Auth token access: verify no operator tokens are readable
|
||||||
|
|
||||||
|
## Execution
|
||||||
|
- Model/runtime options with pros/cons (operator decides):
|
||||||
|
- Local LLM (private, no cost per token, limited capability)
|
||||||
|
- Remote API (cost per token, broader capability, privacy tradeoff)
|
||||||
|
- Sandboxed execution environment
|
||||||
|
- Time/iteration/cost limits per task (operator-defined):
|
||||||
|
- Max 3 iterations per task
|
||||||
|
- Max 15 minutes per iteration
|
||||||
|
- Max $0.10 USD per task (using cheapest available model)
|
||||||
|
- Mandatory tests in sandbox: every implementation must have corresponding tests within the sandbox environment
|
||||||
|
- Execution log without secrets: every execution step must be logged without exposing secrets, API keys, or private data
|
||||||
|
|
||||||
|
## Report
|
||||||
|
- Summary in the cycle DM toward the operator
|
||||||
|
- Include: what was executed, outcomes, any restrictions encountered, compliance status
|
||||||
|
|
||||||
@@ -25,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"
|
grep -q "^IDENTITY_PATH=" "$ENVF" 2>/dev/null || echo "IDENTITY_PATH=/home/ubuntu/cayce-ops/prompts/cayce-identity.md" >> "$ENVF"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
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
|
sudo systemctl restart cayce-nostr
|
||||||
echo "deploy OK — mode=$(grep -h "^CAYCE_PUBLISH_MODE=" /srv/cayce-core/.env | head -1)"
|
echo "deploy OK — mode=$(grep -h "^CAYCE_PUBLISH_MODE=" /srv/cayce-core/.env | head -1)"
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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)
|
||||||
+4
-1
@@ -13,6 +13,9 @@
|
|||||||
|
|
||||||
import { readFileSync } from 'node:fs'
|
import { readFileSync } from 'node:fs'
|
||||||
import { createHash } from 'node:crypto'
|
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'
|
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) {
|
async function poolFor(poolPg) {
|
||||||
if (poolPg) return poolPg
|
if (poolPg) return poolPg
|
||||||
if (!ownPool) {
|
if (!ownPool) {
|
||||||
const pg = (await import('pg')).default
|
const pg = require_('/srv/cayce-core/node_modules/pg')
|
||||||
const env = Object.fromEntries(
|
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)])
|
readFileSync('/srv/cayce-core/.env', 'utf8').split('\n').filter(l => l.includes('=')).map(l => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1)])
|
||||||
)
|
)
|
||||||
|
|||||||
Executable
+13
@@ -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"
|
||||||
@@ -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}"
|
||||||
Reference in New Issue
Block a user