@chidchanun/bcp 0.2.5 → 0.2.6

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.
@@ -0,0 +1,194 @@
1
+ # BCP Framework 0.2.6 — Authorization & Security v2
2
+
3
+ `0.2.6` expands the server authorization model and adds request-origin/CSRF protection primitives while preserving the existing Authentication Platform v2 and route model.
4
+
5
+ > Release state: unreleased development target until RC validation, tagging and npm publication complete.
6
+
7
+ ## Authorization primitives
8
+
9
+ `bcp/auth` now provides flat permission checks:
10
+
11
+ ```ts
12
+ hasPermission()
13
+ assertPermission()
14
+ getUserPermissions()
15
+ ```
16
+
17
+ Permission requirements accept one permission or multiple permissions with `any` / `all` matching.
18
+
19
+ The default user field is:
20
+
21
+ ```text
22
+ permissions
23
+ ```
24
+
25
+ Applications can select another field, such as `scopes`.
26
+
27
+ ## Permission route guards
28
+
29
+ Route guards can require permissions directly:
30
+
31
+ ```ts
32
+ requirePermission()
33
+ createPermissionGuard()
34
+ ```
35
+
36
+ Unauthenticated behavior remains consistent with `requireAuth()`. Authenticated users that fail the permission check receive `403 Forbidden` by default or can be redirected explicitly.
37
+
38
+ ## Policy authorization
39
+
40
+ Resource-aware application policies are now supported through:
41
+
42
+ ```ts
43
+ defineAuthorizationPolicy()
44
+ can()
45
+ cannot()
46
+ authorize()
47
+ AuthorizationError
48
+ ```
49
+
50
+ Policies receive a typed context containing:
51
+
52
+ ```ts
53
+ {
54
+ user,
55
+ resource,
56
+ data,
57
+ }
58
+ ```
59
+
60
+ Policies may be synchronous or asynchronous. `authorize()` throws a `403`-classified `AuthorizationError` when access is denied.
61
+
62
+ ## Same-origin protection
63
+
64
+ `bcp/server` adds request-origin validation for unsafe methods:
65
+
66
+ ```ts
67
+ isSafeHttpMethod()
68
+ isSameOriginRequest()
69
+ requireSameOriginRequest()
70
+ ```
71
+
72
+ The current request origin is always allowed. Additional origins may be listed explicitly.
73
+
74
+ For unsafe methods, BCP checks `Origin` first and then `Referer`. Missing origin information is rejected by default unless the application explicitly enables `allowMissingOrigin`.
75
+
76
+ Malformed, opaque, or unsupported origins are treated as denied instead of surfacing as server errors.
77
+
78
+ ## CSRF protection
79
+
80
+ Signed CSRF tokens are available through:
81
+
82
+ ```ts
83
+ createCsrfToken()
84
+ destroyCsrfToken()
85
+ verifyCsrfToken()
86
+ verifyCsrfRequest()
87
+ requireCsrfRequest()
88
+ ```
89
+
90
+ The default request header is:
91
+
92
+ ```text
93
+ X-BCP-CSRF
94
+ ```
95
+
96
+ The default cookie is:
97
+
98
+ ```text
99
+ bcp_csrf
100
+ ```
101
+
102
+ CSRF cookies are HttpOnly, SameSite=Lax by default, Secure in production, and use a two-hour default lifetime.
103
+
104
+ Token signing resolves secrets in this order:
105
+
106
+ ```text
107
+ explicit secret
108
+ BCP_CSRF_SECRET
109
+ BCP_SESSION_SECRET
110
+ ```
111
+
112
+ Secrets must contain at least 32 UTF-8 bytes.
113
+
114
+ `requireCsrfRequest()` combines same-origin validation and token validation for unsafe methods. GET, HEAD and OPTIONS do not require CSRF tokens.
115
+
116
+ ## Security error classification
117
+
118
+ `RequestSecurityError` reports status `403` and distinguishes:
119
+
120
+ ```text
121
+ INVALID_ORIGIN
122
+ INVALID_CSRF_TOKEN
123
+ ```
124
+
125
+ This allows API routes/actions to map request-security failures to application-specific error responses while retaining a stable security classification.
126
+
127
+ ## Compatibility
128
+
129
+ `0.2.6` does not intentionally remove existing public entrypoints or authentication behavior.
130
+
131
+ Existing APIs remain available:
132
+
133
+ ```text
134
+ auth()
135
+ login()
136
+ logout()
137
+ logoutAll()
138
+ rotateSession()
139
+ requireAuth()
140
+ requireGuest()
141
+ requireRole()
142
+ createAuthGuard()
143
+ createGuestGuard()
144
+ createRoleGuard()
145
+ ```
146
+
147
+ Stateless JWT-cookie authentication remains supported. Revocable stores introduced in `0.2.5` are unchanged.
148
+
149
+ ## Testing and packaging
150
+
151
+ The release adds unit coverage for:
152
+
153
+ - permission `any` / `all` matching,
154
+ - resource authorization policies,
155
+ - permission route guards,
156
+ - same-origin mutation validation,
157
+ - signed CSRF tokens,
158
+ - invalid CSRF token rejection,
159
+ - cross-origin request rejection.
160
+
161
+ Prepared npm package smoke coverage verifies that the `bcp/auth` and `bcp/server` public entrypoints include the Authorization & Security v2 APIs and implementation files.
162
+
163
+ ## Documentation
164
+
165
+ New guide:
166
+
167
+ ```text
168
+ docs/authorization-security.md
169
+ ```
170
+
171
+ Updated documentation contracts include:
172
+
173
+ ```text
174
+ docs/platform-manifest.json
175
+ docs/docs-web-manifest.json
176
+ docs/api-manifest.json
177
+ docs/api-reference.md
178
+ README.md
179
+ ```
180
+
181
+ ## Release validation
182
+
183
+ Before publishing:
184
+
185
+ ```bash
186
+ npm run typecheck
187
+ npm run test:unit
188
+ npm run test:integration
189
+ npm run test:e2e
190
+ npm run test:package
191
+ npm run rc:check
192
+ ```
193
+
194
+ The final `v0.2.6` Git tag must point to the exact commit that passed the complete RC sequence.
package/docs/security.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Security
2
2
 
