@pgpmjs/core 7.25.2 → 7.25.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,11 +14,10 @@ const apply_spec_1 = require("./apply-spec");
14
14
  const profile_1 = require("./profile");
15
15
  const reuse_1 = require("./reuse");
16
16
  const types_1 = require("./types");
17
- // Functions that take a bare schema name as a string-literal first argument.
18
- // The AST pass only remaps dotted literals ('schema.object'); these helpers
19
- // (ubiquitous in verify scripts) name the schema alone, so a string-level
20
- // pre-pass remaps them.
21
- const SCHEMA_NAME_LITERAL_FUNCS = ['verify_schema', 'has_schema_privilege'];
17
+ // has_schema_privilege takes a bare schema name as a string literal. The AST
18
+ // pass only remaps dotted literals ('schema.object') and identity casts
19
+ // ('schema'::regnamespace), so this one is remapped by a string pre-pass.
20
+ const SCHEMA_NAME_LITERAL_FUNCS = ['has_schema_privilege'];
22
21
  const schemaNameLiteralPass = (content, schemaMapping) => {
23
22
  const pattern = new RegExp(`\\b(${SCHEMA_NAME_LITERAL_FUNCS.join('|')})\\s*\\(\\s*'([^']+)'`, 'gi');
24
23
  return content.replace(pattern, (match, _fn, schema) => {
package/apply/reuse.js CHANGED
@@ -22,9 +22,11 @@ const FOLDER_KIND = {
22
22
  procedures: 'procedure',
23
23
  types: 'type'
24
24
  };
25
- // Mirrors materialize.ts: bare-schema-name literal helpers the AST pass can't
26
- // reach are remapped by a narrow string pre-pass.
27
- const SCHEMA_NAME_LITERAL_FUNCS = ['verify_schema', 'has_schema_privilege'];
25
+ // Mirrors materialize.ts.
26
+ // has_schema_privilege takes a bare schema name as a string literal. The AST
27
+ // pass only remaps dotted literals ('schema.object') and identity casts
28
+ // ('schema'::regnamespace), so this one is remapped by a string pre-pass.
29
+ const SCHEMA_NAME_LITERAL_FUNCS = ['has_schema_privilege'];
28
30
  const schemaNameLiteralPass = (content, schemaMapping) => {
29
31
  const pattern = new RegExp(`\\b(${SCHEMA_NAME_LITERAL_FUNCS.join('|')})\\s*\\(\\s*'([^']+)'`, 'gi');
30
32
  return content.replace(pattern, (match, _fn, schema) => {
@@ -0,0 +1,18 @@
1
+ import type { PgConfig } from 'pg-env';
2
+ /** The preamble line `pg_dump` emits, and the one this module rewrites. */
3
+ export declare const EMPTY_SEARCH_PATH_STATEMENT = "SELECT pg_catalog.set_config('search_path', '', false);";
4
+ /** The schemas the source database's extensions live in, sorted. */
5
+ export declare const getExtensionSchemas: (config: PgConfig) => Promise<string[]>;
6
+ /**
7
+ * Replace the dump's empty-`search_path` preamble with one naming `schemas`.
8
+ *
9
+ * Throws when the preamble is missing: a dump whose format we no longer
10
+ * recognize must not be written out as if it had been patched.
11
+ * With no extensions there is nothing an operator lookup could need, so the
12
+ * empty path is already correct and the dump is returned untouched.
13
+ */
14
+ export declare const rewriteSearchPathStatement: (dump: string, schemas: string[]) => string;
15
+ /** Make a captured dump restorable against `config`'s extension schemas. */
16
+ export declare const applyExtensionSearchPath: (config: PgConfig, dump: string) => Promise<string>;
17
+ /** Same, for a dump `pg_dump` wrote to disk itself. */
18
+ export declare const applyExtensionSearchPathToFile: (config: PgConfig, file: string) => Promise<string[]>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyExtensionSearchPathToFile = exports.applyExtensionSearchPath = exports.rewriteSearchPathStatement = exports.getExtensionSchemas = exports.EMPTY_SEARCH_PATH_STATEMENT = void 0;
4
+ /**
5
+ * Making a `pg_dump` artifact restorable when extension operators are in play.
6
+ *
7
+ * `pg_dump` opens every plain dump with
8
+ * `SELECT pg_catalog.set_config('search_path', '', false);` (CVE-2018-1058
9
+ * hardening). Types are schema-qualified in the dump body, but operators are
10
+ * not always qualifiable in the source SQL: a trigger's
11
+ * `WHEN (old.embedding IS DISTINCT FROM new.embedding)` resolves its `=`
12
+ * through `search_path`, and `IS DISTINCT FROM` has no `OPERATOR(schema.=)`
13
+ * form. With an empty path the restore dies with
14
+ * `operator does not exist: public.vector = public.vector`.
15
+ *
16
+ * The fix is to put back exactly the schemas the source database's extensions
17
+ * live in — nothing else, so no user schema re-enters the path and the
18
+ * hardening intent survives.
19
+ */
20
+ const fs_1 = require("fs");
21
+ const pg_cache_1 = require("pg-cache");
22
+ /** The preamble line `pg_dump` emits, and the one this module rewrites. */
23
+ exports.EMPTY_SEARCH_PATH_STATEMENT = "SELECT pg_catalog.set_config('search_path', '', false);";
24
+ /** The schemas the source database's extensions live in, sorted. */
25
+ const getExtensionSchemas = async (config) => {
26
+ const pool = (0, pg_cache_1.getPgPool)(config);
27
+ const res = await pool.query('SELECT DISTINCT n.nspname FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace');
28
+ return res.rows.map((row) => String(row.nspname)).sort();
29
+ };
30
+ exports.getExtensionSchemas = getExtensionSchemas;
31
+ const searchPathLiteral = (schemas) => {
32
+ const path = schemas.map(schema => `"${schema.replace(/"/g, '""')}"`).join(', ');
33
+ return `'${path.replace(/'/g, "''")}'`;
34
+ };
35
+ /**
36
+ * Replace the dump's empty-`search_path` preamble with one naming `schemas`.
37
+ *
38
+ * Throws when the preamble is missing: a dump whose format we no longer
39
+ * recognize must not be written out as if it had been patched.
40
+ * With no extensions there is nothing an operator lookup could need, so the
41
+ * empty path is already correct and the dump is returned untouched.
42
+ */
43
+ const rewriteSearchPathStatement = (dump, schemas) => {
44
+ if (!dump.includes(exports.EMPTY_SEARCH_PATH_STATEMENT)) {
45
+ throw new Error(`pg_dump output does not contain the expected preamble ${exports.EMPTY_SEARCH_PATH_STATEMENT} — ` +
46
+ 'refusing to write a dump whose search_path was not made restorable; ' +
47
+ 'the pg_dump output format has changed and pgpm must be updated');
48
+ }
49
+ if (schemas.length === 0)
50
+ return dump;
51
+ const replacement = `SELECT pg_catalog.set_config('search_path', ${searchPathLiteral(schemas)}, false);`;
52
+ return dump.split(exports.EMPTY_SEARCH_PATH_STATEMENT).join(replacement);
53
+ };
54
+ exports.rewriteSearchPathStatement = rewriteSearchPathStatement;
55
+ /** Make a captured dump restorable against `config`'s extension schemas. */
56
+ const applyExtensionSearchPath = async (config, dump) => (0, exports.rewriteSearchPathStatement)(dump, await (0, exports.getExtensionSchemas)(config));
57
+ exports.applyExtensionSearchPath = applyExtensionSearchPath;
58
+ /** Same, for a dump `pg_dump` wrote to disk itself. */
59
+ const applyExtensionSearchPathToFile = async (config, file) => {
60
+ const schemas = await (0, exports.getExtensionSchemas)(config);
61
+ const dump = (0, fs_1.readFileSync)(file, 'utf8');
62
+ (0, fs_1.writeFileSync)(file, (0, exports.rewriteSearchPathStatement)(dump, schemas), 'utf8');
63
+ return schemas;
64
+ };
65
+ exports.applyExtensionSearchPathToFile = applyExtensionSearchPathToFile;
@@ -8,11 +8,10 @@ import { hasApplySpec, readApplySpec } from './apply-spec';
8
8
  import { loadWorkspaceRoutingProfile, resolveEffectiveApplySpec } from './profile';
9
9
  import { isReuseSpec, materializeReuseModule, resolveSharedModuleName } from './reuse';
10
10
  import { APPLY_SPEC_FILE } from './types';
11
- // Functions that take a bare schema name as a string-literal first argument.
12
- // The AST pass only remaps dotted literals ('schema.object'); these helpers
13
- // (ubiquitous in verify scripts) name the schema alone, so a string-level
14
- // pre-pass remaps them.
15
- const SCHEMA_NAME_LITERAL_FUNCS = ['verify_schema', 'has_schema_privilege'];
11
+ // has_schema_privilege takes a bare schema name as a string literal. The AST
12
+ // pass only remaps dotted literals ('schema.object') and identity casts
13
+ // ('schema'::regnamespace), so this one is remapped by a string pre-pass.
14
+ const SCHEMA_NAME_LITERAL_FUNCS = ['has_schema_privilege'];
16
15
  const schemaNameLiteralPass = (content, schemaMapping) => {
17
16
  const pattern = new RegExp(`\\b(${SCHEMA_NAME_LITERAL_FUNCS.join('|')})\\s*\\(\\s*'([^']+)'`, 'gi');
18
17
  return content.replace(pattern, (match, _fn, schema) => {
@@ -17,9 +17,11 @@ const FOLDER_KIND = {
17
17
  procedures: 'procedure',
18
18
  types: 'type'
19
19
  };
20
- // Mirrors materialize.ts: bare-schema-name literal helpers the AST pass can't
21
- // reach are remapped by a narrow string pre-pass.
22
- const SCHEMA_NAME_LITERAL_FUNCS = ['verify_schema', 'has_schema_privilege'];
20
+ // Mirrors materialize.ts.
21
+ // has_schema_privilege takes a bare schema name as a string literal. The AST
22
+ // pass only remaps dotted literals ('schema.object') and identity casts
23
+ // ('schema'::regnamespace), so this one is remapped by a string pre-pass.
24
+ const SCHEMA_NAME_LITERAL_FUNCS = ['has_schema_privilege'];
23
25
  const schemaNameLiteralPass = (content, schemaMapping) => {
24
26
  const pattern = new RegExp(`\\b(${SCHEMA_NAME_LITERAL_FUNCS.join('|')})\\s*\\(\\s*'([^']+)'`, 'gi');
25
27
  return content.replace(pattern, (match, _fn, schema) => {
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Making a `pg_dump` artifact restorable when extension operators are in play.
3
+ *
4
+ * `pg_dump` opens every plain dump with
5
+ * `SELECT pg_catalog.set_config('search_path', '', false);` (CVE-2018-1058
6
+ * hardening). Types are schema-qualified in the dump body, but operators are
7
+ * not always qualifiable in the source SQL: a trigger's
8
+ * `WHEN (old.embedding IS DISTINCT FROM new.embedding)` resolves its `=`
9
+ * through `search_path`, and `IS DISTINCT FROM` has no `OPERATOR(schema.=)`
10
+ * form. With an empty path the restore dies with
11
+ * `operator does not exist: public.vector = public.vector`.
12
+ *
13
+ * The fix is to put back exactly the schemas the source database's extensions
14
+ * live in — nothing else, so no user schema re-enters the path and the
15
+ * hardening intent survives.
16
+ */
17
+ import { readFileSync, writeFileSync } from 'fs';
18
+ import { getPgPool } from 'pg-cache';
19
+ /** The preamble line `pg_dump` emits, and the one this module rewrites. */
20
+ export const EMPTY_SEARCH_PATH_STATEMENT = "SELECT pg_catalog.set_config('search_path', '', false);";
21
+ /** The schemas the source database's extensions live in, sorted. */
22
+ export const getExtensionSchemas = async (config) => {
23
+ const pool = getPgPool(config);
24
+ const res = await pool.query('SELECT DISTINCT n.nspname FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace');
25
+ return res.rows.map((row) => String(row.nspname)).sort();
26
+ };
27
+ const searchPathLiteral = (schemas) => {
28
+ const path = schemas.map(schema => `"${schema.replace(/"/g, '""')}"`).join(', ');
29
+ return `'${path.replace(/'/g, "''")}'`;
30
+ };
31
+ /**
32
+ * Replace the dump's empty-`search_path` preamble with one naming `schemas`.
33
+ *
34
+ * Throws when the preamble is missing: a dump whose format we no longer
35
+ * recognize must not be written out as if it had been patched.
36
+ * With no extensions there is nothing an operator lookup could need, so the
37
+ * empty path is already correct and the dump is returned untouched.
38
+ */
39
+ export const rewriteSearchPathStatement = (dump, schemas) => {
40
+ if (!dump.includes(EMPTY_SEARCH_PATH_STATEMENT)) {
41
+ throw new Error(`pg_dump output does not contain the expected preamble ${EMPTY_SEARCH_PATH_STATEMENT} — ` +
42
+ 'refusing to write a dump whose search_path was not made restorable; ' +
43
+ 'the pg_dump output format has changed and pgpm must be updated');
44
+ }
45
+ if (schemas.length === 0)
46
+ return dump;
47
+ const replacement = `SELECT pg_catalog.set_config('search_path', ${searchPathLiteral(schemas)}, false);`;
48
+ return dump.split(EMPTY_SEARCH_PATH_STATEMENT).join(replacement);
49
+ };
50
+ /** Make a captured dump restorable against `config`'s extension schemas. */
51
+ export const applyExtensionSearchPath = async (config, dump) => rewriteSearchPathStatement(dump, await getExtensionSchemas(config));
52
+ /** Same, for a dump `pg_dump` wrote to disk itself. */
53
+ export const applyExtensionSearchPathToFile = async (config, file) => {
54
+ const schemas = await getExtensionSchemas(config);
55
+ const dump = readFileSync(file, 'utf8');
56
+ writeFileSync(file, rewriteSearchPathStatement(dump, schemas), 'utf8');
57
+ return schemas;
58
+ };
package/esm/index.js CHANGED
@@ -4,6 +4,7 @@ export * from './core/class/pgpm';
4
4
  export * from './core/template-scaffold';
5
5
  export * from './diff';
6
6
  export * from './dump/pg-dump';
7
+ export * from './dump/search-path';
7
8
  export * from './extensions';
8
9
  export * from './modules/modules';
9
10
  export * from './packaging/check';
package/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from './core/class/pgpm';
4
4
  export * from './core/template-scaffold';
5
5
  export * from './diff';
6
6
  export * from './dump/pg-dump';
7
+ export * from './dump/search-path';
7
8
  export * from './extensions';
8
9
  export * from './modules/modules';
9
10
  export * from './packaging/check';
package/index.js CHANGED
@@ -21,6 +21,7 @@ __exportStar(require("./core/class/pgpm"), exports);
21
21
  __exportStar(require("./core/template-scaffold"), exports);
22
22
  __exportStar(require("./diff"), exports);
23
23
  __exportStar(require("./dump/pg-dump"), exports);
24
+ __exportStar(require("./dump/search-path"), exports);
24
25
  __exportStar(require("./extensions"), exports);
25
26
  __exportStar(require("./modules/modules"), exports);
26
27
  __exportStar(require("./packaging/check"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgpmjs/core",
3
- "version": "7.25.2",
3
+ "version": "7.25.3",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PGPM Package and Migration Tools",
6
6
  "main": "index.js",
@@ -69,5 +69,5 @@
69
69
  "pgsql-parser": "^18.2.3",
70
70
  "yanse": "^0.2.2"
71
71
  },
72
- "gitHead": "905c4c530f09ac15621b554225be060609a4a0a3"
72
+ "gitHead": "a7ba21122bd39663b40fb8fc3afe6eb9b69e0041"
73
73
  }