Skip to content

Commit

Permalink
fix(unarchive): add 0o700 permission to directories
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 5, 2025
1 parent 6e3bc79 commit 12907f5
Showing 1 changed file with 1 addition and 34 deletions.
35 changes: 1 addition & 34 deletions pkg/unarchive/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ type handler struct {
logE *logrus.Entry
}

const readOnlyPerm = 0o200

func allowWrite(fs afero.Fs, path string) (func() error, error) {
originalMode, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("stat a parent directory: %w", err)
}

if originalMode.Mode().Perm()&readOnlyPerm != 0 {
return nil, nil //nolint:nilnil
}

if err := os.Chmod(path, originalMode.Mode()|readOnlyPerm); err != nil {
return nil, fmt.Errorf("chmod parent directory: %w", err)
}
return func() error {
return fs.Chmod(path, originalMode.Mode())
}, nil
}

func (h *handler) normalizePath(nameInArchive string) string {
slashCount := strings.Count(nameInArchive, "/")
backSlashCount := strings.Count(nameInArchive, "\\")
Expand All @@ -60,7 +40,7 @@ func (h *handler) HandleFile(_ context.Context, f archives.FileInfo) error {
}

if f.IsDir() {
if err := h.fs.MkdirAll(dstPath, f.Mode()); err != nil {
if err := h.fs.MkdirAll(dstPath, f.Mode()|0o700); err != nil {

Check failure on line 43 in pkg/unarchive/archives.go

View workflow job for this annotation

GitHub Actions / test / test

Magic number: 0o700, in <argument> detected (mnd)
logerr.WithError(h.logE, err).Warn("create a directory")
return nil
}
Expand All @@ -71,19 +51,6 @@ func (h *handler) HandleFile(_ context.Context, f archives.FileInfo) error {
// return nil
// }

fn, err := allowWrite(h.fs, parentDir)
if err != nil {
logerr.WithError(h.logE, err).Warn("allow write permission temporarily")
return nil
}
if fn != nil {
defer func() {
if err := fn(); err != nil {
logerr.WithError(h.logE, err).Warn("failed to restore the original permission")
}
}()
}

reader, err := f.Open()
if err != nil {
logerr.WithError(h.logE, err).Warn("open a file")
Expand Down

0 comments on commit 12907f5

Please sign in to comment.