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
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
20 changes: 0 additions & 20 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,6 @@ func MACAddress(uniqueID string) string {
return hw.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)
}
}
return ""
}

func defaultCPUs() int {
const x = 4
if hostCPUs := runtime.NumCPU(); hostCPUs < x {
Expand Down
58 changes: 58 additions & 0 deletions pkg/limayaml/defaults_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//go:build !windows

// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0

package limayaml

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/sirupsen/logrus"
)

func hostTimeZone() string {
if tzBytes, err := os.ReadFile("/etc/timezone"); err == nil {
if tz := strings.TrimSpace(string(tzBytes)); tz != "" {
if _, err := time.LoadLocation(tz); err != nil {
logrus.Warnf("invalid timezone found in /etc/timezone: %v", err)
} else {
return tz
}
}
}

if zoneinfoFile, err := filepath.EvalSymlinks("/etc/localtime"); err == nil {
if tz, err := extractTZFromPath(zoneinfoFile); err != nil {
logrus.Warnf("failed to extract timezone from %s: %v", zoneinfoFile, err)
} else {
return tz
}
}

logrus.Warn("unable to determine host timezone, falling back to default value")
return ""
}

func extractTZFromPath(zoneinfoFile string) (string, error) {
if zoneinfoFile == "" {
return "", errors.New("invalid zoneinfo file path")
}

if _, err := os.Stat(zoneinfoFile); os.IsNotExist(err) {
return "", fmt.Errorf("zoneinfo file does not exist: %s", zoneinfoFile)
}

for dir := filepath.Dir(zoneinfoFile); dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
if _, err := os.Stat(filepath.Join(dir, "Etc", "UTC")); err == nil {
return filepath.Rel(dir, zoneinfoFile)
}
}

return "", errors.New("timezone base directory not found")
}
75 changes: 75 additions & 0 deletions pkg/limayaml/defaults_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//go:build !windows

// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0

package limayaml

import (
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

func TestExtractTimezoneFromPath(t *testing.T) {
tmpDir := t.TempDir()

// Create test timezone directory structure
assert.NilError(t, os.MkdirAll(filepath.Join(tmpDir, "Etc"), 0o755))
assert.NilError(t, os.WriteFile(filepath.Join(tmpDir, "Etc", "UTC"), []byte{}, 0o644))
assert.NilError(t, os.WriteFile(filepath.Join(tmpDir, "UTC"), []byte{}, 0o644))
assert.NilError(t, os.MkdirAll(filepath.Join(tmpDir, "Antarctica"), 0o755))
assert.NilError(t, os.WriteFile(filepath.Join(tmpDir, "Antarctica", "Troll"), []byte{}, 0o644))

tests := []struct {
name string
path string
want string
wantErr bool
}{
{
"valid_timezone",
filepath.Join(tmpDir, "Antarctica", "Troll"),
"Antarctica/Troll",
false,
},
{
"root_level_zone",
filepath.Join(tmpDir, "UTC"),
"UTC",
false,
},
{
"outside_zoneinfo",
"/tmp/somefile",
"",
true,
},
{
"empty_path",
"",
"",
true,
},
{
"nonexistent_file",
filepath.Join(tmpDir, "Invalid", "Zone"),
"",
true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractTZFromPath(tt.path)
if tt.wantErr {
assert.Assert(t, err != nil, "expected error but got none")
} else {
assert.NilError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}
11 changes: 11 additions & 0 deletions pkg/limayaml/defaults_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build windows

// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0

package limayaml

func hostTimeZone() string {
// WSL2 will automatically set the timezone
return ""
}
Loading