@opengis/fastify-table 1.5.7 → 1.5.9

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/dist/config.js CHANGED
@@ -11,7 +11,6 @@ const { skipKeys = ["windir"] } = config;
11
11
  Object.assign(config, {
12
12
  storageList: {},
13
13
  allTemplates: config?.allTemplates || {},
14
- skipCheckPolicyRoutes: [],
15
14
  env: process.env?.NODE_ENV,
16
15
  });
17
16
  if (process.env?.NODE_ENV && existsSync(`.env.${process.env.NODE_ENV}`)) {
@@ -80,6 +80,7 @@ handlebars.registerHelper("formatNumber", formatNumber);
80
80
  handlebars.registerHelper("formatRelative", formatRelative);
81
81
  handlebars.registerHelper("formatUnit", formatUnit);
82
82
  handlebars.registerHelper("num_format", numFormat);
83
+ handlebarsSync.registerHelper("num_format", numFormat);
83
84
  handlebars.registerHelper("set", set);
84
85
  // string
85
86
  handlebars.registerHelper("str_replace", strReplace);
@@ -6,7 +6,7 @@ CREATE EXTENSION if not exists pg_trgm SCHEMA public VERSION "1.5";
6
6
  -- drop old
7
7
  DROP TABLE IF EXISTS admin.user_properties;
8
8
  DROP TABLE IF EXISTS admin.table_properties;
9
- DROP SCHEMA IF EXISTS setting cascade;
9
+ -- DROP SCHEMA IF EXISTS setting cascade;
10
10
 
11
11
  CREATE TABLE IF NOT EXISTS admin.properties();
12
12
  ALTER TABLE admin.properties DROP CONSTRAINT IF EXISTS admin_properties_property_id_pkey;
@@ -1,10 +1,14 @@
1
1
  import { config, logger } from "../../../../utils.js";
2
2
  import block from "../sqlInjection.js";
3
- const { skipCheckPolicyRoutes = [] } = config;
4
- const skipCheckPolicy = (path) => skipCheckPolicyRoutes.find((el) => path.includes(el));
5
3
  export default function checkPolicy(req, reply) {
6
4
  const { originalUrl: path, hostname, query, params, headers, method, routeOptions, unittest, } = req;
7
- if (config.local || unittest || config.env === "test") {
5
+ // ! skip locally, skip tests
6
+ if (config.local || unittest || process.env.NODE_ENV === "test") {
7
+ return null;
8
+ }
9
+ // ! skip non-API Requests
10
+ const isApi = ["/files/", "/api/", "/api-user/", "/logger", "/file/"].filter((el) => path.includes(el)).length;
11
+ if (!isApi) {
8
12
  return null;
9
13
  }
10
14
  const body = JSON.stringify(req?.body || {}).substring(30);
@@ -13,26 +17,28 @@ export default function checkPolicy(req, reply) {
13
17
  config.admin ||
14
18
  hostname.startsWith("admin");
15
19
  const user = req.user || req.session?.passport?.user;
16
- const isUser = config?.debug || !!user;
17
20
  const isServer = process.argv[2];
18
- const { policy = [] } = (routeOptions?.config ||
19
- {});
20
- /*= == 0.Check superadmin access === */
21
- if (policy.includes("admin") &&
22
- user?.user_type !== "admin" &&
23
- !config.auth?.disable) {
24
- logger.file("policy/access", {
21
+ const { role, allowSite, // ?
22
+ allowSql, // ?
23
+ policy = [], } = (routeOptions?.config || {});
24
+ const requireReferer = policy?.includes?.("referer") ||
25
+ routeOptions?.config
26
+ ?.requireReferer; // ?
27
+ const isRole = (role && user?.user_type !== role) ||
28
+ (policy.includes("admin") && user?.user_type !== "admin");
29
+ // ! role
30
+ if (isRole) {
31
+ logger.file("policy/role", {
25
32
  path,
26
33
  method,
27
34
  params,
28
35
  query,
29
36
  body,
30
- message: "access restricted: not admin",
31
37
  uid: user?.uid,
32
38
  });
33
39
  return reply.status(403).send("access restricted: 0");
34
40
  }
35
- /*= == 1.File injection === */
41
+ // ! file injection
36
42
  if (JSON.stringify(params || {})?.includes("../") ||
37
43
  JSON.stringify(query || {})?.includes("../") ||
38
44
  path?.includes("../")) {
@@ -42,18 +48,19 @@ export default function checkPolicy(req, reply) {
42
48
  params,
43
49
  query,
44
50
  body,
45
- message: "access restricted: 1",
46
51
  uid: user?.uid,
47
52
  });
48
53
  return reply.status(403).send("access restricted: 1");
49
54
  }
50
- /* === 1.1 File === */
55
+ // ! invalid file extension
51
56
  const allowExtPublic = [".png", ".jpg", ".svg"];
52
57
  const ext = path.toLowerCase().substr(-4);
53
- if (path.includes("files/") && allowExtPublic.includes(ext))
58
+ if (path.includes("files/") && allowExtPublic.includes(ext)) {
54
59
  return null;
55
- /* === 2.SQL Injection policy: no-sql === */
56
- if (!policy.includes("no-sql")) {
60
+ }
61
+ const noSql = Array.isArray(policy) ? policy.includes("no-sql") : allowSql;
62
+ // ! sql injection
63
+ if (!noSql) {
57
64
  // skip polyline param - data filter (geometry bounds)
58
65
  const stopWords = block.filter((el) => path.replace(query.polyline, "").includes(el));
59
66
  if (stopWords?.length) {
@@ -64,17 +71,11 @@ export default function checkPolicy(req, reply) {
64
71
  query,
65
72
  body,
66
73
  stopWords,
67
- message: "access restricted: 2",
68
74
  uid: user?.uid,
69
75
  });
70
76
  return reply.status(403).send("access restricted: 2");
71
77
  }
72
78
  }
73
- /* policy: skip if not API */
74
- const isApi = ["/files/", "/api/", "/api-user/", "/logger", "/file/"].filter((el) => path.includes(el)).length;
75
- if (!isApi) {
76
- return null;
77
- }
78
79
  const validToken = (req.ip === "193.239.152.181" ||
79
80
  req.ip === "127.0.0.1" ||
80
81
  req.ip?.startsWith?.("192.168.") ||
@@ -87,76 +88,55 @@ export default function checkPolicy(req, reply) {
87
88
  user_type: req.ip === "193.239.152.181" || config.debug ? "admin" : "regular",
88
89
  };
89
90
  }
90
- /* === policy: public === */
91
- if (policy.includes("public") ||
92
- skipCheckPolicy(path) ||
93
- !config.pg ||
94
- config.auth?.disable ||
95
- config.local ||
96
- config.debug) {
97
- return null;
98
- }
99
- /* === 0. policy: unauthorized access from admin URL === */
100
- if (!validToken && !user?.uid && isAdmin && !policy.includes("public")) {
101
- logger.file("policy/unauthorized", {
91
+ // ! referer
92
+ if (requireReferer && !headers?.referer?.includes?.(hostname)) {
93
+ logger.file("policy/referer", {
102
94
  path,
103
95
  method,
104
96
  params,
105
97
  query,
106
98
  body,
107
- token: headers?.token,
108
- userId: headers?.uid,
109
- ip: req.ip,
110
- headers,
111
- message: "unauthorized",
99
+ uid: user?.uid,
112
100
  });
113
- return reply.status(401).send("unauthorized");
101
+ return reply.status(403).send("access restricted: 4");
102
+ }
103
+ const isPublic = Array.isArray(policy)
104
+ ? policy.includes("public")
105
+ : policy === "L0";
106
+ // ! public / token
107
+ if (isPublic || validToken || config.debug) {
108
+ return null;
114
109
  }
115
- /* === 3. policy: user === */
116
- if (!validToken &&
117
- !user &&
118
- policy.includes("user") &&
119
- !skipCheckPolicy(path)) {
110
+ const requireUser = Array.isArray(policy)
111
+ ? policy.includes("user")
112
+ : ["L1", "L2", "L3"].includes(policy);
113
+ // ! user required, but not logged in
114
+ if (requireUser && !user) {
120
115
  logger.file("policy/user", {
121
116
  path,
122
117
  method,
123
118
  params,
124
119
  query,
125
120
  body,
126
- message: "access restricted: 3",
127
121
  });
128
122
  return reply.status(403).send("access restricted: 3");
129
123
  }
130
- /* === 4. policy: referer === */
131
- if (!validToken &&
132
- !headers?.referer?.includes?.(hostname) &&
133
- policy.includes("referer")) {
134
- logger.file("policy/referer", {
124
+ const isSite = (Array.isArray(policy) ? policy.includes("site") : allowSite) && !isAdmin; // ? new policy level?
125
+ // ! site
126
+ if (!isSite && false) {
127
+ logger.file("policy/site", {
135
128
  path,
136
129
  method,
137
130
  params,
138
131
  query,
139
132
  body,
140
- message: "access restricted: 4",
133
+ message: "access restricted: 5",
141
134
  uid: user?.uid,
142
135
  });
143
- return reply.status(403).send("access restricted: 4");
144
- }
145
- /* === 5. policy: site auth === */
146
- /* if (!validToken && !policy.includes("site") && !isAdmin) {
147
- logger.file("policy/site", {
148
- path,
149
- method,
150
- params,
151
- query,
152
- body,
153
- message: "access restricted: 5",
154
- uid: user?.uid,
155
- });
156
136
  return reply.status(403).send("access restricted: 5");
157
- }*/
158
- /* === 6. base policy: block non-public api w/ out authorization === */
159
- if (!validToken && isAdmin && !config.debug && user?.uid && isServer) {
137
+ }
138
+ // ! any API by default
139
+ if (isAdmin && !config.debug && user?.uid && isServer) {
160
140
  logger.file("policy/api", {
161
141
  path,
162
142
  method,
@@ -168,6 +148,5 @@ export default function checkPolicy(req, reply) {
168
148
  });
169
149
  return reply.status(403).send("access restricted: 6");
170
150
  }
171
- // console.log(headers);
172
151
  return null;
173
152
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.5.7",
3
+ "version": "1.5.9",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -29,7 +29,7 @@
29
29
  "test": "node --test",
30
30
  "compress": "node compress.js",
31
31
  "dev1": "set NODE_ENV=local&& node server.js",
32
- "dev": "bun start",
32
+ "dev": "NODE_ENV=production bun start",
33
33
  "start": "bun server"
34
34
  },
35
35
  "dependencies": {