create-guren-app 1.19.0 → 1.19.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-guren-app",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.1",
|
|
4
4
|
"description": "Scaffold a Guren app: a Bun-first fullstack TypeScript framework with Laravel-style conventions, Inertia.js + React, Drizzle ORM, and an agent harness for Claude Code and other coding agents.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"guren",
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import { createRateLimitMiddleware, getRequestTranslator, ValidationException } from '@guren/core'
|
|
2
|
-
import type { Context, Middleware } from '@guren/core'
|
|
1
|
+
import { AUTH_CONTEXT_KEY, createRateLimitMiddleware, getRequestTranslator, ValidationException } from '@guren/core'
|
|
2
|
+
import type { AuthContext, Context, Middleware } from '@guren/core'
|
|
3
3
|
|
|
4
4
|
interface ThrottleOptions {
|
|
5
|
-
/** Requests allowed per
|
|
5
|
+
/** Requests allowed per key within the window. */
|
|
6
6
|
limit: number
|
|
7
7
|
windowMs: number
|
|
8
8
|
/** Translation key; `:seconds` is replaced with the wait. */
|
|
9
9
|
messageKey: string
|
|
10
10
|
fallbackMessage: string
|
|
11
|
+
/** Defaults to the client IP and the submitted email. */
|
|
12
|
+
key?: (ctx: Context) => Promise<string>
|
|
13
|
+
/** A request this returns true for is not counted. */
|
|
14
|
+
skip?: (ctx: Context) => Promise<boolean>
|
|
15
|
+
/** The form field an Inertia request shows the message under. */
|
|
16
|
+
errorField?: string
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
interface BunServerEnv {
|
|
@@ -21,18 +27,23 @@ function clientIp(ctx: Context): string | undefined {
|
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
// Hono caches the parsed body, so the controller still reads it after this.
|
|
24
|
-
|
|
30
|
+
// `undefined` when the body cannot be read as an object.
|
|
31
|
+
async function submittedBody(ctx: Context): Promise<Record<string, unknown> | undefined> {
|
|
25
32
|
try {
|
|
26
33
|
const body: unknown = (ctx.req.header('content-type') ?? '').includes('application/json')
|
|
27
34
|
? await ctx.req.json()
|
|
28
35
|
: await ctx.req.parseBody()
|
|
29
|
-
|
|
30
|
-
return typeof email === 'string' ? email.trim().toLowerCase() : undefined
|
|
36
|
+
return typeof body === 'object' && body !== null ? (body as Record<string, unknown>) : undefined
|
|
31
37
|
} catch {
|
|
32
38
|
return undefined
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
|
|
42
|
+
async function submittedEmail(ctx: Context): Promise<string | undefined> {
|
|
43
|
+
const email = (await submittedBody(ctx))?.email
|
|
44
|
+
return typeof email === 'string' ? email.trim().toLowerCase() : undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
36
47
|
function throttleMessage(ctx: Context, options: ThrottleOptions, seconds: number): string {
|
|
37
48
|
const translated = getRequestTranslator(ctx)?.t(options.messageKey, { seconds })
|
|
38
49
|
return translated && translated !== options.messageKey
|
|
@@ -41,9 +52,9 @@ function throttleMessage(ctx: Context, options: ThrottleOptions, seconds: number
|
|
|
41
52
|
}
|
|
42
53
|
|
|
43
54
|
/**
|
|
44
|
-
* Counts every request
|
|
45
|
-
*
|
|
46
|
-
*
|
|
55
|
+
* Counts every request `skip` lets through, per `key`. The default store lives
|
|
56
|
+
* in process memory: pass `store: new RedisRateLimitStore(redis)` (from
|
|
57
|
+
* `@guren/core/redis`) to share counts across instances.
|
|
47
58
|
*/
|
|
48
59
|
function authThrottle(name: string, options: ThrottleOptions): Middleware {
|
|
49
60
|
// Built on the first request: the default store starts a sweep timer, which
|
|
@@ -54,12 +65,13 @@ function authThrottle(name: string, options: ThrottleOptions): Middleware {
|
|
|
54
65
|
limit: options.limit,
|
|
55
66
|
windowMs: options.windowMs,
|
|
56
67
|
keyPrefix: `auth:${name}:`,
|
|
57
|
-
keyGenerator: async (ctx) => `${clientIp(ctx) ?? 'unknown'}|${(await submittedEmail(ctx)) ?? ''}
|
|
68
|
+
keyGenerator: options.key ?? (async (ctx) => `${clientIp(ctx) ?? 'unknown'}|${(await submittedEmail(ctx)) ?? ''}`),
|
|
69
|
+
skip: options.skip,
|
|
58
70
|
onRateLimited: (ctx, retryAfter) => {
|
|
59
71
|
const message = throttleMessage(ctx, options, retryAfter)
|
|
60
72
|
// An Inertia form shows a flashed error; a bare 429 would open its error modal.
|
|
61
73
|
if (ctx.req.header('x-inertia') === 'true') {
|
|
62
|
-
throw ValidationException.withMessages({ message })
|
|
74
|
+
throw ValidationException.withMessages({ [options.errorField ?? 'message']: message })
|
|
63
75
|
}
|
|
64
76
|
return ctx.json({ message, retryAfter }, 429)
|
|
65
77
|
},
|
|
@@ -88,3 +100,22 @@ export const throttlePasswordResetRequest = authThrottle('forgot-password', {
|
|
|
88
100
|
messageKey: 'auth.too_many_requests',
|
|
89
101
|
fallbackMessage: 'Too many requests. Please try again in :seconds seconds.',
|
|
90
102
|
})
|
|
103
|
+
|
|
104
|
+
// Per signed-in user, so a stolen session cannot guess the current password the
|
|
105
|
+
// profile form asks for. Only a request carrying one reaches that check.
|
|
106
|
+
export const throttleProfileUpdate = authThrottle('profile', {
|
|
107
|
+
limit: 5,
|
|
108
|
+
windowMs: 60_000,
|
|
109
|
+
messageKey: 'auth.too_many_requests',
|
|
110
|
+
fallbackMessage: 'Too many requests. Please try again in :seconds seconds.',
|
|
111
|
+
key: async (ctx) => {
|
|
112
|
+
const id = await (ctx.get(AUTH_CONTEXT_KEY) as AuthContext | undefined)?.id()
|
|
113
|
+
return id === undefined || id === null ? `ip:${clientIp(ctx) ?? 'unknown'}` : `user:${String(id)}`
|
|
114
|
+
},
|
|
115
|
+
// Mirrors the controller's `currentPassword === ''`; an unreadable body is counted.
|
|
116
|
+
skip: async (ctx) => {
|
|
117
|
+
const body = await submittedBody(ctx)
|
|
118
|
+
return body !== undefined && (body.currentPassword === undefined || body.currentPassword === '')
|
|
119
|
+
},
|
|
120
|
+
errorField: 'currentPassword',
|
|
121
|
+
})
|