@seip/bluebird-js 0.1.6 → 0.2.0

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.
Files changed (152) hide show
  1. package/.vscode/extensions.json +7 -7
  2. package/.vscode/settings.json +10 -10
  3. package/AGENTS.md +266 -116
  4. package/README.md +258 -245
  5. package/app/models/user.js +19 -0
  6. package/app/resources/html/home.html +137 -0
  7. package/app/resources/html/layouts/main.html +54 -0
  8. package/app/resources/html/notFound.html +14 -0
  9. package/app/resources/html/terms.html +46 -0
  10. package/app/resources/index.html +27 -0
  11. package/app/resources/layouts/AppLayout.jsx +89 -0
  12. package/app/resources/main.jsx +18 -0
  13. package/app/resources/pages/Dashboard.jsx +98 -0
  14. package/app/resources/pages/Home.jsx +140 -0
  15. package/app/resources/pages/Login.jsx +35 -0
  16. package/app/resources/pages/NotFound.jsx +16 -0
  17. package/app/resources/pages/Register.jsx +55 -0
  18. package/app/resources/routes.jsx +54 -0
  19. package/app/resources/spa.js +67 -0
  20. package/app/resources/styles.css +9 -0
  21. package/app/routes/api.js +85 -50
  22. package/app/routes/auth.js +209 -0
  23. package/app/routes/web.js +49 -37
  24. package/core/ai.d.ts +151 -151
  25. package/core/ai.js +1071 -1051
  26. package/core/app.d.ts +2 -18
  27. package/core/app.js +978 -792
  28. package/core/auth.d.ts +2 -13
  29. package/core/auth.js +444 -326
  30. package/core/cli/backup.js +167 -167
  31. package/core/cli/build.js +37 -0
  32. package/core/cli/dev.js +155 -0
  33. package/core/cli/doctor.js +37 -2
  34. package/core/cli/export.js +191 -0
  35. package/core/cli/init.js +101 -332
  36. package/core/cli/nginx.js +1 -1
  37. package/core/cli/templates.js +139 -0
  38. package/core/cli/view.js +73 -136
  39. package/core/config.js +44 -2
  40. package/core/csrf.js +156 -24
  41. package/core/database.js +737 -684
  42. package/core/debug.js +1 -1
  43. package/core/html.js +8 -1
  44. package/core/index.d.ts +1286 -1431
  45. package/core/paths.js +55 -0
  46. package/core/ratelimit.js +10 -1
  47. package/core/react/client/api.js +237 -0
  48. package/core/react/client/auth.js +236 -0
  49. package/core/react/client/components.js +1321 -0
  50. package/core/react/client/i18n.js +338 -0
  51. package/core/react/client/index.d.ts +249 -0
  52. package/core/react/client/index.js +49 -0
  53. package/core/react/client/router.js +26 -0
  54. package/core/react/client/theme.css +512 -0
  55. package/core/render-cache.js +250 -212
  56. package/core/render.d.ts +25 -21
  57. package/core/render.js +613 -603
  58. package/core/router.d.ts +2 -3
  59. package/core/router.js +171 -171
  60. package/core/seo.js +10 -0
  61. package/core/spa.js +599 -0
  62. package/core/view-loader.js +81 -455
  63. package/core/vite-plugin.d.ts +18 -0
  64. package/core/vite-plugin.js +162 -0
  65. package/core/vite.js +250 -145
  66. package/index.js +22 -18
  67. package/jsconfig.json +5 -3
  68. package/package.json +114 -92
  69. package/production.md +8 -3
  70. package/public/js/bluebird.js +1160 -0
  71. package/templates/_auth/app/database.js +19 -0
  72. package/templates/_auth/app/models/user.js +22 -0
  73. package/templates/_auth/app/routes/auth-router.js +304 -0
  74. package/templates/_base/app/resources/html/layouts/main.html +55 -0
  75. package/templates/_base/app/resources/html/notFound.html +14 -0
  76. package/templates/_base/app/resources/index.html +27 -0
  77. package/templates/_base/app/resources/styles.css +9 -0
  78. package/templates/api/app/models/note.js +18 -0
  79. package/templates/api/app/routes/api.js +82 -0
  80. package/templates/api/app/routes/auth.js +11 -0
  81. package/templates/api/index.js +19 -0
  82. package/templates/api/template.json +13 -0
  83. package/templates/auth/app/mail.js +11 -0
  84. package/templates/auth/app/models/password_reset.js +18 -0
  85. package/templates/auth/app/resources/html/home.html +21 -0
  86. package/templates/auth/app/resources/html/partials/nav.html +2 -0
  87. package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
  88. package/templates/auth/app/resources/main.jsx +18 -0
  89. package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
  90. package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
  91. package/templates/auth/app/resources/pages/Login.jsx +36 -0
  92. package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
  93. package/templates/auth/app/resources/pages/Profile.jsx +99 -0
  94. package/templates/auth/app/resources/pages/Register.jsx +50 -0
  95. package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
  96. package/templates/auth/app/resources/routes.jsx +54 -0
  97. package/templates/auth/app/resources/spa.js +29 -0
  98. package/templates/auth/app/routes/api.js +17 -0
  99. package/templates/auth/app/routes/auth.js +13 -0
  100. package/templates/auth/app/routes/web.js +21 -0
  101. package/templates/auth/index.js +19 -0
  102. package/templates/auth/template.json +15 -0
  103. package/templates/blank/app/models/.gitkeep +0 -0
  104. package/templates/blank/app/resources/html/home.html +27 -0
  105. package/templates/blank/app/resources/html/partials/nav.html +1 -0
  106. package/templates/blank/app/resources/main.jsx +16 -0
  107. package/templates/blank/app/resources/pages/Home.jsx +42 -0
  108. package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
  109. package/templates/blank/app/resources/routes.jsx +15 -0
  110. package/templates/blank/app/resources/spa.js +24 -0
  111. package/templates/blank/app/routes/api.js +11 -0
  112. package/templates/blank/app/routes/web.js +21 -0
  113. package/templates/blank/index.js +13 -0
  114. package/templates/blank/template.json +12 -0
  115. package/templates/blog/app/lib/blog.js +149 -0
  116. package/templates/blog/app/models/post.js +22 -0
  117. package/templates/blog/app/resources/html/home.html +28 -0
  118. package/templates/blog/app/resources/html/not-found.html +5 -0
  119. package/templates/blog/app/resources/html/partials/nav.html +2 -0
  120. package/templates/blog/app/resources/html/partials/post-card.html +9 -0
  121. package/templates/blog/app/resources/html/post.html +8 -0
  122. package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
  123. package/templates/blog/app/resources/main.jsx +18 -0
  124. package/templates/blog/app/resources/pages/Login.jsx +31 -0
  125. package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
  126. package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
  127. package/templates/blog/app/resources/pages/Posts.jsx +83 -0
  128. package/templates/blog/app/resources/pages/Register.jsx +43 -0
  129. package/templates/blog/app/resources/routes.jsx +51 -0
  130. package/templates/blog/app/resources/spa.js +25 -0
  131. package/templates/blog/app/resources/styles.css +40 -0
  132. package/templates/blog/app/routes/api.js +77 -0
  133. package/templates/blog/app/routes/auth.js +10 -0
  134. package/templates/blog/app/routes/web.js +114 -0
  135. package/templates/blog/index.js +21 -0
  136. package/templates/blog/template.json +14 -0
  137. package/vite.config.js +11 -0
  138. package/app/views/about.jsx +0 -98
  139. package/app/views/components/Header.jsx +0 -42
  140. package/app/views/components/LiveCounter.jsx +0 -66
  141. package/app/views/dashboard.jsx +0 -300
  142. package/app/views/index.jsx +0 -123
  143. package/app/views/layouts/main/view.jsx +0 -201
  144. package/app/views/legal/terms.html +0 -29
  145. package/app/views/login.jsx +0 -141
  146. package/core/actions.d.ts +0 -4
  147. package/core/actions.js +0 -329
  148. package/core/jsx.d.ts +0 -38
  149. package/core/jsx.js +0 -1432
  150. package/core/vite.d.ts +0 -3
  151. package/public/js/bluebird-client.js +0 -3161
  152. 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
