-
Notifications
You must be signed in to change notification settings - Fork 0
Potential fix for code scanning alert no. 27: Uncontrolled data used in path expression #11
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
base: main
Are you sure you want to change the base?
Conversation
…in path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
| source_dir = agent_dir | ||
| source_items = set(os.listdir(source_dir)) | ||
| try: | ||
| source_items = set(os.listdir(source_dir)) |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 18 days ago
General Fix:
Normalize and resolve all user-controlled paths before use and verify, using a more robust mechanism than a simple string "startswith" check. Also, when using the path as a directory to be listed or accessed, use the authoritative resolved path object or string.
Detailed steps:
- Use
Path.resolve(strict=False)on bothagent_dirandbase_pathto get their true, canonical forms. - Instead of
str(agent_dir).startswith(str(base_path)), usebase_path in agent_dir.parents or agent_dir == base_pathto check containment. - Do the same for
destination_dir. - When assigning
source_dir, use the resolved, verified path for safety. - Update/add imports as needed for
Path.
Lines to change:
- Lines assigning and checking
agent_dir,destination_dir, andsource_dir(lines 251-263). - Strengthen the check and use the correct resolved path objects.
-
Copy modified lines R251-R253 -
Copy modified line R255 -
Copy modified line R258 -
Copy modified line R260
| @@ -248,16 +248,16 @@ | ||
| @working_in_progress("builder_save is not ready for use.") | ||
| @app.post("/builder/app/{app_name}/cancel", response_model_exclude_none=True) | ||
| async def builder_cancel(app_name: str) -> bool: | ||
| base_path = Path.cwd() / agents_dir | ||
| # Safely compose agent_dir | ||
| agent_dir = os.path.normpath(os.path.join(base_path, app_name)) | ||
| base_path = (Path.cwd() / agents_dir).resolve() | ||
| # Safely compose agent_dir, then resolve | ||
| agent_dir = (base_path / app_name).resolve() | ||
| # Ensure agent_dir is within base_path | ||
| if not str(agent_dir).startswith(str(base_path)): | ||
| if base_path not in agent_dir.parents and agent_dir != base_path: | ||
| logger.error(f"Attempted path traversal attack with agent_dir: {agent_dir}") | ||
| return False | ||
| destination_dir = os.path.normpath(os.path.join(agent_dir, "tmp/" + app_name)) | ||
| destination_dir = (agent_dir / ("tmp/" + app_name)).resolve() | ||
| # Ensure destination_dir is also within base_path | ||
| if not str(destination_dir).startswith(str(base_path)): | ||
| if base_path not in destination_dir.parents and destination_dir != base_path: | ||
| logger.error(f"Attempted path traversal attack with destination_dir: {destination_dir}") | ||
| return False | ||
| source_dir = agent_dir |
Potential fix for https://github.com/venkateshpabbati/adk-python/security/code-scanning/27
To safely handle user-provided values like
app_namein filesystem paths, normalization and prefix validation should be performed.Best-Fix Approach:
Normalize all user-derived paths after joining with the intended safe base directory (
base_path) usingos.path.normpath. Then enforce that the normalized path starts with the base directory, preventing path traversal and absolute path exploits. Optionally, reject app names containing suspicious components (such as path separators, '.', '..', etc.). For defense-in-depth,werkzeug.utils.secure_filenamecould further sanitize filenames but for directories that may span multiple segments, normalization and prefix-checking suffices.Required change locations:
agent_diranddestination_dirinbuilder_cancelapp_name/values are used to construct filesystem pathsChange summary:
base_pathandapp_name, normalize the resulting path and check that it remains insidebase_path.Imports needed: None beyond standard libraries (
os,Path, etc.).Suggested fixes powered by Copilot Autofix. Review carefully before merging.