Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hls, webrtc: prevent XSS attack when appending slash to paths (#2766) (#2767) #2772

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions internal/core/hls_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@
dir, fname = pa, ""

if !strings.HasSuffix(dir, "/") {
l := ctx.Request.URL.Path[1:] + "/"
if ctx.Request.URL.RawQuery != "" {
l += "?" + ctx.Request.URL.RawQuery
}
ctx.Writer.Header().Set("Location", l)
ctx.Writer.Header().Set("Location", httpserv.LocationWithTrailingSlash(ctx.Request.URL))

Check warning on line 149 in internal/core/hls_http_server.go

View check run for this annotation

Codecov / codecov/patch

internal/core/hls_http_server.go#L149

Added line #L149 was not covered by tests
ctx.Writer.WriteHeader(http.StatusMovedPermanently)
return
}
Expand Down
6 changes: 1 addition & 5 deletions internal/core/webrtc_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,7 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) {
s.onPage(ctx, ctx.Request.URL.Path[1:len(ctx.Request.URL.Path)-len("/publish")], true)

case ctx.Request.URL.Path[len(ctx.Request.URL.Path)-1] != '/':
l := ctx.Request.URL.Path[1:] + "/"
if ctx.Request.URL.RawQuery != "" {
l += "?" + ctx.Request.URL.RawQuery
}
ctx.Writer.Header().Set("Location", l)
ctx.Writer.Header().Set("Location", httpserv.LocationWithTrailingSlash(ctx.Request.URL))
ctx.Writer.WriteHeader(http.StatusMovedPermanently)

default:
Expand Down
12 changes: 12 additions & 0 deletions internal/protocols/httpserv/location_with_trailing_slash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package httpserv

import "net/url"

// LocationWithTrailingSlash returns the URL in a relative format, with a trailing slash.
func LocationWithTrailingSlash(u *url.URL) string {
l := "./" + u.Path[1:] + "/"
if u.RawQuery != "" {
l += "?" + u.RawQuery
}
return l
}
36 changes: 36 additions & 0 deletions internal/protocols/httpserv/location_with_trailing_slash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package httpserv

import (
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

func TestLocationWithTrailingSlash(t *testing.T) {
for _, ca := range []struct {
name string
url *url.URL
loc string
}{
{
"with query",
&url.URL{
Path: "/test",
RawQuery: "key=value",
},
"./test/?key=value",
},
{
"xss",
&url.URL{
Path: "/www.example.com",
},
"./www.example.com/",
},
} {
t.Run(ca.name, func(t *testing.T) {
require.Equal(t, ca.loc, LocationWithTrailingSlash(ca.url))
})
}
}
Loading