Skip to content

Commit 993ec6a

Browse files
fix(util): safe unzip (#3931)
Signed-off-by: Jiyong Huang <huangjy@emqx.io> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 64c4844 commit 993ec6a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

internal/plugin/native/manager.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ import (
4848
"github.com/lf-edge/ekuiper/v2/pkg/kv"
4949
)
5050

51+
// isSafeArchiveEntry checks if the entry name is safe for extraction
52+
func isSafeArchiveEntry(name string) bool {
53+
// Disallow absolute paths
54+
if strings.HasPrefix(name, "/") || strings.HasPrefix(name, "\\") {
55+
return false
56+
}
57+
// Disallow parent traversal
58+
parts := strings.Split(name, "/")
59+
for _, p := range parts {
60+
if p == ".." {
61+
return false
62+
}
63+
}
64+
parts = strings.Split(name, "\\")
65+
for _, p := range parts {
66+
if p == ".." {
67+
return false
68+
}
69+
}
70+
// Prevent special device files (on Windows)
71+
if strings.HasPrefix(name, "CON") || strings.HasPrefix(name, "PRN") || strings.HasPrefix(name, "AUX") || strings.HasPrefix(name, "NUL") {
72+
return false
73+
}
74+
return true
75+
}
76+
5177
// Manager Initialized in the binder
5278
var (
5379
manager *Manager
@@ -538,6 +564,11 @@ func (rr *Manager) install(t plugin2.PluginType, name, src string, shellParas []
538564
for _, file := range r.File {
539565
zipFiles = append(zipFiles, file.Name)
540566
fileName := file.Name
567+
// Prevent Zip Slip: only allow safe archive entries
568+
if !isSafeArchiveEntry(fileName) {
569+
conf.Log.Errorf("Refuse to extract potentially unsafe archive entry: %s", fileName)
570+
continue
571+
}
541572
switch {
542573
case yamlFile == fileName:
543574
yamlFileChecked = true

0 commit comments

Comments
 (0)