@fatehan/tsrp 1.3.42 → 1.3.44

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.
@@ -1 +1 @@
1
- {"version":3,"file":"system.io.d.ts","sourceRoot":"","sources":["../src/system.io.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,QAAQ,EAET,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AAEnD,QAAA,MAAM,YAAY,GAChB,UAAU,QAAQ,EAAE,EACpB,UAAU,QAAQ,EAAE,EACpB,MAAM,IAAI,KACT,OAAO,EAwGT,CAAC;AA6BF,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"system.io.d.ts","sourceRoot":"","sources":["../src/system.io.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,QAAQ,EAET,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AAEnD,QAAA,MAAM,YAAY,GAChB,UAAU,QAAQ,EAAE,EACpB,UAAU,QAAQ,EAAE,EACpB,MAAM,IAAI,KACT,OAAO,EA2FT,CAAC;AA2IF,eAAe,YAAY,CAAC"}
package/dist/system.io.js CHANGED
@@ -12,19 +12,8 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
12
12
  const tempSystemIo = [...systemIo, ...deviceIo].sort((a, b) => a.sord - b.sord);
13
13
  tempSystemIo.forEach((io) => {
14
14
  var _a;
15
- let items = extractVariables(io.formula);
16
- let formula = io.formula;
17
- let hasIos = true;
18
- for (let i = 0; i < items.length; i++) {
19
- const value = getNestedPropertyValue(data, items[i]);
20
- if (value == null) {
21
- hasIos = false;
22
- break;
23
- }
24
- formula = formula.replace(new RegExp("\\$" + items[i], "g"), value);
25
- }
26
- if (hasIos) {
27
- const value = eval(formula);
15
+ const value = evalExpressionSafe(io.formula, data);
16
+ if (value !== null) {
28
17
  let style = io.activeStyle;
29
18
  if (io.type === devices_1.SystemIo_SystemIoType.BOOLEAN) {
30
19
  style =
@@ -33,13 +22,13 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
33
22
  : io.activeStyle;
34
23
  }
35
24
  let newValue = value;
36
- if (io.unknown != null && io.unknown != undefined) {
25
+ if (io.unknown != null) {
37
26
  for (const key in io.unknown) {
38
27
  let UnkValue = io.unknown[key];
39
28
  if (key.toString().toLowerCase().includes("x")) {
40
- const formula = key.toLowerCase().replace(/x/g, newValue);
41
- const tempVal = eval(formula);
42
- if (tempVal) {
29
+ const formula = key.toLowerCase();
30
+ const tempVal = safeEvalFormula(formula, Number(newValue));
31
+ if (tempVal !== null && tempVal) {
43
32
  newValue = UnkValue;
44
33
  io.unit = "";
45
34
  }
@@ -54,14 +43,16 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
54
43
  }
55
44
  let hidden = false;
56
45
  io.hidden.forEach((item) => {
57
- if (item.toLowerCase().includes("x")) {
58
- const formula = item.toLowerCase().replace(/x/g, newValue);
59
- if (eval(formula)) {
46
+ const lower = item.toLowerCase();
47
+ if (lower.includes("x")) {
48
+ const formula = lower;
49
+ const ok = safeEvalCondition(formula, Number(newValue));
50
+ if (ok === true) {
60
51
  hidden = true;
61
52
  }
62
53
  }
63
54
  else {
64
- if (newValue.toString().includes(item)) {
55
+ if (String(newValue).includes(item)) {
65
56
  hidden = true;
66
57
  }
67
58
  }
@@ -71,7 +62,7 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
71
62
  newValue = Number(newValue).toFixed(io.DecimalCount);
72
63
  }
73
64
  else if (io.DecimalCount === 0) {
74
- newValue = Math.round(newValue).toString();
65
+ newValue = Math.round(Number(newValue)).toString();
75
66
  }
76
67
  if (io.SeparatorsCount && io.SeparatorsCount > 0) {
77
68
  newValue = Separator(String(newValue), (_a = io.SeparatorsCount) !== null && _a !== void 0 ? _a : 0);
@@ -99,24 +90,129 @@ function Separator(value, seprator) {
99
90
  const formattedInt = intPart.replace(regex, ",");
100
91
  return decimalPart ? `${formattedInt}.${decimalPart}` : formattedInt;
101
92
  }
102
- function extractVariables(str) {
103
- const matches = str.match(/\$(\w+(?:\.\w+)*)/g);
104
- if (!matches)
105
- return [];
106
- return matches.map((match) => match.substring(1));
93
+ function safeEvalFormula(formula, xValue) {
94
+ try {
95
+ const replaced = formula.replace(/x/g, String(xValue));
96
+ if (!/^[0-9+\-*/ ().]+$/.test(replaced))
97
+ return null;
98
+ const fn = new Function(`return (${replaced});`);
99
+ const result = fn();
100
+ return typeof result === "number" && !isNaN(result) ? result : null;
101
+ }
102
+ catch {
103
+ return null;
104
+ }
105
+ }
106
+ function safeEvalCondition(formula, xValue) {
107
+ try {
108
+ const replaced = formula.replace(/x/g, String(xValue));
109
+ if (!/^[0-9+\-*/ %().<>=!&|]+$/.test(replaced)) {
110
+ return null;
111
+ }
112
+ const forbiddenPatterns = [/;;/, /process\b/, /constructor\b/, /require\b/, /global\b/];
113
+ if (forbiddenPatterns.some((rx) => rx.test(replaced)))
114
+ return null;
115
+ const fn = new Function(`return (${replaced});`);
116
+ const result = fn();
117
+ if (typeof result === "boolean")
118
+ return result;
119
+ if (typeof result === "number")
120
+ return Boolean(result);
121
+ return null;
122
+ }
123
+ catch {
124
+ return null;
125
+ }
126
+ }
127
+ function isPlainObject(v) {
128
+ return v !== null && typeof v === "object" && !Array.isArray(v) && !(v instanceof Uint8Array);
129
+ }
130
+ function getValueByPathCaseInsensitive(obj, path) {
131
+ if (!path)
132
+ return undefined;
133
+ const parts = path.replace(/\[(\d+)\]/g, '.$1').split('.').filter(Boolean);
134
+ let cur = obj;
135
+ for (const rawPart of parts) {
136
+ if (cur == null)
137
+ return undefined;
138
+ if (/^\d+$/.test(rawPart)) {
139
+ const idx = Number(rawPart);
140
+ if (Array.isArray(cur) || cur instanceof Uint8Array) {
141
+ cur = cur[idx];
142
+ continue;
143
+ }
144
+ else {
145
+ return undefined;
146
+ }
147
+ }
148
+ if (isPlainObject(cur)) {
149
+ const lower = rawPart.toLowerCase();
150
+ const foundKey = Object.keys(cur).find(k => k.toLowerCase() === lower);
151
+ if (foundKey !== undefined) {
152
+ cur = cur[foundKey];
153
+ continue;
154
+ }
155
+ else {
156
+ return undefined;
157
+ }
158
+ }
159
+ return undefined;
160
+ }
161
+ return cur;
107
162
  }
108
- function getNestedPropertyValue(obj, propertyPath) {
109
- const properties = propertyPath.split(".");
110
- let currentObj = obj;
111
- for (const property of properties) {
112
- if (!Object.prototype.hasOwnProperty.call(currentObj, property)) {
163
+ function evalExpressionSafe(template, context) {
164
+ try {
165
+ if (typeof template !== 'string')
166
+ return null;
167
+ const replaced = template.replace(/\$([A-Za-z0-9_.\[\]]+)/gi, (_m, expr) => {
168
+ const value = getValueByPathCaseInsensitive(context, expr);
169
+ if (value === undefined || value === null)
170
+ return "null";
171
+ if (typeof value === "number")
172
+ return String(value);
173
+ if (typeof value === "boolean")
174
+ return value ? "true" : "false";
175
+ if (typeof value === "string")
176
+ return JSON.stringify(value);
177
+ if (value instanceof Uint8Array)
178
+ return `[${Array.from(value).join(',')}]`;
179
+ if (Array.isArray(value)) {
180
+ return `[${value.map(v => (typeof v === 'string' ? JSON.stringify(v) : String(v))).join(',')}]`;
181
+ }
182
+ return "null";
183
+ });
184
+ if (replaced.includes("null")) {
113
185
  return null;
114
186
  }
115
- currentObj = currentObj[property];
116
- if (currentObj === null || currentObj === undefined) {
187
+ const cleaned = replaced
188
+ .replace(/;/g, ' ')
189
+ .replace(/`/g, ' ')
190
+ .replace(/\\/g, ' ')
191
+ .trim();
192
+ const allowedPattern = /^[0-9+\-*/ %().,<>=!&|"\[\]truefalsenull,':\s]*$/i;
193
+ if (!allowedPattern.test(cleaned))
194
+ return null;
195
+ const forbidden = [/constructor\b/i, /process\b/i, /require\b/i, /global\b/i, /__proto__\b/i];
196
+ if (forbidden.some(rx => rx.test(cleaned)))
117
197
  return null;
198
+ if (cleaned.length === 0)
199
+ return null;
200
+ let result;
201
+ try {
202
+ const fn = new Function(`return (${cleaned});`);
203
+ result = fn();
118
204
  }
205
+ catch {
206
+ return null;
207
+ }
208
+ if (result === undefined || result === null)
209
+ return null;
210
+ if (typeof result === "number" || typeof result === "boolean" || typeof result === "string")
211
+ return result;
212
+ return null;
213
+ }
214
+ catch {
215
+ return null;
119
216
  }
120
- return String(currentObj);
121
217
  }
122
218
  exports.default = SystemIoCast;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fatehan/tsrp",
3
- "version": "1.3.42",
3
+ "version": "1.3.44",
4
4
  "description": "fatehan main models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",