@@ -19,6 +19,7 @@ class WAFContextWrapper {
19
19
this . rulesVersion = rulesVersion
20
20
this . addressesToSkip = new Set ( )
21
21
this . knownAddresses = knownAddresses
22
+ this . cachedUserIdActions = new Map ( )
22
23
}
23
24
24
25
run ( { persistent, ephemeral } , raspRule ) {
@@ -27,6 +28,16 @@ class WAFContextWrapper {
27
28
return
28
29
}
29
30
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
+
30
41
const payload = { }
31
42
let payloadHasData = false
32
43
const newAddressesToSkip = new Set ( this . addressesToSkip )
@@ -79,6 +90,12 @@ class WAFContextWrapper {
79
90
80
91
const blockTriggered = ! ! getBlockingAction ( result . actions )
81
92
93
+ // SPECIAL CASE FOR USER_ID
94
+ // TODO: make this universal
95
+ if ( userId && ruleTriggered && blockTriggered ) {
96
+ this . setUserIdCache ( userId , result )
97
+ }
98
+
82
99
Reporter . reportMetrics ( {
83
100
duration : result . totalRuntime / 1e3 ,
84
101
durationExt : parseInt ( end - start ) / 1e3 ,
@@ -105,6 +122,26 @@ class WAFContextWrapper {
105
122
}
106
123
}
107
124
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
+
108
145
dispose ( ) {
109
146
this . ddwafContext . dispose ( )
110
147
}
0 commit comments