fix(musicbrainz): follow the redirect of an id that was merged
When MusicBrainz merges two entries, the old id answers with a redirect to the new one. The client refused every redirect, so the description of that artist failed at every run of the job. The client for MusicBrainz now follows a redirect only to https://musicbrainz.org/ws/2/artist/<id> or /release-group/<id>, with no credentials or port. Any other redirect is still refused. The new id is used for that request only.
This commit is contained in:
parent
5b9f98d8ed
commit
6b40ea06fa
2 changed files with 117 additions and 3 deletions
|
|
@ -13,6 +13,10 @@
|
|||
// User-Agent and to stay at one request per second: both are the business of the
|
||||
// outbound client this one sends its requests through. Errors never repeat the
|
||||
// provider's own text.
|
||||
//
|
||||
// When two entries are merged, the id of the old one answers with a redirect to the new one.
|
||||
// That is the only redirect the client that talks to MusicBrainz may follow (RedirectAllowed is
|
||||
// what to give it); any other is refused, as everywhere else.
|
||||
package musicbrainz
|
||||
|
||||
import (
|
||||
|
|
@ -53,6 +57,18 @@ var (
|
|||
|
||||
var idRe = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
|
||||
// redirectPathRe is where a merged id may lead: the same kind of thing, in the web service.
|
||||
var redirectPathRe = regexp.MustCompile(`^/ws/2/(artist|release-group)/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
|
||||
// RedirectAllowed says whether a redirect may be followed: https, no credentials, no port,
|
||||
// musicbrainz.org itself (not a subdomain), and a path that is again an artist or a release
|
||||
// group id of the web service. It is how a merged id reaches the entry it was merged into.
|
||||
// The follow-up request goes out at once, a second in a row for this one id: merges are rare.
|
||||
func RedirectAllowed(u *url.URL) bool {
|
||||
return u.Scheme == "https" && u.User == nil && u.Port() == "" && u.Hostname() == "musicbrainz.org" &&
|
||||
redirectPathRe.MatchString(u.Path)
|
||||
}
|
||||
|
||||
// Getter performs a GET. *outbound.Client is the one used in production.
|
||||
type Getter interface {
|
||||
Get(ctx context.Context, rawURL string) (*outbound.Response, error)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
|
|
@ -332,3 +333,99 @@ func TestFindReleaseGroupErrors(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- an id that was merged ----------------------------------------------------------------------
|
||||
|
||||
func TestRedirectAllowedOnlyToAnArtistOrReleaseGroupOnMusicBrainz(t *testing.T) {
|
||||
const id = "4e0dffde-ad2d-45b7-9c75-d57ce55de061"
|
||||
cases := []struct {
|
||||
url string
|
||||
want bool
|
||||
}{
|
||||
{"https://musicbrainz.org/ws/2/artist/" + id + "?inc=url-rels&fmt=json", true},
|
||||
{"https://musicbrainz.org/ws/2/release-group/" + id, true},
|
||||
{"http://musicbrainz.org/ws/2/artist/" + id, false}, // not https
|
||||
{"https://musicbrainz.org:8443/ws/2/artist/" + id, false}, // a port
|
||||
{"https://user:pw@musicbrainz.org/ws/2/artist/" + id, false}, // credentials
|
||||
{"https://evil.example/ws/2/artist/" + id, false}, // another host
|
||||
{"https://musicbrainz.org.evil.example/ws/2/artist/" + id, false}, // look-alike host
|
||||
{"https://beta.musicbrainz.org/ws/2/artist/" + id, false}, // a subdomain is not asked
|
||||
{"https://musicbrainz.org/ws/2/artist/not-an-id", false}, // not an id
|
||||
{"https://musicbrainz.org/ws/2/artist/" + id + "/../../login", false}, // a longer path
|
||||
{"https://musicbrainz.org/login", false}, // not the web service
|
||||
{"https://musicbrainz.org/ws/2/recording/" + id, false}, // not a thing Sonda asks for
|
||||
}
|
||||
for _, c := range cases {
|
||||
u, err := url.Parse(c.url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := RedirectAllowed(u); got != c.want {
|
||||
t.Errorf("RedirectAllowed(%s) = %v, want %v", c.url, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// roundTrip lets a test answer for https://musicbrainz.org without the network.
|
||||
type roundTrip func(*http.Request) *http.Response
|
||||
|
||||
func (f roundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r), nil }
|
||||
|
||||
func reply(status int, location, body string) *http.Response {
|
||||
h := http.Header{"Content-Type": {"application/json"}}
|
||||
if location != "" {
|
||||
h.Set("Location", location)
|
||||
}
|
||||
return &http.Response{StatusCode: status, Header: h, Body: io.NopCloser(strings.NewReader(body))}
|
||||
}
|
||||
|
||||
func realClient(rt roundTrip) *Client {
|
||||
return New(outbound.New(outbound.Options{
|
||||
UserAgent: "Sonda/test ( https://example.org )", Retries: outbound.NoRetries,
|
||||
Transport: rt, FollowRedirect: RedirectAllowed,
|
||||
}), BaseURL)
|
||||
}
|
||||
|
||||
const (
|
||||
oldID = "1cf2a9a4-ce75-4bff-b545-a788535ca3a5"
|
||||
newID = "7fa7fc04-1011-4876-8095-ecd232edea87"
|
||||
)
|
||||
|
||||
func TestAnArtistWhoseIdWasMergedIsReadFromTheNewId(t *testing.T) {
|
||||
var asked []string
|
||||
c := realClient(func(r *http.Request) *http.Response {
|
||||
asked = append(asked, r.URL.Host+r.URL.Path)
|
||||
if strings.HasSuffix(r.URL.Path, oldID) {
|
||||
return reply(301, "https://musicbrainz.org/ws/2/artist/"+newID+"?inc=url-rels&fmt=json", "")
|
||||
}
|
||||
return reply(200, "", `{"id":"`+newID+`","relations":[{"type":"wikidata","ended":false,"url":{"resource":"https://www.wikidata.org/wiki/Q42"}}]}`)
|
||||
})
|
||||
got, err := c.WikidataID(context.Background(), oldID)
|
||||
if err != nil || got != "Q42" {
|
||||
t.Fatalf("WikidataID = %q, %v; want Q42", got, err)
|
||||
}
|
||||
want := []string{"musicbrainz.org/ws/2/artist/" + oldID, "musicbrainz.org/ws/2/artist/" + newID}
|
||||
if strings.Join(asked, " ") != strings.Join(want, " ") {
|
||||
t.Errorf("asked %v, want %v", asked, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestARedirectToAnywhereElseIsStillRefused(t *testing.T) {
|
||||
for _, to := range []string{
|
||||
"https://evil.example/ws/2/artist/" + newID,
|
||||
"https://musicbrainz.org/login",
|
||||
"http://musicbrainz.org/ws/2/artist/" + newID,
|
||||
} {
|
||||
var asked []string
|
||||
c := realClient(func(r *http.Request) *http.Response {
|
||||
asked = append(asked, r.URL.String())
|
||||
return reply(301, to, "")
|
||||
})
|
||||
if _, err := c.WikidataID(context.Background(), oldID); !errors.Is(err, ErrUnavailable) {
|
||||
t.Errorf("redirect to %s: err = %v, want ErrUnavailable", to, err)
|
||||
}
|
||||
if len(asked) != 1 {
|
||||
t.Errorf("redirect to %s: %d requests, want only the first: %v", to, len(asked), asked)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue