Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/handler/cooldown_supported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (

func TestCooldownSupportedCoversHandlers(t *testing.T) {
calls := regexp.MustCompile(`(?:IsAllowed|Evaluate)\("([a-z]+)"|withheld\(h\.proxy, "([a-z]+)"|IsAllowed\((helmMetadataEcosystem)`)
// Metadata rewrites moved out of the handlers (#501) check cooldown in
// internal/metadata.
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
extracted, err := filepath.Glob("../metadata/*/*.go")
if err != nil {
t.Fatal(err)
}
files = append(files, extracted...)
found := map[string]bool{}
for _, f := range files {
if strings.HasSuffix(f, "_test.go") {
Expand Down
150 changes: 24 additions & 126 deletions internal/handler/gem.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package handler

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"

"github.com/git-pkgs/proxy/internal/metadata/gem"
"github.com/git-pkgs/proxy/internal/retention"
)

Expand Down Expand Up @@ -87,60 +85,9 @@ func (h *GemHandler) handleDownload(w http.ResponseWriter, r *http.Request) {
}

// GemFilenameIdentity returns the name and version a gem file is cached
// under, read from its filename. A platform build's version keeps its
// platform.
// e.g., "rails-7.1.0.gem" -> ("rails", "7.1.0")
// e.g., "aws-sdk-s3-1.142.0.gem" -> ("aws-sdk-s3", "1.142.0")
// e.g., "sorbet-static-0.5.11002-universal-darwin-22.gem" -> ("sorbet-static", "0.5.11002-universal-darwin-22")
// e.g., "devise-2fa-1.0.0.gem" -> ("devise-2fa", "1.0.0")
//
// A gem file is named {name}-{version}[-{platform}]. RubyGems writes a
// version as dot-separated alphanumeric segments starting with a digit,
// turning any "-" into ".pre.", and a platform starts with a letter (its
// CPU, or java), though it can end in a number (universal-java-17), so
// splitting at the last "-<digit>" would cut the platform. The version
// starts at the first "-<digit>" that leaves such a version and platform,
// so a name with a "-<digit>" segment of its own, such as devise-2fa, is
// kept whole. A filename no split fits is split at the first "-<digit>".
//
// Trying every "-<digit>" costs time quadratic in the filename's length, so
// a filename longer than maxGemFilenameLength is refused.
// under, read from its filename (see gem.FilenameIdentity).
func GemFilenameIdentity(filename string) (name, version string) {
if len(filename) > maxGemFilenameLength {
return "", ""
}
base := strings.TrimSuffix(filename, ".gem")
first := -1
for i := 0; i+1 < len(base); i++ {
if base[i] != '-' || base[i+1] < '0' || base[i+1] > '9' {
continue
}
if first < 0 {
first = i
}
if gemVersionAndPlatform(base[i+1:]) {
return base[:i], base[i+1:]
}
}
if first < 0 {
return "", ""
}
return base[:first], base[first+1:]
}

// maxGemFilenameLength bounds the gem filenames GemFilenameIdentity accepts.
// Real gem filenames are a small fraction of this.
const maxGemFilenameLength = 512

var (
gemVersionPattern = regexp.MustCompile(`^[0-9]+(\.[0-9A-Za-z]+)*$`)
gemPlatformPattern = regexp.MustCompile(`^[A-Za-z][0-9A-Za-z_]*(-[0-9A-Za-z_]+)*$`)
)

// gemVersionAndPlatform reports whether s reads as {version}[-{platform}].
func gemVersionAndPlatform(s string) bool {
version, platform, hasPlatform := strings.Cut(s, "-")
return gemVersionPattern.MatchString(version) && (!hasPlatform || gemPlatformPattern.MatchString(platform))
return gem.FilenameIdentity(filename)
}

// handleCompactIndex serves the compact index for a gem, filtering versions
Expand Down Expand Up @@ -184,9 +131,7 @@ func (h *GemHandler) handleCompactIndex(w http.ResponseWriter, r *http.Request)
if err != nil {
return nil, nil, err
}
out, kept := h.filterIndex(body, name, func(version string) bool {
return docs.withheld(h.proxy, "gem", name, version)
})
out, kept := h.filterIndex(body, name, docs)
return out, kept, nil
})
if err != nil {
Expand Down Expand Up @@ -227,83 +172,36 @@ func (h *GemHandler) indexVersions(doc metadataResult) ([]string, error) {
if err != nil {
return nil, err
}
var versions []string
for rest := body; len(rest) > 0; {
line, tail, _ := bytes.Cut(rest, []byte("\n"))
rest = tail
if version := gemIndexLineVersion(line); gemIndexListsVersion(version) {
versions = append(versions, string(version))
}
}
return versions, nil
}

// gemIndexLineVersion returns the version a compact index line starts with.
func gemIndexLineVersion(line []byte) []byte {
if spaceIdx := bytes.IndexByte(line, ' '); spaceIdx > 0 {
return line[:spaceIdx]
}
return line
return gem.IndexVersions(body), nil
}

// gemIndexListsVersion reports whether a compact index line's first field
// is a version, rather than the "---" that starts the file or an empty line.
func gemIndexListsVersion(field []byte) bool {
return len(field) > 0 && string(field) != "---"
// filterIndex returns the compact index without the lines of the versions
// cooldown holds back, by the publish times in docs, and the versions it
// kept; see gem.FilterIndex.
func (h *GemHandler) filterIndex(body []byte, name string, docs timedDocuments) ([]byte, []string) {
return gem.FilterIndex(name, body, gem.Options{
Cooldown: h.proxy.cooldown(),
EvaluatedAt: h.proxy.clock(),
Published: docs.published,
WithholdUnknown: docs.withholdUnknown,
OnWithheld: h.cooldownFiltered,
})
}

// filterIndex returns the compact index without the lines of versions
// withheld reports, and the versions it kept.
func (h *GemHandler) filterIndex(body []byte, name string, withheld func(string) bool) ([]byte, []string) {
var kept []string
var out bytes.Buffer
out.Grow(len(body))
for rest := body; len(rest) > 0; {
line, tail, _ := bytes.Cut(rest, []byte("\n"))
rest = tail

version := gemIndexLineVersion(line)
if gemIndexListsVersion(version) {
if withheld(string(version)) {
h.proxy.Logger.Info("cooldown: filtering gem version",
"gem", name, "version", string(version))
continue
}
kept = append(kept, string(version))
}
out.Write(line)
out.WriteByte('\n')
}
return out.Bytes(), kept
// cooldownFiltered logs a version a compact index was served without and
// records it for the package page.
func (h *GemHandler) cooldownFiltered(name, version string, publishedAt time.Time) {
h.proxy.Logger.Info("cooldown: filtering gem version", "gem", name, "version", version)
h.proxy.recordWithheld("gem", name, version, publishedAt)
}

// gemVersion represents a version entry from the RubyGems versions API.
type gemVersion struct {
Number string `json:"number"`
Platform string `json:"platform"`
CreatedAt string `json:"created_at"`
}
// gemVersion is a version entry from the RubyGems versions API.
type gemVersion = gem.Version

// parseGemVersions reads the publish times from a versions API response,
// keyed by version as the compact index writes it.
func parseGemVersions(body []byte) (publishTimes, error) {
var versions []gemVersion
if err := json.Unmarshal(body, &versions); err != nil {
return nil, err
}
times := make(publishTimes, len(versions))
for _, v := range versions {
versionStr := v.Number
if v.Platform != "" && v.Platform != "ruby" {
versionStr = v.Number + "-" + v.Platform
}
createdAt, err := time.Parse(time.RFC3339, v.CreatedAt)
if err != nil {
createdAt = time.Time{}
}
times[versionStr] = createdAt
}
return times, nil
return gem.PublishTimes(body)
}

func (h *GemHandler) publishTime(r *http.Request, name, version string) (time.Time, bool, bool, error) {
Expand Down
63 changes: 63 additions & 0 deletions internal/metadata/gem/filename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gem

import (
"regexp"
"strings"
)

// maxFilenameLength bounds the gem filenames FilenameIdentity accepts.
// Real gem filenames are a small fraction of this.
const maxFilenameLength = 512

var (
versionPattern = regexp.MustCompile(`^[0-9]+(\.[0-9A-Za-z]+)*$`)
platformPattern = regexp.MustCompile(`^[A-Za-z][0-9A-Za-z_]*(-[0-9A-Za-z_]+)*$`)
)

// FilenameIdentity returns the name and version a gem file is cached
// under, read from its filename. A platform build's version keeps its
// platform.
// e.g., "rails-7.1.0.gem" -> ("rails", "7.1.0")
// e.g., "aws-sdk-s3-1.142.0.gem" -> ("aws-sdk-s3", "1.142.0")
// e.g., "sorbet-static-0.5.11002-universal-darwin-22.gem" -> ("sorbet-static", "0.5.11002-universal-darwin-22")
// e.g., "devise-2fa-1.0.0.gem" -> ("devise-2fa", "1.0.0")
//
// A gem file is named {name}-{version}[-{platform}]. RubyGems writes a
// version as dot-separated alphanumeric segments starting with a digit,
// turning any "-" into ".pre.", and a platform starts with a letter (its
// CPU, or java), though it can end in a number (universal-java-17), so
// splitting at the last "-<digit>" would cut the platform. The version
// starts at the first "-<digit>" that leaves such a version and platform,
// so a name with a "-<digit>" segment of its own, such as devise-2fa, is
// kept whole. A filename no split fits is split at the first "-<digit>".
//
// Trying every "-<digit>" costs time quadratic in the filename's length, so
// a filename longer than 512 bytes is refused.
func FilenameIdentity(filename string) (name, version string) {
if len(filename) > maxFilenameLength {
return "", ""
}
base := strings.TrimSuffix(filename, ".gem")
first := -1
for i := 0; i+1 < len(base); i++ {
if base[i] != '-' || base[i+1] < '0' || base[i+1] > '9' {
continue
}
if first < 0 {
first = i
}
if versionAndPlatform(base[i+1:]) {
return base[:i], base[i+1:]
}
}
if first < 0 {
return "", ""
}
return base[:first], base[first+1:]
}

// versionAndPlatform reports whether s reads as {version}[-{platform}].
func versionAndPlatform(s string) bool {
version, platform, hasPlatform := strings.Cut(s, "-")
return versionPattern.MatchString(version) && (!hasPlatform || platformPattern.MatchString(platform))
}
43 changes: 43 additions & 0 deletions internal/metadata/gem/imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gem

import (
"os/exec"
"slices"
"strings"
"testing"
)

// TestPortableImports keeps the package free of the proxy's infrastructure:
// no handler, database, storage, metrics, HTTP or logging, so it builds for
// targets such as WASI where those are not available.
func TestPortableImports(t *testing.T) {
goTool, err := exec.LookPath("go")
if err != nil {
t.Skip("go tool not found")
}
out, err := exec.Command(goTool, "list", "-deps", "-f", "{{.Standard}} {{.ImportPath}}", ".").Output()
if err != nil {
t.Fatalf("go list: %v", err)
}
allowed := []string{
"github.com/git-pkgs/cooldown",
"github.com/git-pkgs/proxy/internal/metadata/gem",
"github.com/git-pkgs/proxy/internal/packageurl",
"github.com/git-pkgs/purl",
"github.com/git-pkgs/vers",
"github.com/package-url/packageurl-go",
}
forbiddenStd := []string{"database/sql", "log/slog", "net", "net/http"}
for line := range strings.Lines(string(out)) {
standard, path, _ := strings.Cut(strings.TrimSpace(line), " ")
if standard == "true" {
if slices.Contains(forbiddenStd, path) {
t.Errorf("imports %s", path)
}
continue
}
if !slices.Contains(allowed, path) {
t.Errorf("imports %s, which is not on the portable list", path)
}
}
}
Loading
Loading