121 lines
3.5 KiB
Go
121 lines
3.5 KiB
Go
package isolation
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func (a Agent) verifyProjectBackup(w http.ResponseWriter, r *http.Request) {
|
|
var backup ProjectBackup
|
|
decoder := json.NewDecoder(r.Body)
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(&backup) != nil || decoder.Decode(&struct{}{}) != io.EOF || !safeName.MatchString(backup.HostName) || backup.TenantID == 0 || backup.DatabaseCount < 0 || backup.DatabaseCount > 1 {
|
|
http.Error(w, "invalid backup verification", 400)
|
|
return
|
|
}
|
|
directory, err := a.backupDirectory(backup.TenantID)
|
|
path := filepath.Clean(backup.ArchivePath)
|
|
if err != nil || filepath.Dir(path) != directory || !strings.HasPrefix(filepath.Base(path), backup.HostName+"-") || !strings.HasSuffix(path, ".tar.gz") {
|
|
http.Error(w, "unsafe backup path", 400)
|
|
return
|
|
}
|
|
if err = verifyRecoveryArchive(r.Context(), path, backup.HostName, backup.DatabaseCount); err != nil {
|
|
http.Error(w, "recovery archive is missing, incomplete or corrupt", 412)
|
|
return
|
|
}
|
|
w.WriteHeader(200)
|
|
}
|
|
|
|
func verifyRecoveryArchive(ctx context.Context, path, hostName string, count int) error {
|
|
info, err := os.Lstat(path)
|
|
if err != nil || !info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 {
|
|
return errors.New("unsafe recovery archive")
|
|
}
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
compressed, err := gzip.NewReader(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer compressed.Close()
|
|
reader := tar.NewReader(compressed)
|
|
type database struct {
|
|
Name, Path, SHA256 string
|
|
Bytes int64
|
|
}
|
|
var manifest struct {
|
|
Format int
|
|
HostName string
|
|
Databases []database
|
|
}
|
|
hashes := map[string]string{}
|
|
sizes := map[string]int64{}
|
|
hasManifest := false
|
|
for {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
header, nextErr := reader.Next()
|
|
if nextErr == io.EOF {
|
|
break
|
|
}
|
|
if nextErr != nil {
|
|
return nextErr
|
|
}
|
|
if header.Name == ".sea-vhost-backup/manifest.json" {
|
|
if hasManifest || header.Size > 64<<10 {
|
|
return errors.New("invalid recovery manifest")
|
|
}
|
|
data, readErr := io.ReadAll(reader)
|
|
if readErr != nil {
|
|
return readErr
|
|
}
|
|
if json.Unmarshal(data, &manifest) != nil {
|
|
return errors.New("invalid recovery manifest")
|
|
}
|
|
hasManifest = true
|
|
} else if strings.HasPrefix(header.Name, ".sea-vhost-backup/databases/") {
|
|
if header.Name != ".sea-vhost-backup/databases/"+hostName+".sql" || header.Typeflag != tar.TypeReg || hashes[header.Name] != "" {
|
|
return errors.New("invalid database archive entry")
|
|
}
|
|
digest := sha256.New()
|
|
size, copyErr := io.Copy(digest, reader)
|
|
if copyErr != nil {
|
|
return copyErr
|
|
}
|
|
hashes[header.Name] = fmt.Sprintf("%x", digest.Sum(nil))
|
|
sizes[header.Name] = size
|
|
} else {
|
|
if _, err = io.Copy(io.Discard, reader); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
// Read the gzip trailer as well; tar's end marker alone does not validate CRC.
|
|
if _, err = io.Copy(io.Discard, compressed); err != nil {
|
|
return err
|
|
}
|
|
if !hasManifest || manifest.Format != 2 || manifest.HostName != hostName || len(manifest.Databases) != count || len(hashes) != count {
|
|
return errors.New("incomplete recovery archive")
|
|
}
|
|
for _, db := range manifest.Databases {
|
|
if db.Name != hostName || db.SHA256 != hashes[db.Path] || db.Bytes != sizes[db.Path] || db.Bytes == 0 {
|
|
return errors.New("database recovery checksum mismatch")
|
|
}
|
|
}
|
|
return nil
|
|
}
|