@@ -118,20 +118,18 @@ func isValidLogPath(logPath string) bool {
118118 return true
119119 }
120120
121- // If it's a symlink, follow it
121+ // If it's a symlink, follow it safely
122122 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 )
124126 if err != nil {
127+ logger .Warn ("Failed to resolve symlink (possible circular reference):" , logPath , "error:" , err )
125128 return false
126129 }
127130
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 )
135133 if err != nil {
136134 return false
137135 }
@@ -149,7 +147,12 @@ func IsLogPathUnderWhiteList(path string) bool {
149147 cacheKey := fmt .Sprintf ("isLogPathUnderWhiteList:%s" , path )
150148 res , ok := cache .Get (cacheKey )
151149
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
153156 logDirWhiteList := append ([]string {}, settings .NginxSettings .LogDirWhiteList ... )
154157
155158 accessLogPath := nginx .GetAccessLogPath ()
@@ -165,15 +168,15 @@ func IsLogPathUnderWhiteList(path string) bool {
165168 logDirWhiteList = append (logDirWhiteList , nginx .GetPrefix ())
166169 }
167170
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
175176 }
176- return false
177177 }
178- return res .(bool )
178+
179+ // Cache negative result as well to avoid repeated checks
180+ cache .Set (cacheKey , false , 0 )
181+ return false
179182}
0 commit comments