@@ -61,6 +61,13 @@ export class CsrfGuard {
61
61
*/
62
62
#edge?: Edge
63
63
64
+ /**
65
+ * Creates a new CsrfGuard instance.
66
+ *
67
+ * @param options - CSRF configuration options
68
+ * @param encryption - Encryption service instance
69
+ * @param edge - Optional Edge template engine instance
70
+ */
64
71
constructor ( options : CsrfOptions , encryption : Encryption , edge ?: Edge ) {
65
72
this . #options = options
66
73
this . #encryption = encryption
@@ -71,7 +78,10 @@ export class CsrfGuard {
71
78
}
72
79
73
80
/**
74
- * Find if a request should be validated or not
81
+ * Determines if a request should be validated for CSRF tokens.
82
+ * Checks against allowed methods and routes to ignore.
83
+ *
84
+ * @param ctx - HTTP context object
75
85
*/
76
86
#shouldValidateRequest( ctx : HttpContext ) {
77
87
/**
@@ -106,12 +116,12 @@ export class CsrfGuard {
106
116
}
107
117
108
118
/**
109
- * Read csrf token from one of the following sources.
119
+ * Reads CSRF token from one of the following sources:
120
+ * - `_csrf` form input field
121
+ * - `x-csrf-token` request header
122
+ * - `x-xsrf-token` header (when XSRF cookie is enabled)
110
123
*
111
- * - `_csrf` input
112
- * - `x-csrf-token` header
113
- * - Or `x-xsrf-token` header. The header value must be set by
114
- * reading the `XSRF-TOKEN` cookie.
124
+ * @param ctx - HTTP context object containing the request
115
125
*/
116
126
#getCsrfTokenFromRequest( { request } : HttpContext ) : string | null {
117
127
if ( request . input ( '_csrf' ) ) {
@@ -142,7 +152,10 @@ export class CsrfGuard {
142
152
}
143
153
144
154
/**
145
- * Share csrf helper methods with the view engine.
155
+ * Shares CSRF helper methods with the view engine.
156
+ * Makes csrfToken, csrfMeta(), and csrfField() available in templates.
157
+ *
158
+ * @param ctx - HTTP context object
146
159
*/
147
160
#shareCsrfViewLocals( ctx : HttpContext ) : void {
148
161
if ( ! ctx . view || ! this . #edge) {
@@ -165,16 +178,19 @@ export class CsrfGuard {
165
178
}
166
179
167
180
/**
168
- * Generate a new csrf token using the csrf secret extracted from session.
181
+ * Generates a new CSRF token using the CSRF secret extracted from session.
182
+ *
183
+ * @param csrfSecret - The CSRF secret from the user's session
169
184
*/
170
185
#generateCsrfToken( csrfSecret : string ) : string {
171
186
return this . #tokens. create ( csrfSecret )
172
187
}
173
188
174
189
/**
175
- * Return the existing CSRF secret from the session or create a
176
- * new one. Newly created secret is persisted to session at
177
- * the same time
190
+ * Returns the existing CSRF secret from the session or creates a new one.
191
+ * Newly created secrets are persisted to the session automatically.
192
+ *
193
+ * @param ctx - HTTP context object
178
194
*/
179
195
async #getCsrfSecret( ctx : HttpContext ) : Promise < string > {
180
196
let csrfSecret = ctx . session . get ( this . #secretSessionKey)
@@ -189,10 +205,11 @@ export class CsrfGuard {
189
205
}
190
206
191
207
/**
192
- * Handle csrf verification. First, get the secret,
193
- * next, check if the request method should be
194
- * verified. Next, attach the newly generated
195
- * csrf token to the request object.
208
+ * Handles CSRF verification for the current request.
209
+ * Gets or creates a CSRF secret, generates a token, optionally sets XSRF cookie,
210
+ * shares helpers with views, and validates the request if required.
211
+ *
212
+ * @param ctx - HTTP context object
196
213
*/
197
214
async handle ( ctx : HttpContext ) : Promise < void > {
198
215
const csrfSecret = await this . #getCsrfSecret( ctx )
@@ -230,8 +247,18 @@ export class CsrfGuard {
230
247
}
231
248
232
249
/**
233
- * A factory function that returns a new function to enforce CSRF
234
- * protection
250
+ * A factory function that returns a new function to enforce CSRF protection.
251
+ * Creates a CsrfGuard instance and returns its handle method.
252
+ *
253
+ * @param options - CSRF configuration options
254
+ * @param encryption - Encryption service instance
255
+ * @param edge - Optional Edge template engine instance
256
+ *
257
+ * @example
258
+ * const csrfGuard = csrfFactory({
259
+ * enabled: true,
260
+ * methods: ['POST', 'PUT', 'DELETE']
261
+ * }, encryption, edge)
235
262
*/
236
263
export function csrfFactory ( options : CsrfOptions , encryption : Encryption , edge ?: Edge ) {
237
264
if ( ! options . enabled ) {
0 commit comments