-
Notifications
You must be signed in to change notification settings - Fork 433
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
chore(dev): use ruff as linter and formatter #991
base: develop
Are you sure you want to change the base?
Conversation
Documentation preview |
if not import_path.endswith(".co") and os.path.exists( | ||
os.path.join(root, import_path + ".co") | ||
): | ||
if not import_path.endswith(".co") and os.path.exists(os.path.join(root, import_path + ".co")): |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 2 days ago
To fix the problem, we need to ensure that the constructed file paths are validated and 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 approach will prevent directory traversal attacks by ensuring that the resolved path does not escape the intended directory.
- Normalize the
import_path
usingos.path.normpath
. - Check that the normalized path starts with one of the allowed root directories in
colang_path_dirs
. - If the path is valid, proceed with the file operations; otherwise, raise an exception.
-
Copy modified lines R731-R739 -
Copy modified lines R741-R745
@@ -730,12 +730,17 @@ | ||
actual_path = None | ||
if not os.path.exists(import_path): | ||
for root in colang_path_dirs: | ||
if os.path.exists(os.path.join(root, import_path)): | ||
actual_path = os.path.join(root, import_path) | ||
break | ||
normalized_import_path = os.path.normpath(import_path) | ||
for root in colang_path_dirs: | ||
normalized_root = os.path.normpath(root) | ||
potential_path = os.path.join(normalized_root, normalized_import_path) | ||
if os.path.commonprefix([potential_path, normalized_root]) != normalized_root: | ||
continue | ||
if os.path.exists(potential_path): | ||
actual_path = potential_path | ||
break | ||
|
||
# We also check if we can load it as a file. | ||
if not import_path.endswith(".co") and os.path.exists(os.path.join(root, import_path + ".co")): | ||
actual_path = os.path.join(root, import_path + ".co") | ||
break | ||
# We also check if we can load it as a file. | ||
potential_path_with_ext = potential_path + ".co" | ||
if not import_path.endswith(".co") and os.path.exists(potential_path_with_ext): | ||
actual_path = potential_path_with_ext | ||
break | ||
else: |
No description provided.