import {test} from 'node:test'; import assert from 'node:assert/strict'; import handler from '../api/connect.js'; import { hash, sealTicket } from '../server/connect-crypto.mjs'; function response(){return{headers:{},code:0,body:null,setHeader(name,value){this.headers[name]=value},status(value){this.code=value;return this},json(value){this.body=value;return this},send(value){this.body=value;return this},redirect(code,value){this.code=code;this.headers.Location=value;return this}};} test('HTTP config is public, uncached and exposes no server environment values',async()=>{ const res=response();await handler({method:'GET',url:'/api/desktop/connect/config',query:{action:'config'},headers:{}},res); assert.equal(res.code,200);assert.equal(res.headers['Cache-Control'],'no-store');assert.equal(res.headers['Referrer-Policy'],'no-referrer');assert.equal(res.body.version,1);assert.equal('CONNECT_SECRET' in res.body,false); }); test('HTTP endpoints reject malformed actions, parameters and refresh bodies without reflecting input',async()=>{ for(const req of [ {method:'POST',query:{action:'config'},headers:{}}, {method:'GET',query:{action:'start',provider:['x','instagram']},headers:{}}, {method:'POST',query:{action:'refresh'},headers:{'content-type':'text/plain'},body:'untrusted'}, {method:'POST',query:{action:'refresh'},headers:{'content-type':'application/json','content-length':'24001'},body:{}}, {method:'POST',query:{action:'refresh'},headers:{'content-type':'application/json'},body:'bad json containing a secret'} ]){const res=response();await handler({url:'/api/desktop/connect/refresh',...req},res);assert.ok([400,405].includes(res.code));assert.ok(!JSON.stringify(res.body).includes('secret'));} }); test('refreshing a ticket from before a signing key change tells the user to reconnect', async () => { const refreshToken = 'fixture-refresh-token'; const clientId = 'fixture-client-id'; const ticket = await sealTicket({ provider: 'linkedin', clientId, tokenHash: hash(refreshToken), expiresAt: Date.now() + 60_000, }, 'a'.repeat(64), 'refresh'); const res = response(); await handler({ method: 'POST', url: '/api/desktop/connect/refresh', query: { action: 'refresh' }, headers: { 'content-type': 'application/json' }, body: { provider: 'linkedin', refreshToken, ticket }, }, res, { env: { CONNECT_SECRET: 'b'.repeat(64), LINKEDIN_CLIENT_ID: clientId, LINKEDIN_CLIENT_SECRET: 'fixture-client-secret' } }); assert.equal(res.code, 401); assert.match(res.body.error, /Open Connections in InkSpring and reconnect/); assert.ok(!res.body.error.includes(refreshToken)); assert.ok(!res.body.error.includes(ticket)); }); test('deprecated OAuth actions return a stable 410 without reflecting token values', async () => { for (const provider of ['reddit', 'tiktok', 'substack', 'youtube']) { for (const action of ['start', 'callback', 'refresh']) { const req={url:'/api/connect',query:{action,provider},method:action==='refresh'?'POST':'GET',headers:{'content-type':'application/json'},body:{provider,refreshToken:'fixture-old-secret'}}; const res=response();await handler(req,res); assert.equal(res.code,410);assert.equal(res.headers['Cache-Control'],'no-store'); assert.doesNotMatch(JSON.stringify(res.body),/fixture-old-secret/); } } });