Skip to content

Commit

Permalink
Handle crypt for zip files (#606)
Browse files Browse the repository at this point in the history
Co-authored-by: 杨赫然 <[email protected]>
  • Loading branch information
feiniks and 杨赫然 authored Apr 4, 2023
1 parent ad5ce70 commit db09bae
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions fileserver/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,15 @@ func downloadZipFile(rsp http.ResponseWriter, r *http.Request, data, repoID, use
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{})
err := json.Unmarshal([]byte(data), &obj)
if err != nil {
Expand Down Expand Up @@ -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-Type", "application/octet-stream")

err := packDir(ar, repo, objID, dirName)
err := packDir(ar, repo, objID, dirName, cryptKey)
if err != nil {
log.Printf("failed to pack dir %s: %v", dirName, err)
return nil
Expand All @@ -741,14 +750,14 @@ func downloadZipFile(rsp http.ResponseWriter, r *http.Request, data, repoID, use

for _, v := range dirList {
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) {
log.Printf("failed to pack dir %s: %v", v.Name, err)
}
return nil
}
} else {
if err := packFiles(ar, &v, repo, ""); err != nil {
if err := packFiles(ar, &v, repo, "", cryptKey); err != nil {
if !isNetworkErr(err) {
log.Printf("failed to pack file %s: %v", v.Name, err)
}
Expand Down Expand Up @@ -806,7 +815,7 @@ func parseDirFilelist(repo *repomgr.Repo, obj map[string]interface{}) ([]fsmgr.S
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)
if err != nil {
err := fmt.Errorf("failed to get dir for zip: %v", err)
Expand All @@ -831,11 +840,11 @@ func packDir(ar *zip.Writer, repo *repomgr.Repo, dirID, dirPath string) error {
fileDir := filepath.Join(dirPath, v.Name)
fileDir = strings.TrimLeft(fileDir, "/")
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
}
} else {
if err := packFiles(ar, v, repo, dirPath); err != nil {
if err := packFiles(ar, v, repo, dirPath, cryptKey); err != nil {
return err
}
}
Expand All @@ -844,7 +853,7 @@ func packDir(ar *zip.Writer, repo *repomgr.Repo, dirID, dirPath string) error {
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)
if err != nil {
err := fmt.Errorf("failed to get seafile : %v", err)
Expand All @@ -864,6 +873,23 @@ func packFiles(ar *zip.Writer, dirent *fsmgr.SeafDirent, repo *repomgr.Repo, par
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 {
err := blockmgr.Read(repo.StoreID, blkID, zipFile)
if err != nil {
Expand Down

0 comments on commit db09bae

Please sign in to comment.