- const defaultSecret = propsConfig.jwtSecret || "bluebird-csrf-secret-key-32-chars!";
5
+ let fallbackSecret = null;
6
+ let warnedFallback = false;
6
7
 
7
8
  /**
8
- * Native CSRF protection module using HMAC-SHA256 signed tokens without external dependencies.
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 = process.env.JWT_SECRET || defaultSecret) {
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 = process.env.JWT_SECRET || defaultSecret, maxAgeMs = 86400000) {
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
- const sigBuf = Buffer.from(signature);
51
- const expBuf = Buffer.from(expected);
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
- if (sigBuf.length !== expBuf.length) return false;
54
- return timingSafeEqual(sigBuf, expBuf);
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 || "_csrf";
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 || process.env.JWT_SECRET || defaultSecret;
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 (!token || !Csrf.verifyToken(token, secret, maxAgeMs)) {
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
- const receivedToken =
97
- req.headers[headerName] ||
98
- req.body?.[bodyKey] ||
99
- req.query?.[bodyKey];
229
+ if (allowToken && Csrf.isTokenRequest(req, authCookie)) {
230
+ return next();
231
+ }
100
232
 
101
- if (!receivedToken || !Csrf.verifyToken(receivedToken, secret, maxAgeMs) || receivedToken !== token) {
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.",