@seip/bluebird-js 0.1.7 → 0.2.1
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/AGENTS.md +727 -620
- package/README.md +670 -607
- package/app/models/user.js +19 -0
- package/app/resources/html/home.html +137 -0
- package/app/resources/html/layouts/main.html +55 -0
- package/app/resources/html/notFound.html +14 -0
- package/app/resources/html/table.html +15 -0
- package/app/resources/html/terms.html +46 -0
- package/app/resources/index.html +27 -0
- package/app/resources/layouts/AppLayout.jsx +92 -0
- package/app/resources/main.jsx +18 -0
- package/app/resources/pages/Dashboard.jsx +93 -0
- package/app/resources/pages/Home.jsx +140 -0
- package/app/resources/pages/Login.jsx +35 -0
- package/app/resources/pages/NotFound.jsx +16 -0
- package/app/resources/pages/Register.jsx +55 -0
- package/app/resources/pages/TableDemo.jsx +85 -0
- package/app/resources/routes.jsx +59 -0
- package/app/resources/spa.js +73 -0
- package/app/resources/styles.css +9 -0
- package/app/routes/api.js +129 -50
- package/app/routes/auth.js +209 -0
- package/app/routes/web.js +89 -47
- package/core/ai.js +30 -10
- package/core/app.d.ts +2 -18
- package/core/app.js +987 -801
- package/core/auth.d.ts +2 -13
- package/core/auth.js +444 -326
- package/core/cli/build.js +37 -0
- package/core/cli/dev.js +155 -0
- package/core/cli/doctor.js +37 -2
- package/core/cli/export.js +191 -0
- package/core/cli/init.js +91 -322
- package/core/cli/nginx.js +1 -1
- package/core/cli/templates.js +139 -0
- package/core/cli/view.js +98 -161
- package/core/config.js +44 -2
- package/core/csrf.js +156 -24
- package/core/database.js +1184 -1131
- package/core/debug.js +1 -1
- package/core/html.js +8 -1
- package/core/index.d.ts +126 -310
- package/core/paths.js +55 -0
- package/core/ratelimit.js +10 -1
- package/core/react/client/api.js +239 -0
- package/core/react/client/auth.js +236 -0
- package/core/react/client/components.js +1321 -0
- package/core/react/client/datatable.js +595 -0
- package/core/react/client/i18n.js +440 -0
- package/core/react/client/index.d.ts +314 -0
- package/core/react/client/index.js +50 -0
- package/core/react/client/router.js +26 -0
- package/core/react/client/theme.css +512 -0
- package/core/render-cache.js +250 -212
- package/core/render.d.ts +25 -21
- package/core/render.js +738 -736
- package/core/router.d.ts +2 -3
- package/core/router.js +171 -171
- package/core/seo.js +10 -0
- package/core/spa.js +599 -0
- package/core/view-loader.js +155 -613
- package/core/vite-plugin.d.ts +18 -0
- package/core/vite-plugin.js +162 -0
- package/core/vite.js +250 -145
- package/index.js +22 -18
- package/jsconfig.json +5 -3
- package/package.json +114 -92
- package/production.md +8 -3
- package/public/js/bluebird.js +1886 -0
- package/templates/_auth/app/database.js +19 -0
- package/templates/_auth/app/models/user.js +22 -0
- package/templates/_auth/app/routes/auth-router.js +304 -0
- package/templates/_base/app/resources/html/layouts/main.html +55 -0
- package/templates/_base/app/resources/html/notFound.html +14 -0
- package/templates/_base/app/resources/index.html +27 -0
- package/templates/_base/app/resources/styles.css +9 -0
- package/templates/api/app/models/note.js +18 -0
- package/templates/api/app/routes/api.js +82 -0
- package/templates/api/app/routes/auth.js +11 -0
- package/templates/api/index.js +19 -0
- package/templates/api/template.json +13 -0
- package/templates/auth/app/mail.js +11 -0
- package/templates/auth/app/models/password_reset.js +18 -0
- package/templates/auth/app/resources/html/home.html +21 -0
- package/templates/auth/app/resources/html/partials/nav.html +2 -0
- package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
- package/templates/auth/app/resources/main.jsx +18 -0
- package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
- package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
- package/templates/auth/app/resources/pages/Login.jsx +36 -0
- package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
- package/templates/auth/app/resources/pages/Profile.jsx +99 -0
- package/templates/auth/app/resources/pages/Register.jsx +50 -0
- package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
- package/templates/auth/app/resources/routes.jsx +54 -0
- package/templates/auth/app/resources/spa.js +29 -0
- package/templates/auth/app/routes/api.js +17 -0
- package/templates/auth/app/routes/auth.js +13 -0
- package/templates/auth/app/routes/web.js +21 -0
- package/templates/auth/index.js +19 -0
- package/templates/auth/template.json +15 -0
- package/templates/blank/app/models/.gitkeep +0 -0
- package/templates/blank/app/resources/html/home.html +27 -0
- package/templates/blank/app/resources/html/partials/nav.html +1 -0
- package/templates/blank/app/resources/main.jsx +16 -0
- package/templates/blank/app/resources/pages/Home.jsx +42 -0
- package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
- package/templates/blank/app/resources/routes.jsx +15 -0
- package/templates/blank/app/resources/spa.js +24 -0
- package/templates/blank/app/routes/api.js +11 -0
- package/templates/blank/app/routes/web.js +21 -0
- package/templates/blank/index.js +13 -0
- package/templates/blank/template.json +12 -0
- package/templates/blog/app/lib/blog.js +149 -0
- package/templates/blog/app/models/post.js +22 -0
- package/templates/blog/app/resources/html/home.html +28 -0
- package/templates/blog/app/resources/html/not-found.html +5 -0
- package/templates/blog/app/resources/html/partials/nav.html +2 -0
- package/templates/blog/app/resources/html/partials/post-card.html +9 -0
- package/templates/blog/app/resources/html/post.html +8 -0
- package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
- package/templates/blog/app/resources/main.jsx +18 -0
- package/templates/blog/app/resources/pages/Login.jsx +31 -0
- package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
- package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
- package/templates/blog/app/resources/pages/Posts.jsx +83 -0
- package/templates/blog/app/resources/pages/Register.jsx +43 -0
- package/templates/blog/app/resources/routes.jsx +51 -0
- package/templates/blog/app/resources/spa.js +25 -0
- package/templates/blog/app/resources/styles.css +40 -0
- package/templates/blog/app/routes/api.js +77 -0
- package/templates/blog/app/routes/auth.js +10 -0
- package/templates/blog/app/routes/web.js +114 -0
- package/templates/blog/index.js +21 -0
- package/templates/blog/template.json +14 -0
- package/vite.config.js +11 -0
- package/app/views/components/Header.jsx +0 -42
- package/app/views/components/LiveCounter.jsx +0 -67
- package/app/views/layouts/main/view.jsx +0 -201
- package/app/views/legal/terms.html +0 -29
- package/app/views/pages/about.jsx +0 -98
- package/app/views/pages/dashboard.jsx +0 -278
- package/app/views/pages/index.jsx +0 -123
- package/app/views/pages/login.jsx +0 -141
- package/core/actions.d.ts +0 -4
- package/core/actions.js +0 -355
- package/core/jsx.d.ts +0 -38
- package/core/jsx.js +0 -1597
- package/core/vite.d.ts +0 -3
- package/public/js/bluebird-client.js +0 -3265
- package/public/js/tailwind.js +0 -8
package/core/csrf.js
CHANGED
|
@@ -2,18 +2,60 @@ import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
|
2
2
|
import Config from "./config.js";
|
|
3
3
|
|
|
4
4
|
const propsConfig = Config.props();
|
|
5
|
-
|
|
5
|
+
let fallbackSecret = null;
|
|
6
|
+
let warnedFallback = false;
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Resolves the CSRF signing secret. Uses `JWT_SECRET` (or `CSRF_SECRET`) when defined.
|
|
10
|
+
* Otherwise falls back to a random per-process secret (never a public hardcoded constant)
|
|
11
|
+
* and warns once in production, because tokens will not validate across cluster workers.
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
function resolveSecret() {
|
|
15
|
+
const configured = process.env.CSRF_SECRET || process.env.JWT_SECRET || propsConfig.jwtSecret;
|
|
16
|
+
if (configured) return configured;
|
|
17
|
+
if (!fallbackSecret) {
|
|
18
|
+
fallbackSecret = randomBytes(32).toString("hex");
|
|
19
|
+
}
|
|
20
|
+
if (!warnedFallback && !propsConfig.debug) {
|
|
21
|
+
warnedFallback = true;
|
|
22
|
+
console.warn(
|
|
23
|
+
"[CSRF] JWT_SECRET (or CSRF_SECRET) is not defined. Using a random per-process secret: CSRF tokens will not validate across PM2 cluster workers or restarts."
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return fallbackSecret;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Timing-safe string comparison.
|
|
31
|
+
* @param {string} a
|
|
32
|
+
* @param {string} b
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
function safeEqual(a, b) {
|
|
36
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
37
|
+
const aBuf = Buffer.from(a);
|
|
38
|
+
const bBuf = Buffer.from(b);
|
|
39
|
+
if (aBuf.length !== bBuf.length) return false;
|
|
40
|
+
return timingSafeEqual(aBuf, bBuf);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Native CSRF protection module using HMAC-SHA256 signed double-submit tokens without external dependencies.
|
|
9
45
|
*/
|
|
10
46
|
class Csrf {
|
|
47
|
+
/**
|
|
48
|
+
* Default cookie name used for the CSRF token.
|
|
49
|
+
* @type {string}
|
|
50
|
+
*/
|
|
51
|
+
static cookieName = "_csrf";
|
|
52
|
+
|
|
11
53
|
/**
|
|
12
54
|
* Generates a signed CSRF token string.
|
|
13
55
|
* @param {string} [secret] - Secret key used for signing.
|
|
14
56
|
* @returns {string} Hex encoded token in format salt.timestamp.signature.
|
|
15
57
|
*/
|
|
16
|
-
static generateToken(secret =
|
|
58
|
+
static generateToken(secret = resolveSecret()) {
|
|
17
59
|
const salt = randomBytes(16).toString("hex");
|
|
18
60
|
const timestamp = Date.now().toString(36);
|
|
19
61
|
const signature = createHmac("sha256", secret)
|
|
@@ -30,7 +72,7 @@ class Csrf {
|
|
|
30
72
|
* @param {number} [maxAgeMs=86400000] - Maximum token age in milliseconds (default 24h).
|
|
31
73
|
* @returns {boolean} True if the token is valid.
|
|
32
74
|
*/
|
|
33
|
-
static verifyToken(token, secret =
|
|
75
|
+
static verifyToken(token, secret = resolveSecret(), maxAgeMs = 86400000) {
|
|
34
76
|
if (!token || typeof token !== "string") return false;
|
|
35
77
|
|
|
36
78
|
const parts = token.split(".");
|
|
@@ -47,41 +89,132 @@ class Csrf {
|
|
|
47
89
|
.update(`${salt}.${timestampStr}`)
|
|
48
90
|
.digest("hex");
|
|
49
91
|
|
|
50
|
-
|
|
51
|
-
|
|
92
|
+
return safeEqual(signature, expected);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Timing-safe comparison helper for tokens.
|
|
97
|
+
* @param {string} a
|
|
98
|
+
* @param {string} b
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
static safeEqual(a, b) {
|
|
102
|
+
return safeEqual(a, b);
|
|
103
|
+
}
|
|
52
104
|
|
|
53
|
-
|
|
54
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Returns the default cookie options for the CSRF cookie.
|
|
107
|
+
* The cookie is readable by JavaScript (double-submit pattern) and SameSite=Lax.
|
|
108
|
+
* @returns {import('express').CookieOptions}
|
|
109
|
+
*/
|
|
110
|
+
static cookieOptions() {
|
|
111
|
+
return {
|
|
112
|
+
httpOnly: false,
|
|
113
|
+
sameSite: "lax",
|
|
114
|
+
secure: Config.isDesktop() ? false : Boolean(!propsConfig.debug),
|
|
115
|
+
path: "/",
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns the request's valid CSRF token, generating and setting a fresh `_csrf` cookie when missing
|
|
121
|
+
* or invalid. Used by views, cached pages, and API forms.
|
|
122
|
+
* @param {import('express').Request} req
|
|
123
|
+
* @param {import('express').Response} res
|
|
124
|
+
* @returns {string}
|
|
125
|
+
*/
|
|
126
|
+
static ensure(req, res) {
|
|
127
|
+
if (res?.locals?.csrfToken) return res.locals.csrfToken;
|
|
128
|
+
let token = req?.cookies?.[Csrf.cookieName];
|
|
129
|
+
if (!token || !Csrf.verifyToken(token)) {
|
|
130
|
+
token = Csrf.generateToken();
|
|
131
|
+
if (res && typeof res.cookie === "function" && !res.headersSent) {
|
|
132
|
+
res.cookie(Csrf.cookieName, token, Csrf.cookieOptions());
|
|
133
|
+
}
|
|
134
|
+
if (req?.cookies) req.cookies[Csrf.cookieName] = token;
|
|
135
|
+
}
|
|
136
|
+
if (res?.locals) res.locals.csrfToken = token;
|
|
137
|
+
return token;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Issues a brand new CSRF token cookie. Called automatically on `Auth.login()` / `Auth.logout()`
|
|
142
|
+
* so that a token planted before authentication (cookie fixation) cannot be reused afterwards.
|
|
143
|
+
* @param {import('express').Response} res
|
|
144
|
+
* @returns {string}
|
|
145
|
+
*/
|
|
146
|
+
static rotate(res) {
|
|
147
|
+
const token = Csrf.generateToken();
|
|
148
|
+
if (res && typeof res.cookie === "function" && !res.headersSent) {
|
|
149
|
+
res.cookie(Csrf.cookieName, token, Csrf.cookieOptions());
|
|
150
|
+
}
|
|
151
|
+
if (res?.locals) res.locals.csrfToken = token;
|
|
152
|
+
if (res?.req?.cookies) res.req.cookies[Csrf.cookieName] = token;
|
|
153
|
+
return token;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Validates a double-submit CSRF request: the signed cookie must exist, be valid,
|
|
158
|
+
* and match (timing-safe) the token received in the header or body. Query strings are never accepted.
|
|
159
|
+
* @param {import('express').Request} req
|
|
160
|
+
* @param {Object} [options={}]
|
|
161
|
+
* @returns {boolean}
|
|
162
|
+
*/
|
|
163
|
+
static verifyRequest(req, options = {}) {
|
|
164
|
+
const cookieName = options.cookieName || Csrf.cookieName;
|
|
165
|
+
const headerName = (options.headerName || "x-csrf-token").toLowerCase();
|
|
166
|
+
const bodyKey = options.bodyKey || "_csrf";
|
|
167
|
+
const secret = options.secret || resolveSecret();
|
|
168
|
+
const maxAgeMs = options.maxAgeMs || 86400000;
|
|
169
|
+
const cookieToken = req.cookies?.[cookieName];
|
|
170
|
+
const received = req.headers?.[headerName] || req.body?.[bodyKey];
|
|
171
|
+
if (!cookieToken || !received || typeof received !== "string") return false;
|
|
172
|
+
if (!safeEqual(cookieToken, received)) return false;
|
|
173
|
+
return Csrf.verifyToken(received, secret, maxAgeMs);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Checks whether a request uses non-ambient bearer credentials (`Authorization` header or `X-Auth-Mode: token`)
|
|
178
|
+
* without an auth cookie. Browsers cannot attach these headers cross-site without a CORS preflight,
|
|
179
|
+
* so such requests are not exposed to CSRF (static exports in Electron/Capacitor, mobile, and server clients).
|
|
180
|
+
* @param {import('express').Request} req
|
|
181
|
+
* @param {string} [authCookie="auth"]
|
|
182
|
+
* @returns {boolean}
|
|
183
|
+
*/
|
|
184
|
+
static isTokenRequest(req, authCookie = "auth") {
|
|
185
|
+
if (req.cookies?.[authCookie]) return false;
|
|
186
|
+
const mode = String(req.headers?.["x-auth-mode"] || "").toLowerCase();
|
|
187
|
+
const authorization = String(req.headers?.authorization || "");
|
|
188
|
+
return mode === "token" || /^Bearer\s+\S+/i.test(authorization);
|
|
55
189
|
}
|
|
56
190
|
|
|
57
191
|
/**
|
|
58
192
|
* Express middleware that ensures valid CSRF tokens on mutating HTTP requests.
|
|
193
|
+
* Bearer-token requests without an auth cookie (`Csrf.isTokenRequest`) are exempt unless `allowToken: false`.
|
|
59
194
|
* @param {Object} [options={}] - Configuration options.
|
|
60
195
|
* @param {string} [options.cookieName="_csrf"] - Name of the cookie storing the CSRF token.
|
|
61
196
|
* @param {string} [options.headerName="x-csrf-token"] - Name of the request header.
|
|
62
197
|
* @param {string} [options.bodyKey="_csrf"] - Name of the body field.
|
|
63
198
|
* @param {number} [options.maxAgeMs=86400000] - Token expiration in milliseconds.
|
|
199
|
+
* @param {boolean} [options.allowToken=true] - Exempt bearer-token requests sent without an auth cookie.
|
|
200
|
+
* @param {string} [options.authCookie="auth"] - Auth cookie name checked by the bearer exemption.
|
|
64
201
|
* @returns {Function} Express middleware.
|
|
65
202
|
*/
|
|
66
203
|
static middleware(options = {}) {
|
|
67
|
-
const cookieName = options.cookieName ||
|
|
68
|
-
const headerName = (options.headerName || "x-csrf-token").toLowerCase();
|
|
69
|
-
const bodyKey = options.bodyKey || "_csrf";
|
|
204
|
+
const cookieName = options.cookieName || Csrf.cookieName;
|
|
70
205
|
const maxAgeMs = options.maxAgeMs || 86400000;
|
|
71
|
-
const secret = options.secret ||
|
|
206
|
+
const secret = options.secret || resolveSecret();
|
|
207
|
+
const allowToken = options.allowToken !== false;
|
|
208
|
+
const authCookie = options.authCookie || "auth";
|
|
72
209
|
|
|
73
210
|
return (req, res, next) => {
|
|
74
211
|
let token = req.cookies?.[cookieName];
|
|
212
|
+
const hadValidCookie = Boolean(token && Csrf.verifyToken(token, secret, maxAgeMs));
|
|
75
213
|
|
|
76
|
-
if (!
|
|
214
|
+
if (!hadValidCookie) {
|
|
77
215
|
token = Csrf.generateToken(secret);
|
|
78
216
|
if (typeof res.cookie === "function") {
|
|
79
|
-
res.cookie(cookieName, token,
|
|
80
|
-
httpOnly: false,
|
|
81
|
-
sameSite: "lax",
|
|
82
|
-
secure: Config.isDesktop() ? false : Boolean(!propsConfig.debug),
|
|
83
|
-
path: "/",
|
|
84
|
-
});
|
|
217
|
+
res.cookie(cookieName, token, Csrf.cookieOptions());
|
|
85
218
|
}
|
|
86
219
|
}
|
|
87
220
|
|
|
@@ -93,12 +226,11 @@ class Csrf {
|
|
|
93
226
|
return next();
|
|
94
227
|
}
|
|
95
228
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
req.query?.[bodyKey];
|
|
229
|
+
if (allowToken && Csrf.isTokenRequest(req, authCookie)) {
|
|
230
|
+
return next();
|
|
231
|
+
}
|
|
100
232
|
|
|
101
|
-
if (!
|
|
233
|
+
if (!hadValidCookie || !Csrf.verifyRequest(req, { ...options, secret, maxAgeMs })) {
|
|
102
234
|
return res.status(403).json({
|
|
103
235
|
status: "error",
|
|
104
236
|
message: "Invalid or missing CSRF token.",
|