Skip to content

feat: enhance asset path handling to include config directory and improve assets directory validation #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/assets
/config
/build
/playground
/.idea
Expand Down
28 changes: 26 additions & 2 deletions internal/glance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,32 @@ func isConfigStateValid(config *config) error {
}

if config.Server.AssetsPath != "" {
if _, err := os.Stat(config.Server.AssetsPath); os.IsNotExist(err) {
return fmt.Errorf("assets directory does not exist: %s", config.Server.AssetsPath)
if _, err := os.Stat(config.Server.AssetsPath); err == nil {
return nil
}

workingDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("directory does not exist: %v", err)
}

// Try these paths in order:
possiblePaths := []string{
config.Server.AssetsPath, // Original path
filepath.Join(workingDir, "config"), // Local config directory
filepath.Join(workingDir, config.Server.AssetsPath), // Relative to working dir
"/app/config", // Docker path
}

for _, path := range possiblePaths {
if _, err := os.Stat(path); err == nil {
config.Server.AssetsPath = path
return nil
}
}

if !strings.HasPrefix(config.Server.AssetsPath, "/app/") {
return fmt.Errorf("assets directory not found in any of the expected locations. Tried: %v", possiblePaths)
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/glance/glance.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (p *page) updateOutdatedWidgets() {
}

func (a *application) transformUserDefinedAssetPath(path string) string {
if strings.HasPrefix(path, "/assets/") {
if strings.HasPrefix(path, "/assets/") || strings.HasPrefix(path, "/config/") {
return a.Config.Server.BaseURL + path
}

Expand Down Expand Up @@ -262,6 +262,7 @@ func (a *application) server() (func() error, func() error) {
absAssetsPath, _ = filepath.Abs(a.Config.Server.AssetsPath)
assetsFS := fileServerWithCache(http.Dir(a.Config.Server.AssetsPath), 2*time.Hour)
mux.Handle("/assets/{path...}", http.StripPrefix("/assets/", assetsFS))
mux.Handle("/config/{path...}", http.StripPrefix("/config/", assetsFS))
Copy link
Member

Choose a reason for hiding this comment

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

This is a gigantic security hole because not everyone will use environment variables for their passwords/API keys, and you're exposing their configs.

}

server := http.Server{
Expand Down