fix(streaming): open files through os.Root and tighten token parsing
- fix: open the audio file through os.Root with O_NONBLOCK. A FIFO under a stored path used to block the request forever in open(2) before the regular-file check could run; now it is refused immediately - fix: os.Root also removes the gap between checking the path and opening it, so a symlink swapped in at the last moment cannot leave the library (absolute symlinks are refused even when they point back inside) - fix(api): only "Authorization: Bearer <token>" is accepted again. The query token change had made a bare token in the header pass on every route - fix(config): library.roots must be absolute paths; a relative root made every stream fail and depended on the directory sondad started in - test: FIFO and symlink-to-FIFO without hanging, bare and non-Bearer headers on a normal route and on the stream route, relative roots
This commit is contained in:
parent
49f1051b3f
commit
def20731be
3 changed files with 179 additions and 37 deletions
|
|
@ -2,6 +2,7 @@ package httpapi
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
|
@ -320,3 +321,34 @@ func TestAdminCanStartScan(t *testing.T) {
|
|||
t.Errorf("starts = %d", scan.starts)
|
||||
}
|
||||
}
|
||||
|
||||
// Only "Authorization: Bearer <token>" is accepted. A bare token must not pass.
|
||||
func TestAuthorizationHeaderMustUseTheBearerScheme(t *testing.T) {
|
||||
h := newTestAPI(t)
|
||||
tok := registerAndLogin(t, h)
|
||||
|
||||
for name, header := range map[string]string{
|
||||
"bare token": tok,
|
||||
"basic scheme": "Basic " + tok,
|
||||
"lowercase": "bearer " + tok,
|
||||
"token scheme": "Token " + tok,
|
||||
"empty bearer": "Bearer ",
|
||||
"bearer no gap": "Bearer" + tok,
|
||||
} {
|
||||
req := httptest.NewRequest("GET", "/api/v1/auth/me", nil)
|
||||
req.Header.Set("Authorization", header)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Errorf("%s: status %d, want 401", name, rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/auth/me", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("proper Bearer header: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -87,7 +87,12 @@ func requireAuth(tokens tokenParser, allowQuery bool) gin.HandlerFunc {
|
|||
return func(c *gin.Context) {
|
||||
token := ""
|
||||
if header := c.GetHeader("Authorization"); header != "" {
|
||||
token, _ = strings.CutPrefix(header, "Bearer ")
|
||||
var ok bool
|
||||
if token, ok = strings.CutPrefix(header, "Bearer "); !ok {
|
||||
// Any other scheme, or a bare token, is not accepted.
|
||||
writeError(c, http.StatusUnauthorized, "unauthorized", "missing bearer token")
|
||||
return
|
||||
}
|
||||
} else if allowQuery {
|
||||
token = c.Query("access_token")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,6 +83,17 @@ func TestStreamRequiresAuth(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// A header that is present but not a Bearer token is not rescued by the query.
|
||||
func TestStreamIgnoresQueryTokenWhenAnyAuthorizationHeaderIsPresent(t *testing.T) {
|
||||
f := newStreamFixture(t)
|
||||
for _, header := range []string{f.token, "Basic " + f.token, "Token x"} {
|
||||
rec := f.get(t, "GET", "/api/v1/tracks/1/stream?access_token="+f.token, map[string]string{"Authorization": header})
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Errorf("Authorization %.12q... with a valid query token: %d, want 401", header, rec.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamAcceptsHeaderAndQueryToken(t *testing.T) {
|
||||
f := newStreamFixture(t)
|
||||
if rec := f.get(t, "GET", "/api/v1/tracks/1/stream", f.auth()); rec.Code != http.StatusOK {
|
||||
|
|
|
|||
Loading…
Reference in a new issue