86 lines
3.5 KiB
PL/PgSQL
86 lines
3.5 KiB
PL/PgSQL
-- +goose Up
|
|
-- The audit of the operator search for the attempt behind a leaked task text.
|
|
-- Every search writes one row, whatever it finds, before a result is shown.
|
|
-- A row holds no leaked text: only a keyed digest of the normalized text and its
|
|
-- length, the outcome, and, for a found search, the attempt. The attempt is not
|
|
-- a foreign key, so the row outlives an account deletion. Nothing reads these
|
|
-- rows for a candidate, an employer, a result, a category, or a quota.
|
|
CREATE TABLE assessment_leak_traces (
|
|
id uuid NOT NULL,
|
|
actor text NOT NULL,
|
|
searched_at timestamptz NOT NULL,
|
|
text_digest bytea NOT NULL,
|
|
key_version smallint NOT NULL,
|
|
text_runes integer NOT NULL,
|
|
status text NOT NULL,
|
|
method text,
|
|
attempt_id uuid,
|
|
candidates integer NOT NULL,
|
|
evidence_bits integer NOT NULL,
|
|
CONSTRAINT assessment_leak_traces_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_leak_traces_actor CHECK (
|
|
char_length(actor) <= 128 AND actor ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
),
|
|
CONSTRAINT assessment_leak_traces_digest CHECK (octet_length(text_digest) = 32),
|
|
CONSTRAINT assessment_leak_traces_key_version CHECK (key_version > 0),
|
|
CONSTRAINT assessment_leak_traces_counts CHECK (text_runes >= 0 AND candidates >= 0 AND evidence_bits >= 0),
|
|
CONSTRAINT assessment_leak_traces_status CHECK (status IN ('found', 'ambiguous', 'not_found')),
|
|
CONSTRAINT assessment_leak_traces_method CHECK (
|
|
method IS NULL OR method IN ('invisible_mark', 'visible_code', 'wording', 'variant')
|
|
),
|
|
CONSTRAINT assessment_leak_traces_found CHECK (
|
|
(status = 'found' AND attempt_id IS NOT NULL AND method IS NOT NULL)
|
|
OR (status <> 'found' AND attempt_id IS NULL)
|
|
)
|
|
);
|
|
|
|
CREATE INDEX assessment_leak_traces_attempt_idx
|
|
ON assessment_leak_traces (attempt_id, searched_at)
|
|
WHERE attempt_id IS NOT NULL;
|
|
|
|
CREATE INDEX assessment_leak_traces_searched_idx
|
|
ON assessment_leak_traces (searched_at, id);
|
|
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_leak_traces_immutable() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_leak_traces_immutable',
|
|
MESSAGE = 'assessment leak trace audit records are immutable';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_leak_traces_immutable
|
|
BEFORE UPDATE OR DELETE ON assessment_leak_traces
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_leak_traces_immutable();
|
|
|
|
CREATE TRIGGER assessment_leak_traces_no_truncate
|
|
BEFORE TRUNCATE ON assessment_leak_traces
|
|
FOR EACH STATEMENT EXECUTE FUNCTION assessment_leak_traces_immutable();
|
|
|
|
-- +goose Down
|
|
-- The audit of operator searches is durable evidence. Rolling the schema back
|
|
-- must not remove it as a side effect: an operator exports or resolves it first.
|
|
-- +goose StatementBegin
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM assessment_leak_traces) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'assessment_leak_traces_retained',
|
|
TABLE = 'assessment_leak_traces',
|
|
MESSAGE = 'assessment leak trace audit must be retained before rollback';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
DROP TRIGGER assessment_leak_traces_no_truncate ON assessment_leak_traces;
|
|
DROP TRIGGER assessment_leak_traces_immutable ON assessment_leak_traces;
|
|
DROP FUNCTION assessment_leak_traces_immutable();
|
|
DROP INDEX assessment_leak_traces_searched_idx;
|
|
DROP INDEX assessment_leak_traces_attempt_idx;
|
|
DROP TABLE assessment_leak_traces;
|