@@ -19,6 +19,7 @@ class WAFContextWrapper {
1919 this . rulesVersion = rulesVersion
2020 this . addressesToSkip = new Set ( )
2121 this . knownAddresses = knownAddresses
22+ this . cachedUserIdActions = new Map ( )
2223 }
2324
2425 run ( { persistent, ephemeral } , raspRule ) {
@@ -27,6 +28,16 @@ class WAFContextWrapper {
2728 return
2829 }
2930
31+ // SPECIAL CASE FOR USER_ID
32+ // TODO: make this universal
33+ const userId = persistent ?. [ addresses . USER_ID ] || ephemeral ?. [ addresses . USER_ID ]
34+ if ( userId ) {
35+ const cachedAction = this . cachedUserIdActions . get ( userId )
36+ if ( cachedAction ) {
37+ return cachedAction
38+ }
39+ }
40+
3041 const payload = { }
3142 let payloadHasData = false
3243 const newAddressesToSkip = new Set ( this . addressesToSkip )
@@ -79,6 +90,12 @@ class WAFContextWrapper {
7990
8091 const blockTriggered = ! ! getBlockingAction ( result . actions )
8192
93+ // SPECIAL CASE FOR USER_ID
94+ // TODO: make this universal
95+ if ( userId && ruleTriggered && blockTriggered ) {
96+ this . setUserIdCache ( userId , result )
97+ }
98+
8299 Reporter . reportMetrics ( {
83100 duration : result . totalRuntime / 1e3 ,
84101 durationExt : parseInt ( end - start ) / 1e3 ,
@@ -105,6 +122,26 @@ class WAFContextWrapper {
105122 }
106123 }
107124
125+ setUserIdCache ( userId , result ) {
126+ // using old loops for speed
127+ for ( let i = 0 ; i < result . events . length ; i ++ ) {
128+ const event = result . events [ i ]
129+
130+ for ( let j = 0 ; j < event ?. rule_matches ?. length ; j ++ ) {
131+ const match = event . rule_matches [ j ]
132+
133+ for ( let k = 0 ; k < match ?. parameters ?. length ; k ++ ) {
134+ const parameter = match . parameters [ k ]
135+
136+ if ( parameter ?. address === addresses . USER_ID ) {
137+ this . cachedUserIdActions . set ( userId , result . actions )
138+ return
139+ }
140+ }
141+ }
142+ }
143+ }
144+
108145 dispose ( ) {
109146 this . ddwafContext . dispose ( )
110147 }
0 commit comments