A listener can now play the music of a friend from the web app.
- GET /friends/{friend}/tracks/{id}/stream and /friends/{friend}/albums/{id}/cover,
in the group that accepts the access token in the query, like the local ones.
This server asks the friend with a signed request and passes the bytes on,
forwarding Range and the conditional headers, so a player can seek. The
friend sees this server and never the listener.
- Only audio (for a stream) and a JPEG (for a cover) are passed on, with a
fixed set of headers: Content-Type, Content-Length, Content-Range,
Accept-Ranges, Last-Modified and ETag. Anything else a friend sends, or
what a listener's request carries (cookies, authorization), is not passed
either way, so a friend cannot make this server serve anything else.
- The client of friends can open a streaming answer: no limit on its whole
length, one only on its beginning, no redirect followed, and it ends when
the listener leaves.
- The player holds a friend's track next to the local ones. It streams
through this server, shows the friend's cover, links to the music of the
friend and not to a local page, is told apart from a local track with the
same id, and is never reported as a listen: it does not feed taste.
- The songs of a friend have play buttons, the albums their covers.
- OpenAPI paths, contract test, and two real servers checked end to end: the
bytes of the file arrive unchanged, with ranges, and a song plays in a
browser from the friend's server through the other one.
212 lines
7.2 KiB
Go
212 lines
7.2 KiB
Go
package federation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type closeTracker struct {
|
|
io.Reader
|
|
closed bool
|
|
}
|
|
|
|
func (c *closeTracker) Close() error { c.closed = true; return nil }
|
|
|
|
// mediaRemote is a Remote whose signed streaming GET answers as a script says.
|
|
type mediaRemote struct {
|
|
getRemote
|
|
resp *http.Response
|
|
body *closeTracker
|
|
err error
|
|
sent http.Header
|
|
opened int
|
|
}
|
|
|
|
func (m *mediaRemote) Open(_ context.Context, addr, uri string, auth Auth, header http.Header) (*http.Response, error) {
|
|
m.opened++
|
|
m.addr, m.uri, m.auth, m.sent = addr, uri, auth, header
|
|
if m.err != nil {
|
|
return nil, m.err
|
|
}
|
|
m.resp.Body = m.body
|
|
return m.resp, nil
|
|
}
|
|
|
|
func answer(status int, body string, kv ...string) (*http.Response, *closeTracker) {
|
|
h := http.Header{}
|
|
for i := 0; i+1 < len(kv); i += 2 {
|
|
h.Add(kv[i], kv[i+1])
|
|
}
|
|
return &http.Response{StatusCode: status, Header: h}, &closeTracker{Reader: strings.NewReader(body)}
|
|
}
|
|
|
|
func media(t *testing.T) (*Service, *mediaRemote, int64) {
|
|
t.Helper()
|
|
s, _, _, _, _, id := pairedServers(t)
|
|
m := &mediaRemote{}
|
|
s.remote = m
|
|
return s, m, id
|
|
}
|
|
|
|
func TestAStreamIsPassedOnWithAFixedSetOfHeaders(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, m, id := media(t)
|
|
m.resp, m.body = answer(206, "0123456789",
|
|
"Content-Type", "audio/mpeg", "Content-Range", "bytes 0-9/100", "Content-Length", "10", "Accept-Ranges", "bytes",
|
|
"Last-Modified", "Mon, 02 Jan 2006 15:04:05 GMT", "Set-Cookie", "session=1", "Content-Disposition", "attachment; filename=x.exe",
|
|
"Location", "https://evil.example.org/", "X-Anything", "x")
|
|
|
|
got, err := s.OpenMedia(ctx, id, MediaStream, 9, nil, http.Header{
|
|
"Range": {"bytes=0-9"}, "Authorization": {"Bearer secret"}, "Cookie": {"a=b"}, "If-Modified-Since": {"Mon, 02 Jan 2006 15:04:05 GMT"}, "X-Evil": {"1"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer got.Body.Close()
|
|
if body, _ := io.ReadAll(got.Body); string(body) != "0123456789" || got.Status != 206 {
|
|
t.Errorf("status %d, body %q", got.Status, body)
|
|
}
|
|
for _, k := range []string{"Content-Type", "Content-Range", "Content-Length", "Accept-Ranges", "Last-Modified"} {
|
|
if got.Header.Get(k) == "" {
|
|
t.Errorf("%s was not passed on", k)
|
|
}
|
|
}
|
|
for _, k := range []string{"Set-Cookie", "Content-Disposition", "Location", "X-Anything"} {
|
|
if got.Header.Get(k) != "" {
|
|
t.Errorf("%s was passed on: a friend decides nothing about the answer a listener gets", k)
|
|
}
|
|
}
|
|
if m.uri != "/api/v1/peer/tracks/9/stream" || m.addr != "https://bruno.example.org" || len(m.auth.Key) == 0 {
|
|
t.Errorf("asked %q at %q", m.uri, m.addr)
|
|
}
|
|
// Only what a player sends to seek and to ask again goes to the friend.
|
|
if m.sent.Get("Range") != "bytes=0-9" || m.sent.Get("If-Modified-Since") == "" {
|
|
t.Errorf("sent %v", m.sent)
|
|
}
|
|
for _, k := range []string{"Authorization", "Cookie", "X-Evil"} {
|
|
if m.sent.Get(k) != "" {
|
|
t.Errorf("%s was sent to the friend: it is the listener's, not the friend's", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestACoverAsksForOneOfTwoSizes(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, m, id := media(t)
|
|
m.resp, m.body = answer(200, "jpeg", "Content-Type", "image/jpeg", "ETag", `"abc"`)
|
|
got, err := s.OpenMedia(ctx, id, MediaCover, 4, url.Values{"size": {"small"}, "evil": {"1"}}, http.Header{"If-None-Match": {`"abc"`}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got.Body.Close()
|
|
if m.uri != "/api/v1/peer/albums/4/cover?size=small" || m.sent.Get("If-None-Match") != `"abc"` {
|
|
t.Errorf("asked %q with %v", m.uri, m.sent)
|
|
}
|
|
if _, err := s.OpenMedia(ctx, id, MediaCover, 4, url.Values{"size": {"huge"}}, nil); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("a size that does not exist: %v", err)
|
|
}
|
|
m.resp, m.body = answer(304, "")
|
|
got, err = s.OpenMedia(ctx, id, MediaCover, 4, nil, nil)
|
|
if err != nil || got.Status != 304 {
|
|
t.Errorf("not modified: %+v, %v", got, err)
|
|
}
|
|
if got != nil {
|
|
got.Body.Close()
|
|
}
|
|
}
|
|
|
|
func TestOnlyAudioAndPicturesArePassedOn(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, m, id := media(t)
|
|
for _, tc := range []struct {
|
|
kind, ctype string
|
|
}{
|
|
{MediaStream, "text/html"}, {MediaStream, "application/javascript"}, {MediaStream, "image/jpeg"}, {MediaStream, ""},
|
|
{MediaStream, "multipart/byteranges; boundary=x"}, {MediaCover, "text/html"}, {MediaCover, "image/svg+xml"}, {MediaCover, "audio/mpeg"},
|
|
} {
|
|
m.resp, m.body = answer(200, "<script>alert(1)</script>", "Content-Type", tc.ctype)
|
|
_, err := s.OpenMedia(ctx, id, tc.kind, 1, nil, nil)
|
|
if !errors.Is(err, ErrUnreachable) {
|
|
t.Errorf("%s as %q: err = %v, want ErrUnreachable", tc.kind, tc.ctype, err)
|
|
}
|
|
if !m.body.closed {
|
|
t.Errorf("%s as %q: the friend's answer was left open", tc.kind, tc.ctype)
|
|
}
|
|
}
|
|
for _, ctype := range []string{"audio/mpeg", "audio/flac", "audio/mpeg; charset=binary"} {
|
|
m.resp, m.body = answer(200, "x", "Content-Type", ctype)
|
|
got, err := s.OpenMedia(ctx, id, MediaStream, 1, nil, nil)
|
|
if err != nil {
|
|
t.Errorf("%q: %v", ctype, err)
|
|
continue
|
|
}
|
|
got.Body.Close()
|
|
}
|
|
}
|
|
|
|
func TestWhatAFriendAnswersToAStreamIsToldInOurOwnTerms(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, m, id := media(t)
|
|
for _, tc := range []struct {
|
|
status int
|
|
body string
|
|
want error
|
|
}{
|
|
{404, `{"error":{"code":"track_not_found"}}`, ErrNotFoundThere},
|
|
{404, `{"error":{"code":"cover_not_found"}}`, ErrNotFoundThere},
|
|
{404, `{"error":{"code":"not_found"}}`, ErrNotFriend},
|
|
{401, `{"error":{"code":"unauthorized"}}`, ErrNotFriend},
|
|
{403, `{"error":{"code":"not_approved"}}`, ErrNotApproved},
|
|
{429, `{"error":{"code":"too_many_streams"}}`, ErrTooMany},
|
|
{400, `{"error":{"code":"invalid_input"}}`, ErrInvalidInput},
|
|
{502, `oops`, ErrUnreachable},
|
|
} {
|
|
m.resp, m.body = answer(tc.status, tc.body, "Content-Type", "application/json")
|
|
_, err := s.OpenMedia(ctx, id, MediaStream, 1, nil, nil)
|
|
if !errors.Is(err, tc.want) {
|
|
t.Errorf("%d %q: err = %v, want %v", tc.status, tc.body, err, tc.want)
|
|
}
|
|
if !m.body.closed {
|
|
t.Errorf("%d: the answer was left open", tc.status)
|
|
}
|
|
}
|
|
m.err = ErrUnreachable
|
|
if _, err := s.OpenMedia(ctx, id, MediaStream, 1, nil, nil); !errors.Is(err, ErrUnreachable) {
|
|
t.Errorf("no answer: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOnlyAnActiveFriendCanBeStreamedFrom(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, m, id := media(t)
|
|
m.resp, m.body = answer(200, "x", "Content-Type", "audio/mpeg")
|
|
for name, call := range map[string]func() error{
|
|
"an unknown friend": func() error { _, err := s.OpenMedia(ctx, 999, MediaStream, 1, nil, nil); return err },
|
|
} {
|
|
if err := call(); !errors.Is(err, ErrFriendMissing) {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
}
|
|
for name, call := range map[string]func() error{
|
|
"another kind": func() error { _, err := s.OpenMedia(ctx, id, "info", 1, nil, nil); return err },
|
|
"no id": func() error { _, err := s.OpenMedia(ctx, id, MediaStream, 0, nil, nil); return err },
|
|
"a negative id": func() error { _, err := s.OpenMedia(ctx, id, MediaStream, -3, nil, nil); return err },
|
|
} {
|
|
if err := call(); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
}
|
|
off := false
|
|
_, _ = s.Update(ctx, Patch{Enabled: &off})
|
|
if _, err := s.OpenMedia(ctx, id, MediaStream, 1, nil, nil); !errors.Is(err, ErrFriendMissing) {
|
|
t.Errorf("while off: %v", err)
|
|
}
|
|
if m.opened != 0 {
|
|
t.Errorf("the friend was asked %d times", m.opened)
|
|
}
|
|
}
|