anomalyco/sst · error

illegal file path in tar: %s

Error message

illegal file path in tar: %s

What it means

extractTarGz guards against path traversal ("tar slip") by checking that each entry's joined target path stays inside destDir after cleaning. If hdr.Name escapes the destination (e.g. starts with ../ or is an absolute path), extraction aborts with "illegal file path in tar". This is an intentional security check, not an environment problem.

Source

Thrown at pkg/runtime/python/build.go:424

	if err != nil {
		return fmt.Errorf("failed to create gzip reader: %w", err)
	}
	defer gz.Close()

	tr := tar.NewReader(gz)
	for {
		hdr, err := tr.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return fmt.Errorf("failed to read tar entry: %w", err)
		}

		target := filepath.Join(destDir, hdr.Name)
		// Guard against tar slip
		if !strings.HasPrefix(filepath.Clean(target), filepath.Clean(destDir)+string(os.PathSeparator)) {
			return fmt.Errorf("illegal file path in tar: %s", hdr.Name)
		}

		switch hdr.Typeflag {
		case tar.TypeDir:
			if err := os.MkdirAll(target, 0755); err != nil {
				return err
			}
		case tar.TypeReg:
			if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
				return err
			}
			out, err := os.OpenFile(target, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(hdr.Mode))
			if err != nil {
				return err
			}
			if _, err := io.Copy(out, tr); err != nil {
				out.Close()
				return err

View on GitHub (pinned to a0bd20f762)

Solutions

  1. Do not extract the archive — treat it as untrusted and remove it
  2. Only install packages from trusted sources/verified hashes (pip --require-hashes)
  3. Inspect the archive contents (tar -tzvf) to find the offending entry name
  4. Rebuild/repackage the archive with relative, normalized entry paths if you own it
  5. Pin known-good package versions so compromised uploads are not picked up

Example fix

// before: blindly extracting an untrusted archive
extractTarGz(archiveFile, destDir)

// after: pre-screen entries for traversal attempts
bad := exec.Command("sh", "-c", fmt.Sprintf("tar -tzf %s | grep -E '(^/|\.\./)'", archiveFile))
if err := bad.Run(); err == nil {
    return fmt.Errorf("archive %s contains path traversal entries", archiveFile)
}
extractTarGz(archiveFile, destDir)
Defensive patterns

Strategy: validation

Validate before calling

out, err := exec.Command("tar", "-tzf", archiveFile).Output()
if err != nil {
    return err
}
for _, name := range strings.Split(strings.TrimSpace(string(out)), "\n") {
    cleaned := filepath.Clean(name)
    if strings.HasPrefix(cleaned, "../") || filepath.IsAbs(cleaned) {
        return fmt.Errorf("unsafe entry %q in archive", name)
    }
}

Try / catch

if err := extractTarGz(archiveFile, destDir); err != nil {
    if strings.Contains(err.Error(), "illegal file path in tar") {
        // treat archive as malicious: quarantine it, never retry blindly
        os.Remove(archiveFile)
        return fmt.Errorf("refusing to install untrusted archive: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: The archive contains entry names like "../../../etc/passwd", absolute paths ("/home/user/...") or symlink tricks whose cleaned path does not have destDir as prefix.

Common situations: A maliciously crafted or typo'd package on an internal index; an archive built on another OS with absolute paths embedded; archives generated with a tool that emits leading "./../" segments; running unvetted sdists in CI.

Related errors


AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30). Data as JSON: /api/errors/5983c8801e0b1ed6. Report an issue: GitHub.