Fully rewrite relay

This commit is contained in:
Danny Morabito 2025-06-04 12:43:23 +02:00
parent 190e38dfc1
commit 20ffbd4c6d
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 7CC8056A5A04557E
47 changed files with 3489 additions and 128 deletions

View file

@ -0,0 +1,4 @@
ALTER TABLE events
ADD COLUMN replaced INTEGER NOT NULL DEFAULT 0;
ALTER TABLE events
ADD COLUMN deleted INTEGER NOT NULL DEFAULT 0;

View file

@ -1,2 +0,0 @@
ALTER TABLE events
ADD COLUMN replaced INTEGER NOT NULL DEFAULT 0;

View file

@ -0,0 +1,32 @@
CREATE TABLE logs (
log_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
timestamp TEXT NOT NULL,
level TEXT NOT NULL CHECK (level IN ('DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL')),
message TEXT NOT NULL,
args TEXT, -- JSON string of log arguments
source TEXT, -- tag or source component
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
-- Security-specific fields
event_type TEXT, -- For security events
severity TEXT, -- For security events
remote_addr TEXT,
ccn_pubkey TEXT,
event_id TEXT,
risk_score REAL
);
CREATE INDEX idx_logs_timestamp ON logs(timestamp);
CREATE INDEX idx_logs_level ON logs(level);
CREATE INDEX idx_logs_created_at ON logs(created_at);
CREATE INDEX idx_logs_source ON logs(source);
CREATE INDEX idx_logs_event_type ON logs(event_type);
CREATE INDEX idx_logs_severity ON logs(severity);
CREATE INDEX idx_logs_ccn_pubkey ON logs(ccn_pubkey);
CREATE TRIGGER cleanup_old_logs
AFTER INSERT ON logs
WHEN (SELECT COUNT(*) FROM logs) > 100000
BEGIN
DELETE FROM logs
WHERE created_at < (unixepoch() - 2592000); -- 30 days
END;

View file

@ -0,0 +1,44 @@
-- Fix event_chunks table schema to add proper security constraints for chunked message handling
-- Drop the old table if it exists
DROP TABLE IF EXISTS event_chunks;
-- Create the event_chunks table with correct schema and security constraints
CREATE TABLE event_chunks (
chunk_id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id TEXT NOT NULL,
chunk_index INTEGER NOT NULL,
total_chunks INTEGER NOT NULL CHECK (total_chunks > 0 AND total_chunks <= 1000),
content TEXT NOT NULL,
created_at INTEGER NOT NULL,
ccn_pubkey TEXT NOT NULL,
-- SECURITY: Prevent duplicate chunks and enforce data integrity
UNIQUE(message_id, chunk_index, ccn_pubkey),
-- SECURITY: Ensure chunk_index is within valid bounds
CHECK (chunk_index >= 0 AND chunk_index < total_chunks),
-- SECURITY: Limit message_id length to prevent DoS
CHECK (length(message_id) <= 100),
-- SECURITY: Limit content size to prevent memory exhaustion
CHECK (length(content) <= 65536),
-- SECURITY: Foreign key reference to ensure CCN exists
FOREIGN KEY (ccn_pubkey) REFERENCES ccns(pubkey) ON DELETE CASCADE
);
-- Indexes for performance
CREATE INDEX idx_event_chunks_message_id ON event_chunks(message_id);
CREATE INDEX idx_event_chunks_created_at ON event_chunks(created_at);
CREATE INDEX idx_event_chunks_ccn_pubkey ON event_chunks(ccn_pubkey);
-- SECURITY: Automatic cleanup trigger for old chunks to prevent storage exhaustion
CREATE TRIGGER cleanup_old_chunks
AFTER INSERT ON event_chunks
WHEN (SELECT COUNT(*) FROM event_chunks WHERE created_at < (unixepoch() - 86400)) > 0
BEGIN
DELETE FROM event_chunks
WHERE created_at < (unixepoch() - 86400);
END;

View file

@ -0,0 +1,41 @@
-- Create outbound event queue for offline event creation and reliable relay transmission
-- This allows users to create events when offline and sync them when connectivity is restored
CREATE TABLE outbound_event_queue (
queue_id INTEGER PRIMARY KEY AUTOINCREMENT,
event_id TEXT NOT NULL,
encrypted_event TEXT NOT NULL,
ccn_pubkey TEXT NOT NULL,
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
attempts INTEGER NOT NULL DEFAULT 0,
last_attempt INTEGER NULL,
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sending', 'sent', 'failed')),
error_message TEXT NULL,
-- Ensure one queue entry per event
UNIQUE(event_id),
-- Foreign key constraints
FOREIGN KEY (ccn_pubkey) REFERENCES ccns(pubkey) ON DELETE CASCADE,
FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
);
-- Indexes for efficient querying
CREATE INDEX idx_outbound_queue_status ON outbound_event_queue(status);
CREATE INDEX idx_outbound_queue_ccn_pubkey ON outbound_event_queue(ccn_pubkey);
CREATE INDEX idx_outbound_queue_created_at ON outbound_event_queue(created_at);
CREATE INDEX idx_outbound_queue_last_attempt ON outbound_event_queue(last_attempt);
-- Cleanup trigger for old completed/failed events
CREATE TRIGGER cleanup_old_queue_entries
AFTER UPDATE ON outbound_event_queue
WHEN NEW.status IN ('sent', 'failed') AND NEW.attempts >= 5
BEGIN
-- Keep failed events for 30 days for debugging, sent events for 1 day
DELETE FROM outbound_event_queue
WHERE queue_id = NEW.queue_id
AND (
(status = 'sent' AND created_at < (unixepoch() - 86400)) OR
(status = 'failed' AND created_at < (unixepoch() - 2592000))
);
END;