Skip to content

Commit

Permalink
webrtc: do not pass preflight requests to external auth (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jun 21, 2023
1 parent ba6c0fa commit 01e0b42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
32 changes: 16 additions & 16 deletions internal/core/webrtc_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) {
// remove leading prefix
pa := ctx.Request.URL.Path[1:]

if !strings.HasSuffix(pa, "/whip") && !strings.HasSuffix(pa, "/whep") {
isWHIPorWHEP := strings.HasSuffix(pa, "/whip") || strings.HasSuffix(pa, "/whep")
isPreflight := ctx.Request.Method == http.MethodOptions &&
ctx.Request.Header.Get("Access-Control-Request-Method") != ""

if !isWHIPorWHEP || isPreflight {
switch ctx.Request.Method {
case http.MethodOptions:
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET")
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH")
ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, If-Match")
ctx.Writer.WriteHeader(http.StatusOK)
return
Expand Down Expand Up @@ -242,22 +246,20 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) {
},
})
if authRes.err != nil {
if ctx.Request.Method != http.MethodOptions {
if terr, ok := authRes.err.(pathErrAuth); ok {
if !hasCredentials {
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
ctx.Writer.WriteHeader(http.StatusUnauthorized)
return
}

s.Log(logger.Info, "authentication error: %v", terr.wrapped)
if terr, ok := authRes.err.(pathErrAuth); ok {
if !hasCredentials {
ctx.Header("WWW-Authenticate", `Basic realm="mediamtx"`)
ctx.Writer.WriteHeader(http.StatusUnauthorized)
return
}

ctx.Writer.WriteHeader(http.StatusNotFound)
s.Log(logger.Info, "authentication error: %v", terr.wrapped)
ctx.Writer.WriteHeader(http.StatusUnauthorized)
return
}

ctx.Writer.WriteHeader(http.StatusNotFound)
return
}

switch fname {
Expand All @@ -274,11 +276,9 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) {
case "whip", "whep":
switch ctx.Request.Method {
case http.MethodOptions:
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, POST, PATCH")
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH")
ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, If-Match")
if authRes.err == nil {
ctx.Writer.Header()["Link"] = iceServersToLinkHeader(s.parent.genICEServers())
}
ctx.Writer.Header()["Link"] = iceServersToLinkHeader(s.parent.genICEServers())
ctx.Writer.WriteHeader(http.StatusOK)

case http.MethodPost:
Expand Down
4 changes: 3 additions & 1 deletion internal/core/webrtc_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,13 @@ func TestWebRTCPublish(t *testing.T) {

hc := &http.Client{Transport: &http.Transport{}}

// OPTIONS preflight requests must always work, without authentication
// preflight requests must always work, without authentication
func() {
req, err := http.NewRequest("OPTIONS", "http://localhost:8889/teststream/whip", nil)
require.NoError(t, err)

req.Header.Set("Access-Control-Request-Method", "OPTIONS")

res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
Expand Down

0 comments on commit 01e0b42

Please sign in to comment.