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

ReactPy ASGI Middleware and standalone ReactPy ASGI App #1113

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion src/js/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { app } from "./src/index";
app(document.getElementById("app"));
</script>
<!-- we replace this with user-provided head elements -->
{__head__}
</head>
<body>
Expand Down
Binary file removed src/js/app/public/assets/reactpy-logo.ico
Binary file not shown.
24 changes: 8 additions & 16 deletions src/py/reactpy/reactpy/backend/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ async def _check_if_started(server: uvicorn.Server, started: asyncio.Event) -> N

def safe_client_build_dir_path(path: str) -> Path:
"""Prevent path traversal out of :data:`CLIENT_BUILD_DIR`"""
return traversal_safe_path(
return safe_join_path(
CLIENT_BUILD_DIR, *("index.html" if path in {"", "/"} else path).split("/")
)


def safe_web_modules_dir_path(path: str) -> Path:
"""Prevent path traversal out of :data:`reactpy.config.REACTPY_WEB_MODULES_DIR`"""
return traversal_safe_path(REACTPY_WEB_MODULES_DIR.current, *path.split("/"))
return safe_join_path(REACTPY_WEB_MODULES_DIR.current, *path.split("/"))


def traversal_safe_path(root: str | Path, *unsafe: str | Path) -> Path:
def safe_join_path(root: str | Path, *unsafe: str | Path) -> Path:
"""Raise a ``ValueError`` if the ``unsafe`` path resolves outside the root dir."""
root = os.path.abspath(root)

Expand All @@ -86,9 +86,10 @@ def traversal_safe_path(root: str | Path, *unsafe: str | Path) -> Path:
path = os.path.abspath(os.path.join(root, *unsafe))

if os.path.commonprefix([root, path]) != root:
# If the common prefix is not root directory we resolved outside the root dir
msg = "Unsafe path"
raise ValueError(msg)
# We resolved outside the root dir, potential directory traversal attack.
raise ValueError(
f"Unsafe path detected. Path '{path}' is outside root directory '{root}'"
)

return Path(path)

Expand Down Expand Up @@ -116,16 +117,7 @@ def vdom_head_elements_to_html(head: Sequence[VdomDict] | VdomDict | str) -> str
class CommonOptions:
"""Options for ReactPy's built-in backed server implementations"""

head: Sequence[VdomDict] | VdomDict | str = (
html.title("ReactPy"),
html.link(
{
"rel": "icon",
"href": "/_reactpy/assets/reactpy-logo.ico",
"type": "image/x-icon",
}
),
)
head: Sequence[VdomDict] | VdomDict | str = (html.title("ReactPy App"),)
"""Add elements to the ``<head>`` of the application.

For example, this can be used to customize the title of the page, link extra
Expand Down
Loading