@@ -118,20 +118,18 @@ func isValidLogPath(logPath string) bool {
118
118
return true
119
119
}
120
120
121
- // If it's a symlink, follow it
121
+ // If it's a symlink, follow it safely
122
122
if fileInfo .Mode ()& os .ModeSymlink != 0 {
123
- linkTarget , err := os .Readlink (logPath )
123
+ // Use EvalSymlinks to safely resolve the entire symlink chain
124
+ // This function detects circular symlinks and returns an error
125
+ resolvedPath , err := filepath .EvalSymlinks (logPath )
124
126
if err != nil {
127
+ logger .Warn ("Failed to resolve symlink (possible circular reference):" , logPath , "error:" , err )
125
128
return false
126
129
}
127
130
128
- // Make the link target path absolute if it's relative
129
- if ! filepath .IsAbs (linkTarget ) {
130
- linkTarget = filepath .Join (filepath .Dir (logPath ), linkTarget )
131
- }
132
-
133
- // Check the target file
134
- targetInfo , err := os .Stat (linkTarget )
131
+ // Check the resolved target file
132
+ targetInfo , err := os .Stat (resolvedPath )
135
133
if err != nil {
136
134
return false
137
135
}
@@ -149,7 +147,12 @@ func IsLogPathUnderWhiteList(path string) bool {
149
147
cacheKey := fmt .Sprintf ("isLogPathUnderWhiteList:%s" , path )
150
148
res , ok := cache .Get (cacheKey )
151
149
152
- // Deep copy the whitelist
150
+ // If cached, return the result directly
151
+ if ok {
152
+ return res .(bool )
153
+ }
154
+
155
+ // Only build the whitelist when cache miss occurs
153
156
logDirWhiteList := append ([]string {}, settings .NginxSettings .LogDirWhiteList ... )
154
157
155
158
accessLogPath := nginx .GetAccessLogPath ()
@@ -165,15 +168,15 @@ func IsLogPathUnderWhiteList(path string) bool {
165
168
logDirWhiteList = append (logDirWhiteList , nginx .GetPrefix ())
166
169
}
167
170
168
- // No cache, check it
169
- if ! ok {
170
- for _ , whitePath := range logDirWhiteList {
171
- if helper .IsUnderDirectory (path , whitePath ) {
172
- cache .Set (cacheKey , true , 0 )
173
- return true
174
- }
171
+ // Check if path is under any whitelist directory
172
+ for _ , whitePath := range logDirWhiteList {
173
+ if helper .IsUnderDirectory (path , whitePath ) {
174
+ cache .Set (cacheKey , true , 0 )
175
+ return true
175
176
}
176
- return false
177
177
}
178
- return res .(bool )
178
+
179
+ // Cache negative result as well to avoid repeated checks
180
+ cache .Set (cacheKey , false , 0 )
181
+ return false
179
182
}
0 commit comments