71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Einbetten im Rahmen (Nextcloud "External sites") und kompakte Darstellung (CI)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("fastapi")
|
|
|
|
from helpers import new_client
|
|
|
|
pytestmark = pytest.mark.db
|
|
|
|
|
|
def test_embedding_forbidden_by_default(app: Any) -> None:
|
|
resp = new_client(app).get("/login")
|
|
assert resp.headers["X-Frame-Options"] == "DENY"
|
|
assert "frame-ancestors 'none'" in resp.headers["Content-Security-Policy"]
|
|
assert 'class="brand"' in resp.text
|
|
|
|
|
|
@pytest.fixture
|
|
def embed_app(db_settings: Any, monkeypatch: pytest.MonkeyPatch) -> Any:
|
|
from surveybox import db
|
|
from surveybox import settings as settings_mod
|
|
from surveybox.upgrade import run_upgrade
|
|
from surveybox.web.app import create_app
|
|
|
|
monkeypatch.setenv("SURVEYBOX_FRAME_ANCESTORS", "https://cloud.example.de")
|
|
settings_mod.get_settings.cache_clear()
|
|
settings = settings_mod.get_settings()
|
|
run_upgrade(settings, db.get_engine())
|
|
return create_app()
|
|
|
|
|
|
def test_allowed_ancestors_and_compact_mode(embed_app: Any) -> None:
|
|
client = new_client(embed_app)
|
|
resp = client.get("/login")
|
|
assert "X-Frame-Options" not in resp.headers
|
|
assert ("frame-ancestors 'self' https://cloud.example.de"
|
|
in resp.headers["Content-Security-Policy"])
|
|
assert 'class="brand"' in resp.text # außerhalb des Rahmens wie immer
|
|
# Im Rahmen: ohne Logo und Fußzeile, mit "Neuer Tab"
|
|
framed = client.get("/login", headers={"Sec-Fetch-Dest": "iframe"}).text
|
|
assert 'class="brand"' not in framed and "topbar compact" in framed and "?embed=0" in framed
|
|
# ?embed=1 merkt sich die Darstellung, ?embed=0 schaltet sie ab
|
|
client.get("/login?embed=1")
|
|
assert "topbar compact" in client.get("/login").text
|
|
client.get("/login?embed=0")
|
|
assert "topbar compact" not in client.get("/login").text
|
|
|
|
|
|
def test_invalid_ancestors_are_refused(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
pytest.importorskip("pydantic_settings")
|
|
from pydantic import ValidationError
|
|
|
|
from surveybox.settings import Settings
|
|
|
|
monkeypatch.setenv("SURVEYBOX_PUBLIC_URL", "https://umfrage.example.de")
|
|
monkeypatch.setenv("SURVEYBOX_SECRET_KEY", "x" * 40)
|
|
monkeypatch.setenv("SURVEYBOX_DB_PASSWORD", "x")
|
|
for value in ("http://cloud.example.de", "*", "https://a.de/pfad", "'unsafe-inline'"):
|
|
monkeypatch.setenv("SURVEYBOX_FRAME_ANCESTORS", value)
|
|
with pytest.raises(ValidationError):
|
|
Settings()
|
|
monkeypatch.setenv("SURVEYBOX_FRAME_ANCESTORS", "https://Cloud.example.de/, https://b.de:8443")
|
|
settings = Settings()
|
|
assert settings.frame_ancestors_csp == "'self' https://cloud.example.de https://b.de:8443"
|