mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
fix: fresh temp file for zips, reject year 10000 (#682)
A planted <archive>.tmp set the archive's mode or redirected the write through a symlink. CoreData let 10000-01-01 through, which time.Time.MarshalJSON rejects, so a JSON export failed.
This commit is contained in:
1 parent
c86f54a010
commit
9ac25d2902
4 files changed
+59
-5
No files matched your search
@@ -7,7 +7,7 @@ const (
|
||||
// chromiumEpochOffsetMicros is Chromium's base::Time::kTimeTToMicrosecondsOffset, from 1601-01-01 to the Unix epoch.
|
||||
chromiumEpochOffsetMicros = 11644473600000000
|
||||
coreDataEpochOffset = 978307200
|
||||
// maxCoreDataSeconds keeps CoreData inside int64 and below year 9999 before the float conversion.
|
||||
// maxCoreDataSeconds is 10000-01-01 in CoreData time; every earlier value fits int64 and time.Time.MarshalJSON.
|
||||
maxCoreDataSeconds = 252423993600
|
||||
)
|
||||
|
||||
@@ -42,7 +42,7 @@ func Unix(seconds int64) time.Time {
|
||||
|
||||
// CoreData converts CFAbsoluteTime, seconds since 2001-01-01 UTC.
|
||||
func CoreData(seconds float64) time.Time {
|
||||
if seconds <= 0 || seconds > maxCoreDataSeconds {
|
||||
if seconds <= 0 || seconds >= maxCoreDataSeconds {
|
||||
return time.Time{}
|
||||
}
|
||||
whole := int64(seconds)
|
||||
|
||||
@@ -133,6 +133,13 @@ func TestCoreData_NegativeReturnsZeroTime(t *testing.T) {
|
||||
assert.True(t, CoreData(-1).IsZero())
|
||||
}
|
||||
|
||||
func TestCoreData_OutOfJSONRangeReturnsZero(t *testing.T) {
|
||||
assert.True(t, CoreData(maxCoreDataSeconds).IsZero(), "10000-01-01 is past what time.Time.MarshalJSON accepts")
|
||||
encoded, err := CoreData(maxCoreDataSeconds - 1).MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
assert.JSONEq(t, `"9999-12-31T23:59:59Z"`, string(encoded))
|
||||
}
|
||||
|
||||
func TestCoreData_FractionalSecondsPreserved(t *testing.T) {
|
||||
got := CoreData(float64(anchorCoreDataSeconds) + 0.5)
|
||||
assert.Equal(t, 500*int64(time.Millisecond), int64(got.Nanosecond()))
|
||||
|
||||
@@ -13,7 +13,8 @@ import (
|
||||
// Write renames a finished temp file into place, so archivePath is never a partial zip and a failure keeps the old one.
|
||||
// Names are slash-separated paths relative to root, and each becomes the entry of that name.
|
||||
func Write(archivePath, root string, names []string) (err error) {
|
||||
temporary, err := os.OpenFile(archivePath+".tmp", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
||||
// CreateTemp makes a new 0600 file under an unpredictable name, so a planted file or symlink is never written through.
|
||||
temporary, err := os.CreateTemp(filepath.Dir(archivePath), filepath.Base(archivePath)+".*.tmp")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create %s: %w", archivePath, err)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestWrite_ZipsOnlyTheNamedFiles(t *testing.T) {
|
||||
|
||||
require.NoError(t, Write(archive, root, []string{"chrome/Default/History"}))
|
||||
assert.Equal(t, map[string]string{"chrome/Default/History": "history"}, entries(t, archive))
|
||||
assert.NoFileExists(t, archive+".tmp")
|
||||
assert.Equal(t, []string{"out.zip"}, filesIn(t, filepath.Dir(archive)))
|
||||
}
|
||||
|
||||
func TestWrite_FailureKeepsAnExistingArchive(t *testing.T) {
|
||||
@@ -32,7 +32,7 @@ func TestWrite_FailureKeepsAnExistingArchive(t *testing.T) {
|
||||
got, err := os.ReadFile(archive)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "precious", string(got))
|
||||
assert.NoFileExists(t, archive+".tmp", "no temp file may survive a failed zip")
|
||||
assert.Equal(t, []string{"out.zip"}, filesIn(t, out), "no temp file may survive a failed zip")
|
||||
}
|
||||
|
||||
// TestWrite_IsOwnerOnly keeps an archive as private as the browser files and records it holds.
|
||||
@@ -47,6 +47,52 @@ func TestWrite_IsOwnerOnly(t *testing.T) {
|
||||
assert.Equal(t, os.FileMode(0o600), info.Mode().Perm())
|
||||
}
|
||||
|
||||
func TestWrite_LeavesAPlantedTempFileAlone(t *testing.T) {
|
||||
out := t.TempDir()
|
||||
archive := filepath.Join(out, "out.zip")
|
||||
writeFile(t, archive+".tmp", "planted")
|
||||
require.NoError(t, os.Chmod(archive+".tmp", 0o644))
|
||||
|
||||
require.NoError(t, Write(archive, t.TempDir(), nil))
|
||||
planted, err := os.ReadFile(archive + ".tmp")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "planted", string(planted))
|
||||
assert.Equal(t, []string{"out.zip", "out.zip.tmp"}, filesIn(t, out))
|
||||
if runtime.GOOS != "windows" {
|
||||
info, err := os.Stat(archive)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, os.FileMode(0o600), info.Mode().Perm(), "a planted 0644 file must not set the archive's mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrite_DoesNotFollowAPlantedSymlink(t *testing.T) {
|
||||
archive := filepath.Join(t.TempDir(), "out.zip")
|
||||
victim := filepath.Join(t.TempDir(), "victim")
|
||||
writeFile(t, victim, "precious")
|
||||
if err := os.Symlink(victim, archive+".tmp"); err != nil {
|
||||
t.Skipf("cannot create a symlink here: %v", err)
|
||||
}
|
||||
|
||||
require.NoError(t, Write(archive, t.TempDir(), nil))
|
||||
got, err := os.ReadFile(victim)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "precious", string(got))
|
||||
info, err := os.Lstat(archive)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, info.Mode().IsRegular(), "the archive must be a file, not the planted symlink")
|
||||
}
|
||||
|
||||
func filesIn(t *testing.T, dir string) []string {
|
||||
t.Helper()
|
||||
dirEntries, err := os.ReadDir(dir)
|
||||
require.NoError(t, err)
|
||||
names := make([]string, 0, len(dirEntries))
|
||||
for _, entry := range dirEntries {
|
||||
names = append(names, entry.Name())
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func entries(t *testing.T, archive string) map[string]string {
|
||||
t.Helper()
|
||||
zipReader, err := zip.OpenReader(archive)
|
||||
|
||||
Reference in new issue
Block a user