Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion internal/core/persistence/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package persistence

import (
"path"
"strings"

"github.com/langgenius/dify-cloud-kit/oss"

"github.com/langgenius/dify-plugin-daemon/internal/types/app"
Expand All @@ -12,8 +15,18 @@ var (
)

func InitPersistence(oss oss.OSS, config *app.Config) {
storagePath := config.PersistenceStoragePath
if prefix := strings.Trim(config.StoragePathPrefix, "/"); prefix != "" {
for _, seg := range strings.Split(prefix, "/") {
if seg == ".." {
log.Panic("STORAGE_PATH_PREFIX must not contain '..'")
}
}
storagePath = path.Join(prefix, storagePath)
}
Comment on lines +19 to +26
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The validation logic for STORAGE_PATH_PREFIX is duplicated here and in internal/core/plugin_manager/manager.go. Additionally, the current check only prevents .. in the prefix itself. If config.PersistenceStoragePath contains .. segments, it could still escape the intended namespace. It is safer to validate the final joined path to ensure it remains within the prefix boundaries.


persistence = &Persistence{
storage: NewWrapper(oss, config.PersistenceStoragePath),
storage: NewWrapper(oss, storagePath),
maxStorageSize: config.PersistenceStorageMaxSize,
}

Expand Down
23 changes: 20 additions & 3 deletions internal/core/plugin_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plugin_manager

import (
"fmt"
"path"
"strings"

lru "github.com/hashicorp/golang-lru/v2"
Expand Down Expand Up @@ -58,20 +59,36 @@ var (
)

func InitGlobalManager(oss oss.OSS, config *app.Config) *PluginManager {
prefix := strings.Trim(config.StoragePathPrefix, "/")
if prefix != "" {
for _, seg := range strings.Split(prefix, "/") {
if seg == ".." {
log.Panic("STORAGE_PATH_PREFIX must not contain '..'")
}
}
}

joinPrefix := func(p string) string {
if prefix == "" {
return p
}
return path.Join(prefix, p)
}
Comment on lines +71 to +76
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The joinPrefix helper should ensure that the resulting path does not escape the configured prefix. If the input path p contains .. segments, path.Join might resolve it to a location outside of the prefix. Validating the joined path against the prefix is a more robust approach.


mediaBucket := media_transport.NewAssetsBucket(
oss,
config.PluginMediaCachePath,
joinPrefix(config.PluginMediaCachePath),
config.PluginMediaCacheSize,
)

installedBucket := media_transport.NewInstalledBucket(
oss,
config.PluginInstalledPath,
joinPrefix(config.PluginInstalledPath),
)

packageBucket := media_transport.NewPackageBucket(
oss,
config.PluginPackageCachePath,
joinPrefix(config.PluginPackageCachePath),
)

pluginAssetCache, err := lru.New[string, []byte](int(config.PluginAssetCacheSize))
Expand Down
3 changes: 3 additions & 0 deletions internal/types/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ type Config struct {
DBReadTimeout time.Duration `envconfig:"DB_READ_TIMEOUT" default:"30s"`
DBWriteTimeout time.Duration `envconfig:"DB_WRITE_TIMEOUT" default:"30s"`

// global storage path prefix
StoragePathPrefix string `envconfig:"STORAGE_PATH_PREFIX"`

// persistence storage
PersistenceStoragePath string `envconfig:"PERSISTENCE_STORAGE_PATH"`
PersistenceStorageMaxSize int64 `envconfig:"PERSISTENCE_STORAGE_MAX_SIZE"`
Expand Down
Loading