fix(dump): only allow imports from the same version they were dumped on

Previously, Vikunja would allow imports from any version which then caused problems since the table structure might have changed between releases. This change now checks if the current version is the same as the one the dump was created on.
This commit is contained in:
kolaente 2024-02-13 21:25:31 +01:00
parent 77a779acea
commit 2facbae0d7
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 40 additions and 0 deletions

View File

@ -20,10 +20,12 @@ import (
"archive/zip"
"bufio"
"bytes"
vversion "code.vikunja.io/api/pkg/version"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/hashicorp/go-version"
"io"
"os"
"sort"
@ -63,6 +65,7 @@ func Restore(filename string) error {
// Find the configFile, database and files files
var configFile *zip.File
var dotEnvFile *zip.File
var versionFile *zip.File
dbfiles := make(map[string]*zip.File)
filesFiles := make(map[string]*zip.File)
for _, file := range r.File {
@ -81,6 +84,43 @@ func Restore(filename string) error {
}
if strings.HasPrefix(file.Name, "files/") {
filesFiles[strings.ReplaceAll(file.Name, "files/", "")] = file
continue
}
if file.Name == "VERSION" {
versionFile = file
}
}
///////
// Check if we're restoring to the same version as the dump
if versionFile == nil {
return fmt.Errorf("dump does not contain VERSION file, refusing to continue")
}
vf, err := versionFile.Open()
if err != nil {
return fmt.Errorf("could not open version file: %w", err)
}
var bufVersion bytes.Buffer
if _, err := bufVersion.ReadFrom(vf); err != nil {
return fmt.Errorf("could not read version file: %w", err)
}
versionString := bufVersion.String()
if versionString == "dev" && vversion.Version == "dev" {
log.Debugf("Importing from dev version")
} else {
dumpedVersion, err := version.NewVersion(bufVersion.String())
if err != nil {
return err
}
currentVersion, err := version.NewVersion(vversion.Version)
if err != nil {
return err
}
if !dumpedVersion.Equal(currentVersion) {
return fmt.Errorf("export was created with version %s but this is %s - please make sure you are running the same Vikunja version before restoring", dumpedVersion, currentVersion)
}
}