mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
fix(db): let a plugin call only an allowlist of Postgres's catalog functions
The plugin SQL guard refused functions by name, so every function that reads the catalogs or the server and was not listed passed: obj_description, col_description and shobj_description returned the comments on TREK's tables, columns, database and roles by OID, format_type named a type by its OID, and version(), current_database(), inet_server_addr() and the session keywords described the server. A call to a pg_catalog function is now refused unless the function is on PLUGIN_FUNCTIONS (string, number, date and time, JSON, array, aggregate, window, UUID, range, geometry, network and text search functions), in either spelling of a call, f(...) and the column notation (x).f. The catalog's function names of Postgres 14 to 18 are kept in assets/postgres/catalog-functions.txt, and PGPLUG-021 fails on the lane when the server has a function that list does not name, so a new major cannot widen what a plugin may call. The session keywords current_user, session_user, current_role, user, system_user, current_catalog and current_schema are refused unquoted.
This commit is contained in:
@@ -0,0 +1,496 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
/**
|
||||
* Which of Postgres's own functions a plugin's SQL may call.
|
||||
*
|
||||
* A plugin's statements run on the plugin's `search_path`, its schema and
|
||||
* `pg_temp`, and a plugin cannot create a function (the guard refuses CREATE
|
||||
* FUNCTION). So an unqualified call either finds nothing, finds one of the
|
||||
* functions its range types bring along, or finds a function of `pg_catalog`,
|
||||
* which Postgres searches first. Many of those read the catalogs or the
|
||||
* server rather than the values handed to them: `obj_description` and
|
||||
* `col_description` return the comment on any object by its OID,
|
||||
* `format_type` names a type by its OID, `version()`, `current_database()` and
|
||||
* `inet_server_addr()` describe the server, `txid_current()` the cluster's
|
||||
* activity. Listing those one by one would miss the next one Postgres adds.
|
||||
*
|
||||
* So the rule is an allowlist: a call to a function of `pg_catalog` is
|
||||
* refused unless its name is in {@link PLUGIN_FUNCTIONS}, the string, math,
|
||||
* date and time, JSON, array, range, aggregate, window, UUID and text search
|
||||
* functions a data plugin plausibly needs, each of which works only on its
|
||||
* arguments. A name counts as called when `(` follows it (`version()`,
|
||||
* `"version" ()`) or when it follows `).`, Postgres's column notation for a
|
||||
* call (`(16386::oid).obj_description`). A plugin's own table or type may
|
||||
* still carry such a name where it cannot be a call: after INTO, TABLE, VIEW,
|
||||
* REFERENCES or IF NOT EXISTS (`INSERT INTO version (...)`).
|
||||
*
|
||||
* Which names are functions of `pg_catalog` is read from
|
||||
* `assets/postgres/catalog-functions.txt`: every function name of every
|
||||
* supported major (14 to 18; the boot refuses any other server). The Postgres
|
||||
* lane compares it with `pg_proc` on the server it runs on and fails on a
|
||||
* name missing there (PGPLUG-021), so a new major cannot add a function a
|
||||
* plugin may call without someone deciding whether it belongs here.
|
||||
*
|
||||
* The SQL keywords that call a function without parentheses
|
||||
* ({@link SESSION_KEYWORDS}: `current_user`, `current_catalog`, ...) are
|
||||
* refused unquoted wherever they appear; quoted they are plain names. A
|
||||
* name starting with `pg_` is refused anywhere by the guard itself.
|
||||
*/
|
||||
|
||||
/** The functions of `pg_catalog` a plugin may call. Each must exist in the catalog list (a unit test checks). */
|
||||
export const PLUGIN_FUNCTIONS: ReadonlySet<string> = new Set([
|
||||
// Strings and bytes.
|
||||
'ascii',
|
||||
'bit_count',
|
||||
'bit_length',
|
||||
'btrim',
|
||||
'casefold',
|
||||
'char_length',
|
||||
'character_length',
|
||||
'chr',
|
||||
'concat',
|
||||
'concat_ws',
|
||||
'convert',
|
||||
'convert_from',
|
||||
'convert_to',
|
||||
'crc32',
|
||||
'crc32c',
|
||||
'decode',
|
||||
'encode',
|
||||
'format',
|
||||
'get_bit',
|
||||
'get_byte',
|
||||
'initcap',
|
||||
'is_normalized',
|
||||
'left',
|
||||
'length',
|
||||
'like',
|
||||
'lower',
|
||||
'lpad',
|
||||
'ltrim',
|
||||
'md5',
|
||||
'normalize',
|
||||
'octet_length',
|
||||
'overlay',
|
||||
'position',
|
||||
'quote_ident',
|
||||
'quote_literal',
|
||||
'quote_nullable',
|
||||
'regexp_count',
|
||||
'regexp_instr',
|
||||
'regexp_like',
|
||||
'regexp_match',
|
||||
'regexp_matches',
|
||||
'regexp_replace',
|
||||
'regexp_split_to_array',
|
||||
'regexp_split_to_table',
|
||||
'regexp_substr',
|
||||
'repeat',
|
||||
'replace',
|
||||
'reverse',
|
||||
'right',
|
||||
'rpad',
|
||||
'rtrim',
|
||||
'set_bit',
|
||||
'set_byte',
|
||||
'sha224',
|
||||
'sha256',
|
||||
'sha384',
|
||||
'sha512',
|
||||
'split_part',
|
||||
'starts_with',
|
||||
'string_to_array',
|
||||
'string_to_table',
|
||||
'strpos',
|
||||
'substr',
|
||||
'substring',
|
||||
'to_ascii',
|
||||
'to_bin',
|
||||
'to_hex',
|
||||
'to_oct',
|
||||
'translate',
|
||||
'unistr',
|
||||
'upper',
|
||||
// Numbers.
|
||||
'abs',
|
||||
'acos',
|
||||
'acosd',
|
||||
'acosh',
|
||||
'asin',
|
||||
'asind',
|
||||
'asinh',
|
||||
'atan',
|
||||
'atan2',
|
||||
'atan2d',
|
||||
'atand',
|
||||
'atanh',
|
||||
'cbrt',
|
||||
'ceil',
|
||||
'ceiling',
|
||||
'cos',
|
||||
'cosd',
|
||||
'cosh',
|
||||
'cot',
|
||||
'cotd',
|
||||
'degrees',
|
||||
'div',
|
||||
'erf',
|
||||
'erfc',
|
||||
'exp',
|
||||
'factorial',
|
||||
'floor',
|
||||
'gamma',
|
||||
'gcd',
|
||||
'lcm',
|
||||
'lgamma',
|
||||
'ln',
|
||||
'log',
|
||||
'log10',
|
||||
'min_scale',
|
||||
'mod',
|
||||
'pi',
|
||||
'pow',
|
||||
'power',
|
||||
'radians',
|
||||
'random',
|
||||
'random_normal',
|
||||
'round',
|
||||
'scale',
|
||||
'sign',
|
||||
'sin',
|
||||
'sind',
|
||||
'sinh',
|
||||
'sqrt',
|
||||
'tan',
|
||||
'tand',
|
||||
'tanh',
|
||||
'trim_scale',
|
||||
'trunc',
|
||||
'width_bucket',
|
||||
// Dates and times.
|
||||
'age',
|
||||
'clock_timestamp',
|
||||
'date_add',
|
||||
'date_bin',
|
||||
'date_part',
|
||||
'date_subtract',
|
||||
'date_trunc',
|
||||
'extract',
|
||||
'isfinite',
|
||||
'justify_days',
|
||||
'justify_hours',
|
||||
'justify_interval',
|
||||
'make_date',
|
||||
'make_interval',
|
||||
'make_time',
|
||||
'make_timestamp',
|
||||
'make_timestamptz',
|
||||
'now',
|
||||
'overlaps',
|
||||
'statement_timestamp',
|
||||
'timeofday',
|
||||
'timezone',
|
||||
'to_char',
|
||||
'to_date',
|
||||
'to_number',
|
||||
'to_timestamp',
|
||||
'transaction_timestamp',
|
||||
// Conversions, also the type names written with a modifier (`varchar(20)`, `numeric(10, 2)`).
|
||||
'bit',
|
||||
'bool',
|
||||
'bpchar',
|
||||
'bytea',
|
||||
'char',
|
||||
'date',
|
||||
'float4',
|
||||
'float8',
|
||||
'int2',
|
||||
'int4',
|
||||
'int8',
|
||||
'interval',
|
||||
'numeric',
|
||||
'text',
|
||||
'time',
|
||||
'timestamp',
|
||||
'timestamptz',
|
||||
'timetz',
|
||||
'varbit',
|
||||
'varchar',
|
||||
// JSON.
|
||||
'array_to_json',
|
||||
'json_agg',
|
||||
'json_agg_strict',
|
||||
'json_array_elements',
|
||||
'json_array_elements_text',
|
||||
'json_array_length',
|
||||
'json_build_array',
|
||||
'json_build_object',
|
||||
'json_each',
|
||||
'json_each_text',
|
||||
'json_extract_path',
|
||||
'json_extract_path_text',
|
||||
'json_object',
|
||||
'json_object_agg',
|
||||
'json_object_agg_strict',
|
||||
'json_object_agg_unique',
|
||||
'json_object_agg_unique_strict',
|
||||
'json_object_keys',
|
||||
'json_populate_record',
|
||||
'json_populate_recordset',
|
||||
'json_strip_nulls',
|
||||
'json_to_record',
|
||||
'json_to_recordset',
|
||||
'json_typeof',
|
||||
'jsonb_agg',
|
||||
'jsonb_agg_strict',
|
||||
'jsonb_array_elements',
|
||||
'jsonb_array_elements_text',
|
||||
'jsonb_array_length',
|
||||
'jsonb_build_array',
|
||||
'jsonb_build_object',
|
||||
'jsonb_each',
|
||||
'jsonb_each_text',
|
||||
'jsonb_extract_path',
|
||||
'jsonb_extract_path_text',
|
||||
'jsonb_insert',
|
||||
'jsonb_object',
|
||||
'jsonb_object_agg',
|
||||
'jsonb_object_agg_strict',
|
||||
'jsonb_object_agg_unique',
|
||||
'jsonb_object_agg_unique_strict',
|
||||
'jsonb_object_keys',
|
||||
'jsonb_path_exists',
|
||||
'jsonb_path_exists_tz',
|
||||
'jsonb_path_match',
|
||||
'jsonb_path_match_tz',
|
||||
'jsonb_path_query',
|
||||
'jsonb_path_query_array',
|
||||
'jsonb_path_query_array_tz',
|
||||
'jsonb_path_query_first',
|
||||
'jsonb_path_query_first_tz',
|
||||
'jsonb_path_query_tz',
|
||||
'jsonb_populate_record',
|
||||
'jsonb_populate_record_valid',
|
||||
'jsonb_populate_recordset',
|
||||
'jsonb_pretty',
|
||||
'jsonb_set',
|
||||
'jsonb_set_lax',
|
||||
'jsonb_strip_nulls',
|
||||
'jsonb_to_record',
|
||||
'jsonb_to_recordset',
|
||||
'jsonb_typeof',
|
||||
'row_to_json',
|
||||
'to_json',
|
||||
'to_jsonb',
|
||||
// Arrays and sets.
|
||||
'array_append',
|
||||
'array_cat',
|
||||
'array_dims',
|
||||
'array_fill',
|
||||
'array_length',
|
||||
'array_lower',
|
||||
'array_ndims',
|
||||
'array_position',
|
||||
'array_positions',
|
||||
'array_prepend',
|
||||
'array_remove',
|
||||
'array_replace',
|
||||
'array_reverse',
|
||||
'array_sample',
|
||||
'array_shuffle',
|
||||
'array_sort',
|
||||
'array_to_string',
|
||||
'array_upper',
|
||||
'cardinality',
|
||||
'generate_series',
|
||||
'generate_subscripts',
|
||||
'trim_array',
|
||||
'unnest',
|
||||
'num_nonnulls',
|
||||
'num_nulls',
|
||||
// Aggregates.
|
||||
'any_value',
|
||||
'array_agg',
|
||||
'avg',
|
||||
'bit_and',
|
||||
'bit_or',
|
||||
'bit_xor',
|
||||
'bool_and',
|
||||
'bool_or',
|
||||
'corr',
|
||||
'count',
|
||||
'covar_pop',
|
||||
'covar_samp',
|
||||
'every',
|
||||
'max',
|
||||
'min',
|
||||
'mode',
|
||||
'percentile_cont',
|
||||
'percentile_disc',
|
||||
'range_agg',
|
||||
'range_intersect_agg',
|
||||
'regr_avgx',
|
||||
'regr_avgy',
|
||||
'regr_count',
|
||||
'regr_intercept',
|
||||
'regr_r2',
|
||||
'regr_slope',
|
||||
'regr_sxx',
|
||||
'regr_sxy',
|
||||
'regr_syy',
|
||||
'stddev',
|
||||
'stddev_pop',
|
||||
'stddev_samp',
|
||||
'string_agg',
|
||||
'sum',
|
||||
'var_pop',
|
||||
'var_samp',
|
||||
'variance',
|
||||
// Window functions.
|
||||
'cume_dist',
|
||||
'dense_rank',
|
||||
'first_value',
|
||||
'lag',
|
||||
'last_value',
|
||||
'lead',
|
||||
'nth_value',
|
||||
'ntile',
|
||||
'percent_rank',
|
||||
'rank',
|
||||
'row_number',
|
||||
// UUIDs.
|
||||
'gen_random_uuid',
|
||||
'uuid_extract_timestamp',
|
||||
'uuid_extract_version',
|
||||
'uuidv4',
|
||||
'uuidv7',
|
||||
// Ranges and the plugin's own enum types.
|
||||
'datemultirange',
|
||||
'daterange',
|
||||
'enum_first',
|
||||
'enum_last',
|
||||
'enum_range',
|
||||
'int4multirange',
|
||||
'int4range',
|
||||
'int8multirange',
|
||||
'int8range',
|
||||
'isempty',
|
||||
'lower_inc',
|
||||
'lower_inf',
|
||||
'multirange',
|
||||
'nummultirange',
|
||||
'numrange',
|
||||
'range_merge',
|
||||
'tsmultirange',
|
||||
'tsrange',
|
||||
'tstzmultirange',
|
||||
'tstzrange',
|
||||
'upper_inc',
|
||||
'upper_inf',
|
||||
// Geometry.
|
||||
'area',
|
||||
'bound_box',
|
||||
'box',
|
||||
'center',
|
||||
'circle',
|
||||
'diagonal',
|
||||
'diameter',
|
||||
'height',
|
||||
'isclosed',
|
||||
'isopen',
|
||||
'line',
|
||||
'lseg',
|
||||
'npoints',
|
||||
'path',
|
||||
'pclose',
|
||||
'point',
|
||||
'polygon',
|
||||
'popen',
|
||||
'radius',
|
||||
'slope',
|
||||
'width',
|
||||
// Network addresses.
|
||||
'abbrev',
|
||||
'broadcast',
|
||||
'cidr',
|
||||
'family',
|
||||
'host',
|
||||
'hostmask',
|
||||
'inet_merge',
|
||||
'inet_same_family',
|
||||
'macaddr',
|
||||
'macaddr8',
|
||||
'masklen',
|
||||
'netmask',
|
||||
'network',
|
||||
'set_masklen',
|
||||
// Full text search. ts_stat and ts_rewrite run SQL handed to them as text and stay refused.
|
||||
'array_to_tsvector',
|
||||
'json_to_tsvector',
|
||||
'jsonb_to_tsvector',
|
||||
'numnode',
|
||||
'phraseto_tsquery',
|
||||
'plainto_tsquery',
|
||||
'querytree',
|
||||
'setweight',
|
||||
'strip',
|
||||
'to_tsquery',
|
||||
'to_tsvector',
|
||||
'ts_delete',
|
||||
'ts_filter',
|
||||
'ts_headline',
|
||||
'ts_rank',
|
||||
'ts_rank_cd',
|
||||
'tsvector_to_array',
|
||||
'websearch_to_tsquery',
|
||||
// The TABLESAMPLE methods (`TABLESAMPLE SYSTEM (10)`).
|
||||
'bernoulli',
|
||||
'system',
|
||||
]);
|
||||
|
||||
/**
|
||||
* The SQL keywords that call a catalog function without parentheses and
|
||||
* describe the session or the server: the role names, the database and the
|
||||
* schema. Refused unquoted anywhere; a quoted `"user"` is a column.
|
||||
*/
|
||||
export const SESSION_KEYWORDS: ReadonlySet<string> = new Set([
|
||||
'current_catalog',
|
||||
'current_role',
|
||||
'current_schema',
|
||||
'current_user',
|
||||
'session_user',
|
||||
'system_user',
|
||||
'user',
|
||||
]);
|
||||
|
||||
/** The function names of `pg_catalog` on every supported major, one per line, sorted. */
|
||||
export const CATALOG_FUNCTIONS_FILE = path.join(
|
||||
__dirname,
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'assets',
|
||||
'postgres',
|
||||
'catalog-functions.txt',
|
||||
);
|
||||
|
||||
let catalog: ReadonlySet<string> | undefined;
|
||||
|
||||
/** The names in {@link CATALOG_FUNCTIONS_FILE}, read once. */
|
||||
export function catalogFunctionNames(): ReadonlySet<string> {
|
||||
catalog ??= new Set(
|
||||
fs
|
||||
.readFileSync(CATALOG_FUNCTIONS_FILE, 'utf8')
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line !== ''),
|
||||
);
|
||||
return catalog;
|
||||
}
|
||||
|
||||
/** Whether calling `name` reaches a function of `pg_catalog` a plugin may not call. */
|
||||
export function refusedCatalogCall(name: string): boolean {
|
||||
return !PLUGIN_FUNCTIONS.has(name) && catalogFunctionNames().has(name);
|
||||
}
|
||||
Reference in New Issue
Block a user