The name of an archive entry is joined to a directory without checking that it stays inside

tx0/go:archive-entry-pathrecommendedwarningsecurity, cwe-22

What it finds

filepath.Join(dest, header.Name) for an entry of a tar or zip archive: entry names are chosen by whoever made the archive, and a name such as ../../etc/cron.d/job or an absolute path makes the joined path point outside dest, so extracting writes files anywhere the process may. filepath.IsLocal on the name, or a check that the joined path still starts with the cleaned directory, keeps extraction inside.

Reported is a call of filepath.Join or path.Join with an argument x.Name where x is the value of a range over r.File, with archive/zip imported, or the first result of a call of Next(), with archive/tar imported. The function must not call filepath.IsLocal, filepath.Rel, strings.HasPrefix or strings.Contains, any of which may be the check.

From the rule's source, archive_entry_path.rs.

Example from the rule's tests
package p import (	"archive/tar"	"io"	"os"	"path/filepath") func extract(r io.Reader, dest string) error {	tr := tar.NewReader(r)	for {		hdr, err := tr.Next()		if err != nil {			return err		}		target := filepath.Join(dest, hdr.Name)tx0 reports: hdr.Name comes from the archive and may lead outside the directory		if err := os.WriteFile(target, nil, 0o600); err != nil {			return err		}	}} func checked(r io.Reader, dest string) error {	tr := tar.NewReader(r)	hdr, err := tr.Next()	if err != nil {		return err	}	if !filepath.IsLocal(hdr.Name) {		return nil	}	return os.WriteFile(filepath.Join(dest, hdr.Name), nil, 0o600)}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:archive-entry-path" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:archive-entry-path] reason="..."

tx0 explain tx0/go:archive-entry-path shows the same in the terminal. Found a false positive? Report it.