Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ tests/e2e/screenshots/*.png
tmp_*
*.d.ts
node_modules/
/app/public/js/*
/app/public/js/*
.jinja_cache/
17 changes: 16 additions & 1 deletion api/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,29 @@
from fastapi.templating import Jinja2Templates
from authlib.common.errors import AuthlibBaseError
from authlib.integrations.starlette_client import OAuth
from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache
from starlette.config import Config

from api.auth.user_management import validate_and_cache_user

# Router
auth_router = APIRouter()
TEMPLATES_DIR = str((Path(__file__).resolve().parents[1] / "../app/templates").resolve())
templates = Jinja2Templates(directory=TEMPLATES_DIR)

TEMPLATES_CACHE_DIR = "./.jinja_cache"
os.makedirs(TEMPLATES_CACHE_DIR, exist_ok=True) # ✅ ensures the folder exists

templates = Jinja2Templates(
env=Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
bytecode_cache=FileSystemBytecodeCache(
directory=TEMPLATES_CACHE_DIR,
pattern="%s.cache"
),
auto_reload=True
)
Comment on lines +28 to +35

Check warning

Code scanning / CodeQL

Jinja2 templating with autoescape=False Medium

Using jinja2 templates with autoescape=False can potentially allow XSS attacks.

Copilot Autofix

AI about 1 year ago

To fix this problem, configure the Jinja2 Environment instance to enable autoescaping for templates that may be served as HTML or XML. Jinja2 provides the select_autoescape utility, which automatically enables escaping on templates ending in .html, .htm, .xml, and other relevant extensions. The ideal fix is to pass autoescape=select_autoescape(['html', 'xml', 'j2']) when constructing the Environment. Thus, in api/routes/auth.py, update lines 27–36 to use:

from jinja2 import select_autoescape
...
templates = Jinja2Templates(
    env=Environment(
        loader=FileSystemLoader(TEMPLATES_DIR),
        bytecode_cache=FileSystemBytecodeCache(
            directory=TEMPLATES_CACHE_DIR,
            pattern="%s.cache"
        ),
        auto_reload=True,
        autoescape=select_autoescape(['html', 'xml', 'j2'])
    )
)

Also ensure that select_autoescape is imported. Only lines relating to the Jinja2 environment creation (and the import) need editing; no other changes are necessary.


Suggested changeset 1
api/routes/auth.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/api/routes/auth.py b/api/routes/auth.py
--- a/api/routes/auth.py
+++ b/api/routes/auth.py
@@ -12,7 +12,7 @@
 from fastapi.templating import Jinja2Templates
 from authlib.common.errors import AuthlibBaseError
 from authlib.integrations.starlette_client import OAuth
-from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache
+from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache, select_autoescape
 from starlette.config import Config
 
 from api.auth.user_management import validate_and_cache_user
@@ -31,7 +31,8 @@
             directory=TEMPLATES_CACHE_DIR,
             pattern="%s.cache"
         ),
-        auto_reload=True
+        auto_reload=True,
+        autoescape=select_autoescape(['html', 'xml', 'j2'])
     )
 )
 
EOF
@@ -12,7 +12,7 @@
from fastapi.templating import Jinja2Templates
from authlib.common.errors import AuthlibBaseError
from authlib.integrations.starlette_client import OAuth
from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache
from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache, select_autoescape
from starlette.config import Config

from api.auth.user_management import validate_and_cache_user
@@ -31,7 +31,8 @@
directory=TEMPLATES_CACHE_DIR,
pattern="%s.cache"
),
auto_reload=True
auto_reload=True,
autoescape=select_autoescape(['html', 'xml', 'j2'])
)
)

Copilot is powered by AI and may make mistakes. Always verify output.
)


# ---- Helpers ----
def _get_provider_client(request: Request, provider: str):
Expand Down
Loading