This commit is contained in:
@@ -11,10 +11,11 @@ import (
|
||||
"net/mail"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
"code.gitea.io/gitea/modules/zstd"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/validation"
|
||||
"gitea.dev/modules/zstd"
|
||||
|
||||
"github.com/blakesmith/ar"
|
||||
"github.com/ulikunitz/xz"
|
||||
@@ -36,18 +37,36 @@ const (
|
||||
controlTar = "control.tar"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMissingControlFile = util.NewInvalidArgumentErrorf("control file is missing")
|
||||
ErrUnsupportedCompression = util.NewInvalidArgumentErrorf("unsupported compression algorithm")
|
||||
ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid")
|
||||
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
|
||||
ErrInvalidArchitecture = util.NewInvalidArgumentErrorf("package architecture is invalid")
|
||||
var GlobalVars = sync.OnceValue(func() (ret struct {
|
||||
ErrMissingControlFile error
|
||||
ErrUnsupportedCompression error
|
||||
ErrInvalidName error
|
||||
ErrInvalidVersion error
|
||||
ErrInvalidArchitecture error
|
||||
|
||||
namePattern *regexp.Regexp
|
||||
versionPattern *regexp.Regexp
|
||||
symbolPattern *regexp.Regexp
|
||||
},
|
||||
) {
|
||||
ret.ErrMissingControlFile = util.NewInvalidArgumentErrorf("control file is missing")
|
||||
ret.ErrUnsupportedCompression = util.NewInvalidArgumentErrorf("unsupported compression algorithm")
|
||||
ret.ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid")
|
||||
ret.ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
|
||||
ret.ErrInvalidArchitecture = util.NewInvalidArgumentErrorf("package architecture is invalid")
|
||||
|
||||
// https://www.debian.org/doc/debian-policy/ch-controlfields.html#source
|
||||
namePattern = regexp.MustCompile(`\A[a-z0-9][a-z0-9+-.]+\z`)
|
||||
ret.namePattern = regexp.MustCompile(`\A[a-z0-9][a-z0-9+-.]+\z`)
|
||||
// https://www.debian.org/doc/debian-policy/ch-controlfields.html#version
|
||||
versionPattern = regexp.MustCompile(`\A(?:(0|[1-9][0-9]*):)?[a-zA-Z0-9.+~]+(?:-[a-zA-Z0-9.+-~]+)?\z`)
|
||||
)
|
||||
ret.versionPattern = regexp.MustCompile(`\A(?:(0|[1-9][0-9]*):)?[a-zA-Z0-9.+~]+(?:-[a-zA-Z0-9.+-~]+)?\z`)
|
||||
|
||||
// distribution and component are taken from the request path and written
|
||||
// verbatim into the generated line-based Release and Packages indices (and
|
||||
// into the pool/<distribution>/<component> paths referenced from them), so
|
||||
// they must be restricted to a character set that cannot break that format.
|
||||
ret.symbolPattern = regexp.MustCompile(`\A[a-zA-Z0-9][a-zA-Z0-9.~+_-]*\z`)
|
||||
return ret
|
||||
})
|
||||
|
||||
type Package struct {
|
||||
Name string
|
||||
@@ -64,6 +83,10 @@ type Metadata struct {
|
||||
Dependencies []string `json:"dependencies,omitempty"`
|
||||
}
|
||||
|
||||
func IsValidDistributionOrComponent(s string) bool {
|
||||
return GlobalVars().symbolPattern.MatchString(s)
|
||||
}
|
||||
|
||||
// ParsePackage parses the Debian package file
|
||||
// https://manpages.debian.org/bullseye/dpkg-dev/deb.5.en.html
|
||||
func ParsePackage(r io.Reader) (*Package, error) {
|
||||
@@ -109,10 +132,13 @@ func ParsePackage(r io.Reader) (*Package, error) {
|
||||
|
||||
inner = zr
|
||||
default:
|
||||
return nil, ErrUnsupportedCompression
|
||||
return nil, GlobalVars().ErrUnsupportedCompression
|
||||
}
|
||||
|
||||
tr := tar.NewReader(inner)
|
||||
// bound the decompressed control archive: it holds only the small control file
|
||||
// and maintainer scripts, so a much larger stream is a decompression bomb
|
||||
const maxControlTarSize = 32 * 1024 * 1024
|
||||
tr := tar.NewReader(io.LimitReader(inner, maxControlTarSize))
|
||||
for {
|
||||
hd, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
@@ -133,7 +159,7 @@ func ParsePackage(r io.Reader) (*Package, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrMissingControlFile
|
||||
return nil, GlobalVars().ErrMissingControlFile
|
||||
}
|
||||
|
||||
// ParseControlFile parses a Debian control file to retrieve the metadata
|
||||
@@ -145,20 +171,35 @@ func ParseControlFile(r io.Reader) (*Package, error) {
|
||||
key := ""
|
||||
var depends strings.Builder
|
||||
var control strings.Builder
|
||||
var description strings.Builder
|
||||
|
||||
s := bufio.NewScanner(io.TeeReader(r, &control))
|
||||
// https://www.debian.org/doc/debian-policy/ch-controlfields.html#syntax-of-control-files
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
line := s.Text()
|
||||
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
// A binary package control file holds exactly one stanza. Stop at the
|
||||
// blank line that terminates it, otherwise a crafted control file could
|
||||
// smuggle additional stanzas (with attacker-chosen Filename/Package
|
||||
// fields) into the generated repository "Packages" index.
|
||||
if control.Len() == 0 {
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
control.WriteString(line)
|
||||
control.WriteByte('\n')
|
||||
|
||||
// a leading space or tab marks a folded continuation line that belongs to the previous field
|
||||
// (identified by key), not a new "Key: value" pair; only the multi-line fields append here.
|
||||
// Continuation lines may themselves contain a colon, so they must not be re-split on ":".
|
||||
if line[0] == ' ' || line[0] == '\t' {
|
||||
switch key {
|
||||
case "Description":
|
||||
p.Metadata.Description += line
|
||||
description.WriteString(line)
|
||||
case "Depends":
|
||||
depends.WriteString(trimmed)
|
||||
}
|
||||
@@ -185,7 +226,8 @@ func ParseControlFile(r io.Reader) (*Package, error) {
|
||||
p.Metadata.Maintainer = a.Name
|
||||
}
|
||||
case "Description":
|
||||
p.Metadata.Description = value
|
||||
description.Reset()
|
||||
description.WriteString(value)
|
||||
case "Depends":
|
||||
depends.WriteString(value)
|
||||
case "Homepage":
|
||||
@@ -199,16 +241,18 @@ func ParseControlFile(r io.Reader) (*Package, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !namePattern.MatchString(p.Name) {
|
||||
return nil, ErrInvalidName
|
||||
if !GlobalVars().namePattern.MatchString(p.Name) {
|
||||
return nil, GlobalVars().ErrInvalidName
|
||||
}
|
||||
if !versionPattern.MatchString(p.Version) {
|
||||
return nil, ErrInvalidVersion
|
||||
if !GlobalVars().versionPattern.MatchString(p.Version) {
|
||||
return nil, GlobalVars().ErrInvalidVersion
|
||||
}
|
||||
if p.Architecture == "" {
|
||||
return nil, ErrInvalidArchitecture
|
||||
return nil, GlobalVars().ErrInvalidArchitecture
|
||||
}
|
||||
|
||||
p.Metadata.Description = description.String()
|
||||
|
||||
dependencies := strings.Split(depends.String(), ",")
|
||||
for i := range dependencies {
|
||||
dependencies[i] = strings.TrimSpace(dependencies[i])
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/zstd"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/zstd"
|
||||
|
||||
"github.com/blakesmith/ar"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -49,7 +49,7 @@ func TestParsePackage(t *testing.T) {
|
||||
|
||||
p, err := ParsePackage(data)
|
||||
assert.Nil(t, p)
|
||||
assert.ErrorIs(t, err, ErrMissingControlFile)
|
||||
assert.ErrorIs(t, err, GlobalVars().ErrMissingControlFile)
|
||||
})
|
||||
|
||||
t.Run("Compression", func(t *testing.T) {
|
||||
@@ -58,7 +58,7 @@ func TestParsePackage(t *testing.T) {
|
||||
|
||||
p, err := ParsePackage(data)
|
||||
assert.Nil(t, p)
|
||||
assert.ErrorIs(t, err, ErrUnsupportedCompression)
|
||||
assert.ErrorIs(t, err, GlobalVars().ErrUnsupportedCompression)
|
||||
})
|
||||
|
||||
var buf bytes.Buffer
|
||||
@@ -141,7 +141,7 @@ func TestParseControlFile(t *testing.T) {
|
||||
for _, name := range []string{"", "-cd"} {
|
||||
p, err := ParseControlFile(buildContent(name, packageVersion, packageArchitecture))
|
||||
assert.Nil(t, p)
|
||||
assert.ErrorIs(t, err, ErrInvalidName)
|
||||
assert.ErrorIs(t, err, GlobalVars().ErrInvalidName)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -149,14 +149,14 @@ func TestParseControlFile(t *testing.T) {
|
||||
for _, version := range []string{"", "1-", ":1.0", "1_0"} {
|
||||
p, err := ParseControlFile(buildContent(packageName, version, packageArchitecture))
|
||||
assert.Nil(t, p)
|
||||
assert.ErrorIs(t, err, ErrInvalidVersion)
|
||||
assert.ErrorIs(t, err, GlobalVars().ErrInvalidVersion)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidArchitecture", func(t *testing.T) {
|
||||
p, err := ParseControlFile(buildContent(packageName, packageVersion, ""))
|
||||
assert.Nil(t, p)
|
||||
assert.ErrorIs(t, err, ErrInvalidArchitecture)
|
||||
assert.ErrorIs(t, err, GlobalVars().ErrInvalidArchitecture)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
@@ -184,4 +184,63 @@ func TestParseControlFile(t *testing.T) {
|
||||
assert.NotNil(t, p)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SingleStanzaOnly", func(t *testing.T) {
|
||||
// A control file with a trailing stanza must not leak the extra fields into
|
||||
// p.Control, otherwise buildPackagesIndices would emit a second package entry
|
||||
// with an attacker-chosen Filename into the repository "Packages" index.
|
||||
content := bytes.NewBufferString("Package: realpkg\nVersion: 1.0.0\nArchitecture: amd64\nMaintainer: a <a@b.c>\nDescription: real\n\nPackage: openssl\nVersion: 99.0\nArchitecture: amd64\nFilename: pool/main/o/openssl/evil.deb\nDescription: spoofed\n")
|
||||
|
||||
p, err := ParseControlFile(content)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, p)
|
||||
assert.Equal(t, "realpkg", p.Name)
|
||||
assert.Equal(t, "1.0.0", p.Version)
|
||||
assert.NotContains(t, p.Control, "openssl")
|
||||
assert.NotContains(t, p.Control, "evil.deb")
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateDistributionOrComponent(t *testing.T) {
|
||||
bad := []string{
|
||||
"",
|
||||
".",
|
||||
"..",
|
||||
"-stable",
|
||||
".hidden",
|
||||
"a/b",
|
||||
"a b",
|
||||
"bookworm\nSigned-By: evil",
|
||||
"main\nFilename: pool/x",
|
||||
"a\tb",
|
||||
}
|
||||
for _, name := range bad {
|
||||
assert.False(t, IsValidDistributionOrComponent(name), "bad=%q", name)
|
||||
}
|
||||
|
||||
good := []string{
|
||||
"stable",
|
||||
"bookworm",
|
||||
"bookworm-backports",
|
||||
"stable-updates",
|
||||
"main",
|
||||
"non-free-firmware",
|
||||
"a",
|
||||
"1",
|
||||
}
|
||||
for _, name := range good {
|
||||
assert.True(t, IsValidDistributionOrComponent(name), "good=%q", name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseControlFileMultilineDescription verifies a multi-line Description is assembled in order
|
||||
// (the parser accumulates it in a strings.Builder); it guards the assembled value, not its timing.
|
||||
func TestParseControlFileMultilineDescription(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("Package: testpkg\nVersion: 1.0\nArchitecture: amd64\nDescription: short summary\n more details\n even more\n")
|
||||
|
||||
p, err := ParseControlFile(&buf)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, p)
|
||||
assert.Equal(t, "short summary more details even more", p.Metadata.Description)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user