Skip to content

Commit

Permalink
Safer zip upload
Browse files Browse the repository at this point in the history
This change uses the same mechanism that omegaup-update-problem uses
when creating an initial repository: First do it on a temporary
directory and then just before returning success, rename it to its final
destination. That way we prevent having dangling directories in the
filesystem.

Fixes: #1
  • Loading branch information
lhchavez committed Feb 4, 2019
1 parent b86b6e1 commit 8bc07e1
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ziphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,13 +1469,31 @@ func (h *zipUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

var repo *git.Repository
commitCallback := func() error { return nil }
if create {
repo, err = InitRepository(repositoryPath)
dir, err := ioutil.TempDir(filepath.Dir(repositoryPath), "repository")
if err != nil {
h.log.Error("Failed to create temporary directory", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer os.RemoveAll(dir)

if err := os.Chmod(dir, 0755); err != nil {
h.log.Error("Failed to chmod temporary directory", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

repo, err = InitRepository(dir)
if err != nil {
h.log.Error("failed to init repository", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
commitCallback = func() error {
return os.Rename(dir, repositoryPath)
}
} else {
repo, err = git.OpenRepository(repositoryPath)
if err != nil {
Expand Down Expand Up @@ -1520,6 +1538,11 @@ func (h *zipUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Error: cause.Error(),
}
} else {
if err := commitCallback(); err != nil {
h.log.Info("push successful, but commit failed", "path", repositoryPath, "result", updateResult, "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.log.Info("push successful", "path", repositoryPath, "result", updateResult)
w.WriteHeader(http.StatusOK)
}
Expand Down

0 comments on commit 8bc07e1

Please sign in to comment.