Skip to content

Commit 6e80998

Browse files
committed
enhance: improve log path validation logic
1 parent 2bf2e13 commit 6e80998

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"ms-azuretools.vscode-docker",
3232
"akino.i18n-gettext",
3333
"github.vscode-github-actions",
34-
"vue.volar"
34+
"vue.volar",
35+
"eamodio.gitlens"
3536
]
3637
}
3738
},

internal/nginx/errors.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package nginx
33
import "github.com/uozi-tech/cosy"
44

55
var (
6-
e = cosy.NewErrorScope("nginx")
7-
ErrNginx = e.New(50000, "nginx error: {0}")
8-
ErrBlockIsNil = e.New(50001, "block is nil")
9-
ErrReloadFailed = e.New(50002, "reload nginx failed: {0}")
6+
e = cosy.NewErrorScope("nginx")
7+
ErrNginx = e.New(50000, "nginx error: {0}")
8+
ErrBlockIsNil = e.New(50001, "block is nil")
9+
ErrReloadFailed = e.New(50002, "reload nginx failed: {0}")
10+
ErrNginxTOutputEmpty = e.New(50003, "nginx -T output is empty")
1011
)

internal/nginx_log/nginx_log.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)