Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/web/backend/settings/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (s *Settings) HandleSaveConfig(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := validateEnvText(string(data)); err != nil {
http.Error(w, "invalid .env content: "+err.Error(), http.StatusBadRequest)
return
}
if err := os.WriteFile(s.cfg.WebEnvPath, data, 0600); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
25 changes: 22 additions & 3 deletions src/web/backend/settings/settings.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package settings

import (
"strings"
"os"
"fmt"
"os"
"strings"

"explo/src/web/backend/app"
)
Expand All @@ -21,6 +21,7 @@ type Settings struct {
func NewSettings(Config app.Config) *Settings {
return &Settings{cfg: Config}
}

// parseEnvText parses key=value lines, ignoring comments, blanks and unquotes variables
func (s *Settings) ParseEnvText(text string) map[string]string {
out := map[string]string{}
Expand Down Expand Up @@ -49,6 +50,24 @@ func (s *Settings) ParseEnvText(text string) map[string]string {
return out
}

// validateEnvText rejects content that isn't valid .env syntax before it's written to
// disk. cleanenv.ReadConfig fails hard on any non-blank, non-comment line without a "=",
// which causes a crash loop for the container with no way tofix since the UI becomes
// inaccessible.Mirrors ParseEnvText's line rules,but treats a missing "=" as an error
// instead of silently skipping the line.
func validateEnvText(text string) error {
for i, line := range strings.Split(text, "\n") {
t := strings.TrimSpace(line)
if t == "" || strings.HasPrefix(t, "#") {
continue
}
if !strings.Contains(t, "=") {
return fmt.Errorf("line %d is not valid .env syntax (expected KEY=value): %q", i+1, t)
}
}
return nil
}

// updateEnvKeys reads the env file (falling back to fallback if missing), updates the
// given key=value pairs in-place preserving comments, and writes the result back.
func (s *Settings) UpdateEnvKeys(updates map[string]string, fallback []byte) error {
Expand Down Expand Up @@ -119,4 +138,4 @@ func formatEnvValue(v string) string {
}

return v
}
}
Loading