54 lines
4.0 KiB
TypeScript
54 lines
4.0 KiB
TypeScript
import { pollProblem } from "./server-poll-execution-model.js"
|
|
|
|
export const SERVER_POLL_SETTINGS_TABLES = ["traffic_settings", "uptime_settings", "internet_path_settings"] as const
|
|
/** Exact UTF-8 function body, including whitespace. This is a reviewed source
|
|
* pin, not a digest learned from the live database. No public adapter yet uses
|
|
* this candidate: catalog validation alone is not an authority issuer. */
|
|
export const SERVER_POLL_SETTINGS_EPOCH_BODY_SHA256 = "3d62b03c1753acdaf91d9de306527c2065b17c606d311aa8502271ea6654741f"
|
|
export const SERVER_POLL_SETTINGS_EPOCH_INVENTORY_SQL = `/* poll-settings:epoch-inventory */
|
|
SELECT c.relname AS table_name,c.relkind,a.attnotnull AS not_null,
|
|
a.atttypid='pg_catalog.uuid'::pg_catalog.regtype AS uuid_type,
|
|
a.attgenerated::text AS generated,a.attidentity::text AS identity,
|
|
pg_catalog.pg_get_expr(d.adbin,d.adrelid) AS default_expression,
|
|
t.tgname AS trigger_name,t.tgenabled AS enabled,t.tgtype::int AS trigger_type,
|
|
t.tgnargs::int AS argument_count,t.tgattr::text AS attribute_numbers,
|
|
t.tgqual IS NULL AS unconditional,t.tgconstraint::text AS constraint_oid,
|
|
t.tgoldtable IS NULL AND t.tgnewtable IS NULL AS no_transition_tables,
|
|
pn.nspname AS function_schema,p.proname AS function_name,l.lanname AS language,
|
|
p.prosecdef AS security_definer,p.proleakproof AS leakproof,p.provolatile AS volatility,
|
|
p.prokind AS function_kind,p.pronargs::int AS function_arguments,
|
|
p.prorettype='pg_catalog.trigger'::pg_catalog.regtype AS trigger_return,p.proconfig AS configuration,
|
|
pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(p.prosrc,'UTF8')),'hex') AS body_sha256
|
|
FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
|
|
LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid=c.oid AND a.attname='poll_configuration_epoch' AND NOT a.attisdropped
|
|
LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=c.oid AND d.adnum=a.attnum
|
|
-- All non-internal triggers, so an additional trigger cannot silently override
|
|
-- this epoch later in the same statement.
|
|
LEFT JOIN pg_catalog.pg_trigger t ON t.tgrelid=c.oid AND NOT t.tgisinternal
|
|
LEFT JOIN pg_catalog.pg_proc p ON p.oid=t.tgfoid
|
|
LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid=p.pronamespace
|
|
LEFT JOIN pg_catalog.pg_language l ON l.oid=p.prolang
|
|
WHERE n.nspname='public' AND c.relname=ANY($1::text[]) ORDER BY c.relname,t.tgname LIMIT 5`
|
|
|
|
/** The fixed trusted SQL reader must supply this observation in the same
|
|
* protected context as the settings rows. A caller-supplied observation is
|
|
* never sufficient to issue an approval proof. Missing/extra guards refuse. */
|
|
export function assertServerPollSettingsEpochCatalog(rows: Record<string, unknown>[]): void {
|
|
const fail = (): never => pollProblem("STATE_UNAVAILABLE", "Не подтверждена защита версии настроек опроса")
|
|
if (!Array.isArray(rows) || rows.length !== 3 || rows.some(row => !row || typeof row !== "object" || Array.isArray(row))
|
|
|| new Set(rows.map(row => row.table_name)).size !== 3) fail()
|
|
for (const row of rows) {
|
|
if (!SERVER_POLL_SETTINGS_TABLES.some(table => table === row.table_name)
|
|
|| row.relkind !== "r" || row.not_null !== true || row.uuid_type !== true
|
|
|| row.generated !== "" || row.identity !== "" || row.default_expression !== "gen_random_uuid()"
|
|
|| row.trigger_name !== "server_poll_settings_epoch" || row.enabled !== "A" || row.trigger_type !== 23
|
|
|| row.argument_count !== 0 || row.attribute_numbers !== "" || row.unconditional !== true
|
|
|| row.constraint_oid !== "0" || row.no_transition_tables !== true
|
|
|| row.function_schema !== "public" || row.function_name !== "server_poll_settings_epoch_guard"
|
|
|| row.language !== "plpgsql" || row.security_definer !== false || row.leakproof !== false
|
|
|| row.volatility !== "v" || row.function_kind !== "f" || row.function_arguments !== 0
|
|
|| row.trigger_return !== true || JSON.stringify(row.configuration) !== '["search_path=pg_catalog"]'
|
|
|| row.body_sha256 !== SERVER_POLL_SETTINGS_EPOCH_BODY_SHA256) fail()
|
|
}
|
|
}
|