forked from mtusi2026/mtusi2026lab7
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,127 @@
|
||||
"""Строки конвейера для тестов значений роли worker и сверки заданий (change
|
||||
`track1-worker-compromise`, design, решения 6 и 8): вложение с заданием и
|
||||
вариантами — мимо приложения, соединением администратора."""
|
||||
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
import asyncpg
|
||||
|
||||
from conftest import TEST_DB
|
||||
|
||||
PIPELINE_VERSION = 1
|
||||
VARIANTS = ("thumbnail", "crop", "optimized")
|
||||
|
||||
|
||||
async def add_world(connection: asyncpg.Connection) -> tuple[uuid.UUID, ...]:
|
||||
"""Пользователь, чат и участник: владелец вложений."""
|
||||
user, chat = uuid.uuid4(), uuid.uuid4()
|
||||
await connection.execute(
|
||||
"INSERT INTO users (id, username, display_name, password_hash, "
|
||||
"created_at) VALUES ($1, $2, 'u', '$argon2id$v=19$m=65536,t=3,p=4$"
|
||||
"c29tZXNhbHQ$c29tZWhhc2g', now())",
|
||||
user,
|
||||
f"u{user.hex[:12]}",
|
||||
)
|
||||
await connection.execute(
|
||||
"INSERT INTO chats (id, title, created_at) VALUES ($1, 't', now())",
|
||||
chat,
|
||||
)
|
||||
await connection.execute(
|
||||
"INSERT INTO chat_members (chat_id, user_id, position, created_at) "
|
||||
"VALUES ($1, $2, 0, now())",
|
||||
chat,
|
||||
user,
|
||||
)
|
||||
return user, chat
|
||||
|
||||
|
||||
def variant_key(
|
||||
attachment: uuid.UUID,
|
||||
name: str,
|
||||
*,
|
||||
namespace: str = TEST_DB,
|
||||
version: int = PIPELINE_VERSION,
|
||||
) -> str:
|
||||
return f"{namespace}/attachments/{attachment}/v{version}-{name}"
|
||||
|
||||
|
||||
async def add_variants(
|
||||
connection: asyncpg.Connection,
|
||||
attachment: uuid.UUID,
|
||||
names: tuple[str, ...] = VARIANTS,
|
||||
) -> None:
|
||||
for name in names:
|
||||
await connection.execute(
|
||||
INSERT_VARIANT_SQL,
|
||||
attachment,
|
||||
name,
|
||||
variant_key(attachment, name),
|
||||
)
|
||||
|
||||
|
||||
INSERT_VARIANT_SQL = (
|
||||
"INSERT INTO attachment_variants (attachment_id, pipeline_version, "
|
||||
"variant, object_key, content_type, size_bytes, width, height, sha256, "
|
||||
"created_at) VALUES ($1, 1, $2, $3, 'image/jpeg', 1, 1, 1, "
|
||||
"decode(repeat('ab', 32), 'hex'), now())"
|
||||
)
|
||||
|
||||
|
||||
async def add_attachment(
|
||||
connection: asyncpg.Connection,
|
||||
world: tuple[uuid.UUID, ...],
|
||||
*,
|
||||
status: str,
|
||||
state: str | None = None,
|
||||
staging: bool = True,
|
||||
variants: tuple[str, ...] = (),
|
||||
age_seconds: int = 3600,
|
||||
**job: Any,
|
||||
) -> uuid.UUID:
|
||||
"""Вложение `status` с заданием `state` (`None` — без задания); `job` —
|
||||
колонки задания выражениями SQL."""
|
||||
user, chat = world
|
||||
attachment = uuid.uuid4()
|
||||
namespace = TEST_DB
|
||||
await connection.execute(
|
||||
"INSERT INTO attachments (id, chat_id, owner_id, filename, "
|
||||
"content_type, size_bytes, sha256, object_key, staging_key, status, "
|
||||
"error_code, updated_at, created_at) VALUES ($1, $2, $3, 'a.png', "
|
||||
"'image/png', 1, decode(repeat('ab', 32), 'hex'), $4, $5, $6, $7, "
|
||||
"now() - make_interval(secs => $8), "
|
||||
"now() - make_interval(secs => $8))",
|
||||
attachment,
|
||||
chat,
|
||||
user,
|
||||
f"{namespace}/attachments/{attachment}/original",
|
||||
f"{namespace}/staging/{attachment}/original" if staging else None,
|
||||
status,
|
||||
"processing_failed" if status == "failed" else None,
|
||||
float(age_seconds),
|
||||
)
|
||||
if state is not None:
|
||||
columns = {
|
||||
"lease_expires_at": (
|
||||
"now() + interval '60 s'" if state == "processing" else "NULL"
|
||||
),
|
||||
"published_at": "now() - interval '1 s'",
|
||||
"next_attempt_at": "NULL",
|
||||
"attempt": "1",
|
||||
"attempt_base": "0",
|
||||
"updated_at": "now()",
|
||||
**job,
|
||||
}
|
||||
names = ", ".join(columns)
|
||||
values = ", ".join(columns.values())
|
||||
await connection.execute(
|
||||
"INSERT INTO attachment_jobs (id, attachment_id, "
|
||||
f"pipeline_version, state, created_at, {names}) VALUES "
|
||||
"(gen_random_uuid(), $1, 1, $2, "
|
||||
f"now() - make_interval(secs => $3), {values})",
|
||||
attachment,
|
||||
state,
|
||||
float(age_seconds),
|
||||
)
|
||||
await add_variants(connection, attachment, variants)
|
||||
return attachment
|
||||
Reference in New Issue
Block a user