xtekky/gpt4free · critical

unsafe path in archive: %s

Error message

unsafe path in archive: %s

What it means

extractRuntime's tar walker in g4f-go/download.go strips the top-level directory and leading '/'/'./' prefixes, then filepath.Clean's each entry name. This error fires when a cleaned relative name is exactly '..' or starts with '../' — a path traversal (zip-slip style) attempt that would write outside the extraction destination, so extraction is aborted.

Source

Thrown at g4f-go/download.go:312

		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		rel := hdr.Name
		if top != "" {
			rel = strings.TrimPrefix(rel, top+"/")
			rel = strings.TrimPrefix(rel, top)
		}
		rel = strings.TrimPrefix(rel, "/")
		rel = strings.TrimPrefix(rel, "./")
		name := filepath.Clean(rel)
		if name == "." || name == "" {
			continue
		}
		if name == ".." || strings.HasPrefix(name, ".."+string(os.PathSeparator)) {
			return fmt.Errorf("unsafe path in archive: %s", hdr.Name)
		}
		target := filepath.Join(dest, name)
		if !strings.HasPrefix(target, filepath.Clean(dest)+string(os.PathSeparator)) {
			return fmt.Errorf("unsafe path in archive: %s", hdr.Name)
		}

		switch hdr.Typeflag {
		case tar.TypeDir:
			if err := os.MkdirAll(target, 0o755); err != nil {
				return err
			}
		case tar.TypeReg, tar.TypeRegA:
			if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
				return err
			}
			out, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(hdr.Mode)&0o777)
			if err != nil {
				return err

View on GitHub (pinned to 973504e177)

Solutions

  1. Do not extract this archive — delete the cached file and re-download from the official URL
  2. Inspect the archive: tar -tf <cachePath> | grep -E '(^|/)\.\./' to list offending entries
  3. Verify the manifest's sha256 pin is present and correct so tampered artifacts are caught before extraction
  4. Report the artifact if the official source itself ships traversal paths

Example fix

# before
# unsafe path in archive: top/../../evil.sh

# after
tar -tf .g4f-runtime/runtime-*.tar.gz | grep '\.\.'   # identify bad entries
rm .g4f-runtime/runtime-*.tar.gz                        # discard
curl -L -o .g4f-runtime/runtime-*.tar.gz "<official-url>"  # re-fetch from trusted source
Defensive patterns

Strategy: validation

Validate before calling

// scan a tar for traversal entries before extracting
func tarIsSafe(path string) (bool, error) {
    f, err := os.Open(path)
    if err != nil { return false, err }
    defer f.Close()
    tr := tar.NewReader(f)
    for {
        hdr, err := tr.Next()
        if err == io.EOF { return true, nil }
        if err != nil { return false, err }
        name := filepath.Clean(hdr.Name)
        if name == ".." || strings.HasPrefix(name, "..") {
            return false, nil
        }
    }
}

Try / catch

if err := extractRuntime(binDir, cachePath); err != nil {
    if strings.Contains(err.Error(), "unsafe path in archive") {
        // quarantine the archive, alert: possible supply-chain tampering
    }
    return err
}

Prevention

When it happens

Trigger: A runtime tarball containing entries like '../../etc/passwd' or 'top/../../../bin/sh' after top-dir stripping. In practice: a maliciously crafted or badly assembled archive, or an archive whose genuine layout includes parent references.

Common situations: Downloading runtimes from untrusted/mirrored URLs; supply-chain tampering (this guard is exactly what catches it); malformed archives produced by unusual build tooling.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/f846c8e0e667f33e. Report an issue: GitHub.