@astratra/security 1.2.0 → 1.3.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.
- package/README.md +23 -1
- package/package.json +1 -1
- package/src/cors.js +56 -0
- package/src/index.d.ts +19 -0
- package/src/index.js +1 -0
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @astratra/security
|
|
2
2
|
|
|
3
3
|
Primitives de sécurité applicative pour l'authentification, l'autorisation,
|
|
4
|
-
le rate limiting, une CSP configurable, un WAF heuristique et
|
|
4
|
+
le CORS, le rate limiting, une CSP configurable, un WAF heuristique et
|
|
5
5
|
WebAuthn/passkeys — génériques et découplées de toute base de données, ORM
|
|
6
6
|
ou liste de rôles fixe. Dépend de `@astratra/core`.
|
|
7
7
|
|
|
@@ -9,6 +9,28 @@ Partout où un vrai projet a besoin de persistance (révocation de session,
|
|
|
9
9
|
stockage de credentials WebAuthn, alerte de brute-force), c'est un
|
|
10
10
|
callback/adapter injecté, jamais un appel base de données codé en dur.
|
|
11
11
|
|
|
12
|
+
## CORS
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
const { createCorsMiddleware } = require('@astratra/security');
|
|
16
|
+
|
|
17
|
+
// À monter EN PREMIER, avant tout autre middleware — les en-têtes CORS,
|
|
18
|
+
// y compris sur la réponse de preflight OPTIONS, doivent être posés avant
|
|
19
|
+
// qu'un autre handler ne puisse court-circuiter la requête.
|
|
20
|
+
app.use(createCorsMiddleware({
|
|
21
|
+
allowedOrigins: (process.env.CORS_ORIGIN || '').split(',').filter(Boolean)
|
|
22
|
+
}));
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Astratra n'impose aucune politique CORS fixe — les origines autorisées sont
|
|
26
|
+
spécifiques à chaque projet. `@astratra/saas-kit` expose cette primitive via
|
|
27
|
+
`options.cors`, montée automatiquement au bon endroit — voir son README.
|
|
28
|
+
|
|
29
|
+
Origines `http://127.0.0.1`/`http://localhost` (tout port) autorisées par
|
|
30
|
+
défaut hors `NODE_ENV=production`, désactivable via `allowDevOrigins: false`.
|
|
31
|
+
`credentials: false` retire `Access-Control-Allow-Credentials` si tu n'as
|
|
32
|
+
pas besoin des cookies cross-origin.
|
|
33
|
+
|
|
12
34
|
## Auth JWT + RBAC
|
|
13
35
|
|
|
14
36
|
```js
|
package/package.json
CHANGED
package/src/cors.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const DEFAULT_ALLOWED_HEADERS = 'content-type, authorization, x-csrf-token';
|
|
2
|
+
const DEFAULT_ALLOWED_METHODS = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
|
|
3
|
+
|
|
4
|
+
const isAllowedDevOrigin = (origin) => {
|
|
5
|
+
if (process.env.NODE_ENV === 'production') return false;
|
|
6
|
+
try {
|
|
7
|
+
const url = new URL(origin);
|
|
8
|
+
return ['127.0.0.1', 'localhost'].includes(url.hostname);
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* CORS middleware. Astratra intentionally has no fixed opinion on which
|
|
16
|
+
* origins to allow — that's project-specific — but leaving CORS entirely
|
|
17
|
+
* unaddressed pushed every consumer (including create-astratra-app's own
|
|
18
|
+
* generated template) to hand-roll the same ~15 lines. This promotes that
|
|
19
|
+
* proven logic into a shared, configurable primitive, matching the pattern
|
|
20
|
+
* already used for createWafMiddleware / createCspMiddleware.
|
|
21
|
+
*
|
|
22
|
+
* Passing `options.cors` to createSaasApp mounts this as the FIRST
|
|
23
|
+
* middleware, ahead of everything else — CORS headers, including on the
|
|
24
|
+
* preflight OPTIONS response, must be set before any other handler can
|
|
25
|
+
* short-circuit the request. Omit `options.cors` and nothing changes.
|
|
26
|
+
*/
|
|
27
|
+
const createCorsMiddleware = (options = {}) => {
|
|
28
|
+
const allowed = new Set(options.allowedOrigins || []);
|
|
29
|
+
const allowDevOrigins = options.allowDevOrigins !== false;
|
|
30
|
+
const allowCredentials = options.credentials !== false;
|
|
31
|
+
const allowedHeaders = options.allowedHeaders || DEFAULT_ALLOWED_HEADERS;
|
|
32
|
+
const allowedMethods = options.allowedMethods || DEFAULT_ALLOWED_METHODS;
|
|
33
|
+
|
|
34
|
+
return (req, res, next) => {
|
|
35
|
+
const origin = req.headers && (req.headers.origin || req.headers.Origin);
|
|
36
|
+
if (origin && (allowed.has(origin) || (allowDevOrigins && isAllowedDevOrigin(origin)))) {
|
|
37
|
+
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
38
|
+
res.setHeader('Vary', 'Origin');
|
|
39
|
+
if (allowCredentials) {
|
|
40
|
+
res.setHeader('Access-Control-Allow-Credentials', 'true');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
res.setHeader('Access-Control-Allow-Headers', allowedHeaders);
|
|
44
|
+
res.setHeader('Access-Control-Allow-Methods', allowedMethods);
|
|
45
|
+
|
|
46
|
+
if (String(req.method || 'GET').toUpperCase() === 'OPTIONS') {
|
|
47
|
+
res.status(204).end();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
return next();
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
module.exports = {
|
|
55
|
+
createCorsMiddleware
|
|
56
|
+
};
|
package/src/index.d.ts
CHANGED
|
@@ -22,6 +22,25 @@ export type NextFunction = (error?: unknown) => unknown;
|
|
|
22
22
|
export type RequestHandler = (req: RequestLike, res: ResponseLike, next: NextFunction) => unknown;
|
|
23
23
|
export type Awaitable<T> = T | Promise<T>;
|
|
24
24
|
|
|
25
|
+
export interface CorsMiddlewareOptions {
|
|
26
|
+
/** Explicit list of allowed origins (exact match on the Origin header). */
|
|
27
|
+
allowedOrigins?: string[];
|
|
28
|
+
/** Allow http(s)://127.0.0.1|localhost:* outside NODE_ENV=production. Default true. */
|
|
29
|
+
allowDevOrigins?: boolean;
|
|
30
|
+
/** Sets Access-Control-Allow-Credentials: true. Default true (matches cookie-based sessions). */
|
|
31
|
+
credentials?: boolean;
|
|
32
|
+
allowedHeaders?: string;
|
|
33
|
+
allowedMethods?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Astratra has no fixed opinion on which origins to allow — that's
|
|
38
|
+
* project-specific — but leaves this primitive available rather than
|
|
39
|
+
* making every consumer hand-roll it. Mount first, ahead of every other
|
|
40
|
+
* middleware, so CORS headers apply to the preflight OPTIONS response too.
|
|
41
|
+
*/
|
|
42
|
+
export function createCorsMiddleware(options?: CorsMiddlewareOptions): RequestHandler;
|
|
43
|
+
|
|
25
44
|
export interface AuthMiddlewareOptions<TDecoded = Record<string, unknown>> {
|
|
26
45
|
secret: string;
|
|
27
46
|
legacySecret?: string;
|