@opengis/fastify-table 1.1.150 → 1.1.151

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/config.js CHANGED
@@ -5,6 +5,7 @@ const config = fileName ? JSON.parse(fs.readFileSync(fileName)) : {};
5
5
 
6
6
  Object.assign(config, {
7
7
  allTemplates: config?.allTemplates || {},
8
+ skipCheckPolicyRoutes: [],
8
9
  });
9
10
 
10
11
  export default config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.1.150",
3
+ "version": "1.1.151",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -27,16 +27,16 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@fastify/sensible": "^5.0.0",
30
- "@fastify/url-data": "^5.4.0",
31
- "@opengis/fastify-hb": "^1.4.8",
30
+ "@fastify/url-data": "5.4.0",
31
+ "@opengis/fastify-hb": "1.4.8",
32
32
  "fastify": "^4.26.1",
33
33
  "fastify-plugin": "^4.0.0",
34
- "ioredis": "^5.3.2",
35
- "js-yaml": "^4.1.0",
36
- "pg": "^8.11.3",
37
- "pino": "^9.5.0",
38
- "pino-abstract-transport": "^2.0.0",
39
- "uglify-js": "^3.19.3"
34
+ "ioredis": "5.3.2",
35
+ "js-yaml": "4.1.0",
36
+ "pg": "8.11.3",
37
+ "pino": "9.5.0",
38
+ "pino-abstract-transport": "2.0.0",
39
+ "uglify-js": "3.19.3"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@panzoom/panzoom": "^4.5.1",
@@ -1,6 +1,7 @@
1
1
  import { config, logger } from '../../../../utils.js';
2
2
  import block from '../sqlInjection.js';
3
3
 
4
+ const { prefix = '/api', skipCheckPolicyRoutes = [] } = config;
4
5
  /**
5
6
  * Middleware func
6
7
  *
@@ -11,7 +12,7 @@ import block from '../sqlInjection.js';
11
12
  * @returns {object|null} Returns object
12
13
  */
13
14
 
14
- export default function checkPolicy(req) {
15
+ export default function checkPolicy(req, reply) {
15
16
  const {
16
17
  originalUrl: path, hostname, query, params, headers, method, session, routeOptions, unittest,
17
18
  } = req;
@@ -27,10 +28,10 @@ export default function checkPolicy(req) {
27
28
 
28
29
  /*= == 0.Check superadmin access === */
29
30
  if (policy.includes('superadmin') && user?.user_type !== 'superadmin') {
30
- logger.file('access', {
31
+ logger.file('policy/access', {
31
32
  path, method, params, query, body, message: 'access restricted: not superadmin', uid: user?.uid,
32
33
  });
33
- return { message: 'access restricted: 0', status: 403 };
34
+ return reply.status(403).send('access restricted: 0');
34
35
  }
35
36
 
36
37
  /*= == 1.File injection === */
@@ -38,7 +39,7 @@ export default function checkPolicy(req) {
38
39
  logger.file('injection/file', {
39
40
  path, method, params, query, body, message: 'access restricted: 1', uid: user?.uid,
40
41
  });
41
- return { message: 'access restricted: 1', status: 403 };
42
+ return reply.status(403).send('access restricted: 1');
42
43
  }
43
44
 
44
45
  /* === 1.1 File === */
@@ -54,11 +55,11 @@ export default function checkPolicy(req) {
54
55
  logger.file('injection/sql', {
55
56
  path, method, params, query, body, stopWords, message: 'access restricted: 2', uid: user?.uid,
56
57
  });
57
- return { message: 'access restricted: 2', status: 403 };
58
+ return reply.status(403).send('access restricted: 2');
58
59
  }
59
60
  }
60
61
  /* policy: skip if not API */
61
- const isApi = ['/files/', '/api/format/', '/api', '/api-user/', '/logger', '/file/'].filter((el) => path.includes(el)).length;
62
+ const isApi = ['/files/', '/api/', '/api-user/', '/logger', '/file/'].filter((el) => path.includes(el)).length;
62
63
  if (!isApi) {
63
64
  return null;
64
65
  }
@@ -68,12 +69,23 @@ export default function checkPolicy(req) {
68
69
  return null;
69
70
  }
70
71
 
72
+ /* === 0. policy: unauthorized access from admin URL === */
73
+ if (!user?.uid && !config.auth?.disable && isAdmin && !policy.includes('public') && !skipCheckPolicyRoutes.filter((el) => el).find(el => req.url.includes(el))) {
74
+ if (!req.url.startsWith(prefix) && req.url.startsWith('/api')) {
75
+ return reply.redirect(config?.auth?.redirect || '/login');
76
+ }
77
+ logger.file('policy/unauthorized', {
78
+ path, method, params, query, body, message: 'unauthorized',
79
+ });
80
+ return reply.status(401).send('unauthorized');
81
+ }
82
+
71
83
  /* === 3. policy: user === */
72
- if (!user && policy.includes('user') && false) {
84
+ if (!user && policy.includes('user')) {
73
85
  logger.file('policy/user', {
74
86
  path, method, params, query, body, message: 'access restricted: 3',
75
87
  });
76
- return { message: 'access restricted: 3', status: 403 };
88
+ return reply.status(403).send('access restricted: 3');
77
89
  }
78
90
 
79
91
  /* === 4. policy: referer === */
@@ -81,7 +93,7 @@ export default function checkPolicy(req) {
81
93
  logger.file('policy/referer', {
82
94
  path, method, params, query, body, message: 'access restricted: 4', uid: user?.uid,
83
95
  });
84
- return { message: 'access restricted: 4', status: 403 };
96
+ return reply.status(403).send('access restricted: 4');
85
97
  }
86
98
 
87
99
  /* === 5. policy: site auth === */
@@ -90,7 +102,7 @@ export default function checkPolicy(req) {
90
102
  logger.file('policy/site', {
91
103
  path, method, params, query, body, message: 'access restricted: 5', uid: user?.uid,
92
104
  });
93
- return { message: 'access restricted: 5', status: 403 };
105
+ return reply.status(403).send('access restricted: 5');
94
106
  }
95
107
 
96
108
  /* === 6. base policy: block api, except login === */
@@ -99,7 +111,7 @@ export default function checkPolicy(req) {
99
111
  logger.file('policy/api', {
100
112
  path, method, params, query, body, message: 'access restricted: 6', uid: user?.uid,
101
113
  });
102
- return { message: 'access restricted: 6', status: 403 };
114
+ return reply.status(403).send('access restricted: 6');
103
115
  }
104
116
 
105
117
  return null;
@@ -2,9 +2,9 @@ import checkPolicy from './funcs/checkPolicy.js';
2
2
 
3
3
  async function plugin(fastify) {
4
4
  fastify.addHook('preParsing', async (request, reply) => {
5
- const hookData = checkPolicy(request);
6
- if (hookData?.status && hookData?.message) {
7
- return reply.status(hookData?.status).send(hookData.message);
5
+ const resp = checkPolicy(request, reply);
6
+ if (resp) {
7
+ return resp;
8
8
  }
9
9
  });
10
10
  }