Static analysis (SAST) check · Go

Zip slip (Go)

An archive entry's name is joined to the destination directory without checking where the result points.

Why it matters

An entry named ../../x is written outside the destination.

How to fix it

Reject the entry unless filepath.IsLocal(name) is true, or check that the joined path still has the destination as its prefix.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Go)
package fixture

import (
	"archive/zip"
	"os"
	"path/filepath"
)

func unpack(reader *zip.Reader, dest string) error {
	for _, entry := range reader.File {
		target := filepath.Join(dest, entry.Name)
		if err := os.MkdirAll(filepath.Dir(target), 0o750); err != nil {
			return err
		}
	}
	return nil
}
Fixed: not reported (Go)
package fixture

import (
	"archive/zip"
	"errors"
	"os"
	"path/filepath"
)

func unpack(reader *zip.Reader, dest string) error {
	for _, entry := range reader.File {
		if !filepath.IsLocal(entry.Name) {
			return errors.New("archive entry escapes the destination")
		}
		target := filepath.Join(dest, entry.Name)
		if err := os.MkdirAll(filepath.Dir(target), 0o750); err != nil {
			return err
		}
	}
	return nil
}

What TrustIvan reads

Runs on repository scans of Go source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.