3
- BCP Framework applies a security gateway in both development and standalone production modes.
3
+ BCP Framework applies a security gateway in both development and standalone production modes and exposes server-side mutation protection primitives through `bcp/server`.
4
4
 
5
5
  ## Default response headers
6
6
 
@@ -40,6 +40,58 @@ server: {
40
40
 
41
41
  Requests exceeding the limit are rejected by the outer gateway with HTTP 413 before reaching the API handler.
42
42
 
43
+ ## Same-origin mutation protection — 0.2.6+
44
+
45
+ Cookie-authenticated unsafe requests can validate their browser origin:
46
+
47
+ ```ts
48
+ import {
49
+ requireSameOriginRequest,
50
+ } from "bcp/server";
51
+
52
+ await requireSameOriginRequest();
53
+ ```
54
+
55
+ BCP validates `Origin` first and falls back to `Referer`. GET, HEAD and OPTIONS are treated as safe methods. Unsafe methods without origin information are rejected by default.
56
+
57
+ Additional trusted origins can be configured per check with `allowedOrigins`.
58
+
59
+ Malformed, opaque or unsupported origins are denied instead of surfacing as server errors.
60
+
61
+ ## CSRF protection — 0.2.6+
62
+
63
+ BCP also exposes signed CSRF tokens:
64
+
65
+ ```ts
66
+ import {
67
+ createCsrfToken,
68
+ requireCsrfRequest,
69
+ } from "bcp/server";
70
+
71
+ const token =
72
+ await createCsrfToken();
73
+
74
+ // Render the token through a trusted application response,
75
+ // then submit it with the mutation.
76
+
77
+ await requireCsrfRequest({
78
+ token: submittedToken,
79
+ });
80
+ ```
81
+
82
+ Default header/cookie:
83
+
84
+ ```text
85
+ X-BCP-CSRF
86
+ bcp_csrf
87
+ ```
88
+
89
+ Token signing uses `BCP_CSRF_SECRET` when configured and otherwise falls back to `BCP_SESSION_SECRET`. Secrets must contain at least 32 UTF-8 bytes.
90
+
91
+ `requireCsrfRequest()` combines same-origin validation and CSRF token verification for unsafe methods.
92
+
93
+ Read more: [Authorization & Security v2](authorization-security.md).
94
+
43
95
  ## Static assets
44
96
 
45
97
  Public asset resolution decodes the URL, rejects null bytes, resolves the candidate beneath `public/`, resolves filesystem symlinks and verifies the final real path remains inside the public directory. This prevents path and symlink traversal from escaping the public root.
@@ -55,3 +107,5 @@ Security config values reject carriage return, line feed and null bytes to preve
55
107
  ## Deployment note
56
108
 
57
109
  The security gateway is the outer production layer, so security headers and body limits also apply to response-cache hits, middleware responses, static assets and error responses.
110
+
111
+ Authorization, same-origin checks and CSRF validation remain request-handler/guard responsibilities and should be enforced wherever a mutation or protected operation occurs.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chidchanun/bcp",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "BCP Framework - a React full-stack framework with file-based routing, SSR, APIs, middleware, islands, caching and standalone production builds.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -24,13 +24,32 @@ export {
24
24
  type MemoryAuthSessionStore,
25
25
  } from "../../server/src/auth-session-store.js";
26
26
 
27
+ export {
28
+ assertPermission,
29
+ authorize,
30
+ can,
31
+ cannot,
32
+ defineAuthorizationPolicy,
33
+ getUserPermissions,
34
+ hasPermission,
35
+ AuthorizationError,
36
+
37
+ type AuthorizationContext,
38
+ type AuthorizationMatch,
39
+ type AuthorizationPolicy,
40
+ type PermissionCheckOptions,
41
+ type PermissionRequirement,
42
+ } from "../../server/src/authorization.js";
43
+
27
44
  export {
28
45
  createAuthGuard,
29
46
  createGuestGuard,
47
+ createPermissionGuard,
30
48
  createRoleGuard,
31
49
  getGuardAuth,
32
50
  requireAuth,
33
51
  requireGuest,
52
+ requirePermission,
34
53
  requireRole,
35
54
 
36
55
  type AuthGuardData,
@@ -40,6 +59,7 @@ export {
40
59
  type GuestGuardResult,
41
60
  type RequireAuthOptions,
42
61
  type RequireGuestOptions,
62
+ type RequirePermissionOptions,
43
63
  type RequireRoleOptions,
44
64
  type RequiredRole,
45
65
  } from "../../server/src/auth-guard.js";
@@ -15,6 +15,22 @@ export {
15
15
  type ResponseCookieOptions,
16
16
  } from "../../server/src/request-context.js";
17
17
 
18
+ export {
19
+ createCsrfToken,
20
+ destroyCsrfToken,
21
+ isSafeHttpMethod,
22
+ isSameOriginRequest,
23
+ requireCsrfRequest,
24
+ requireSameOriginRequest,
25
+ verifyCsrfRequest,
26
+ verifyCsrfToken,
27
+ RequestSecurityError,
28
+
29
+ type CsrfTokenOptions,
30
+ type SameOriginOptions,
31
+ type VerifyCsrfRequestOptions,
32
+ } from "../../server/src/request-security.js";
33
+
18
34
  export {
19
35
  attachRequestId,
20
36
  createLogger,
@@ -4,6 +4,11 @@ import {
4
4
  type AuthSession,
5
5
  type AuthUser,
6
6
  } from "./auth.js";
7
+ import {
8
+ hasPermission,
9
+ type AuthorizationMatch,
10
+ type PermissionRequirement,
11
+ } from "./authorization.js";
7
12
  import {
8
13
  redirect,
9
14
  type RedirectStatus,
@@ -31,7 +36,15 @@ extends AuthOptions {
31
36
  export interface RequireRoleOptions
32
37
  extends RequireAuthOptions {
33
38
  roleField?: string;
34
- match?: "any" | "all";
39
+ match?: AuthorizationMatch;
40
+ forbiddenRedirectTo?: string | URL | null;
41
+ forbiddenRedirectStatus?: RedirectStatus;
42
+ }
43
+
44
+ export interface RequirePermissionOptions
45
+ extends RequireAuthOptions {
46
+ permissionField?: string;
47
+ match?: AuthorizationMatch;
35
48
  forbiddenRedirectTo?: string | URL | null;
36
49
  forbiddenRedirectStatus?: RedirectStatus;
37
50
  }
@@ -186,25 +199,59 @@ export async function requireRole<
186
199
  )
187
200
  );
188
201
 
189
- if (!allowed) {
190
- if (
191
- forbiddenRedirectTo !== null
192
- ) {
193
- return redirect(
194
- forbiddenRedirectTo,
195
- forbiddenRedirectStatus
196
- );
197
- }
202
+ return finalizeAuthorization(
203
+ allowed,
204
+ authenticated,
205
+ forbiddenRedirectTo,
206
+ forbiddenRedirectStatus
207
+ );
208
+ }
198
209
 
199
- return new Response(
200
- "Forbidden",
210
+ export async function requirePermission<
211
+ TUser extends AuthUser = AuthUser,
212
+ TData extends object = Record<string, never>
213
+ >(
214
+ requiredPermission:
215
+ PermissionRequirement,
216
+ options:
217
+ RequirePermissionOptions = {}
218
+ ): Promise<AuthGuardResult<TUser, TData>> {
219
+ const {
220
+ permissionField =
221
+ "permissions",
222
+ match = "any",
223
+ forbiddenRedirectTo = null,
224
+ forbiddenRedirectStatus = 303,
225
+ ...authOptions
226
+ } = options;
227
+ const authenticated =
228
+ await requireAuth<TUser, TData>(
229
+ authOptions
230
+ );
231
+
232
+ if (
233
+ authenticated instanceof Response
234
+ ) {
235
+ return authenticated;
236
+ }
237
+
238
+ const allowed =
239
+ hasPermission(
240
+ authenticated.auth.user,
241
+ requiredPermission,
201
242
  {
202
- status: 403,
243
+ field:
244
+ permissionField,
245
+ match,
203
246
  }
204
247
  );
205
- }
206
248
 
207
- return authenticated;
249
+ return finalizeAuthorization(
250
+ allowed,
251
+ authenticated,
252
+ forbiddenRedirectTo,
253
+ forbiddenRedirectStatus
254
+ );
208
255
  }
209
256
 
210
257
  export function createAuthGuard<
@@ -245,6 +292,22 @@ export function createRoleGuard<
245
292
  );
246
293
  }
247
294
 
295
+ export function createPermissionGuard<
296
+ TUser extends AuthUser = AuthUser,
297
+ TData extends object = Record<string, never>
298
+ >(
299
+ requiredPermission:
300
+ PermissionRequirement,
301
+ options:
302
+ RequirePermissionOptions = {}
303
+ ): AuthGuardFunction<TUser, TData> {
304
+ return () =>
305
+ requirePermission<TUser, TData>(
306
+ requiredPermission,
307
+ options
308
+ );
309
+ }
310
+
248
311
  export function getGuardAuth<
249
312
  TUser extends AuthUser = AuthUser,
250
313
  TData extends object = Record<string, never>
@@ -293,6 +356,39 @@ export function getGuardAuth<
293
356
  AuthSession<TUser, TData>;
294
357
  }
295
358
 
359
+ function finalizeAuthorization<
360
+ TUser extends AuthUser,
361
+ TData extends object
362
+ >(
363
+ allowed: boolean,
364
+ authenticated:
365
+ AuthGuardData<TUser, TData>,
366
+ forbiddenRedirectTo:
367
+ string | URL | null,
368
+ forbiddenRedirectStatus:
369
+ RedirectStatus
370
+ ): Promise<AuthGuardResult<TUser, TData>> | AuthGuardData<TUser, TData> | Response {
371
+ if (allowed) {
372
+ return authenticated;
373
+ }
374
+
375
+ if (
376
+ forbiddenRedirectTo !== null
377
+ ) {
378
+ return redirect(
379
+ forbiddenRedirectTo,
380
+ forbiddenRedirectStatus
381
+ );
382
+ }
383
+
384
+ return new Response(
385
+ "Forbidden",
386
+ {
387
+ status: 403,
388
+ }
389
+ );
390
+ }
391
+
296
392
  function normalizeRequiredRoles(
297
393
  value: RequiredRole
298
394
  ): string[] {