Files
gitea/modules/packages/debian/ar.go
T
silverwind 1424a2b7c2 fix(packages): replace blakesmith/ar with an in-tree reader (#40401)
https://github.com/blakesmith/ar is unmaintained since 2019 and panics
on a negative member size (https://pkg.go.dev/vuln/GO-2026-6905), with
no fixed version. Debian packages only need a small subset of the `ar`
format, so this reads it in-tree.

- Malformed or truncated archives are rejected with a 400 instead of a
500 or a panic
- Parsing stops after the first `control.tar` member, deb(5) allows only
one

Assisted-by: Claude Code:claude-opus-5-5
2026-11-17 03:14:40 +00:00

44 lines
1.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package debian
import (
"io"
"strconv"
"strings"
"gitea.dev/modules/util"
)
const arMagic = "!<arch>\n"
type arReader struct {
io.LimitedReader
pad int64
}
func newArReader(r io.Reader) (*arReader, error) {
var magic [len(arMagic)]byte
if _, err := io.ReadFull(r, magic[:]); err != nil || string(magic[:]) != arMagic {
return nil, GlobalVars().ErrInvalidArchive
}
return &arReader{LimitedReader: io.LimitedReader{R: r}}, nil
}
func (ar *arReader) next() (string, error) {
if _, err := io.CopyN(io.Discard, ar.R, ar.N+ar.pad); err != nil {
return "", GlobalVars().ErrInvalidArchive
}
var header [60]byte
if _, err := io.ReadFull(ar.R, header[:]); err != nil {
return "", util.Iif(err == io.EOF, err, GlobalVars().ErrInvalidArchive)
}
size, err := strconv.ParseInt(strings.TrimRight(string(header[48:58]), " "), 10, 64)
if err != nil || size < 0 || string(header[58:]) != "`\n" {
return "", GlobalVars().ErrInvalidArchive
}
ar.N, ar.pad = size, size%2
return strings.TrimSuffix(strings.TrimRight(string(header[:16]), " "), "/"), nil // GNU ar terminates names with a slash
}