test: enforce liveview embedding boundary (#54)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package liveview
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"yovision.local/sense/app/sense/media"
|
||||
)
|
||||
|
||||
func TestPlayerAllowsOnlySameOriginEmbedding(t *testing.T) {
|
||||
service, err := NewService("http://127.0.0.1:8889", time.Minute)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
route := media.Route{ID: "device:main", Path: "sense_device_main", Desired: "running", Actual: "ready"}
|
||||
service.route = func(context.Context, string) (media.Route, error) { return route, nil }
|
||||
service.refresh = func(context.Context, string) (media.Route, error) { return route, nil }
|
||||
service.sessions["view_test"] = Session{ID: "view_test", RouteID: route.ID, ExpiresAt: time.Now().Add(time.Minute)}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/liveview/sessions/view_test/player", nil)
|
||||
req.SetPathValue("id", "view_test")
|
||||
res := httptest.NewRecorder()
|
||||
NewModule(service).player(res, req)
|
||||
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", res.Code)
|
||||
}
|
||||
if got := res.Header().Get("X-Frame-Options"); got != "SAMEORIGIN" {
|
||||
t.Fatalf("X-Frame-Options = %q", got)
|
||||
}
|
||||
csp := res.Header().Get("Content-Security-Policy")
|
||||
if !strings.Contains(csp, "frame-ancestors 'self'") {
|
||||
t.Fatalf("Content-Security-Policy = %q", csp)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user