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

First pass at implementing cors #1109

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion mesop/features/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from mesop.runtime import OnLoadHandler, PageConfig, runtime
from mesop.security.security_policy import SecurityPolicy
from mesop.security.cors import CORS


def page(
Expand All @@ -12,6 +13,7 @@ def page(
title: str | None = None,
stylesheets: list[str] | None = None,
security_policy: SecurityPolicy | None = None,
cors: CORS | None = None,
on_load: OnLoadHandler | None = None,
) -> Callable[[Callable[[], None]], Callable[[], None]]:
"""Defines a page in a Mesop application.
Expand Down Expand Up @@ -42,7 +44,8 @@ def wrapper() -> None:
page_fn=wrapper,
title=title or f"Mesop: {path}",
stylesheets=deepcopy(stylesheets or default_stylesheets),
security_policy=deepcopy(security_policy)
security_policy=deepcopy(security_policy),
cors = deepcopy(cors)
if security_policy
else SecurityPolicy(),
on_load=on_load,
Expand Down
2 changes: 2 additions & 0 deletions mesop/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from mesop.exceptions import MesopDeveloperException, MesopUserException
from mesop.key import Key
from mesop.security.security_policy import SecurityPolicy
from mesop.security.cors import CORS
from mesop.utils.backoff import exponential_backoff
from mesop.warn import warn

Expand All @@ -33,6 +34,7 @@ class PageConfig:
title: str
stylesheets: list[str]
security_policy: SecurityPolicy
cors: CORS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a separate parameter in PageConfig, I would put CORS into SecurityPolicy (i.e. allowed_cors_origins) since CORS is security-related

on_load: OnLoadHandler | None


Expand Down
5 changes: 5 additions & 0 deletions mesop/security/cors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dataclasses import dataclass, field

@dataclass(kw_only=True)
class CORS:
allowed_origins: list[str] = field(default_factory=list)
6 changes: 6 additions & 0 deletions mesop/server/static_file_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ def add_security_headers(response: Response):
"report-uri": "/__csp__",
}
)

if page_config and page_config.cors:
cors = page_config.cors
if cors.allowed_origins:
response.headers["Access-Control-Allow-Origin"] = ", ".join(cors.allowed_origins)

if page_config and page_config.stylesheets:
csp["style-src"] += " " + " ".join(
[sanitize_url_for_csp(url) for url in page_config.stylesheets]
Expand Down