@heliyos/heliyos-api-core 1.0.67 → 1.0.68
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/middleware.js +54 -9
- package/package.json +1 -1
package/dist/middleware.js
CHANGED
|
@@ -19,6 +19,55 @@ const serve_static_1 = __importDefault(require("serve-static"));
|
|
|
19
19
|
const authentication_1 = require("./authentication");
|
|
20
20
|
const allowedOrigin_1 = require("./allowedOrigin");
|
|
21
21
|
const customError_1 = require("./@types/globals/customError");
|
|
22
|
+
const genericErrorMessage = "Something went wrong";
|
|
23
|
+
const defaultErrorStatusCode = 500;
|
|
24
|
+
const nonProductionEnvironments = new Set(["development", "local", "test"]);
|
|
25
|
+
const sensitiveErrorMessagePatterns = [
|
|
26
|
+
/https?:\/\//i,
|
|
27
|
+
/\bapi\.[a-z0-9.-]+\b/i,
|
|
28
|
+
/\btraceback\b/i,
|
|
29
|
+
/\bstack trace\b/i,
|
|
30
|
+
/\b(httpx|requests|axios)\b/i,
|
|
31
|
+
/\bparallel(?:\.ai)?\b/i,
|
|
32
|
+
];
|
|
33
|
+
const isProductionEnvironment = () => {
|
|
34
|
+
const nodeEnv = (process.env.NODE_ENV || "").toLowerCase();
|
|
35
|
+
return !nonProductionEnvironments.has(nodeEnv);
|
|
36
|
+
};
|
|
37
|
+
const toStatusCode = (status) => Number.isFinite(Number(status))
|
|
38
|
+
? Number(status)
|
|
39
|
+
: defaultErrorStatusCode;
|
|
40
|
+
const isSensitiveErrorMessage = (message) => {
|
|
41
|
+
if (!message) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
if (message.length > 220 || message.includes("\n") || message.includes("\r")) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return sensitiveErrorMessagePatterns.some((pattern) => pattern.test(message));
|
|
48
|
+
};
|
|
49
|
+
const sanitizeErrorMessage = (message, statusCode) => {
|
|
50
|
+
if (!isProductionEnvironment()) {
|
|
51
|
+
return message || genericErrorMessage;
|
|
52
|
+
}
|
|
53
|
+
const normalizedMessage = (message || "").trim();
|
|
54
|
+
if (!normalizedMessage) {
|
|
55
|
+
return genericErrorMessage;
|
|
56
|
+
}
|
|
57
|
+
if (statusCode >= 500 && isSensitiveErrorMessage(normalizedMessage)) {
|
|
58
|
+
return genericErrorMessage;
|
|
59
|
+
}
|
|
60
|
+
if (statusCode < 500 && isSensitiveErrorMessage(normalizedMessage)) {
|
|
61
|
+
return genericErrorMessage;
|
|
62
|
+
}
|
|
63
|
+
return normalizedMessage;
|
|
64
|
+
};
|
|
65
|
+
const sanitizeErrorDescription = (description) => {
|
|
66
|
+
if (isProductionEnvironment()) {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
return description;
|
|
70
|
+
};
|
|
22
71
|
/**
|
|
23
72
|
* Function to handle invalid routes
|
|
24
73
|
* @param _
|
|
@@ -134,10 +183,10 @@ const apply_cors = (req, res, next) => {
|
|
|
134
183
|
*/
|
|
135
184
|
const handle_errors = (error, _, res, __) => {
|
|
136
185
|
const { message, description, stack, status } = error;
|
|
137
|
-
const
|
|
186
|
+
const statusCode = toStatusCode(status);
|
|
138
187
|
const response = {
|
|
139
|
-
message,
|
|
140
|
-
description,
|
|
188
|
+
message: sanitizeErrorMessage(message, statusCode),
|
|
189
|
+
description: sanitizeErrorDescription(description),
|
|
141
190
|
};
|
|
142
191
|
// Log original error
|
|
143
192
|
console.error("=== Begin Error ===\n---\n" +
|
|
@@ -153,14 +202,10 @@ const handle_errors = (error, _, res, __) => {
|
|
|
153
202
|
"Stack: " +
|
|
154
203
|
stack +
|
|
155
204
|
"\n---\n=== End Error ===");
|
|
156
|
-
if (!errorStatus) {
|
|
157
|
-
response.message = "Internal server error";
|
|
158
|
-
}
|
|
159
205
|
// Provide stack track in env development and local
|
|
160
|
-
if (
|
|
161
|
-
process.env.NODE_ENV === "local") {
|
|
206
|
+
if (!isProductionEnvironment()) {
|
|
162
207
|
response.error = Object.assign({ stack }, error);
|
|
163
208
|
}
|
|
164
209
|
// Send status and response
|
|
165
|
-
return res.status(
|
|
210
|
+
return res.status(statusCode).json(response);
|
|
166
211
|
};
|