@@ -34,6 +34,8 @@ async function runFindTriggers(
34
34
options = { }
35
35
) {
36
36
const { isGet } = options ;
37
+
38
+ // Run beforeFind trigger - may modify query or return objects directly
37
39
const result = await triggers . maybeRunQueryTrigger (
38
40
triggers . Types . beforeFind ,
39
41
className ,
@@ -48,11 +50,14 @@ async function runFindTriggers(
48
50
restWhere = result . restWhere || restWhere ;
49
51
restOptions = result . restOptions || restOptions ;
50
52
53
+ // Short-circuit path: beforeFind returned objects directly
54
+ // Security risk: These objects may have been fetched with master privileges
51
55
if ( result ?. objects ) {
52
56
const objectsFromBeforeFind = result . objects ;
53
57
54
58
let objectsForAfterFind = objectsFromBeforeFind ;
55
59
60
+ // Security check: Re-filter objects if not master to ensure ACL/CLP compliance
56
61
if ( ! auth ?. isMaster && ! auth ?. isMaintenance ) {
57
62
const ids = ( Array . isArray ( objectsFromBeforeFind ) ? objectsFromBeforeFind : [ objectsFromBeforeFind ] )
58
63
. map ( o => ( o && ( o . id || o . objectId ) ) || null )
@@ -61,6 +66,7 @@ async function runFindTriggers(
61
66
if ( ids . length > 0 ) {
62
67
const refilterWhere = isGet ? { objectId : ids [ 0 ] } : { objectId : { $in : ids } } ;
63
68
69
+ // Re-query with proper security: no triggers to avoid infinite loops
64
70
const refilterQuery = await RestQuery ( {
65
71
method : isGet ? RestQuery . Method . get : RestQuery . Method . find ,
66
72
config,
@@ -79,6 +85,7 @@ async function runFindTriggers(
79
85
}
80
86
}
81
87
88
+ // Run afterFind trigger on security-filtered objects
82
89
const afterFindProcessedObjects = await triggers . maybeRunAfterFindTrigger (
83
90
triggers . Types . afterFind ,
84
91
auth ,
@@ -95,6 +102,7 @@ async function runFindTriggers(
95
102
} ;
96
103
}
97
104
105
+ // Normal path: execute database query with modified conditions
98
106
const query = await RestQuery ( {
99
107
method : isGet ? RestQuery . Method . get : RestQuery . Method . find ,
100
108
config,
0 commit comments