-
Notifications
You must be signed in to change notification settings - Fork 1
fix(gateway): resolve CodeQL path injection with pathlib containment checks #676
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,11 @@ | |||||||||||||||||||||||||||||||||||||||||
| from fastapi import APIRouter, HTTPException, Query | |||||||||||||||||||||||||||||||||||||||||
| from fastapi.responses import Response, HTMLResponse | |||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | |||||||||||||||||||||||||||||||||||||||||
| from typing import List, Dict, Any, Optional | |||||||||||||||||||||||||||||||||||||||||
| import json, os, math, re | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
| _SAFE_SHAPE_RE = re.compile(r"^[a-zA-Z0-9._-]+$") | |||||||||||||||||||||||||||||||||||||||||
| DATA_DIR = Path("data").resolve() | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
| from gateway.api.chit import Constellation, CGP, decode_constellations | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
|
|
@@ -107,10 +109,12 @@ | ||||||||||||||||||||||||||||||||||||||||
| safe_id = os.path.basename(shape_id) | |||||||||||||||||||||||||||||||||||||||||
| if not safe_id or safe_id != shape_id or not _SAFE_SHAPE_RE.match(safe_id): | |||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status_code=400, detail="invalid shape_id") | |||||||||||||||||||||||||||||||||||||||||
| path = os.path.join("data", f"{safe_id}.json") | |||||||||||||||||||||||||||||||||||||||||
| if not os.path.exists(path): | |||||||||||||||||||||||||||||||||||||||||
| resolved = (DATA_DIR / f"{safe_id}.json").resolve() | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
| if not resolved.is_relative_to(DATA_DIR): | |||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status_code=400, detail="invalid shape_id") | |||||||||||||||||||||||||||||||||||||||||
| if not resolved.exists(): | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status_code=404, detail="shape not found") | |||||||||||||||||||||||||||||||||||||||||
| with open(path, "r", encoding="utf-8") as f: | |||||||||||||||||||||||||||||||||||||||||
| with open(resolved, "r", encoding="utf-8") as f: | |||||||||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Uncontrolled data used in path expression High
This path depends on a
user-provided value Error loading related location Loading
Copilot AutofixAI 7 months ago General approach: Ensure that any path derived from user input is strictly confined to a known safe root directory and cannot escape it via path traversal, absolute paths, symlinks, or unusual Specific fix for this code:
Concrete changes (in pmoves/services/gateway/gateway/api/viz.py):
This preserves existing behavior for valid
Suggested changeset
1
pmoves/services/gateway/gateway/api/viz.py
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||||||||||||||
| obj = json.load(f) | |||||||||||||||||||||||||||||||||||||||||
| try: | |||||||||||||||||||||||||||||||||||||||||
| cgp = CGP.model_validate(obj) | |||||||||||||||||||||||||||||||||||||||||
|
|
@@ -194,10 +198,12 @@ | ||||||||||||||||||||||||||||||||||||||||
| safe_id = os.path.basename(shape_id) | |||||||||||||||||||||||||||||||||||||||||
| if not safe_id or safe_id != shape_id or not _SAFE_SHAPE_RE.match(safe_id): | |||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status_code=400, detail="invalid shape_id") | |||||||||||||||||||||||||||||||||||||||||
| path = os.path.join("data", f"{safe_id}.json") | |||||||||||||||||||||||||||||||||||||||||
| if not os.path.exists(path): | |||||||||||||||||||||||||||||||||||||||||
| resolved = (DATA_DIR / f"{safe_id}.json").resolve() | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
| if not resolved.is_relative_to(DATA_DIR): | |||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status_code=400, detail="invalid shape_id") | |||||||||||||||||||||||||||||||||||||||||
| if not resolved.exists(): | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
| raise HTTPException(status_code=404, detail="shape not found") | |||||||||||||||||||||||||||||||||||||||||
| obj = json.loads(open(path, "r", encoding="utf-8").read()) | |||||||||||||||||||||||||||||||||||||||||
| obj = json.loads(resolved.read_text(encoding="utf-8")) | |||||||||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Uncontrolled data used in path expression High
This path depends on a
user-provided value Error loading related location Loading Copilot AutofixAI 7 months ago Copilot could not generate an autofix suggestion Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support. |
|||||||||||||||||||||||||||||||||||||||||
| cgp = CGP.model_validate(obj) | |||||||||||||||||||||||||||||||||||||||||
| out = [] | |||||||||||||||||||||||||||||||||||||||||
| for si, s in enumerate(cgp.super_nodes): | |||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.