1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-08-12 04:03:12 +00:00

Handle crypt for zip files (#606)

Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
feiniks 2023-04-04 14:07:28 +08:00 committed by GitHub
parent ad5ce70ffb
commit db09baec1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -688,6 +688,15 @@ func downloadZipFile(rsp http.ResponseWriter, r *http.Request, data, repoID, use
return &appError{nil, msg, http.StatusBadRequest} return &appError{nil, msg, http.StatusBadRequest}
} }
var cryptKey *seafileCrypt
if repo.IsEncrypted {
key, err := parseCryptKey(rsp, repoID, user, repo.EncVersion)
if err != nil {
return err
}
cryptKey = key
}
obj := make(map[string]interface{}) obj := make(map[string]interface{})
err := json.Unmarshal([]byte(data), &obj) err := json.Unmarshal([]byte(data), &obj)
if err != nil { if err != nil {
@ -720,7 +729,7 @@ func downloadZipFile(rsp http.ResponseWriter, r *http.Request, data, repoID, use
rsp.Header().Set("Content-Disposition", contFileName) rsp.Header().Set("Content-Disposition", contFileName)
rsp.Header().Set("Content-Type", "application/octet-stream") rsp.Header().Set("Content-Type", "application/octet-stream")
err := packDir(ar, repo, objID, dirName) err := packDir(ar, repo, objID, dirName, cryptKey)
if err != nil { if err != nil {
log.Printf("failed to pack dir %s: %v", dirName, err) log.Printf("failed to pack dir %s: %v", dirName, err)
return nil return nil
@ -741,14 +750,14 @@ func downloadZipFile(rsp http.ResponseWriter, r *http.Request, data, repoID, use
for _, v := range dirList { for _, v := range dirList {
if fsmgr.IsDir(v.Mode) { if fsmgr.IsDir(v.Mode) {
if err := packDir(ar, repo, v.ID, v.Name); err != nil { if err := packDir(ar, repo, v.ID, v.Name, cryptKey); err != nil {
if !isNetworkErr(err) { if !isNetworkErr(err) {
log.Printf("failed to pack dir %s: %v", v.Name, err) log.Printf("failed to pack dir %s: %v", v.Name, err)
} }
return nil return nil
} }
} else { } else {
if err := packFiles(ar, &v, repo, ""); err != nil { if err := packFiles(ar, &v, repo, "", cryptKey); err != nil {
if !isNetworkErr(err) { if !isNetworkErr(err) {
log.Printf("failed to pack file %s: %v", v.Name, err) log.Printf("failed to pack file %s: %v", v.Name, err)
} }
@ -806,7 +815,7 @@ func parseDirFilelist(repo *repomgr.Repo, obj map[string]interface{}) ([]fsmgr.S
return direntList, nil return direntList, nil
} }
func packDir(ar *zip.Writer, repo *repomgr.Repo, dirID, dirPath string) error { func packDir(ar *zip.Writer, repo *repomgr.Repo, dirID, dirPath string, cryptKey *seafileCrypt) error {
dirent, err := fsmgr.GetSeafdir(repo.StoreID, dirID) dirent, err := fsmgr.GetSeafdir(repo.StoreID, dirID)
if err != nil { if err != nil {
err := fmt.Errorf("failed to get dir for zip: %v", err) err := fmt.Errorf("failed to get dir for zip: %v", err)
@ -831,11 +840,11 @@ func packDir(ar *zip.Writer, repo *repomgr.Repo, dirID, dirPath string) error {
fileDir := filepath.Join(dirPath, v.Name) fileDir := filepath.Join(dirPath, v.Name)
fileDir = strings.TrimLeft(fileDir, "/") fileDir = strings.TrimLeft(fileDir, "/")
if fsmgr.IsDir(v.Mode) { if fsmgr.IsDir(v.Mode) {
if err := packDir(ar, repo, v.ID, fileDir); err != nil { if err := packDir(ar, repo, v.ID, fileDir, cryptKey); err != nil {
return err return err
} }
} else { } else {
if err := packFiles(ar, v, repo, dirPath); err != nil { if err := packFiles(ar, v, repo, dirPath, cryptKey); err != nil {
return err return err
} }
} }
@ -844,7 +853,7 @@ func packDir(ar *zip.Writer, repo *repomgr.Repo, dirID, dirPath string) error {
return nil return nil
} }
func packFiles(ar *zip.Writer, dirent *fsmgr.SeafDirent, repo *repomgr.Repo, parentPath string) error { func packFiles(ar *zip.Writer, dirent *fsmgr.SeafDirent, repo *repomgr.Repo, parentPath string, cryptKey *seafileCrypt) error {
file, err := fsmgr.GetSeafile(repo.StoreID, dirent.ID) file, err := fsmgr.GetSeafile(repo.StoreID, dirent.ID)
if err != nil { if err != nil {
err := fmt.Errorf("failed to get seafile : %v", err) err := fmt.Errorf("failed to get seafile : %v", err)
@ -864,6 +873,23 @@ func packFiles(ar *zip.Writer, dirent *fsmgr.SeafDirent, repo *repomgr.Repo, par
return err return err
} }
if cryptKey != nil {
for _, blkID := range file.BlkIDs {
var buf bytes.Buffer
blockmgr.Read(repo.StoreID, blkID, &buf)
decoded, err := cryptKey.decrypt(buf.Bytes())
if err != nil {
err := fmt.Errorf("failed to decrypt block %s: %v", blkID, err)
return err
}
_, err = zipFile.Write(decoded)
if err != nil {
return err
}
}
return nil
}
for _, blkID := range file.BlkIDs { for _, blkID := range file.BlkIDs {
err := blockmgr.Read(repo.StoreID, blkID, zipFile) err := blockmgr.Read(repo.StoreID, blkID, zipFile)
if err != nil { if err != nil {