Security fixes: swarm signature verification and error handling
This commit is contained in:
@@ -2,12 +2,15 @@
|
||||
* Swarm Announce Endpoint
|
||||
*
|
||||
* POST: Receive announcements from other nodes joining the swarm
|
||||
*
|
||||
* SECURITY: All requests must be cryptographically signed by the sender node.
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { upsertSwarmNode } from '@/lib/swarm/registry';
|
||||
import { buildAnnouncement } from '@/lib/swarm/discovery';
|
||||
import { verifySwarmRequest } from '@/lib/swarm/signature';
|
||||
import type { SwarmNodeInfo } from '@/lib/swarm/types';
|
||||
|
||||
const announcementSchema = z.object({
|
||||
@@ -21,7 +24,11 @@ const announcementSchema = z.object({
|
||||
postCount: z.number().optional(),
|
||||
capabilities: z.array(z.enum(['handles', 'gossip', 'relay', 'search', 'interactions'])).optional(),
|
||||
timestamp: z.string().optional(),
|
||||
signature: z.string().optional(),
|
||||
});
|
||||
|
||||
// Schema including signature for verification
|
||||
const signedAnnouncementSchema = announcementSchema.extend({
|
||||
signature: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -29,39 +36,53 @@ const announcementSchema = z.object({
|
||||
*
|
||||
* Receives an announcement from another node and responds with our info.
|
||||
* This is how nodes introduce themselves to the swarm.
|
||||
*
|
||||
* SECURITY: All announcement requests must be signed by the sender node.
|
||||
*/
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const announcement = announcementSchema.parse(body);
|
||||
const data = signedAnnouncementSchema.parse(body);
|
||||
|
||||
const ourDomain = process.env.NEXT_PUBLIC_NODE_DOMAIN;
|
||||
|
||||
// Don't process announcements from ourselves
|
||||
if (announcement.domain === ourDomain) {
|
||||
if (data.domain === ourDomain) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Cannot announce to self' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// SECURITY: Verify the node signature before processing
|
||||
const { signature, ...payload } = data;
|
||||
const isValid = await verifySwarmRequest(payload, signature, data.domain);
|
||||
|
||||
if (!isValid) {
|
||||
console.warn(`[Swarm] Invalid signature for announcement from ${data.domain}`);
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid signature' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// Add/update the announcing node in our registry
|
||||
const nodeInfo: SwarmNodeInfo = {
|
||||
domain: announcement.domain,
|
||||
name: announcement.name,
|
||||
description: announcement.description,
|
||||
logoUrl: announcement.logoUrl,
|
||||
publicKey: announcement.publicKey,
|
||||
softwareVersion: announcement.softwareVersion,
|
||||
userCount: announcement.userCount,
|
||||
postCount: announcement.postCount,
|
||||
capabilities: announcement.capabilities,
|
||||
domain: data.domain,
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
logoUrl: data.logoUrl,
|
||||
publicKey: data.publicKey,
|
||||
softwareVersion: data.softwareVersion,
|
||||
userCount: data.userCount,
|
||||
postCount: data.postCount,
|
||||
capabilities: data.capabilities,
|
||||
lastSeenAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const { isNew } = await upsertSwarmNode(nodeInfo, 'announcement');
|
||||
|
||||
console.log(`[Swarm] ${isNew ? 'New' : 'Known'} node announced: ${announcement.domain}`);
|
||||
console.log(`[Swarm] ${isNew ? 'New' : 'Known'} node announced: ${data.domain}`);
|
||||
|
||||
// Respond with our own info
|
||||
const ourAnnouncement = await buildAnnouncement();
|
||||
|
||||
@@ -54,6 +54,7 @@ export async function GET(request: NextRequest) {
|
||||
handle: participant2Handle,
|
||||
displayName: participant2Handle,
|
||||
avatarUrl: null as string | null,
|
||||
did: '' as string,
|
||||
};
|
||||
|
||||
// Try to get cached user info
|
||||
@@ -103,6 +104,7 @@ export async function GET(request: NextRequest) {
|
||||
handle: cachedUser.handle,
|
||||
displayName: (cachedUser as any).displayName || cachedUser.handle,
|
||||
avatarUrl: (cachedUser as any).avatarUrl || null,
|
||||
did: (cachedUser as any).did || '',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
* Swarm Gossip Endpoint
|
||||
*
|
||||
* POST: Exchange node and handle information with other nodes
|
||||
*
|
||||
* SECURITY: All requests must be cryptographically signed by the sender node.
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { processGossip } from '@/lib/swarm/gossip';
|
||||
import { markNodeSuccess } from '@/lib/swarm/registry';
|
||||
import { verifySwarmRequest } from '@/lib/swarm/signature';
|
||||
import type { SwarmGossipPayload } from '@/lib/swarm/types';
|
||||
|
||||
const handleSchema = z.object({
|
||||
@@ -38,36 +41,55 @@ const gossipPayloadSchema = z.object({
|
||||
since: z.string().optional(),
|
||||
});
|
||||
|
||||
// Schema including signature for verification
|
||||
const signedGossipSchema = gossipPayloadSchema.extend({
|
||||
signature: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/swarm/gossip
|
||||
*
|
||||
* Receives gossip from another node and responds with our own data.
|
||||
* This is the core of the epidemic protocol - nodes exchange what they know.
|
||||
*
|
||||
* SECURITY: All gossip requests must be signed by the sender node.
|
||||
*/
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const payload = gossipPayloadSchema.parse(body) as SwarmGossipPayload;
|
||||
const data = signedGossipSchema.parse(body);
|
||||
|
||||
const ourDomain = process.env.NEXT_PUBLIC_NODE_DOMAIN;
|
||||
|
||||
// Don't process gossip from ourselves
|
||||
if (payload.sender === ourDomain) {
|
||||
if (data.sender === ourDomain) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Cannot gossip with self' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`[Swarm] Gossip from ${payload.sender}: ${payload.nodes.length} nodes, ${payload.handles?.length || 0} handles`);
|
||||
// SECURITY: Verify the node signature before processing
|
||||
const { signature, ...payload } = data;
|
||||
const isValid = await verifySwarmRequest(payload, signature, data.sender);
|
||||
|
||||
if (!isValid) {
|
||||
console.warn(`[Swarm] Invalid signature for gossip from ${data.sender}`);
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid signature' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`[Swarm] Gossip from ${data.sender}: ${data.nodes.length} nodes, ${data.handles?.length || 0} handles`);
|
||||
|
||||
// Process the incoming gossip and build our response
|
||||
const response = await processGossip(payload);
|
||||
const response = await processGossip(payload as SwarmGossipPayload);
|
||||
|
||||
// Mark the sender as successfully contacted
|
||||
await markNodeSuccess(payload.sender);
|
||||
await markNodeSuccess(data.sender);
|
||||
|
||||
console.log(`[Swarm] Gossip response to ${payload.sender}: ${response.nodes.length} nodes, ${response.handles?.length || 0} handles`);
|
||||
console.log(`[Swarm] Gossip response to ${data.sender}: ${response.nodes.length} nodes, ${response.handles?.length || 0} handles`);
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user