@seip/bluebird-js 0.1.6 → 0.1.7
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/.vscode/extensions.json +7 -7
- package/.vscode/settings.json +10 -10
- package/AGENTS.md +620 -576
- package/README.md +607 -602
- package/app/routes/web.js +47 -47
- package/app/views/components/Header.jsx +42 -42
- package/app/views/components/LiveCounter.jsx +22 -21
- package/app/views/layouts/main/view.jsx +201 -201
- package/app/views/{dashboard.jsx → pages/dashboard.jsx} +278 -300
- package/app/views/{index.jsx → pages/index.jsx} +123 -123
- package/app/views/{login.jsx → pages/login.jsx} +141 -141
- package/core/actions.d.ts +1 -1
- package/core/actions.js +355 -329
- package/core/ai.d.ts +151 -151
- package/core/ai.js +1051 -1051
- package/core/app.js +10 -10
- package/core/cli/backup.js +167 -167
- package/core/cli/init.js +10 -10
- package/core/cli/view.js +161 -161
- package/core/database.js +460 -460
- package/core/index.d.ts +1470 -1431
- package/core/jsx.d.ts +38 -38
- package/core/jsx.js +1597 -1432
- package/core/render.js +736 -728
- package/core/view-loader.js +613 -529
- package/package.json +1 -1
- package/public/js/bluebird-client.js +3264 -3160
- /package/app/views/{about.jsx → pages/about.jsx} +0 -0
package/core/actions.js
CHANGED
|
@@ -1,329 +1,355 @@
|
|
|
1
|
-
import Csrf from "./csrf.js";
|
|
2
|
-
import Validator from "./validate.js";
|
|
3
|
-
import Render from "./render.js";
|
|
4
|
-
|
|
5
|
-
const DEFAULT_MAX_BYTES = 64 * 1024;
|
|
6
|
-
const DEFAULT_MAX_DEPTH = 6;
|
|
7
|
-
const DEFAULT_MAX_KEYS = 100;
|
|
8
|
-
const BANNED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Recursively sanitizes and validates an incoming action payload object to prevent
|
|
12
|
-
* prototype pollution (`__proto__`, `constructor`, `prototype`), excessive nesting depth,
|
|
13
|
-
* and key-flooding memory attacks.
|
|
14
|
-
*
|
|
15
|
-
* @param {any} input
|
|
16
|
-
* @param {number} [maxDepth=DEFAULT_MAX_DEPTH]
|
|
17
|
-
* @param {number} [maxKeys=DEFAULT_MAX_KEYS]
|
|
18
|
-
* @param {number} [currentDepth=0]
|
|
19
|
-
* @param {{ count: number }} [keyCounter={ count: 0 }]
|
|
20
|
-
* @returns {any}
|
|
21
|
-
*/
|
|
22
|
-
export function sanitizeActionPayload(
|
|
23
|
-
input,
|
|
24
|
-
maxDepth = DEFAULT_MAX_DEPTH,
|
|
25
|
-
maxKeys = DEFAULT_MAX_KEYS,
|
|
26
|
-
currentDepth = 0,
|
|
27
|
-
keyCounter = { count: 0 }
|
|
28
|
-
) {
|
|
29
|
-
if (input === null || input === undefined) return input;
|
|
30
|
-
if (typeof input !== "object") return input;
|
|
31
|
-
|
|
32
|
-
if (currentDepth > maxDepth) {
|
|
33
|
-
const err = new Error("Payload nesting depth limit exceeded.");
|
|
34
|
-
err.statusCode = 413;
|
|
35
|
-
throw err;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (Array.isArray(input)) {
|
|
39
|
-
if (input.length > maxKeys) {
|
|
40
|
-
const err = new Error(`Payload array length exceeds maximum allowed (${maxKeys}).`);
|
|
41
|
-
err.statusCode = 413;
|
|
42
|
-
throw err;
|
|
43
|
-
}
|
|
44
|
-
return input.map((item) =>
|
|
45
|
-
sanitizeActionPayload(item, maxDepth, maxKeys, currentDepth + 1, keyCounter)
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const clean = Object.create(null);
|
|
50
|
-
const entries = Object.entries(input);
|
|
51
|
-
for (const [k, v] of entries) {
|
|
52
|
-
if (BANNED_KEYS.has(k)) continue;
|
|
53
|
-
keyCounter.count++;
|
|
54
|
-
if (keyCounter.count > maxKeys) {
|
|
55
|
-
const err = new Error(`Payload key count exceeds maximum allowed (${maxKeys}).`);
|
|
56
|
-
err.statusCode = 413;
|
|
57
|
-
throw err;
|
|
58
|
-
}
|
|
59
|
-
clean[k] = sanitizeActionPayload(v, maxDepth, maxKeys, currentDepth + 1, keyCounter);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return { ...clean };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Parses byte size strings like "64kb", "1mb", or numbers into integer bytes.
|
|
67
|
-
* @param {number|string} [limit]
|
|
68
|
-
* @returns {number}
|
|
69
|
-
*/
|
|
70
|
-
export function parseByteLimit(limit) {
|
|
71
|
-
if (typeof limit === "number" && limit > 0) return limit;
|
|
72
|
-
if (typeof limit === "string") {
|
|
73
|
-
const match = limit.trim().match(/^(\d+(?:\.\d+)?)\s*(b|kb|mb)?$/i);
|
|
74
|
-
if (match) {
|
|
75
|
-
const num = parseFloat(match[1]);
|
|
76
|
-
const unit = (match[2] || "b").toLowerCase();
|
|
77
|
-
if (unit === "mb") return Math.floor(num * 1024 * 1024);
|
|
78
|
-
if (unit === "kb") return Math.floor(num * 1024);
|
|
79
|
-
return Math.floor(num);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return DEFAULT_MAX_BYTES;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Native Server Actions manager for Bluebird.js.
|
|
87
|
-
* Wraps server mutation handlers with automatic CSRF verification, strict payload size/depth/key
|
|
88
|
-
* guards, optional schema validation, and reactive partial SSR re-rendering.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
:
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
1
|
+
import Csrf from "./csrf.js";
|
|
2
|
+
import Validator from "./validate.js";
|
|
3
|
+
import Render from "./render.js";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_MAX_BYTES = 64 * 1024;
|
|
6
|
+
const DEFAULT_MAX_DEPTH = 6;
|
|
7
|
+
const DEFAULT_MAX_KEYS = 100;
|
|
8
|
+
const BANNED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Recursively sanitizes and validates an incoming action payload object to prevent
|
|
12
|
+
* prototype pollution (`__proto__`, `constructor`, `prototype`), excessive nesting depth,
|
|
13
|
+
* and key-flooding memory attacks.
|
|
14
|
+
*
|
|
15
|
+
* @param {any} input
|
|
16
|
+
* @param {number} [maxDepth=DEFAULT_MAX_DEPTH]
|
|
17
|
+
* @param {number} [maxKeys=DEFAULT_MAX_KEYS]
|
|
18
|
+
* @param {number} [currentDepth=0]
|
|
19
|
+
* @param {{ count: number }} [keyCounter={ count: 0 }]
|
|
20
|
+
* @returns {any}
|
|
21
|
+
*/
|
|
22
|
+
export function sanitizeActionPayload(
|
|
23
|
+
input,
|
|
24
|
+
maxDepth = DEFAULT_MAX_DEPTH,
|
|
25
|
+
maxKeys = DEFAULT_MAX_KEYS,
|
|
26
|
+
currentDepth = 0,
|
|
27
|
+
keyCounter = { count: 0 }
|
|
28
|
+
) {
|
|
29
|
+
if (input === null || input === undefined) return input;
|
|
30
|
+
if (typeof input !== "object") return input;
|
|
31
|
+
|
|
32
|
+
if (currentDepth > maxDepth) {
|
|
33
|
+
const err = new Error("Payload nesting depth limit exceeded.");
|
|
34
|
+
err.statusCode = 413;
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (Array.isArray(input)) {
|
|
39
|
+
if (input.length > maxKeys) {
|
|
40
|
+
const err = new Error(`Payload array length exceeds maximum allowed (${maxKeys}).`);
|
|
41
|
+
err.statusCode = 413;
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
return input.map((item) =>
|
|
45
|
+
sanitizeActionPayload(item, maxDepth, maxKeys, currentDepth + 1, keyCounter)
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const clean = Object.create(null);
|
|
50
|
+
const entries = Object.entries(input);
|
|
51
|
+
for (const [k, v] of entries) {
|
|
52
|
+
if (BANNED_KEYS.has(k)) continue;
|
|
53
|
+
keyCounter.count++;
|
|
54
|
+
if (keyCounter.count > maxKeys) {
|
|
55
|
+
const err = new Error(`Payload key count exceeds maximum allowed (${maxKeys}).`);
|
|
56
|
+
err.statusCode = 413;
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
clean[k] = sanitizeActionPayload(v, maxDepth, maxKeys, currentDepth + 1, keyCounter);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { ...clean };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Parses byte size strings like "64kb", "1mb", or numbers into integer bytes.
|
|
67
|
+
* @param {number|string} [limit]
|
|
68
|
+
* @returns {number}
|
|
69
|
+
*/
|
|
70
|
+
export function parseByteLimit(limit) {
|
|
71
|
+
if (typeof limit === "number" && limit > 0) return limit;
|
|
72
|
+
if (typeof limit === "string") {
|
|
73
|
+
const match = limit.trim().match(/^(\d+(?:\.\d+)?)\s*(b|kb|mb)?$/i);
|
|
74
|
+
if (match) {
|
|
75
|
+
const num = parseFloat(match[1]);
|
|
76
|
+
const unit = (match[2] || "b").toLowerCase();
|
|
77
|
+
if (unit === "mb") return Math.floor(num * 1024 * 1024);
|
|
78
|
+
if (unit === "kb") return Math.floor(num * 1024);
|
|
79
|
+
return Math.floor(num);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return DEFAULT_MAX_BYTES;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Native Server Actions manager for Bluebird.js.
|
|
87
|
+
* Wraps server mutation handlers with automatic CSRF verification, strict payload size/depth/key
|
|
88
|
+
* guards, optional schema validation, and reactive partial SSR re-rendering.
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* Shorthand helper to mark any async function as a Server Action.
|
|
92
|
+
* Supports (fn) or (options, fn) signatures.
|
|
93
|
+
*
|
|
94
|
+
* @param {Object|Function} optionsOrFn
|
|
95
|
+
* @param {Function} [maybeFn]
|
|
96
|
+
* @returns {Function}
|
|
97
|
+
*/
|
|
98
|
+
export function action(optionsOrFn, maybeFn) {
|
|
99
|
+
const handler = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
|
|
100
|
+
const options = typeof optionsOrFn === "function" ? {} : (optionsOrFn || {});
|
|
101
|
+
|
|
102
|
+
const fn = function bluebirdServerAction(...args) {
|
|
103
|
+
if (typeof handler === "function") {
|
|
104
|
+
return handler(...args);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
fn.__bb_action__ = true;
|
|
109
|
+
fn.__isBluebirdAction = true;
|
|
110
|
+
fn.options = options;
|
|
111
|
+
fn.handler = handler;
|
|
112
|
+
if (handler && handler.name && handler.name !== "anonymous" && handler.name !== "handlerFn") {
|
|
113
|
+
fn.__actionName = handler.name;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return fn;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export class Actions {
|
|
120
|
+
/**
|
|
121
|
+
* Defines a single server action or a map of server actions with security and validation options.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* // Single action with schema validation and 32KB max payload
|
|
125
|
+
* export const actions = {
|
|
126
|
+
* updateProfile: Actions.define(
|
|
127
|
+
* {
|
|
128
|
+
* schema: { name: { required: true, min: 2 } },
|
|
129
|
+
* maxBytes: "32kb",
|
|
130
|
+
* },
|
|
131
|
+
* async ({ body, req, user }) => {
|
|
132
|
+
* await User.update(user.id, body, { only: ["name"] });
|
|
133
|
+
* return { updated: true };
|
|
134
|
+
* }
|
|
135
|
+
* ),
|
|
136
|
+
* };
|
|
137
|
+
*
|
|
138
|
+
* @param {Object|Function} optionsOrMap - Options object, action map, or handler function.
|
|
139
|
+
* @param {Function} [handlerFn] - Action handler function when first argument is options.
|
|
140
|
+
* @returns {Object}
|
|
141
|
+
*/
|
|
142
|
+
static define(optionsOrMap, handlerFn) {
|
|
143
|
+
if (typeof optionsOrMap === "function") {
|
|
144
|
+
return action({}, optionsOrMap);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (typeof handlerFn === "function") {
|
|
148
|
+
return action(optionsOrMap || {}, handlerFn);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (optionsOrMap && typeof optionsOrMap === "object") {
|
|
152
|
+
const out = {};
|
|
153
|
+
for (const [key, val] of Object.entries(optionsOrMap)) {
|
|
154
|
+
if (typeof val === "function") {
|
|
155
|
+
out[key] = action({}, val);
|
|
156
|
+
out[key].__actionName = key;
|
|
157
|
+
} else if (val && typeof val === "object" && typeof val.handler === "function") {
|
|
158
|
+
out[key] = action(val.options || val, val.handler);
|
|
159
|
+
out[key].__actionName = key;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Express route handler that dispatches incoming POST requests to the matching Server Action
|
|
170
|
+
* exported by a view module (`export const actions = { ... }`).
|
|
171
|
+
*
|
|
172
|
+
* Security checks performed automatically before invoking the action:
|
|
173
|
+
* 1. CSRF verification (header `x-csrf-token` or body `_csrf` matched against signed `_csrf` cookie).
|
|
174
|
+
* 2. Payload byte size guard (`Content-Length` and serialized body byte length, default 64KB).
|
|
175
|
+
* 3. Prototype pollution & deep-nesting guard (`__proto__`, `constructor`, `prototype`, maxDepth, maxKeys).
|
|
176
|
+
* 4. Optional `Validator` schema check if `schema` is configured on the action.
|
|
177
|
+
*
|
|
178
|
+
* @param {string} viewName - View name associated with the route.
|
|
179
|
+
* @param {Record<string, any>} actionsMap - Exported `actions` object from the view module.
|
|
180
|
+
* @returns {Function} Express route handler.
|
|
181
|
+
*/
|
|
182
|
+
static createDispatcher(viewName, actionsMap) {
|
|
183
|
+
return async (req, res) => {
|
|
184
|
+
const actionName =
|
|
185
|
+
req.headers["x-bluebird-action"] ||
|
|
186
|
+
req.body?._action ||
|
|
187
|
+
req.query?._action ||
|
|
188
|
+
"default";
|
|
189
|
+
|
|
190
|
+
const rawAction =
|
|
191
|
+
actionsMap[actionName] ||
|
|
192
|
+
(actionName === "default" && typeof actionsMap === "function" ? actionsMap : null);
|
|
193
|
+
|
|
194
|
+
if (!rawAction) {
|
|
195
|
+
return res.status(404).json({
|
|
196
|
+
status: "error",
|
|
197
|
+
message: `Action '${String(actionName)}' not found on view '${viewName}'.`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const actionDef =
|
|
202
|
+
typeof rawAction === "function" && !rawAction.__bb_action__
|
|
203
|
+
? { options: {}, handler: rawAction }
|
|
204
|
+
: rawAction;
|
|
205
|
+
const opts = actionDef.options || {};
|
|
206
|
+
const handler = actionDef.handler || (typeof rawAction === "function" ? rawAction : null);
|
|
207
|
+
|
|
208
|
+
if (typeof handler !== "function") {
|
|
209
|
+
return res.status(404).json({
|
|
210
|
+
status: "error",
|
|
211
|
+
message: `Invalid handler for action '${String(actionName)}'.`,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const maxBytes = parseByteLimit(opts.maxBytes ?? opts.limit ?? DEFAULT_MAX_BYTES);
|
|
216
|
+
const maxDepth = opts.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
217
|
+
const maxKeys = opts.maxKeys ?? DEFAULT_MAX_KEYS;
|
|
218
|
+
|
|
219
|
+
const contentLength = parseInt(req.headers["content-length"] || "0", 10);
|
|
220
|
+
if (contentLength > maxBytes) {
|
|
221
|
+
return res.status(413).json({
|
|
222
|
+
status: "error",
|
|
223
|
+
message: `Action payload too large (exceeds ${maxBytes} bytes).`,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const rawBodyStr = JSON.stringify(req.body || {});
|
|
228
|
+
if (Buffer.byteLength(rawBodyStr, "utf-8") > maxBytes) {
|
|
229
|
+
return res.status(413).json({
|
|
230
|
+
status: "error",
|
|
231
|
+
message: `Action payload size exceeds maximum allowed (${maxBytes} bytes).`,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (opts.csrf !== false) {
|
|
236
|
+
const cookieToken = req.cookies?.["_csrf"];
|
|
237
|
+
const headerToken =
|
|
238
|
+
req.headers["x-csrf-token"] ||
|
|
239
|
+
req.body?._csrf ||
|
|
240
|
+
req.query?._csrf;
|
|
241
|
+
|
|
242
|
+
if (
|
|
243
|
+
!cookieToken ||
|
|
244
|
+
!headerToken ||
|
|
245
|
+
cookieToken !== headerToken ||
|
|
246
|
+
!Csrf.verifyToken(headerToken)
|
|
247
|
+
) {
|
|
248
|
+
return res.status(403).json({
|
|
249
|
+
status: "error",
|
|
250
|
+
message: "Invalid or missing CSRF token for server action.",
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
let safeBody = {};
|
|
256
|
+
try {
|
|
257
|
+
safeBody = sanitizeActionPayload(req.body || {}, maxDepth, maxKeys);
|
|
258
|
+
delete safeBody._csrf;
|
|
259
|
+
delete safeBody._action;
|
|
260
|
+
} catch (err) {
|
|
261
|
+
return res.status(err.statusCode || 400).json({
|
|
262
|
+
status: "error",
|
|
263
|
+
message: err.message || "Invalid action payload.",
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (opts.schema) {
|
|
268
|
+
const validator =
|
|
269
|
+
opts.schema instanceof Validator
|
|
270
|
+
? opts.schema
|
|
271
|
+
: new Validator(opts.schema, req.lang || "en");
|
|
272
|
+
const validation = await validator.validate(safeBody);
|
|
273
|
+
if (!validation.success) {
|
|
274
|
+
return res.status(400).json({
|
|
275
|
+
status: "error",
|
|
276
|
+
message: validation.message?.[0] || "Validation failed.",
|
|
277
|
+
errors: validation.errors,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const ctx = Object.assign(
|
|
283
|
+
{},
|
|
284
|
+
safeBody,
|
|
285
|
+
{
|
|
286
|
+
req,
|
|
287
|
+
res,
|
|
288
|
+
user: req.user,
|
|
289
|
+
body: safeBody,
|
|
290
|
+
payload: safeBody,
|
|
291
|
+
params: req.params || {},
|
|
292
|
+
query: req.query || {},
|
|
293
|
+
action: String(actionName),
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
const result = await handler(ctx, ctx);
|
|
298
|
+
if (res.headersSent) return;
|
|
299
|
+
|
|
300
|
+
if (result && typeof result === "object" && typeof result.redirect === "string") {
|
|
301
|
+
return Render.redirect(req, res, result.redirect);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const wantsPartial =
|
|
305
|
+
req.headers["x-bluebird-partial"] === "1" ||
|
|
306
|
+
req.query?._partial === "1" ||
|
|
307
|
+
(result && typeof result === "object" && result.reRender === true);
|
|
308
|
+
|
|
309
|
+
if (wantsPartial) {
|
|
310
|
+
const extraProps =
|
|
311
|
+
result && typeof result === "object" && !Array.isArray(result)
|
|
312
|
+
? result.props || result
|
|
313
|
+
: {};
|
|
314
|
+
return Render.send(res, viewName, {
|
|
315
|
+
user: req.user,
|
|
316
|
+
params: req.params,
|
|
317
|
+
query: req.query,
|
|
318
|
+
actionResult: result,
|
|
319
|
+
...extraProps,
|
|
320
|
+
_layout: false,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const expectsJson =
|
|
325
|
+
req.headers["x-bluebird-action"] ||
|
|
326
|
+
req.headers["x-requested-with"] === "XMLHttpRequest" ||
|
|
327
|
+
req.headers.accept?.includes("application/json");
|
|
328
|
+
|
|
329
|
+
if (expectsJson) {
|
|
330
|
+
return res.status(200).json({
|
|
331
|
+
status: "success",
|
|
332
|
+
action: String(actionName),
|
|
333
|
+
data: result ?? null,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const extraProps =
|
|
338
|
+
result && typeof result === "object" && !Array.isArray(result)
|
|
339
|
+
? result.props || result
|
|
340
|
+
: {};
|
|
341
|
+
return Render.send(res, viewName, {
|
|
342
|
+
user: req.user,
|
|
343
|
+
params: req.params,
|
|
344
|
+
query: req.query,
|
|
345
|
+
actionResult: result,
|
|
346
|
+
...extraProps,
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
Actions.action = action;
|
|
353
|
+
Actions.fn = action;
|
|
354
|
+
|
|
355
|
+
export default Actions;
|