/* The two podcast routes, against the real server: who may call them, and which feed URLs this process will agree to fetch. The feed rule is the one that matters, and it matters more here than in any other route: a podcast feed can live on any host in the world, so there is no allowlist to fall back on — only the rule in `safeFeedUrl`. This container runs inside somebody's house, and the URL arrives from a browser, so `http://192.168.1.1/…` is not a bad feed, it is a request this process must never make. */ import { test } from 'node:test'; import assert from 'node:assert/strict'; import crypto from 'node:crypto'; import net from 'node:net'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { spawn } from 'node:child_process'; import { fileURLToPath } from 'node:url'; const API = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); const SECRET = crypto.randomBytes(32).toString('hex'); function mintSession(uid, sv = 0) { const payload = `${uid}:${Date.now() + 86400000}:${sv}`; return payload + '.' + crypto.createHmac('sha256', SECRET).update(payload).digest('base64url'); } const cookie = uid => ({ Cookie: `gymsid=${mintSession(uid)}` }); const freePort = () => new Promise(r => { const s = net.createServer(); s.listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => r(p)); }); }); async function startServer(t) { const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gym-podcasts-')); fs.writeFileSync(path.join(dataDir, 'secret'), SECRET, { mode: 0o600 }); fs.writeFileSync(path.join(dataDir, 'db.json'), JSON.stringify({ users: [{ id: 'u_p', name: 'One', created: '2026-01-01T00:00:00.000Z' }], creds: [], subs: [], invites: [] })); const port = await freePort(); const child = spawn(process.execPath, ['server.js'], { cwd: API, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, PORT: String(port), DATA_DIR: dataDir, ORIGIN: 'http://localhost:8080', RP_ID: 'localhost' } }); const h = { api: `http://127.0.0.1:${port}`, log: '' }; child.stdout.on('data', d => h.log += d); child.stderr.on('data', d => h.log += d); t.after(() => { child.kill('SIGKILL'); fs.rmSync(dataDir, { recursive: true, force: true }); }); let up = false; for (let i = 0; i < 100 && !up; i++) { try { up = (await fetch(`${h.api}/api/health`)).ok } catch { /* not up yet */ } if (!up) await new Promise(r => setTimeout(r, 100)); } assert.ok(up, `server never came up:\n${h.log}`); return h; } test('the podcast list needs a session', async t => { const h = await startServer(t); const r = await fetch(`${h.api}/api/podcasts`); assert.equal(r.status, 401); }); test('so does a show', async t => { const h = await startServer(t); const r = await fetch(`${h.api}/api/podcasts/show?feed=${encodeURIComponent('https://feeds.example.com/x.rss')}`); assert.equal(r.status, 401); }); test('a feed URL that is not public https is refused before any request is made', async t => { const h = await startServer(t); for (const feed of [ 'http://169.254.169.254/latest/meta-data/', // the cloud metadata address 'http://127.0.0.1:3000/api/health', 'https://127.0.0.1/x.rss', 'https://192.168.1.1/x.rss', 'https://10.0.0.5/x.rss', 'https://router.local/x.rss', 'https://[::1]/x.rss', 'http://feeds.example.com/x.rss', // plain http, even on a real host 'https://user:pw@feeds.example.com/x.rss', 'file:///etc/passwd', 'not a url', '' ]) { const r = await fetch(`${h.api}/api/podcasts/show?feed=${encodeURIComponent(feed)}`, { headers: cookie('u_p') }); assert.equal(r.status, 400, `${feed} is refused`); assert.match((await r.json()).error, /feed this server will fetch/); } }); test('the instance says it supports podcasts', async t => { const h = await startServer(t); const cfg = await (await fetch(`${h.api}/api/config`)).json(); assert.ok(cfg.features.includes('podcasts'), 'features: ' + cfg.features.join(',')); });