Files
team/server/channels/app/extract_plugin_tar.go
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

92 lines
2.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/hanzoteam/server/server/public/shared/mlog"
)
// extractTarGz takes in an io.Reader containing the bytes for a .tar.gz file and
// a destination string to extract to.
func extractTarGz(gzipStream io.Reader, dst string) error {
if dst == "" {
return errors.New("no destination path provided")
}
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return errors.Wrap(err, "failed to initialize gzip reader")
}
defer uncompressedStream.Close()
tarReader := tar.NewReader(uncompressedStream)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return errors.Wrap(err, "failed to read next file from archive")
}
// Preemptively check type flag to avoid reporting a misleading error in
// trying to sanitize the header name.
switch header.Typeflag {
case tar.TypeDir:
case tar.TypeReg:
default:
mlog.Warn("skipping unsupported header type on extracting tar file", mlog.String("header_type", string(header.Typeflag)), mlog.String("header_name", header.Name))
continue
}
// filepath.HasPrefix is deprecated, so we just use strings.HasPrefix to ensure
// the target path remains rooted at dst and has no `../` escaping outside.
path := filepath.Join(dst, header.Name)
if !strings.HasPrefix(path, dst) {
return errors.Errorf("failed to sanitize path %s", header.Name)
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(path, 0744); err != nil && !os.IsExist(err) {
return err
}
case tar.TypeReg:
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0744); err != nil {
return err
}
copyFile := func() error {
outFile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode))
if err != nil {
return err
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return err
}
return nil
}
if err := copyFile(); err != nil {
return err
}
}
}
return nil
}