lighthouse: add local fixes and exploit tooling
This commit is contained in:
1 parent
21a8646fd9
commit
8987f08ee1
11 files changed
+935
-30
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
@@ -15,13 +16,18 @@ inline void append(const std::string& created_at, const std::string& queue,
|
||||
std::lock_guard guard(lock);
|
||||
auto directory = std::filesystem::path("/var/log/lighthouse") / created_at.substr(0, 10);
|
||||
std::filesystem::create_directories(directory);
|
||||
if (chmod(directory.c_str(), 0755) != 0)
|
||||
if (chmod(directory.c_str(), 0700) != 0)
|
||||
throw std::runtime_error("cannot secure journal directory");
|
||||
auto path = directory / (queue + ".log");
|
||||
std::ofstream file(path, std::ios::app);
|
||||
if (!file || chmod(path.c_str(), 0644) != 0)
|
||||
if (!file || chmod(path.c_str(), 0600) != 0)
|
||||
throw std::runtime_error("cannot open journal");
|
||||
file << instant << " queue=" << queue << " topic=" << topic << " ciphertext=" << cipher << "\n";
|
||||
const auto line = instant + " queue=" + queue + " topic=" + topic + " ciphertext=" + cipher + "\n";
|
||||
const auto position = file.tellp();
|
||||
if (position < 0) throw std::runtime_error("cannot size journal");
|
||||
constexpr std::uintmax_t max_journal_bytes = 512 * 1024;
|
||||
if (static_cast<std::uintmax_t>(position) + line.size() > max_journal_bytes) return;
|
||||
file << line;
|
||||
file.close();
|
||||
if (!file) throw std::runtime_error("cannot write journal");
|
||||
}
|
||||
|
||||
+60
-21
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <charconv>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
@@ -54,11 +55,32 @@ static json message_row(const Statement& query) {
|
||||
return {{"id", query.number(0)}, {"topic", query.text(1)}, {"ciphertext", query.text(2)}};
|
||||
}
|
||||
|
||||
// Expired rows are already invisible to readers. Reclaim a bounded batch on
|
||||
// each public write so cleanup cannot hold the shared database lock for long.
|
||||
static void prune_expired() {
|
||||
Statement prune(db, R"SQL(
|
||||
DELETE FROM messages WHERE id IN (
|
||||
SELECT id FROM messages WHERE expires_at<=? ORDER BY expires_at LIMIT 256
|
||||
)
|
||||
)SQL");
|
||||
prune.bind(1, epoch_now()).next();
|
||||
}
|
||||
|
||||
static long long after_id(const httplib::Request& request) {
|
||||
if (!request.has_param("after_id")) return 0;
|
||||
const auto value = request.get_param_value("after_id");
|
||||
long long parsed = 0;
|
||||
const auto result = std::from_chars(value.data(), value.data() + value.size(), parsed);
|
||||
if (value.empty() || result.ec != std::errc{} || result.ptr != value.data() + value.size() || parsed < 0)
|
||||
throw HttpError(400, "bad after_id");
|
||||
return parsed;
|
||||
}
|
||||
|
||||
static void initialize() {
|
||||
fs::create_directories("/data");
|
||||
fs::create_directories("/var/log/lighthouse");
|
||||
fs::create_directories(filters::work_root);
|
||||
if (chmod("/data", 0700) || chmod("/var/log/lighthouse", 0755) ||
|
||||
if (chmod("/data", 0700) || chmod("/var/log/lighthouse", 0700) ||
|
||||
chmod(filters::work_root, 0711))
|
||||
throw std::runtime_error("cannot set storage permissions");
|
||||
if (sqlite3_open("/data/lighthouse.db", &db) != SQLITE_OK)
|
||||
@@ -84,6 +106,7 @@ static void initialize() {
|
||||
listing TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS messages_by_queue ON messages(queue_id,id);
|
||||
CREATE INDEX IF NOT EXISTS messages_by_expiry ON messages(expires_at);
|
||||
CREATE INDEX IF NOT EXISTS revisions_by_queue ON filter_revisions(queue_id,id DESC);
|
||||
)SQL");
|
||||
bool has_read_at = false;
|
||||
@@ -112,6 +135,7 @@ static void queue_routes(httplib::Server& server) {
|
||||
auto retention = number_field(body, "retention_seconds", 3600, 60, 604800);
|
||||
auto id = uuid(), key = random_hex(48), created = utc_now();
|
||||
std::lock_guard guard(db_lock);
|
||||
prune_expired();
|
||||
Statement insert(db, "INSERT INTO queues(id,read_key,label,retention_seconds,created_at) VALUES(?,?,?,?,?)");
|
||||
insert.bind(1, id).bind(2, key).bind(3, label).bind(4, retention).bind(5, created).next();
|
||||
reply(response, 201, {{"id", id}, {"read_key", key}, {"label", label},
|
||||
@@ -146,6 +170,7 @@ static void filter_routes(httplib::Server& server) {
|
||||
{
|
||||
std::lock_guard guard(db_lock);
|
||||
require_queue(id);
|
||||
require_key(request);
|
||||
}
|
||||
auto artifact = filters::compile(source);
|
||||
auto state = artifact.ready ? "ready" : "rejected";
|
||||
@@ -153,6 +178,8 @@ static void filter_routes(httplib::Server& server) {
|
||||
{
|
||||
std::lock_guard guard(db_lock);
|
||||
Transaction transaction(db);
|
||||
require_queue(id);
|
||||
require_key(request);
|
||||
Statement insert(db, R"SQL(
|
||||
INSERT INTO filter_revisions(queue_id,label,state,wasm_path,compiler_output,source,listing,created_at)
|
||||
VALUES(?,?,?,?,?,?,?,?)
|
||||
@@ -263,8 +290,7 @@ static void message_routes(httplib::Server& server) {
|
||||
{
|
||||
std::lock_guard guard(db_lock);
|
||||
Transaction transaction(db);
|
||||
Statement prune(db, "DELETE FROM messages WHERE queue_id=? AND expires_at<=?");
|
||||
prune.bind(1, id).bind(2, epoch_now()).next();
|
||||
prune_expired();
|
||||
Statement insert(db, R"SQL(
|
||||
INSERT INTO messages(queue_id,ciphertext,topic,not_before,expires_at,created_at) VALUES(?,?,?,?,?,?)
|
||||
)SQL");
|
||||
@@ -279,27 +305,40 @@ static void message_routes(httplib::Server& server) {
|
||||
|
||||
server.Get(R"(/api/queues/([0-9a-f-]{36})/messages)", endpoint([](const auto& request, auto& response) {
|
||||
auto show_read = request.has_param("show_read") && request.get_param_value("show_read") == "true";
|
||||
std::lock_guard guard(db_lock);
|
||||
require_key(request);
|
||||
auto moment = epoch_now();
|
||||
Transaction transaction(db);
|
||||
Statement query(db, R"SQL(
|
||||
SELECT id,topic,ciphertext FROM messages
|
||||
WHERE queue_id=? AND not_before<=? AND expires_at>?
|
||||
AND (? OR read_at IS NULL) ORDER BY id
|
||||
)SQL");
|
||||
query.bind(1, request.matches[1].str()).bind(2, moment).bind(3, moment).bind(4, show_read ? 1LL : 0LL);
|
||||
auto after = after_id(request);
|
||||
constexpr long long page_size = 128;
|
||||
auto messages = json::array();
|
||||
while (query.next()) messages.push_back(message_row(query));
|
||||
if (!show_read) {
|
||||
Statement mark(db, R"SQL(
|
||||
UPDATE messages SET read_at=? WHERE queue_id=? AND not_before<=?
|
||||
AND expires_at>? AND read_at IS NULL
|
||||
bool has_more = false;
|
||||
long long last = after;
|
||||
{
|
||||
std::lock_guard guard(db_lock);
|
||||
require_key(request);
|
||||
auto moment = epoch_now();
|
||||
Transaction transaction(db);
|
||||
Statement query(db, R"SQL(
|
||||
SELECT id,topic,ciphertext FROM messages
|
||||
WHERE queue_id=? AND id>? AND not_before<=? AND expires_at>?
|
||||
AND (? OR read_at IS NULL) ORDER BY id LIMIT ?
|
||||
)SQL");
|
||||
mark.bind(1, moment).bind(2, request.matches[1].str()).bind(3, moment).bind(4, moment).next();
|
||||
query.bind(1, request.matches[1].str()).bind(2, after).bind(3, moment)
|
||||
.bind(4, moment).bind(5, show_read ? 1LL : 0LL).bind(6, page_size + 1);
|
||||
while (query.next()) {
|
||||
if (messages.size() == page_size) { has_more = true; break; }
|
||||
last = query.number(0);
|
||||
messages.push_back(message_row(query));
|
||||
}
|
||||
if (!show_read && !messages.empty()) {
|
||||
Statement mark(db, R"SQL(
|
||||
UPDATE messages SET read_at=? WHERE queue_id=? AND id>? AND id<=?
|
||||
AND not_before<=? AND expires_at>? AND read_at IS NULL
|
||||
)SQL");
|
||||
mark.bind(1, moment).bind(2, request.matches[1].str()).bind(3, after)
|
||||
.bind(4, last).bind(5, moment).bind(6, moment).next();
|
||||
}
|
||||
transaction.commit();
|
||||
}
|
||||
transaction.commit();
|
||||
reply(response, 200, {{"messages", messages}});
|
||||
reply(response, 200, {{"messages", messages}, {"has_more", has_more},
|
||||
{"next_after", has_more ? last : 0}});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user