@opengis/fastify-table 1.1.121 → 1.1.122

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.1.121",
3
+ "version": "1.1.122",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": ["fastify", "table", "crud", "pg", "backend" ],
@@ -9,18 +9,19 @@ export default async function getMeta(opt) {
9
9
 
10
10
  if (data[table]) return data[table];
11
11
 
12
- if (!pg.tlist?.includes(table)) {
12
+ if (!pg.tlist?.includes(table.replace(/"/g, ''))) {
13
13
  return { error: `${table} - not found`, status: 400 };
14
14
  }
15
15
 
16
- const { fields } = await pg.query(`select * from ${table} where $1=$1 limit 0`, [1]);
17
- const pks1 = await pg.query(`SELECT json_object_agg(c.conrelid::regclass, a.attname) as pks1 FROM pg_constraint c
18
- left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1] WHERE c.contype='p'::"char" and c.conrelid::regclass = $1::regclass`, [table])
19
- .then(el => el.rows[0]?.pks1 || {});
16
+ const { fields = [] } = await pg.query(`select * from ${table} limit 0`);
17
+ const { pks1 = {} } = await pg.query(`SELECT json_object_agg(c.conrelid::regclass, a.attname) as pks1
18
+ FROM pg_constraint c
19
+ left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1]
20
+ WHERE c.contype='p'::"char" and c.conrelid::regclass = $1::regclass`, [table]).then(el => el.rows?.[0] || {});
20
21
 
21
22
  const pk = table.startsWith('public.') ? pks1[table.replace('public.', '')] : pks1[table];
22
23
 
23
- const geomAttr = fields.find((el) => pg.pgType[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
24
+ const geomAttr = fields.find((el) => pg.pgType?.[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
24
25
 
25
26
  const res = {
26
27
  pk, columns: fields, geom: geomAttr, view: pg.relkinds?.[table] === 'v',
@@ -13,20 +13,21 @@ import block from '../sqlInjection.js';
13
13
 
14
14
  export default function checkPolicy(req) {
15
15
  const {
16
- originalUrl: path, hostname, query, params, headers, method,
16
+ originalUrl: path, hostname, query, params, headers, method, session, routeOptions,
17
17
  } = req;
18
- const isAdmin = process.env.NODE_ENV === 'admin';
19
- const user = req.user || req.session?.passport?.user;
18
+ const body = JSON.stringify(req?.body || {}).substring(30);
19
+ const isAdmin = process.env.NODE_ENV === 'admin' || hostname.split(':').shift() === config.adminDomain || hostname.startsWith('admin');
20
+ const user = req.user || session?.passport?.user;
20
21
 
21
22
  const isUser = config?.debug || !!user;
22
23
 
23
24
  const isServer = process.argv[2];
24
- const { policy = [] } = req.routeOptions?.config || {};
25
+ const { policy = [] } = routeOptions?.config || {};
25
26
 
26
27
  /*= == 0.Check superadmin access === */
27
28
  if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
28
29
  logger.file('access', {
29
- path, method, params, query, body: JSON.stringify(req?.body || {}).substring(30), message: 'access restricted: not superadmin', uid: user?.uid,
30
+ path, method, params, query, body, message: 'access restricted: not superadmin', uid: user?.uid,
30
31
  });
31
32
  return { message: 'access restricted: 0', status: 403 };
32
33
  }
@@ -34,7 +35,7 @@ export default function checkPolicy(req) {
34
35
  /*= == 1.File injection === */
35
36
  if (JSON.stringify(params || {})?.includes('../') || JSON.stringify(query || {})?.includes('../') || path?.includes('../')) {
36
37
  logger.file('injection/file', {
37
- path, method, params, query, message: 'access restricted: 1', uid: user?.uid,
38
+ path, method, params, query, body, message: 'access restricted: 1', uid: user?.uid,
38
39
  });
39
40
  return { message: 'access restricted: 1', status: 403 };
40
41
  }
@@ -50,7 +51,7 @@ export default function checkPolicy(req) {
50
51
  const stopWords = block.filter((el) => path.replace(query.polyline, '').includes(el));
51
52
  if (stopWords?.length) {
52
53
  logger.file('injection/sql', {
53
- path, method, stopWords, message: 'access restricted: 2', uid: user?.uid,
54
+ path, method, params, query, body, stopWords, message: 'access restricted: 2', uid: user?.uid,
54
55
  });
55
56
  return { message: 'access restricted: 2', status: 403 };
56
57
  }
@@ -68,14 +69,16 @@ export default function checkPolicy(req) {
68
69
 
69
70
  /* === 3. policy: user === */
70
71
  if (!user && policy.includes('user') && false) {
71
- logger.file('policy/user', { path, method, message: 'access restricted: 3' });
72
+ logger.file('policy/user', {
73
+ path, method, params, query, body, message: 'access restricted: 3',
74
+ });
72
75
  return { message: 'access restricted: 3', status: 403 };
73
76
  }
74
77
 
75
78
  /* === 4. policy: referer === */
76
79
  if (!headers?.referer?.includes?.(hostname) && policy.includes('referer') && !config.local && !config.debug) {
77
80
  logger.file('policy/referer', {
78
- path, method, message: 'access restricted: 4', uid: user?.uid,
81
+ path, method, params, query, body, message: 'access restricted: 4', uid: user?.uid,
79
82
  });
80
83
  return { message: 'access restricted: 4', status: 403 };
81
84
  }
@@ -84,7 +87,7 @@ export default function checkPolicy(req) {
84
87
  if (!policy.includes('site') && !isAdmin && !config.local && !config.debug
85
88
  && !['/auth/redirect', `${config.prefix || '/api'}/login`].find(el => path.includes(el))) {
86
89
  logger.file('policy/site', {
87
- path, method, message: 'access restricted: 5', uid: user?.uid,
90
+ path, method, params, query, body, message: 'access restricted: 5', uid: user?.uid,
88
91
  });
89
92
  return { message: 'access restricted: 5', status: 403 };
90
93
  }
@@ -93,7 +96,7 @@ export default function checkPolicy(req) {
93
96
  if (isAdmin && !isUser && isServer && !config.local && !config.debug
94
97
  && !path.startsWith(`${config.prefix || '/api'}/login`)) {
95
98
  logger.file('policy/api', {
96
- path, method, message: 'access restricted: 6', uid: user?.uid,
99
+ path, method, params, query, body, message: 'access restricted: 6', uid: user?.uid,
97
100
  });
98
101
  return { message: 'access restricted: 6', status: 403 };
99
102
  }