pkgmanager: getCratesLatest panics with index out of range on 404 or empty crates.io responses

Aperta Adatta ai principianti
#1,222 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
backend, cli

Direzione di ricerca

Il panic si trova in getCratesLatest in internal/pkgmanager/crates.io.go, intorno alle righe 18-33, dove details.Versions[0] viene letto senza verificare resp.StatusCode né la lunghezza dello slice. Inizia da lì, poi esegui il test di riproduzione TestGetCratesLatest404 in internal/pkgmanager. È completato quando un 404 o una lista di versioni vuota restituisce un errore o una stringa vuota invece di andare in panic, così che worker.ResolvePkg segnali un pacchetto sconosciuto e cmd/downloader prosegua.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Resolving the latest version of a crates.io package panics with runtime error: index out of range [0] with length 0 when the crate has been removed or does not exist on crates.io, or when crates.io returns a JSON response without any versions entries (for example, when running cmd/downloader with an unversioned pkg:cargo/... entry such as MAL-2022-1's removed rustdecimal crate, or cmd/analyze -ecosystem crates.io -package ... without -version).

mkdir -p out
printf 'pkg:cargo/rustdecimal\npkg:cargo/[email protected]\n' > packages.txt
go run ./cmd/downloader -f packages.txt -d ./out

The same code path can be reproduced offline in internal/pkgmanager against a crates.io HTTP 404 JSON error body:

cat <<'EOF' > internal/pkgmanager/crates_404_test.go
package pkgmanager

import (
	"bytes"
	"io"
	"net/http"
	"testing"
)

type roundTripFunc func(*http.Request) (*http.Response, error)

func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }

func TestGetCratesLatest404(t *testing.T) {
	orig := http.DefaultTransport
	defer func() { http.DefaultTransport = orig }()
	http.DefaultTransport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
		return &http.Response{
			StatusCode: http.StatusNotFound,
			Body:       io.NopCloser(bytes.NewBufferString(`{"errors":[{"detail":"Not Found"}]}`)),
			Header:     make(http.Header),
		}, nil
	})

	_, _ = getCratesLatest("nonexistent-crate-xyz")
}
EOF
go test -v ./internal/pkgmanager -run TestGetCratesLatest404

Actual: getCratesLatest panics at internal/pkgmanager/crates.io.go:32 with runtime error: index out of range [0] with length 0, aborting cmd/downloader before pkg:cargo/[email protected] is attempted.

Expected: getCratesLatest returns an error (or "" so worker.ResolvePkg returns unknown package name '...', matching npm, pypi, rubygems, and packagist), allowing cmd/downloader to print the error for line 1 and continue downloading remaining packages as documented in cmd/downloader/README.md.

Tested on rel-38 (98cd1130815a3b5fb1cac44661ef93fa16da6962) and main (a149a6f2e1179b8f0c05da6845d8513d360e3b1a).

In internal/pkgmanager/crates.io.go#L18-L33, getCratesLatest does not check resp.StatusCode and unconditionally returns details.Versions[0].Num after json.NewDecoder(resp.Body).Decode(&details), which succeeds with an empty Versions slice on crates.io JSON error responses (such as {"errors":[{"detail":"crate `rustdecimal` does not exist"}]} or {"errors":[{"detail":"Not Found"}]}) or on crates with zero versions.

Credit: https://ucsbplse.github.io/

Lingua principale
Go
Stelle
926
Fork
76
Merge medio
13m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ossf/package-analysis

Tutte le issue di ossf/package-analysis

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.