@davaux/csrf 0.8.0 → 0.8.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/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +70 -0
- package/dist/index.js.map +1 -0
- package/package.json +7 -4
- package/CLAUDE.md +0 -95
- package/src/index.ts +0 -121
- package/tsconfig.json +0 -17
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { MiddlewareFn } from 'davaux';
|
|
2
|
+
declare module 'davaux' {
|
|
3
|
+
interface State {
|
|
4
|
+
csrf: {
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export interface CsrfOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Form field name to check on `application/x-www-form-urlencoded` requests.
|
|
12
|
+
* Default: `'_csrf'`
|
|
13
|
+
*/
|
|
14
|
+
field?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Request header name to check (case-insensitive).
|
|
17
|
+
* Default: `'x-csrf-token'`
|
|
18
|
+
*/
|
|
19
|
+
header?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Session key used to store the token.
|
|
22
|
+
* Default: `'_csrf'`
|
|
23
|
+
*/
|
|
24
|
+
sessionKey?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* CSRF protection middleware. Generates a random token per session and rejects
|
|
28
|
+
* mutating requests (`POST`, `PUT`, `PATCH`, `DELETE`) that do not carry a
|
|
29
|
+
* matching token in either the request header or a form field.
|
|
30
|
+
*
|
|
31
|
+
* Requires `@davaux/session` — register `sessionMiddleware` before this in
|
|
32
|
+
* your middleware chain. The current token is available as `ctx.state.csrf.token`
|
|
33
|
+
* for embedding in forms.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // _global.ts
|
|
37
|
+
* import { csrfMiddleware } from '@davaux/csrf'
|
|
38
|
+
* export default csrfMiddleware({ field: '_csrf' })
|
|
39
|
+
*/
|
|
40
|
+
export declare function csrfMiddleware(options?: CsrfOptions): MiddlewareFn;
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAI1C,OAAO,QAAQ,QAAQ,CAAC;IACtB,UAAU,KAAK;QACb,IAAI,EAAE;YAAE,KAAK,EAAE,MAAM,CAAA;SAAE,CAAA;KACxB;CACF;AAID,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,WAAgB,GAAG,YAAY,CAuCtE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
|
|
3
|
+
// ─── Middleware ───────────────────────────────────────────────────────────────
|
|
4
|
+
/**
|
|
5
|
+
* CSRF protection middleware. Generates a random token per session and rejects
|
|
6
|
+
* mutating requests (`POST`, `PUT`, `PATCH`, `DELETE`) that do not carry a
|
|
7
|
+
* matching token in either the request header or a form field.
|
|
8
|
+
*
|
|
9
|
+
* Requires `@davaux/session` — register `sessionMiddleware` before this in
|
|
10
|
+
* your middleware chain. The current token is available as `ctx.state.csrf.token`
|
|
11
|
+
* for embedding in forms.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // _global.ts
|
|
15
|
+
* import { csrfMiddleware } from '@davaux/csrf'
|
|
16
|
+
* export default csrfMiddleware({ field: '_csrf' })
|
|
17
|
+
*/
|
|
18
|
+
export function csrfMiddleware(options = {}) {
|
|
19
|
+
const field = options.field ?? '_csrf';
|
|
20
|
+
const headerName = (options.header ?? 'x-csrf-token').toLowerCase();
|
|
21
|
+
const sessionKey = options.sessionKey ?? '_csrf';
|
|
22
|
+
return async (ctx, next) => {
|
|
23
|
+
if (!ctx.state.session) {
|
|
24
|
+
throw new Error('@davaux/csrf: sessionMiddleware must be registered before csrfMiddleware');
|
|
25
|
+
}
|
|
26
|
+
// Get or generate the token for this session
|
|
27
|
+
let token = ctx.state.session.get(sessionKey);
|
|
28
|
+
if (!token) {
|
|
29
|
+
token = randomBytes(32).toString('hex');
|
|
30
|
+
ctx.state.session.set(sessionKey, token);
|
|
31
|
+
}
|
|
32
|
+
ctx.state.csrf = { token };
|
|
33
|
+
// Safe methods pass through immediately
|
|
34
|
+
const method = (ctx.req.method ?? 'GET').toUpperCase();
|
|
35
|
+
if (SAFE_METHODS.has(method)) {
|
|
36
|
+
return next();
|
|
37
|
+
}
|
|
38
|
+
// Mutating requests must carry a valid token
|
|
39
|
+
const submitted = await resolveSubmitted(ctx.req, ctx.req.headers[headerName], field, ctx);
|
|
40
|
+
if (!submitted || !safeEqual(submitted, token)) {
|
|
41
|
+
ctx.res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
42
|
+
ctx.res.end('Forbidden: invalid or missing CSRF token');
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
await next();
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
49
|
+
async function resolveSubmitted(req, headerValue, field, ctx) {
|
|
50
|
+
if (headerValue)
|
|
51
|
+
return headerValue;
|
|
52
|
+
const contentType = req.headers['content-type'] ?? '';
|
|
53
|
+
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
54
|
+
const body = await ctx.form();
|
|
55
|
+
return body[field];
|
|
56
|
+
}
|
|
57
|
+
if (contentType.includes('multipart/form-data')) {
|
|
58
|
+
const { fields } = await ctx.multipart();
|
|
59
|
+
return fields[field];
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
function safeEqual(a, b) {
|
|
64
|
+
const aBuf = Buffer.from(a);
|
|
65
|
+
const bBuf = Buffer.from(b);
|
|
66
|
+
if (aBuf.length !== bBuf.length)
|
|
67
|
+
return false;
|
|
68
|
+
return timingSafeEqual(aBuf, bBuf);
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@davaux/csrf",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "CSRF protection middleware for Davaux",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "David L Dyess II",
|
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
"url": "https://codeberg.org/davaux/davaux/issues"
|
|
14
14
|
},
|
|
15
15
|
"homepage": "https://codeberg.org/davaux/davaux#readme",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
16
19
|
"exports": {
|
|
17
20
|
".": {
|
|
18
21
|
"import": "./dist/index.js",
|
|
@@ -24,8 +27,8 @@
|
|
|
24
27
|
"typecheck": "tsc --noEmit"
|
|
25
28
|
},
|
|
26
29
|
"peerDependencies": {
|
|
27
|
-
"@davaux/session": ">=0.8.
|
|
28
|
-
"davaux": ">=0.8.
|
|
30
|
+
"@davaux/session": ">=0.8.1",
|
|
31
|
+
"davaux": ">=0.8.1"
|
|
29
32
|
},
|
|
30
33
|
"devDependencies": {
|
|
31
34
|
"@davaux/session": "*",
|
|
@@ -33,4 +36,4 @@
|
|
|
33
36
|
"davaux": "*",
|
|
34
37
|
"typescript": "^6.0.3"
|
|
35
38
|
}
|
|
36
|
-
}
|
|
39
|
+
}
|
package/CLAUDE.md
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
<!-- pka-generated -->
|
|
2
|
-
# @davaux/csrf
|
|
3
|
-
|
|
4
|
-
> Generated by Project Knowledge Analyzer on 2026-06-06T21:53:10.185Z
|
|
5
|
-
|
|
6
|
-
## Overview
|
|
7
|
-
|
|
8
|
-
CSRF protection middleware for Davaux
|
|
9
|
-
|
|
10
|
-
**Version**: 0.8.0
|
|
11
|
-
**Author**: David L Dyess II
|
|
12
|
-
**License**: MIT
|
|
13
|
-
**Repository**: https://codeberg.org/davaux/davaux#readme
|
|
14
|
-
|
|
15
|
-
## Tech Stack
|
|
16
|
-
|
|
17
|
-
- **Language**: TypeScript
|
|
18
|
-
- **Module System**: ESM (`type: module`)
|
|
19
|
-
|
|
20
|
-
## Commands
|
|
21
|
-
|
|
22
|
-
- `npm run build` — tsc
|
|
23
|
-
- `npm run typecheck` — tsc --noEmit
|
|
24
|
-
|
|
25
|
-
## Project Structure
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
├── CLAUDE.md
|
|
29
|
-
├── README.md
|
|
30
|
-
├── package.json
|
|
31
|
-
├── tsconfig.json
|
|
32
|
-
└── src/
|
|
33
|
-
└── index.ts
|
|
34
|
-
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Entry Points
|
|
38
|
-
|
|
39
|
-
- `src/index.ts`
|
|
40
|
-
|
|
41
|
-
## Files by Type
|
|
42
|
-
|
|
43
|
-
### Documentation (2)
|
|
44
|
-
- `CLAUDE.md`
|
|
45
|
-
- `README.md`
|
|
46
|
-
|
|
47
|
-
### Config (2)
|
|
48
|
-
- `package.json`
|
|
49
|
-
- `tsconfig.json`
|
|
50
|
-
|
|
51
|
-
### Module (1)
|
|
52
|
-
- `src/index.ts`
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
## Git
|
|
56
|
-
- **Branch**: main
|
|
57
|
-
- **Last Commit**: chore: Add alpha status note to README
|
|
58
|
-
- **Author**: David Dyess II
|
|
59
|
-
- **Date**: 2026-06-06 15:52:27 -0600
|
|
60
|
-
- **Remote**: https://codeberg.org/davaux/davaux.git
|
|
61
|
-
|
|
62
|
-
### Recent Commits
|
|
63
|
-
```
|
|
64
|
-
f527031 chore: Add alpha status note to README
|
|
65
|
-
90c819e chore: Add repo info to package.json files
|
|
66
|
-
b200d9d feat(davaux)!: Add OmlCacheConfig - opt-in with includes option or opt-out with excludes option; Fix OML implementation to follow OML spec - use output instead of return
|
|
67
|
-
3bce0c2 chore: Update and add READMEs to packages; update ROADMAP
|
|
68
|
-
fc27c20 fix(davaux): Remove old dist folder on new builds; fix server port per DavauxConfig
|
|
69
|
-
c760659 feat(davaux): Add minify CSS in production builds
|
|
70
|
-
c899ab8 fix(davaux): Added method JS and JSX extenstion to scanner
|
|
71
|
-
7d99f04 feat(davaux): Add support for declarative partial updates
|
|
72
|
-
3b47f37 chore: Bump package versions to 0.8.0
|
|
73
|
-
aa0460f chore: Add CHANGELOG.md
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Dependencies
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
### Development
|
|
80
|
-
@davaux/session, @types/node, davaux, typescript
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
## Exported Symbols
|
|
91
|
-
|
|
92
|
-
**`src/index.ts`**
|
|
93
|
-
`csrfMiddleware`, `CsrfOptions`
|
|
94
|
-
|
|
95
|
-
|
package/src/index.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { randomBytes, timingSafeEqual } from 'node:crypto'
|
|
2
|
-
import type {} from '@davaux/session'
|
|
3
|
-
import type { MiddlewareFn } from 'davaux'
|
|
4
|
-
|
|
5
|
-
// ─── State augmentation ───────────────────────────────────────────────────────
|
|
6
|
-
|
|
7
|
-
declare module 'davaux' {
|
|
8
|
-
interface State {
|
|
9
|
-
csrf: { token: string }
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// ─── Options ──────────────────────────────────────────────────────────────────
|
|
14
|
-
|
|
15
|
-
export interface CsrfOptions {
|
|
16
|
-
/**
|
|
17
|
-
* Form field name to check on `application/x-www-form-urlencoded` requests.
|
|
18
|
-
* Default: `'_csrf'`
|
|
19
|
-
*/
|
|
20
|
-
field?: string
|
|
21
|
-
/**
|
|
22
|
-
* Request header name to check (case-insensitive).
|
|
23
|
-
* Default: `'x-csrf-token'`
|
|
24
|
-
*/
|
|
25
|
-
header?: string
|
|
26
|
-
/**
|
|
27
|
-
* Session key used to store the token.
|
|
28
|
-
* Default: `'_csrf'`
|
|
29
|
-
*/
|
|
30
|
-
sessionKey?: string
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS'])
|
|
34
|
-
|
|
35
|
-
// ─── Middleware ───────────────────────────────────────────────────────────────
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* CSRF protection middleware. Generates a random token per session and rejects
|
|
39
|
-
* mutating requests (`POST`, `PUT`, `PATCH`, `DELETE`) that do not carry a
|
|
40
|
-
* matching token in either the request header or a form field.
|
|
41
|
-
*
|
|
42
|
-
* Requires `@davaux/session` — register `sessionMiddleware` before this in
|
|
43
|
-
* your middleware chain. The current token is available as `ctx.state.csrf.token`
|
|
44
|
-
* for embedding in forms.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* // _global.ts
|
|
48
|
-
* import { csrfMiddleware } from '@davaux/csrf'
|
|
49
|
-
* export default csrfMiddleware({ field: '_csrf' })
|
|
50
|
-
*/
|
|
51
|
-
export function csrfMiddleware(options: CsrfOptions = {}): MiddlewareFn {
|
|
52
|
-
const field = options.field ?? '_csrf'
|
|
53
|
-
const headerName = (options.header ?? 'x-csrf-token').toLowerCase()
|
|
54
|
-
const sessionKey = options.sessionKey ?? '_csrf'
|
|
55
|
-
|
|
56
|
-
return async (ctx, next) => {
|
|
57
|
-
if (!ctx.state.session) {
|
|
58
|
-
throw new Error('@davaux/csrf: sessionMiddleware must be registered before csrfMiddleware')
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Get or generate the token for this session
|
|
62
|
-
let token = ctx.state.session.get<string>(sessionKey)
|
|
63
|
-
if (!token) {
|
|
64
|
-
token = randomBytes(32).toString('hex')
|
|
65
|
-
ctx.state.session.set(sessionKey, token)
|
|
66
|
-
}
|
|
67
|
-
ctx.state.csrf = { token }
|
|
68
|
-
|
|
69
|
-
// Safe methods pass through immediately
|
|
70
|
-
const method = (ctx.req.method ?? 'GET').toUpperCase()
|
|
71
|
-
if (SAFE_METHODS.has(method)) {
|
|
72
|
-
return next()
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Mutating requests must carry a valid token
|
|
76
|
-
const submitted = await resolveSubmitted(
|
|
77
|
-
ctx.req,
|
|
78
|
-
ctx.req.headers[headerName] as string | undefined,
|
|
79
|
-
field,
|
|
80
|
-
ctx,
|
|
81
|
-
)
|
|
82
|
-
if (!submitted || !safeEqual(submitted, token)) {
|
|
83
|
-
ctx.res.writeHead(403, { 'Content-Type': 'text/plain; charset=utf-8' })
|
|
84
|
-
ctx.res.end('Forbidden: invalid or missing CSRF token')
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
await next()
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
93
|
-
|
|
94
|
-
async function resolveSubmitted(
|
|
95
|
-
req: import('node:http').IncomingMessage,
|
|
96
|
-
headerValue: string | undefined,
|
|
97
|
-
field: string,
|
|
98
|
-
ctx: import('davaux').RequestContext,
|
|
99
|
-
): Promise<string | undefined> {
|
|
100
|
-
if (headerValue) return headerValue
|
|
101
|
-
|
|
102
|
-
const contentType = req.headers['content-type'] ?? ''
|
|
103
|
-
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
104
|
-
const body = await ctx.form()
|
|
105
|
-
return body[field] as string | undefined
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (contentType.includes('multipart/form-data')) {
|
|
109
|
-
const { fields } = await ctx.multipart()
|
|
110
|
-
return fields[field]
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return undefined
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function safeEqual(a: string, b: string): boolean {
|
|
117
|
-
const aBuf = Buffer.from(a)
|
|
118
|
-
const bBuf = Buffer.from(b)
|
|
119
|
-
if (aBuf.length !== bBuf.length) return false
|
|
120
|
-
return timingSafeEqual(aBuf, bBuf)
|
|
121
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationMap": true,
|
|
9
|
-
"sourceMap": true,
|
|
10
|
-
"outDir": "./dist",
|
|
11
|
-
"rootDir": "./src",
|
|
12
|
-
"skipLibCheck": true,
|
|
13
|
-
"lib": ["ESNext"],
|
|
14
|
-
"types": ["node"]
|
|
15
|
-
},
|
|
16
|
-
"include": ["src/**/*"]
|
|
17
|
-
}
|