Skip to content

Add timezone validation #3679

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

Merged
merged 2 commits into from
Jul 4, 2025
Merged
Changes from 1 commit
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
52 changes: 39 additions & 13 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"
"sync"
"text/template"
"time"

"github.com/coreos/go-semver/semver"
"github.com/docker/go-units"
Expand Down Expand Up @@ -137,21 +138,22 @@ func MACAddress(uniqueID string) string {

func hostTimeZone() string {
// WSL2 will automatically set the timezone
if runtime.GOOS != "windows" {
tz, err := os.ReadFile("/etc/timezone")
if err == nil {
return strings.TrimSpace(string(tz))
}
zoneinfoFile, err := filepath.EvalSymlinks("/etc/localtime")
if err == nil {
for baseDir := filepath.Dir(zoneinfoFile); baseDir != "/"; baseDir = filepath.Dir(baseDir) {
if _, err = os.Stat(filepath.Join(baseDir, "Etc/UTC")); err == nil {
return strings.TrimPrefix(zoneinfoFile, baseDir+"/")
}
}
logrus.Warnf("could not locate zoneinfo directory from %q", zoneinfoFile)
if runtime.GOOS == "windows" {
return ""
}

if tzBytes, err := os.ReadFile("/etc/timezone"); err == nil {
if tz := strings.TrimSpace(string(tzBytes)); isValidTimezone(tz) {
return tz
}
}

if zoneinfoFile, err := filepath.EvalSymlinks("/etc/localtime"); err == nil {
if tz := extractTimezoneFromPath(zoneinfoFile); isValidTimezone(tz) {
return tz
}
}

return ""
}

Expand Down Expand Up @@ -1309,3 +1311,27 @@ func unique(s []string) []string {
}
return list
}

func isValidTimezone(tz string) bool {
if tz == "" {
return false
}
_, err := time.LoadLocation(tz)
if err != nil {
if len(tz) > 30 {
tz = tz[:30] + "..."
}
logrus.Warnf("invalid timezone %q", tz)
return false
}
return true
}

func extractTimezoneFromPath(zoneinfoFile string) string {
for baseDir := filepath.Dir(zoneinfoFile); baseDir != "/"; baseDir = filepath.Dir(baseDir) {
if _, err := os.Stat(filepath.Join(baseDir, "Etc/UTC")); err == nil {
return strings.TrimPrefix(zoneinfoFile, baseDir+string(os.PathSeparator))
}
}
return ""
}
Loading