feat: confine a compromised worker and drill its compromise end to end
- narrow the worker S3 user to staging reads and attachments writes - add database rules the worker role cannot bypass: no return to pending, variant keys of its own attachment, known error codes only - let the cleaner remove a ready source only after its own SHA-256 check - add make jobs_audit [REPAIR=1] to name and requeue damaged jobs - add make worker_compromise: isolated before/after stands, one operation table, response windows, job safety, controls and committed hypotheses Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
-- Возврат заданий (make jobs_audit REPAIR=1): после блокировки таблиц на запись
|
||||
-- (jobs.sh) и jobs_classes.sql, в той же транзакции. Повтор меняет 0 строк.
|
||||
--
|
||||
-- В очередь по исходнику: задание queued с новым бюджетом и без сроков (его
|
||||
-- публикует relay), вложение queued, строки вариантов удалены — обработка
|
||||
-- запишет их заново теми же ключами эффекта. Номер попытки не уменьшается,
|
||||
-- кроме испорченного: он fencing-токен, и прежняя попытка записать не может.
|
||||
CREATE TEMP TABLE requeue ON COMMIT DROP AS
|
||||
SELECT attachment_id, job_id, pipeline_version FROM incident
|
||||
WHERE has_source AND (
|
||||
stuck_time OR state_mismatch OR false_success
|
||||
OR (attempt_number AND state IN ('queued', 'processing'))
|
||||
);
|
||||
|
||||
WITH deleted AS (
|
||||
DELETE FROM attachment_variants v USING requeue r
|
||||
WHERE v.attachment_id = r.attachment_id
|
||||
AND v.pipeline_version = r.pipeline_version
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired variant_rows_deleted ' || count(*) FROM deleted;
|
||||
|
||||
WITH updated AS (
|
||||
UPDATE attachment_jobs j SET
|
||||
state = 'queued',
|
||||
attempt = least(j.attempt, 1000000),
|
||||
attempt_base = least(j.attempt, 1000000),
|
||||
last_error_code = NULL,
|
||||
next_attempt_at = NULL,
|
||||
lease_expires_at = NULL,
|
||||
published_at = NULL,
|
||||
finished_at = NULL,
|
||||
updated_at = greatest(now(), j.created_at)
|
||||
FROM requeue r WHERE j.id = r.job_id
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired jobs_requeued ' || count(*) FROM updated;
|
||||
|
||||
-- принятое состояние без задания: обработать исходник заново
|
||||
WITH inserted AS (
|
||||
INSERT INTO attachment_jobs (id, attachment_id, pipeline_version, state,
|
||||
attempt, attempt_base, updated_at, created_at)
|
||||
SELECT gen_random_uuid(), r.attachment_id, 1, 'queued', 0, 0, now(), now()
|
||||
FROM requeue r WHERE r.job_id IS NULL
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired jobs_created ' || count(*) FROM inserted;
|
||||
|
||||
UPDATE attachments a SET
|
||||
status = 'queued',
|
||||
error_code = NULL,
|
||||
updated_at = greatest(now(), a.created_at)
|
||||
FROM requeue r WHERE a.id = r.attachment_id;
|
||||
|
||||
-- сверенное уборщиком: исходника нет, результат цел
|
||||
WITH restored AS (
|
||||
UPDATE attachments a SET
|
||||
status = 'ready',
|
||||
error_code = NULL,
|
||||
updated_at = greatest(now(), a.created_at)
|
||||
FROM incident i
|
||||
WHERE i.attachment_id = a.id AND i.verified_damaged
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired verified_restored ' || count(*) FROM restored;
|
||||
|
||||
UPDATE attachment_jobs j SET
|
||||
state = 'succeeded',
|
||||
attempt = least(j.attempt, 1000000),
|
||||
attempt_base = least(j.attempt_base, j.attempt, 1000000),
|
||||
last_error_code = NULL,
|
||||
next_attempt_at = NULL,
|
||||
lease_expires_at = NULL,
|
||||
finished_at = now(),
|
||||
updated_at = greatest(now(), j.created_at)
|
||||
FROM incident i
|
||||
WHERE i.job_id = j.id AND i.verified_damaged;
|
||||
|
||||
-- номер попытки у failed: replay прибавил бы к нему и переполнил
|
||||
WITH numbered AS (
|
||||
UPDATE attachment_jobs j SET
|
||||
attempt = least(j.attempt, 1000000),
|
||||
attempt_base = least(j.attempt, 1000000)
|
||||
FROM incident i
|
||||
WHERE i.job_id = j.id AND i.attempt_number
|
||||
AND i.state = 'failed' AND NOT i.verified_damaged
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired attempt_numbers ' || count(*) FROM numbered;
|
||||
|
||||
-- время строки в будущем у остальных
|
||||
WITH dated AS (
|
||||
UPDATE attachments a SET updated_at = greatest(now(), a.created_at)
|
||||
FROM incident i
|
||||
WHERE i.attachment_id = a.id AND i.future_updated_at
|
||||
AND a.updated_at > now() + interval '1 minute'
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired future_attachments ' || count(*) FROM dated;
|
||||
|
||||
WITH dated AS (
|
||||
UPDATE attachment_jobs j SET updated_at = greatest(now(), j.created_at)
|
||||
FROM incident i
|
||||
WHERE i.job_id = j.id AND i.future_updated_at
|
||||
AND j.updated_at > now() + interval '1 minute'
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT 'repaired future_jobs ' || count(*) FROM dated;
|
||||
Reference in New Issue
Block a user