@@ -9,7 +9,6 @@ typedef struct Rules_t RulesSet;
9
9
#endif
10
10
#include <modsecurity/transaction.h>
11
11
12
-
13
12
#include <sys/socket.h>
14
13
#include <unistd.h>
15
14
#include <string.h>
@@ -136,12 +135,15 @@ VCL_VOID v_matchproto_(td_sec_sec__init)
136
135
int error ;
137
136
(void )vcl_name ;
138
137
138
+ if (ctx -> method != VCL_MET_INIT ) {
139
+ VRT_fail (ctx , "[vmodsec] - init can only be called from vcl_init{}" );
140
+ }
139
141
/* Sanity check */
140
142
CHECK_OBJ_NOTNULL (ctx , VRT_CTX_MAGIC );
141
143
AN (vpp );
142
144
AZ (* vpp );
143
145
144
- VSL (SLT_Error , 0 , "[vmodsec] - object [%s] initialized using modsecurity %s" ,
146
+ VSL (SLT_Debug , 0 , "[vmodsec] - object [%s] initialized using modsecurity %s" ,
145
147
vcl_name , MODSECURITY_VERSION );
146
148
147
149
modsec = msc_init ();
@@ -188,6 +190,11 @@ VCL_INT v_matchproto_(td_sec_sec_add_rule)
188
190
const char * error = NULL ;
189
191
VSL (SLT_Debug , 0 , "[vmodsec] - [%s] - VCL provided rule" , rule );
190
192
CHECK_OBJ_NOTNULL (vp , VMOD_SEC_SEC_MAGIC_BITS );
193
+
194
+ if (ctx -> method != VCL_MET_INIT ) {
195
+ VRT_fail (ctx , "[vmodsec] - .add_rule can only be called from vcl_init{}" );
196
+ }
197
+
191
198
ret = msc_rules_add (vp -> rules_set , rule , & error );
192
199
if (ret < 0 )
193
200
{
@@ -208,6 +215,10 @@ VCL_INT v_matchproto_(td_sec_sec_add_rules)
208
215
{
209
216
int ret ;
210
217
const char * error = NULL ;
218
+ if (ctx -> method != VCL_MET_INIT ) {
219
+ VRT_fail (ctx , "[vmodsec] - .add_rules can only be called from vcl_init{}" );
220
+ }
221
+
211
222
212
223
VSL (SLT_Debug , 0 , "[vmodsec] - [%s] - Try to load the rules" , args -> rules_path );
213
224
CHECK_OBJ_NOTNULL (vp , VMOD_SEC_SEC_MAGIC_BITS );
@@ -271,6 +282,10 @@ VCL_INT v_matchproto_(td_sec_sec_new_conn)
271
282
CHECK_OBJ_NOTNULL (ctx , VRT_CTX_MAGIC );
272
283
CHECK_OBJ_NOTNULL (vp , VMOD_SEC_SEC_MAGIC_BITS );
273
284
285
+ if (ctx -> method != VCL_MET_RECV ) {
286
+ VRT_fail (ctx , "[vmodsec] - new_conn can only be called from vcl_recv{}" );
287
+ }
288
+
274
289
struct vmod_priv * p ;
275
290
if (args -> arg1 == NULL ) {
276
291
return 0 ;
@@ -337,6 +352,7 @@ VCL_INT v_matchproto_(td_sec_sec_process_url)
337
352
VSL (SLT_Error , ctx -> sp -> vxid , "[vmodsec] - connection has not been started, closing" );
338
353
return -1 ;
339
354
}
355
+
340
356
struct vmod_sec_struct_trans_int * transInt = (struct vmod_sec_struct_trans_int * )priv -> priv ;
341
357
/* This will be used to Initialise the original URL */
342
358
msc_process_uri (transInt -> trans , req_url , protocol , http_version );
@@ -352,16 +368,17 @@ VCL_INT v_matchproto_(td_sec_sec_process_url)
352
368
353
369
/* Handling headers */
354
370
unsigned u ;
355
- const struct http * hp = ctx -> req -> http ;
371
+ const struct http * hp = ctx -> http_req ;
356
372
#ifdef VMOD_SEC_DEBUG
357
373
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Found %d headers, Start at %d, need to ingest %d headers" , hp -> nhd , HTTP_HDR_FIRST , hp -> nhd - HTTP_HDR_FIRST );
358
374
#endif
359
- // Freed after loop
360
- char * headerName = malloc (8192 );
361
- char * headerValue = malloc (8192 );
375
+ int headerCount = hp -> nhd - HTTP_HDR_FIRST ;
376
+ char * * headersNames = ( char * * ) malloc (sizeof ( char * ) * headerCount );
377
+ char * * headersValues = ( char * * ) malloc (sizeof ( char * ) * headerCount );
362
378
363
379
for (u = HTTP_HDR_FIRST ; u < hp -> nhd ; u ++ )
364
380
{
381
+ int headerPos = u - HTTP_HDR_FIRST ;
365
382
Tcheck (hp -> hd [u ]);
366
383
const char * header = hp -> hd [u ].b ;
367
384
long int hlen = strlen (header );
@@ -372,24 +389,32 @@ VCL_INT v_matchproto_(td_sec_sec_process_url)
372
389
continue ;
373
390
}
374
391
/* Copy headers */
375
- strncpy (headerName , header , pos );
376
- headerName [pos ] = '\0' ;
392
+ headersNames [headerPos ] = (char * )malloc (pos + 1 );
393
+ strncpy (headersNames [headerPos ], header , pos );
394
+ headersNames [headerPos ][pos ] = '\0' ;
377
395
// Find spaces
378
396
pos += 1 /* : */ + strspn (& header [pos + 1 ], " \r\n\t" ); // LWS = [CRLF] 1*( SP | HT ) chr(9,10,13,32)
379
- strncpy (headerValue , & header [pos ], hlen - pos );
380
- headerValue [hlen - pos ] = '\0' ;
381
- msc_add_request_header (transInt -> trans , headerName , headerValue );
397
+ // Copy value
398
+ headersValues [headerPos ] = (char * )malloc (hlen - pos + 1 );
399
+ strncpy (headersValues [headerPos ], & header [pos ], hlen - pos );
400
+ headersValues [headerPos ][hlen - pos ] = '\0' ;
401
+ // FIXME : use msc_add_n_request_header
402
+ msc_add_request_header (transInt -> trans , headersNames [headerPos ], headersValues [headerPos ]);
382
403
#ifdef VMOD_SEC_DEBUG
383
404
VSL (SLT_Debug , ctx -> sp -> vxid ,
384
- "[vmodsec] - Additional header provided %s: %s" , headerName , headerValue );
405
+ "[vmodsec] - Additional header provided %s: %s" , headersNames [ headerPos ], headersValues [ headerPos ] );
385
406
#endif
386
407
}
387
- free (headerName );
388
- free (headerValue );
389
408
#ifdef VMOD_SEC_DEBUG
390
409
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Processing Request Headers" );
391
410
#endif
392
411
msc_process_request_headers (transInt -> trans );
412
+ for (u = 0 ; u < headerCount ; ++ u ) {
413
+ free (headersNames [u ]);
414
+ free (headersValues [u ]);
415
+ }
416
+ free (headersNames );
417
+ free (headersValues );
393
418
return process_intervention (transInt );
394
419
}
395
420
@@ -431,10 +456,16 @@ VCL_INT v_matchproto_(td_sec_sec_do_process_request_body)
431
456
if (capture_body == 1 )
432
457
{
433
458
const struct http * hp = ctx -> req -> http ;
459
+ if (ctx -> req -> req_body_status == BS_NONE )
460
+ {
461
+ msc_process_request_body (transInt -> trans );
462
+ return process_intervention (transInt );
463
+ }
434
464
if (ctx -> req -> req_body_status != BS_CACHED )
435
465
{
436
466
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Unbuffered req.body" );
437
- return -1 ;
467
+ msc_process_request_body (transInt -> trans );
468
+ return process_intervention (transInt );
438
469
}
439
470
440
471
int ret ;
@@ -447,8 +478,8 @@ VCL_INT v_matchproto_(td_sec_sec_do_process_request_body)
447
478
{
448
479
VSL (SLT_Error , ctx -> sp -> vxid ,
449
480
"[vmodsec] - Iteration on req.body didn't succeed. %d" , ret );
450
-
451
- return -1 ;
481
+ msc_process_request_body ( transInt -> trans );
482
+ return process_intervention ( transInt ) ;
452
483
}
453
484
454
485
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Processing Request Body" );
@@ -475,18 +506,20 @@ VCL_INT v_matchproto_(td_sec_sec_process_response)
475
506
476
507
/* Handling headers */
477
508
unsigned u ;
478
- const struct http * hp = ctx -> req -> resp ;
509
+ const struct http * hp = ctx -> http_resp ;
479
510
#ifdef VMOD_SEC_DEBUG
480
511
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Processing Response Headers" );
481
512
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Found %d headers, Start at %d, need to ingest %d headers" ,
482
513
hp -> nhd , HTTP_HDR_FIRST , hp -> nhd - HTTP_HDR_FIRST );
483
514
#endif
515
+ int headerCount = hp -> nhd - HTTP_HDR_FIRST ;
484
516
// freed after loop
485
- char * headerName = malloc (8192 );
486
- char * headerValue = malloc (8192 ) ;
517
+ char * * headersNames = ( char * * ) malloc (sizeof ( char * ) * headerCount );
518
+ char * * headersValues = ( char * * ) malloc (sizeof ( char * ) * headerCount ); ;
487
519
488
520
for (u = HTTP_HDR_FIRST ; u < hp -> nhd ; u ++ )
489
521
{
522
+ int headerPos = u - HTTP_HDR_FIRST ;
490
523
Tcheck (hp -> hd [u ]);
491
524
const char * header = hp -> hd [u ].b ;
492
525
long int hlen = strlen (header );
@@ -497,21 +530,27 @@ VCL_INT v_matchproto_(td_sec_sec_process_response)
497
530
continue ;
498
531
}
499
532
/* Copy headers */
500
- strncpy (headerName , header , pos );
501
- headerName [pos ] = '\0' ;
533
+ headersNames [headerPos ] = malloc (pos + 1 );
534
+ strncpy (headersNames [headerPos ], header , pos );
535
+ headersNames [headerPos ][pos ] = '\0' ;
502
536
// Find spaces
503
537
pos += 1 /* : */ + strspn (& header [pos + 1 ], " \r\n\t" ); // LWS = [CRLF] 1*( SP | HT ) chr(9,10,13,32)
504
- strncpy (headerValue , & header [pos ], hlen - pos );
505
- headerValue [hlen - pos ] = '\0' ;
506
- msc_add_response_header (transInt -> trans , headerName , headerValue );
538
+ headersValues [headerPos ] = (char * )malloc (hlen - pos + 1 );
539
+ strncpy (headersValues [headerPos ], & header [pos ], hlen - pos );
540
+ headersValues [headerPos ][hlen - pos ] = '\0' ;
541
+ msc_add_response_header (transInt -> trans , headersNames [headerPos ], headersValues [headerPos ]);
507
542
#ifdef VMOD_SEC_DEBUG
508
543
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Additional response header provided %s: %s" ,
509
- headerName , headerValue );
544
+ headersNames [ headerPos ], headersValues [ headerPos ] );
510
545
#endif
511
546
}
512
- free (headerName );
513
- free (headerValue );
514
547
msc_process_response_headers (transInt -> trans , ctx -> req -> resp -> status , protocol );
548
+ for (u = 0 ; u < headerCount ; ++ u ) {
549
+ free (headersNames [u ]);
550
+ free (headersValues [u ]);
551
+ }
552
+ free (headersNames );
553
+ free (headersValues );
515
554
return process_intervention (transInt );
516
555
}
517
556
@@ -562,7 +601,8 @@ VCL_INT v_matchproto_(td_sec_sec_do_process_response_body)
562
601
VSL (SLT_Error , ctx -> sp -> vxid ,
563
602
"[vmodsec] - Iteration on resp.body didn't succeed. %d" , ret );
564
603
565
- return -1 ;
604
+ msc_process_response_body (transInt -> trans );
605
+ return process_intervention (transInt );
566
606
}
567
607
568
608
VSL (SLT_Debug , ctx -> sp -> vxid , "[vmodsec] - Processing Response Body" );
0 commit comments