Changeset 6d5ce0c in Klonkt for src/services/ActivityPubService.js
- Timestamp:
- 07/29/2026 10:50:32 AM (6 weeks ago)
- Branches:
- main
- Children:
- 08ab8ad
- Parents:
- c8e03c6
- File:
-
- 1 edited
-
src/services/ActivityPubService.js (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/services/ActivityPubService.js
rc8e03c6 r6d5ce0c 1345 1345 1346 1346 // Handle an incoming inbox POST. slugParam = null for the shared /ap/inbox. 1347 export async function handleInbox(req, slugParam ) {1347 export async function handleInbox(req, slugParam, preVerified = null) { 1348 1348 const act = req.body || {}; 1349 1349 const type = act.type; … … 1352 1352 const ip = req.ip || (req.connection && req.connection.remoteAddress) || '?'; 1353 1353 const base = (process.env.PUBLIC_BASE_URL || `${req.protocol}://${req.get('host')}`).replace(/\/+$/, ''); 1354 const verified = await verifyRequest(req).catch(() => null); 1354 // preVerified is the loopback (see deliverToActor): a delivery between two 1355 // actors on THIS instance never crosses a socket, so there is no signature to 1356 // check — but we do know who signed, because we signed it. Handing that in 1357 // keeps everything below identical, including the actor-versus-signer check, 1358 // which is exactly the check that must not be skipped for being local. 1359 const verified = preVerified || await verifyRequest(req).catch(() => null); 1355 1360 1356 1361 // ENFORCE HTTP signatures: a data-affecting activity must be signed by the very … … 1534 1539 const actorUri = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id); 1535 1540 const resolveActor = async (uri) => ((verified && verified.id === uri) ? verified : await fetchActor(uri).catch(() => null)); 1536 // Activities from our OWN actors are already stored via ap_outbox — don't re-store. 1537 const isLocalActor = !!(base && actorUri && actorUri.startsWith(`${base}/ap/users/`)); 1541 // Our OWN activity is already stored via ap_outbox: don't store it twice. 1542 // "Our own" means THIS inbox's owner, not "anyone who happens to live on this 1543 // machine". The old reading dropped every activity between two sites on one 1544 // instance, so a note from a co-located guardian to its ward was accepted 1545 // with a 202 and then quietly thrown away: no mention, no away, no help 1546 // request. Neighbours are not us (Robins regel, 29-7: on this machine 1547 // everything behaves as if every Klonkt were somewhere else). 1548 const isLocalActor = !!(actorUri && slugParam && actorUri === actorId(base, slugParam)); 1538 1549 1539 1550 // Inbound reply: a Create whose object replies to one of our notes (post OR comment). … … 2253 2264 awayUntil = Guardianship.availability.parseEndTime(object.endTime); 2254 2265 if (!awayUntil || awayUntil <= Date.now()) return { status: 400, error: 'away_needs_an_end' }; 2255 // A ward we host ourselves never receives its own delivery 2256 // (private ranges, loopback): apply locally, the way the 2257 // handshake commit does. 2258 const meUri = selfActorId(site.slug); 2259 for (const uri of recipients) { 2260 const wslug = uri.startsWith(`${base}/`) ? slugFromActorUrl(uri) : null; 2261 if (wslug && Guardianship.listGuardians(wslug).some((g) => g.other_uri === meUri)) { 2262 Guardianship.availability.declareAway(wslug, meUri, awayUntil); 2263 } 2264 } 2266 // No local shortcut here: the note below reaches a ward on this 2267 // instance through the loopback, and its inbox handler applies the 2268 // absence like it does for a ward anywhere else. One path. 2265 2269 } 2266 2270 const r = await deliverDirectNote(site, { recipients, text: plain, language: object.language || null, inReplyTo: typeof object.inReplyTo === 'string' ? object.inReplyTo : null, attachments: atts, helpRequest: help, awayUntil }); … … 3807 3811 const keys = getOrCreateKeys(site.slug); 3808 3812 const payload = { '@context': AP_CONTEXT, ...activity }; 3813 // Co-location is a TRANSPORT detail, never a decision path (Robins regel, 3814 // 29-7). An inbox on this machine is not reachable over HTTP from this 3815 // machine, and should not be, so a local recipient is handed the activity 3816 // straight into the same inbox handler the wire would reach. Everything 3817 // above this line therefore behaves as if every Klonkt were remote: one code 3818 // path, exercised by every deployment, including the checks. Two bugs in one 3819 // day came from having a second, local-only path that hid a broken remote 3820 // one. 3821 const localSlug = localSlugOf(actorUri); 3822 if (localSlug && db.prepare('SELECT 1 FROM sites WHERE slug = ?').get(localSlug)) { 3823 const host = (() => { try { return new URL(selfActorId(site.slug)).host; } catch { return ''; } })(); 3824 const req = { body: payload, ip: 'loopback', protocol: 'https', get: () => host, headers: {} }; 3825 // The signer is us, and we say so: the actor-versus-signer check runs 3826 // exactly as it does over the wire, so a mismatch fails here too. 3827 const status = await handleInbox(req, localSlug, { id: me }).catch(() => 500); 3828 const ok = status >= 200 && status < 300; 3829 console.log('[AP]', activity.type, ok ? 'delivered (loopback) →' : `got ${status} (loopback) from`, actorUri); 3830 return { delivered: ok, inbox: `${actorUri}/inbox`, loopback: true, status }; 3831 } 3809 3832 const a = await fetchActor(actorUri).catch(() => null); 3810 3833 const inbox = a && (a.inbox || (a.endpoints && a.endpoints.sharedInbox)); … … 3822 3845 } 3823 3846 Guardianship.wireDelivery({ 3824 actorId, fetchActor, deriveHandle, escHtml, linkUrls, linkHashtags,3847 actorId, fetchActor, localActor, deliverTo: deliverToActor, deriveHandle, escHtml, linkUrls, linkHashtags, 3825 3848 getOutboxRow: (id) => iStmts().getO.get(id), 3826 3849 buildReplyNote, AP_CONTEXT, getOrCreateKeys, deliver, enqueueDelivery, 3827 3850 }); 3851 /** 3852 * The actor document of a site WE host, read straight from the database. 3853 * Same shape fetchActor returns for anyone else, plus `local: true` so the 3854 * caller can take the loopback instead of a POST to our own hostname. 3855 * Null for an actor we do not host: that one really is fetched. 3856 */ 3857 function localActor(actorUri) { 3858 const slug = localSlugOf(actorUri); 3859 if (!slug) return null; 3860 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); 3861 const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug); 3862 if (!site) return null; 3863 // primary_slug is what buildActor uses to pick '/' over '/user/<slug>'; the 3864 // actor route sets it the same way before building. 3865 const p = db.prepare('SELECT slug FROM sites WHERE is_primary = 1').get(); 3866 try { return { ...buildActor(base, { ...site, primary_slug: p && p.slug }), local: true }; } catch { return null; } 3867 } 3828 3868 // Which local site (if any) hosts this actor URI — used by the handshake to 3829 3869 // apply the local side of a commit and to derive a ward's existing guardians.
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)