Skip to content

Conversation

@muhammad-fiaz
Copy link
Owner

  • Improved Vite Env Configs and Formatted

Copilot AI review requested due to automatic review settings March 3, 2025 14:35
@muhammad-fiaz muhammad-fiaz added enhancement New feature or request dependencies Pull requests that update a dependency file labels Mar 3, 2025
@muhammad-fiaz muhammad-fiaz moved this to In progress in PyWebflow Mar 3, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR refactors the asset injection process, updates the Vite configuration for production environments, and cleans up API route definitions and type definitions in both the frontend and backend. Key changes include:

  • Refactored asset injection and logging in the frontend hooks.
  • Updated FastAPI endpoints and route definitions in the webflow modules.
  • Modified Vite configuration to use the react-swc plugin along with production-specific settings.

Reviewed Changes

File Description
packages/frontend/src/styles.ts Added new style imports.
packages/frontend/src/hooks/assets.ts Refactored to use a single async asset injection function with improved logging.
webflow/modules/webflow_api.py Introduced new API endpoints and initialization logic for serving static assets and HTML.
webflow/modules/routes.py Updated route definitions to leverage the WebFlow_API class directly and simplify responses.
packages/frontend/src/Layout.tsx Added fallback logic for configuration values and enhanced error handling when fetching config.
packages/frontend/src/App.tsx Added loading and error states on sidebar API calls.
packages/frontend/src/components/InjectedHtml.tsx Switched from dangerouslySetInnerHTML to sanitized HTML parsing using DOMPurify and html-react-parser.
packages/frontend/src/Root.tsx Updated imports to remove duplicate style imports and adjusted config fetching logic.
packages/frontend/vite.config.ts Changed to react-swc and added production-specific proxy and minification options.
webflow/modules/types.py Adjusted type definitions for sidebar-related models to be more flexible.

Copilot reviewed 23 out of 23 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

webflow/modules/webflow_api.py:83

  • Consider renaming the 'sidebar' method to 'setSidebar' to avoid confusion with the 'sidebar' property and improve clarity.
def sidebar(visible: bool, label: str, default_open: bool, items: List[Dict[str, str]]):

packages/frontend/vite.config.ts:26

  • The 'cssMinify' option is not a standard Vite configuration option and might be ignored; verify its validity or remove it if unnecessary.
cssMinify: 'esbuild',

logly.warn("Static directory not set.")
raise HTTPException(status_code=500, detail="Static directory not set")
file_path = Path(cls.static_dir) / filename
if file_path.exists():

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix

AI 9 months ago

To fix the problem, we need to validate and sanitize the user-provided filename before using it to construct file paths. The best way to do this is to normalize the path and ensure it is contained within the static directory. This can be achieved by using os.path.normpath to remove any ".." segments and then checking that the normalized path starts with the static directory.

  1. Normalize the filename using os.path.normpath.
  2. Ensure that the normalized path starts with the static directory.
  3. If the path is valid, proceed to serve the file; otherwise, raise an HTTP exception.
Suggested changeset 1
webflow/modules/webflow_api.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/webflow/modules/webflow_api.py b/webflow/modules/webflow_api.py
--- a/webflow/modules/webflow_api.py
+++ b/webflow/modules/webflow_api.py
@@ -1,2 +1,3 @@
 import datetime
+import os
 from pathlib import Path
@@ -155,3 +156,7 @@
             raise HTTPException(status_code=500, detail="Static directory not set")
-        file_path = Path(cls.static_dir) / filename
+        normalized_path = Path(os.path.normpath(filename))
+        file_path = Path(cls.static_dir) / normalized_path
+        if not str(file_path.resolve()).startswith(str(Path(cls.static_dir).resolve())):
+            logly.warn(f"Attempt to access file outside static directory: {filename}")
+            raise HTTPException(status_code=400, detail="Invalid file path")
         if file_path.exists():
EOF
@@ -1,2 +1,3 @@
import datetime
import os
from pathlib import Path
@@ -155,3 +156,7 @@
raise HTTPException(status_code=500, detail="Static directory not set")
file_path = Path(cls.static_dir) / filename
normalized_path = Path(os.path.normpath(filename))
file_path = Path(cls.static_dir) / normalized_path
if not str(file_path.resolve()).startswith(str(Path(cls.static_dir).resolve())):
logly.warn(f"Attempt to access file outside static directory: {filename}")
raise HTTPException(status_code=400, detail="Invalid file path")
if file_path.exists():
Copilot is powered by AI and may make mistakes. Always verify output.
elif file_path.suffix == ".html":
media_type = "text/html"
headers = {"Cache-Control": "no-cache, no-store, must-revalidate"}
return FileResponse(str(file_path), media_type=media_type, headers=headers)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix

AI 9 months ago

To fix the problem, we need to ensure that the constructed file path is contained within a safe root directory. This can be achieved by normalizing the path using os.path.normpath and then checking that the normalized path starts with the root directory. This will prevent path traversal attacks.

  1. Normalize the path using os.path.normpath.
  2. Check that the normalized path starts with the root directory (cls.static_dir).
  3. If the check fails, raise an HTTP exception.
Suggested changeset 1
webflow/modules/webflow_api.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/webflow/modules/webflow_api.py b/webflow/modules/webflow_api.py
--- a/webflow/modules/webflow_api.py
+++ b/webflow/modules/webflow_api.py
@@ -156,12 +156,16 @@
         file_path = Path(cls.static_dir) / filename
-        if file_path.exists():
+        normalized_path = file_path.resolve()
+        if not str(normalized_path).startswith(str(Path(cls.static_dir).resolve())):
+            logly.warn(f"Attempt to access file outside of static directory: {filename}")
+            raise HTTPException(status_code=403, detail="Access to the requested file is forbidden")
+        if normalized_path.exists():
             media_type = "application/octet-stream"
-            if file_path.suffix == ".css":
+            if normalized_path.suffix == ".css":
                 media_type = "text/css"
-            elif file_path.suffix == ".js":
+            elif normalized_path.suffix == ".js":
                 media_type = "application/javascript"
-            elif file_path.suffix == ".html":
+            elif normalized_path.suffix == ".html":
                 media_type = "text/html"
             headers = {"Cache-Control": "no-cache, no-store, must-revalidate"}
-            return FileResponse(str(file_path), media_type=media_type, headers=headers)
+            return FileResponse(str(normalized_path), media_type=media_type, headers=headers)
         else:
EOF
@@ -156,12 +156,16 @@
file_path = Path(cls.static_dir) / filename
if file_path.exists():
normalized_path = file_path.resolve()
if not str(normalized_path).startswith(str(Path(cls.static_dir).resolve())):
logly.warn(f"Attempt to access file outside of static directory: {filename}")
raise HTTPException(status_code=403, detail="Access to the requested file is forbidden")
if normalized_path.exists():
media_type = "application/octet-stream"
if file_path.suffix == ".css":
if normalized_path.suffix == ".css":
media_type = "text/css"
elif file_path.suffix == ".js":
elif normalized_path.suffix == ".js":
media_type = "application/javascript"
elif file_path.suffix == ".html":
elif normalized_path.suffix == ".html":
media_type = "text/html"
headers = {"Cache-Control": "no-cache, no-store, must-revalidate"}
return FileResponse(str(file_path), media_type=media_type, headers=headers)
return FileResponse(str(normalized_path), media_type=media_type, headers=headers)
else:
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file enhancement New feature or request

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

2 participants